Skip to main content

pytest_language_server/providers/
definition.rs

1//! Go-to-definition provider for pytest fixtures.
2
3use super::Backend;
4use tower_lsp_server::jsonrpc::Result;
5use tower_lsp_server::ls_types::*;
6use tracing::info;
7
8impl Backend {
9    /// Handle goto_definition request
10    pub async fn handle_goto_definition(
11        &self,
12        params: GotoDefinitionParams,
13    ) -> Result<Option<GotoDefinitionResponse>> {
14        let uri = params.text_document_position_params.text_document.uri;
15        let position = params.text_document_position_params.position;
16
17        info!(
18            "goto_definition request: uri={:?}, line={}, char={}",
19            uri, position.line, position.character
20        );
21
22        if let Some(file_path) = self.uri_to_path(&uri) {
23            info!(
24                "Looking for fixture definition at {:?}:{}:{}",
25                file_path, position.line, position.character
26            );
27
28            if let Some(definition) = self.fixture_db.find_fixture_definition(
29                &file_path,
30                position.line,
31                position.character,
32            ) {
33                info!("Found definition: {:?}", definition);
34                let Some(def_uri) = self.path_to_uri(&definition.file_path) else {
35                    return Ok(None);
36                };
37
38                let def_line = Self::internal_line_to_lsp(definition.line);
39                let location = Location {
40                    uri: def_uri.clone(),
41                    range: Self::create_point_range(def_line, 0),
42                };
43                info!("Returning location: {:?}", location);
44                return Ok(Some(GotoDefinitionResponse::Scalar(location)));
45            } else {
46                info!("No fixture definition found");
47            }
48        }
49
50        Ok(None)
51    }
52}