diff options
author | Norbert Preining <norbert@preining.info> | 2022-12-30 03:01:26 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2022-12-30 03:01:26 +0000 |
commit | 844019377157163b461e0fd4a66592e61963a530 (patch) | |
tree | 32f61991c6a5acc3a3359ffc2cdefdd2aa004268 /support/texlab/src/features/definition | |
parent | 55c69feeef908f49007708be194b7bb1c607f302 (diff) |
CTAN sync 202212300301
Diffstat (limited to 'support/texlab/src/features/definition')
-rw-r--r-- | support/texlab/src/features/definition/command.rs | 20 | ||||
-rw-r--r-- | support/texlab/src/features/definition/document.rs | 50 | ||||
-rw-r--r-- | support/texlab/src/features/definition/entry.rs | 24 | ||||
-rw-r--r-- | support/texlab/src/features/definition/label.rs | 37 | ||||
-rw-r--r-- | support/texlab/src/features/definition/string.rs | 33 |
5 files changed, 67 insertions, 97 deletions
diff --git a/support/texlab/src/features/definition/command.rs b/support/texlab/src/features/definition/command.rs index 599c792dfd..cf9fe1b511 100644 --- a/support/texlab/src/features/definition/command.rs +++ b/support/texlab/src/features/definition/command.rs @@ -1,26 +1,20 @@ -use std::sync::Arc; - -use lsp_types::GotoDefinitionParams; use rowan::ast::AstNode; -use crate::{features::cursor::CursorContext, syntax::latex}; +use crate::{syntax::latex, util::cursor::CursorContext}; use super::DefinitionResult; -pub(super) fn goto_command_definition( - context: &CursorContext<GotoDefinitionParams>, -) -> Option<Vec<DefinitionResult>> { +pub(super) fn goto_definition(context: &CursorContext) -> Option<Vec<DefinitionResult>> { let name = context .cursor - .as_latex() + .as_tex() .filter(|token| token.kind().is_command_name())?; let origin_selection_range = name.text_range(); - for document in context.request.workspace.documents_by_uri.values() { - if let Some(data) = document.data.as_latex() { - let root = latex::SyntaxNode::new_root(data.green.clone()); - + for document in context.related() { + if let Some(data) = document.parse(context.db).as_tex() { + let root = data.root(context.db); if let Some(result) = root .descendants() .filter_map(latex::CommandDefinition::cast) @@ -32,7 +26,7 @@ pub(super) fn goto_command_definition( .find_map(|def| { Some(DefinitionResult { origin_selection_range, - target_uri: Arc::clone(&document.uri), + target: document, target_range: latex::small_range(&def), target_selection_range: def.name()?.command()?.text_range(), }) diff --git a/support/texlab/src/features/definition/document.rs b/support/texlab/src/features/definition/document.rs index 1c09bbc88d..58237f7521 100644 --- a/support/texlab/src/features/definition/document.rs +++ b/support/texlab/src/features/definition/document.rs @@ -1,40 +1,30 @@ -use std::sync::Arc; - -use lsp_types::GotoDefinitionParams; use rowan::TextRange; -use crate::features::cursor::CursorContext; +use crate::{db::dependency_graph, util::cursor::CursorContext}; use super::DefinitionResult; -pub(super) fn goto_document_definition( - context: &CursorContext<GotoDefinitionParams>, -) -> Option<Vec<DefinitionResult>> { - let data = context.request.main_document().data.as_latex()?; - - for include in data - .extras - .explicit_links +pub(super) fn goto_definition(context: &CursorContext) -> Option<Vec<DefinitionResult>> { + let db = context.db; + context + .workspace + .parents(db, context.document) .iter() - .filter(|link| link.stem_range.contains_inclusive(context.offset)) - { - for target in &include.targets { - if context - .request - .workspace - .documents_by_uri - .values() - .any(|document| document.uri.as_ref() == target.as_ref()) - { - return Some(vec![DefinitionResult { - origin_selection_range: include.stem_range, - target_uri: Arc::clone(target), + .copied() + .chain(std::iter::once(context.document)) + .flat_map(|parent| dependency_graph(db, parent).edges) + .filter(|edge| edge.source == context.document) + .find_map(|edge| { + let range = edge.origin?.link.range(db); + if range.contains_inclusive(context.offset) { + Some(vec![DefinitionResult { + origin_selection_range: range, + target: edge.target, target_range: TextRange::default(), target_selection_range: TextRange::default(), - }]); + }]) + } else { + None } - } - } - - None + }) } diff --git a/support/texlab/src/features/definition/entry.rs b/support/texlab/src/features/definition/entry.rs index 1dcd983af1..a945b18c41 100644 --- a/support/texlab/src/features/definition/entry.rs +++ b/support/texlab/src/features/definition/entry.rs @@ -1,24 +1,21 @@ -use std::sync::Arc; - -use lsp_types::GotoDefinitionParams; use rowan::ast::AstNode; use crate::{ - features::cursor::CursorContext, syntax::{ bibtex::{self, HasName}, latex, }, + util::cursor::CursorContext, }; use super::DefinitionResult; -pub(super) fn goto_entry_definition( - context: &CursorContext<GotoDefinitionParams>, -) -> Option<Vec<DefinitionResult>> { +pub(super) fn goto_definition(context: &CursorContext) -> Option<Vec<DefinitionResult>> { + let db = context.db; + let word = context .cursor - .as_latex() + .as_tex() .filter(|token| token.kind() == latex::WORD)?; let key = latex::Key::cast(word.parent()?)?; @@ -27,16 +24,13 @@ pub(super) fn goto_entry_definition( let origin_selection_range = latex::small_range(&key); - for document in context.request.workspace.documents_by_uri.values() { - if let Some(data) = document.data.as_bibtex() { - for entry in bibtex::SyntaxNode::new_root(data.green.clone()) - .children() - .filter_map(bibtex::Entry::cast) - { + for document in context.related() { + if let Some(data) = document.parse(db).as_bib() { + for entry in data.root(db).children().filter_map(bibtex::Entry::cast) { if let Some(key) = entry.name_token().filter(|k| k.text() == word.text()) { return Some(vec![DefinitionResult { origin_selection_range, - target_uri: Arc::clone(&document.uri), + target: document, target_selection_range: key.text_range(), target_range: entry.syntax().text_range(), }]); diff --git a/support/texlab/src/features/definition/label.rs b/support/texlab/src/features/definition/label.rs index 867e8a1076..a1cc4ed15b 100644 --- a/support/texlab/src/features/definition/label.rs +++ b/support/texlab/src/features/definition/label.rs @@ -1,31 +1,32 @@ -use std::sync::Arc; - -use lsp_types::GotoDefinitionParams; - -use crate::{features::cursor::CursorContext, find_label_definition, render_label, syntax::latex}; +use crate::{ + db::analysis::label, + util::{self, cursor::CursorContext}, +}; use super::DefinitionResult; -pub(super) fn goto_label_definition( - context: &CursorContext<GotoDefinitionParams>, -) -> Option<Vec<DefinitionResult>> { +pub(super) fn goto_definition(context: &CursorContext) -> Option<Vec<DefinitionResult>> { + let db = context.db; let (name_text, origin_selection_range) = context .find_label_name_key() .or_else(|| context.find_label_name_command())?; - for document in context.request.workspace.documents_by_uri.values() { - if let Some(data) = document.data.as_latex() { - let root = latex::SyntaxNode::new_root(data.green.clone()); - if let Some(definition) = find_label_definition(&root, &name_text) { - let target_selection_range = latex::small_range(&definition.name()?.key()?); - let target_range = - render_label(&context.request.workspace, &name_text, Some(definition)) - .map(|label| label.range) - .unwrap_or(target_selection_range); + for document in context.related() { + if let Some(data) = document.parse(db).as_tex() { + if let Some(label) = data + .analyze(db) + .labels(db) + .iter() + .filter(|label| matches!(label.origin(db), label::Origin::Definition(_))) + .find(|label| label.name(db).text(db) == name_text.as_str()) + { + let target_selection_range = label.range(db); + let target_range = util::label::render(db, document, *label) + .map_or(target_selection_range, |label| label.range); return Some(vec![DefinitionResult { origin_selection_range, - target_uri: Arc::clone(&document.uri), + target: document, target_range, target_selection_range, }]); diff --git a/support/texlab/src/features/definition/string.rs b/support/texlab/src/features/definition/string.rs index c774e24e1d..44ea1e9094 100644 --- a/support/texlab/src/features/definition/string.rs +++ b/support/texlab/src/features/definition/string.rs @@ -1,43 +1,34 @@ -use std::sync::Arc; - -use lsp_types::GotoDefinitionParams; use rowan::ast::AstNode; use crate::{ - features::cursor::CursorContext, syntax::bibtex::{self, HasName}, + util::cursor::CursorContext, }; use super::DefinitionResult; -pub(super) fn goto_string_definition( - context: &CursorContext<GotoDefinitionParams>, -) -> Option<Vec<DefinitionResult>> { - let main_document = context.request.main_document(); - - let data = main_document.data.as_bibtex()?; +pub(super) fn goto_definition(context: &CursorContext) -> Option<Vec<DefinitionResult>> { + let db = context.db; + let data = context.document.parse(db).as_bib()?; let key = context .cursor - .as_bibtex() + .as_bib() .filter(|token| token.kind() == bibtex::NAME)?; bibtex::Value::cast(key.parent()?)?; let origin_selection_range = key.text_range(); - for string in bibtex::SyntaxNode::new_root(data.green.clone()) + data.root(db) .children() .filter_map(bibtex::StringDef::cast) - { - if let Some(string_name) = string.name_token().filter(|k| k.text() == key.text()) { - return Some(vec![DefinitionResult { + .find_map(|string| { + let string_name = string.name_token().filter(|k| k.text() == key.text())?; + Some(vec![DefinitionResult { origin_selection_range, - target_uri: Arc::clone(&main_document.uri), + target: context.document, target_selection_range: string_name.text_range(), target_range: string.syntax().text_range(), - }]); - } - } - - None + }]) + }) } |