diff options
Diffstat (limited to 'support/texlab/src/features/definition/command.rs')
-rw-r--r-- | support/texlab/src/features/definition/command.rs | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/support/texlab/src/features/definition/command.rs b/support/texlab/src/features/definition/command.rs new file mode 100644 index 0000000000..a69796c33f --- /dev/null +++ b/support/texlab/src/features/definition/command.rs @@ -0,0 +1,131 @@ +use cancellation::CancellationToken; +use lsp_types::{GotoDefinitionParams, LocationLink}; + +use crate::{ + features::cursor::CursorContext, + syntax::{latex, CstNode}, + LineIndexExt, +}; + +pub fn goto_command_definition( + context: &CursorContext<GotoDefinitionParams>, + cancellation_token: &CancellationToken, +) -> Option<Vec<LocationLink>> { + let main_document = context.request.main_document(); + + let name = context + .cursor + .as_latex() + .filter(|token| token.kind().is_command_name())?; + + let origin_selection_range = main_document + .line_index + .line_col_lsp_range(name.text_range()); + + for document in &context.request.subset.documents { + if let Some(data) = document.data.as_latex() { + for node in data.root.descendants() { + cancellation_token.result().ok()?; + + if let Some(defintion) = latex::CommandDefinition::cast(node).filter(|def| { + def.name() + .and_then(|name| name.command()) + .map(|name| name.text()) + == Some(name.text()) + }) { + let target_selection_range = document + .line_index + .line_col_lsp_range(defintion.name()?.command()?.text_range()); + + let target_range = document + .line_index + .line_col_lsp_range(defintion.small_range()); + + return Some(vec![LocationLink { + origin_selection_range: Some(origin_selection_range), + target_uri: document.uri.as_ref().clone().into(), + target_range, + target_selection_range, + }]); + } + } + } + } + + None +} + +#[cfg(test)] +mod tests { + use indoc::indoc; + use lsp_types::Range; + + use crate::{features::testing::FeatureTester, RangeExt}; + + use super::*; + + #[test] + fn test_empty_latex_document() { + let request = FeatureTester::builder() + .files(vec![("main.tex", "")]) + .main("main.tex") + .line(0) + .character(0) + .build() + .definition(); + + let context = CursorContext::new(request); + + let actual_links = goto_command_definition(&context, CancellationToken::none()); + + assert!(actual_links.is_none()); + } + + #[test] + fn test_empty_bibtex_document() { + let request = FeatureTester::builder() + .files(vec![("main.bib", "")]) + .main("main.bib") + .line(0) + .character(0) + .build() + .definition(); + + let context = CursorContext::new(request); + let actual_links = goto_command_definition(&context, CancellationToken::none()); + + assert!(actual_links.is_none()); + } + + #[test] + fn test_command_definition() { + let tester = FeatureTester::builder() + .files(vec![( + "main.tex", + indoc! { + r#" + \DeclareMathOperator{\foo}{foo} + \foo + "# + }, + )]) + .main("main.tex") + .line(1) + .character(2) + .build(); + let target_uri = tester.uri("main.tex").as_ref().clone().into(); + + let request = tester.definition(); + let context = CursorContext::new(request); + let actual_links = goto_command_definition(&context, CancellationToken::none()).unwrap(); + + let expected_links = vec![LocationLink { + origin_selection_range: Some(Range::new_simple(1, 0, 1, 4)), + target_uri, + target_range: Range::new_simple(0, 0, 0, 31), + target_selection_range: Range::new_simple(0, 21, 0, 25), + }]; + + assert_eq!(actual_links, expected_links); + } +} |