summaryrefslogtreecommitdiff
path: root/support/texlab/src/reference
diff options
context:
space:
mode:
Diffstat (limited to 'support/texlab/src/reference')
-rw-r--r--support/texlab/src/reference/bibtex_entry.rs339
-rw-r--r--support/texlab/src/reference/bibtex_string.rs394
-rw-r--r--support/texlab/src/reference/latex_label.rs307
-rw-r--r--support/texlab/src/reference/mod.rs21
4 files changed, 567 insertions, 494 deletions
diff --git a/support/texlab/src/reference/bibtex_entry.rs b/support/texlab/src/reference/bibtex_entry.rs
index 23e09bdbb6..2acf7bdfef 100644
--- a/support/texlab/src/reference/bibtex_entry.rs
+++ b/support/texlab/src/reference/bibtex_entry.rs
@@ -1,76 +1,65 @@
-use crate::range::RangeExt;
-use crate::syntax::*;
-use crate::workspace::*;
-use futures_boxed::boxed;
-use lsp_types::{Location, ReferenceParams};
+use crate::{
+ feature::{FeatureProvider, FeatureRequest},
+ protocol::{Location, RangeExt, ReferenceParams, Url},
+ syntax::{bibtex, latex, SyntaxNode},
+ workspace::DocumentContent,
+};
+use async_trait::async_trait;
-#[derive(Debug, PartialEq, Eq, Clone)]
+#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct BibtexEntryReferenceProvider;
+#[async_trait]
impl FeatureProvider for BibtexEntryReferenceProvider {
type Params = ReferenceParams;
type Output = Vec<Location>;
- #[boxed]
- async fn execute<'a>(&'a self, request: &'a FeatureRequest<ReferenceParams>) -> Vec<Location> {
- let mut references = Vec::new();
- if let Some(key) = Self::find_key(request) {
- for document in request.related_documents() {
- match &document.tree {
- SyntaxTree::Latex(tree) => tree
+ async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
+ let mut refs = Vec::new();
+ if let Some(key) = Self::find_key(req) {
+ for doc in req.related() {
+ match &doc.content {
+ DocumentContent::Latex(table) => table
.citations
.iter()
- .flat_map(LatexCitation::keys)
+ .flat_map(|citation| citation.keys(&table))
.filter(|citation| citation.text() == key)
- .map(|citation| {
- Location::new(document.uri.clone().into(), citation.range())
- })
- .for_each(|location| references.push(location)),
- SyntaxTree::Bibtex(tree) => {
- if request.params.context.include_declaration {
- for entry in tree.entries() {
- if let Some(key_token) = &entry.key {
- if key_token.text() == key {
- let uri = document.uri.clone();
- let location = Location::new(uri.into(), key_token.range());
- references.push(location);
- }
- }
- }
+ .map(|citation| Location::new(doc.uri.clone().into(), citation.range()))
+ .for_each(|location| refs.push(location)),
+ DocumentContent::Bibtex(tree) => {
+ if req.params.context.include_declaration {
+ let uri: Url = doc.uri.clone().into();
+ tree.children(tree.root)
+ .filter_map(|node| tree.as_entry(node))
+ .filter_map(|entry| entry.key.as_ref())
+ .filter(|key_tok| key_tok.text() == key)
+ .map(|key_tok| Location::new(uri.clone(), key_tok.range()))
+ .for_each(|location| refs.push(location));
}
}
}
}
}
- references
+ refs
}
}
impl BibtexEntryReferenceProvider {
- fn find_key(request: &FeatureRequest<ReferenceParams>) -> Option<&str> {
- match &request.document().tree {
- SyntaxTree::Latex(tree) => tree
+ fn find_key(req: &FeatureRequest<ReferenceParams>) -> Option<&str> {
+ let pos = req.params.text_document_position.position;
+ match &req.current().content {
+ DocumentContent::Latex(table) => table
.citations
.iter()
- .flat_map(LatexCitation::keys)
- .find(|key| {
- key.range()
- .contains(request.params.text_document_position.position)
- })
- .map(LatexToken::text),
- SyntaxTree::Bibtex(tree) => {
- for entry in tree.entries() {
- if let Some(key) = &entry.key {
- if key
- .range()
- .contains(request.params.text_document_position.position)
- {
- return Some(key.text());
- }
- }
- }
- None
- }
+ .flat_map(|citation| citation.keys(&table))
+ .find(|key| key.range().contains(pos))
+ .map(latex::Token::text),
+ DocumentContent::Bibtex(tree) => tree
+ .children(tree.root)
+ .filter_map(|node| tree.as_entry(node))
+ .filter_map(|entry| entry.key.as_ref())
+ .find(|key| key.range().contains(pos))
+ .map(bibtex::Token::text),
}
}
}
@@ -78,120 +67,152 @@ impl BibtexEntryReferenceProvider {
#[cfg(test)]
mod tests {
use super::*;
- use crate::range::RangeExt;
- use lsp_types::{Position, Range};
-
- #[test]
- fn test_entry() {
- let references = test_feature(
- BibtexEntryReferenceProvider,
- FeatureSpec {
- files: vec![
- FeatureSpec::file("foo.bib", "@article{foo, bar = {baz}}"),
- FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
- FeatureSpec::file("baz.tex", "\\cite{foo}"),
- ],
- main_file: "foo.bib",
- position: Position::new(0, 9),
- include_declaration: false,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![Location::new(
- FeatureSpec::uri("bar.tex"),
- Range::new_simple(1, 6, 1, 9)
- )]
- );
+ use crate::{feature::FeatureTester, protocol::Range};
+ use indoc::indoc;
+
+ #[tokio::test]
+ async fn entry() {
+ let actual_refs = FeatureTester::new()
+ .file("foo.bib", r#"@article{foo, bar = {baz}}"#)
+ .file(
+ "bar.tex",
+ indoc!(
+ r#"
+ \addbibresource{foo.bib}
+ \cite{foo}
+ "#
+ ),
+ )
+ .file("baz.tex", r#"\cite{foo}"#)
+ .main("foo.bib")
+ .position(0, 9)
+ .test_reference(BibtexEntryReferenceProvider)
+ .await;
+
+ let expected_refs = vec![Location::new(
+ FeatureTester::uri("bar.tex").into(),
+ Range::new_simple(1, 6, 1, 9),
+ )];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_entry_include_declaration() {
- let references = test_feature(
- BibtexEntryReferenceProvider,
- FeatureSpec {
- files: vec![
- FeatureSpec::file("foo.bib", "@article{foo, bar = {baz}}"),
- FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
- FeatureSpec::file("baz.tex", "\\cite{foo}"),
- ],
- main_file: "foo.bib",
- position: Position::new(0, 9),
- include_declaration: true,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![
- Location::new(FeatureSpec::uri("foo.bib"), Range::new_simple(0, 9, 0, 12)),
- Location::new(FeatureSpec::uri("bar.tex"), Range::new_simple(1, 6, 1, 9)),
- ]
- );
+ #[tokio::test]
+ async fn entry_include_declaration() {
+ let actual_refs = FeatureTester::new()
+ .file("foo.bib", r#"@article{foo, bar = {baz}}"#)
+ .file(
+ "bar.tex",
+ indoc!(
+ r#"
+ \addbibresource{foo.bib}
+ \cite{foo}
+ "#
+ ),
+ )
+ .file("baz.tex", r#"\cite{foo}"#)
+ .main("foo.bib")
+ .position(0, 9)
+ .include_declaration()
+ .test_reference(BibtexEntryReferenceProvider)
+ .await;
+
+ let expected_refs = vec![
+ Location::new(
+ FeatureTester::uri("foo.bib").into(),
+ Range::new_simple(0, 9, 0, 12),
+ ),
+ Location::new(
+ FeatureTester::uri("bar.tex").into(),
+ Range::new_simple(1, 6, 1, 9),
+ ),
+ ];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_citation() {
- let references = test_feature(
- BibtexEntryReferenceProvider,
- FeatureSpec {
- files: vec![
- FeatureSpec::file("foo.bib", "@article{foo, bar = {baz}}"),
- FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
- FeatureSpec::file("baz.tex", "\\cite{foo}"),
- ],
- main_file: "bar.tex",
- position: Position::new(1, 8),
- include_declaration: false,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![Location::new(
- FeatureSpec::uri("bar.tex"),
- Range::new_simple(1, 6, 1, 9)
- )]
- );
+ #[tokio::test]
+ async fn citation() {
+ let actual_refs = FeatureTester::new()
+ .file("foo.bib", r#"@article{foo, bar = {baz}}"#)
+ .file(
+ "bar.tex",
+ indoc!(
+ r#"
+ \addbibresource{foo.bib}
+ \cite{foo}
+ "#
+ ),
+ )
+ .file("baz.tex", r#"\cite{foo}"#)
+ .main("bar.tex")
+ .position(1, 8)
+ .test_reference(BibtexEntryReferenceProvider)
+ .await;
+
+ let expected_refs = vec![Location::new(
+ FeatureTester::uri("bar.tex").into(),
+ Range::new_simple(1, 6, 1, 9),
+ )];
+
+ assert_eq!(actual_refs, expected_refs);
+ }
+
+ #[tokio::test]
+ async fn citation_include_declaration() {
+ let actual_refs = FeatureTester::new()
+ .file("foo.bib", r#"@article{foo, bar = {baz}}"#)
+ .file(
+ "bar.tex",
+ indoc!(
+ r#"
+ \addbibresource{foo.bib}
+ \cite{foo}
+ "#
+ ),
+ )
+ .file("baz.tex", r#"\cite{foo}"#)
+ .main("bar.tex")
+ .position(1, 8)
+ .include_declaration()
+ .test_reference(BibtexEntryReferenceProvider)
+ .await;
+
+ let expected_refs = vec![
+ Location::new(
+ FeatureTester::uri("bar.tex").into(),
+ Range::new_simple(1, 6, 1, 9),
+ ),
+ Location::new(
+ FeatureTester::uri("foo.bib").into(),
+ Range::new_simple(0, 9, 0, 12),
+ ),
+ ];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_citation_include_declaration() {
- let references = test_feature(
- BibtexEntryReferenceProvider,
- FeatureSpec {
- files: vec![
- FeatureSpec::file("foo.bib", "@article{foo, bar = {baz}}"),
- FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
- FeatureSpec::file("baz.tex", "\\cite{foo}"),
- ],
- main_file: "bar.tex",
- position: Position::new(1, 9),
- include_declaration: true,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![
- Location::new(FeatureSpec::uri("bar.tex"), Range::new_simple(1, 6, 1, 9)),
- Location::new(FeatureSpec::uri("foo.bib"), Range::new_simple(0, 9, 0, 12)),
- ]
- );
+ #[tokio::test]
+ async fn empty_latex_document() {
+ let actual_refs = FeatureTester::new()
+ .file("main.tex", "")
+ .main("main.tex")
+ .position(0, 0)
+ .test_reference(BibtexEntryReferenceProvider)
+ .await;
+
+ assert!(actual_refs.is_empty());
}
- #[test]
- fn test_empty() {
- let references = test_feature(
- BibtexEntryReferenceProvider,
- FeatureSpec {
- files: vec![FeatureSpec::file("foo.tex", "")],
- main_file: "foo.tex",
- position: Position::new(0, 0),
- ..FeatureSpec::default()
- },
- );
- assert!(references.is_empty());
+ #[tokio::test]
+ async fn empty_bibtex_document() {
+ let actual_refs = FeatureTester::new()
+ .file("main.bib", "")
+ .main("main.bib")
+ .position(0, 0)
+ .test_reference(BibtexEntryReferenceProvider)
+ .await;
+
+ assert!(actual_refs.is_empty());
}
}
diff --git a/support/texlab/src/reference/bibtex_string.rs b/support/texlab/src/reference/bibtex_string.rs
index 58888dbf11..5f9804fbd6 100644
--- a/support/texlab/src/reference/bibtex_string.rs
+++ b/support/texlab/src/reference/bibtex_string.rs
@@ -1,235 +1,253 @@
-use crate::range::RangeExt;
-use crate::syntax::*;
-use crate::workspace::*;
-use futures_boxed::boxed;
-use lsp_types::{Location, Position, ReferenceParams, Url};
-
-#[derive(Debug, PartialEq, Eq, Clone, Copy)]
+use crate::{
+ feature::{FeatureProvider, FeatureRequest},
+ protocol::{Location, Position, RangeExt, ReferenceParams, Url},
+ syntax::{
+ bibtex::{self, Visitor},
+ SyntaxNode,
+ },
+ workspace::DocumentContent,
+};
+use async_trait::async_trait;
+use petgraph::graph::NodeIndex;
+
+#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct BibtexStringReferenceProvider;
+#[async_trait]
impl FeatureProvider for BibtexStringReferenceProvider {
type Params = ReferenceParams;
type Output = Vec<Location>;
- #[boxed]
- async fn execute<'a>(&'a self, request: &'a FeatureRequest<ReferenceParams>) -> Vec<Location> {
- let mut references = Vec::new();
- if let SyntaxTree::Bibtex(tree) = &request.document().tree {
- if let Some(name) =
- Self::find_name(tree, request.params.text_document_position.position)
- {
- let uri: Url = request.document().uri.clone().into();
- if request.params.context.include_declaration {
- for string in tree.strings() {
- if let Some(string_name) = &string.name {
- if string_name.text() == name.text() {
- references.push(Location::new(uri.clone(), string_name.range()));
- }
- }
- }
+ async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
+ let mut refs = Vec::new();
+ if let DocumentContent::Bibtex(tree) = &req.current().content {
+ if let Some(name) = Self::find_name(tree, req.params.text_document_position.position) {
+ let uri: Url = req.current().uri.clone().into();
+ if req.params.context.include_declaration {
+ tree.children(tree.root)
+ .filter_map(|node| tree.as_string(node))
+ .filter_map(|string| string.name.as_ref())
+ .filter(|string| string.text() == name.text())
+ .for_each(|string| refs.push(Location::new(uri.clone(), string.range())));
}
let mut visitor = BibtexStringReferenceVisitor::default();
- visitor.visit_root(&tree.root);
+ visitor.visit(tree, tree.root);
visitor
- .references
+ .refs
.into_iter()
.filter(|reference| reference.text() == name.text())
- .map(|reference| Location::new(uri.clone(), reference.range()))
- .for_each(|reference| references.push(reference));
+ .for_each(|reference| refs.push(Location::new(uri.clone(), reference.range())));
}
}
- references
+ refs
}
}
impl BibtexStringReferenceProvider {
- fn find_name(tree: &BibtexSyntaxTree, position: Position) -> Option<&BibtexToken> {
- let mut nodes = tree.find(position);
+ fn find_name(tree: &bibtex::Tree, pos: Position) -> Option<&bibtex::Token> {
+ let mut nodes = tree.find(pos);
nodes.reverse();
- match (&nodes[0], nodes.get(1)) {
- (BibtexNode::Word(word), Some(BibtexNode::Field(_)))
- | (BibtexNode::Word(word), Some(BibtexNode::Concat(_))) => Some(&word.token),
- (BibtexNode::String(string), _) => string
+ let node0 = &tree.graph[nodes[0]];
+ let node1 = nodes.get(1).map(|node| &tree.graph[*node]);
+ match (node0, node1) {
+ (bibtex::Node::Word(word), Some(bibtex::Node::Field(_)))
+ | (bibtex::Node::Word(word), Some(bibtex::Node::Concat(_))) => Some(&word.token),
+ (bibtex::Node::String(string), _) => string
.name
.as_ref()
- .filter(|name| name.range().contains(position)),
+ .filter(|name| name.range().contains(pos)),
_ => None,
}
}
}
#[derive(Debug, Default)]
-struct BibtexStringReferenceVisitor<'a> {
- references: Vec<&'a BibtexToken>,
+pub struct BibtexStringReferenceVisitor<'a> {
+ refs: Vec<&'a bibtex::Token>,
}
-impl<'a> BibtexVisitor<'a> for BibtexStringReferenceVisitor<'a> {
- fn visit_root(&mut self, root: &'a BibtexRoot) {
- BibtexWalker::walk_root(self, root);
- }
-
- fn visit_comment(&mut self, _comment: &'a BibtexComment) {}
-
- fn visit_preamble(&mut self, preamble: &'a BibtexPreamble) {
- BibtexWalker::walk_preamble(self, preamble);
- }
-
- fn visit_string(&mut self, string: &'a BibtexString) {
- BibtexWalker::walk_string(self, string);
- }
-
- fn visit_entry(&mut self, entry: &'a BibtexEntry) {
- BibtexWalker::walk_entry(self, entry);
- }
-
- fn visit_field(&mut self, field: &'a BibtexField) {
- if let Some(BibtexContent::Word(word)) = &field.content {
- self.references.push(&word.token);
- }
-
- BibtexWalker::walk_field(self, field);
- }
-
- fn visit_word(&mut self, _word: &'a BibtexWord) {}
-
- fn visit_command(&mut self, _command: &'a BibtexCommand) {}
-
- fn visit_quoted_content(&mut self, content: &'a BibtexQuotedContent) {
- BibtexWalker::walk_quoted_content(self, content);
- }
-
- fn visit_braced_content(&mut self, content: &'a BibtexBracedContent) {
- BibtexWalker::walk_braced_content(self, content);
- }
-
- fn visit_concat(&mut self, concat: &'a BibtexConcat) {
- if let BibtexContent::Word(word) = &concat.left {
- self.references.push(&word.token);
- }
+impl<'a> bibtex::Visitor<'a> for BibtexStringReferenceVisitor<'a> {
+ fn visit(&mut self, tree: &'a bibtex::Tree, node: NodeIndex) {
+ match &tree.graph[node] {
+ bibtex::Node::Root(_)
+ | bibtex::Node::Comment(_)
+ | bibtex::Node::Preamble(_)
+ | bibtex::Node::String(_)
+ | bibtex::Node::Entry(_)
+ | bibtex::Node::Word(_)
+ | bibtex::Node::Command(_)
+ | bibtex::Node::QuotedContent(_)
+ | bibtex::Node::BracedContent(_) => (),
+ bibtex::Node::Field(_) => {
+ if let Some(word) = tree
+ .children(node)
+ .next()
+ .and_then(|content| tree.as_word(content))
+ {
+ self.refs.push(&word.token);
+ }
+ }
+ bibtex::Node::Concat(_) => {
+ let mut children = tree.children(node);
+ if let Some(word) = children.next().and_then(|left| tree.as_word(left)) {
+ self.refs.push(&word.token);
+ }
- if let Some(BibtexContent::Word(word)) = &concat.right {
- self.references.push(&word.token);
+ if let Some(word) = children.next().and_then(|right| tree.as_word(right)) {
+ self.refs.push(&word.token);
+ }
+ }
}
-
- BibtexWalker::walk_concat(self, concat);
+ tree.walk(self, node);
}
}
#[cfg(test)]
mod tests {
use super::*;
- use crate::range::RangeExt;
- use lsp_types::{Position, Range};
-
- #[test]
- fn test_definition() {
- let references = test_feature(
- BibtexStringReferenceProvider,
- FeatureSpec {
- files: vec![FeatureSpec::file(
- "foo.bib",
- "@string{foo = {Foo}}\n@string{bar = {Bar}}\n@article{baz, author = foo}",
- )],
- main_file: "foo.bib",
- position: Position::new(2, 24),
- include_declaration: false,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![Location::new(
- FeatureSpec::uri("foo.bib"),
- Range::new_simple(2, 23, 2, 26)
- )]
- );
+ use crate::{feature::FeatureTester, protocol::Range};
+ use indoc::indoc;
+
+ #[tokio::test]
+ async fn definition() {
+ let actual_refs = FeatureTester::new()
+ .file(
+ "main.bib",
+ indoc!(
+ r#"
+ @string{foo = {Foo}}
+ @string{bar = {Bar}}
+ @article{baz, author = foo}
+ "#
+ ),
+ )
+ .main("main.bib")
+ .position(2, 24)
+ .test_reference(BibtexStringReferenceProvider)
+ .await;
+
+ let expected_refs = vec![Location::new(
+ FeatureTester::uri("main.bib").into(),
+ Range::new_simple(2, 23, 2, 26),
+ )];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_definition_include_declaration() {
- let references = test_feature(
- BibtexStringReferenceProvider,
- FeatureSpec {
- files: vec![FeatureSpec::file(
- "foo.bib",
- "@string{foo = {Foo}}\n@string{bar = {Bar}}\n@article{baz, author = foo}",
- )],
- main_file: "foo.bib",
- position: Position::new(2, 24),
- include_declaration: true,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![
- Location::new(FeatureSpec::uri("foo.bib"), Range::new_simple(0, 8, 0, 11)),
- Location::new(FeatureSpec::uri("foo.bib"), Range::new_simple(2, 23, 2, 26))
- ]
- );
+ #[tokio::test]
+ async fn definition_include_declaration() {
+ let actual_refs = FeatureTester::new()
+ .file(
+ "main.bib",
+ indoc!(
+ r#"
+ @string{foo = {Foo}}
+ @string{bar = {Bar}}
+ @article{baz, author = foo}
+ "#
+ ),
+ )
+ .main("main.bib")
+ .position(2, 24)
+ .include_declaration()
+ .test_reference(BibtexStringReferenceProvider)
+ .await;
+
+ let expected_refs = vec![
+ Location::new(
+ FeatureTester::uri("main.bib").into(),
+ Range::new_simple(0, 8, 0, 11),
+ ),
+ Location::new(
+ FeatureTester::uri("main.bib").into(),
+ Range::new_simple(2, 23, 2, 26),
+ ),
+ ];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_reference() {
- let references = test_feature(
- BibtexStringReferenceProvider,
- FeatureSpec {
- files: vec![FeatureSpec::file(
- "foo.bib",
- "@string{foo = {Foo}}\n@string{bar = {Bar}}\n@article{baz, author = foo}",
- )],
- main_file: "foo.bib",
- position: Position::new(0, 10),
- include_declaration: false,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![Location::new(
- FeatureSpec::uri("foo.bib"),
- Range::new_simple(2, 23, 2, 26)
- )]
- );
+ #[tokio::test]
+ async fn reference() {
+ let actual_refs = FeatureTester::new()
+ .file(
+ "main.bib",
+ indoc!(
+ r#"
+ @string{foo = {Foo}}
+ @string{bar = {Bar}}
+ @article{baz, author = foo}
+ "#
+ ),
+ )
+ .main("main.bib")
+ .position(0, 10)
+ .test_reference(BibtexStringReferenceProvider)
+ .await;
+
+ let expected_refs = vec![Location::new(
+ FeatureTester::uri("main.bib").into(),
+ Range::new_simple(2, 23, 2, 26),
+ )];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_reference_include_declaration() {
- let references = test_feature(
- BibtexStringReferenceProvider,
- FeatureSpec {
- files: vec![FeatureSpec::file(
- "foo.bib",
- "@string{foo = {Foo}}\n@string{bar = {Bar}}\n@article{baz, author = foo}",
- )],
- main_file: "foo.bib",
- position: Position::new(0, 10),
- include_declaration: true,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![
- Location::new(FeatureSpec::uri("foo.bib"), Range::new_simple(0, 8, 0, 11)),
- Location::new(FeatureSpec::uri("foo.bib"), Range::new_simple(2, 23, 2, 26))
- ]
- );
+ #[tokio::test]
+ async fn reference_include_declaration() {
+ let actual_refs = FeatureTester::new()
+ .file(
+ "main.bib",
+ indoc!(
+ r#"
+ @string{foo = {Foo}}
+ @string{bar = {Bar}}
+ @article{baz, author = foo}
+ "#
+ ),
+ )
+ .main("main.bib")
+ .position(0, 10)
+ .include_declaration()
+ .test_reference(BibtexStringReferenceProvider)
+ .await;
+
+ let expected_refs = vec![
+ Location::new(
+ FeatureTester::uri("main.bib").into(),
+ Range::new_simple(0, 8, 0, 11),
+ ),
+ Location::new(
+ FeatureTester::uri("main.bib").into(),
+ Range::new_simple(2, 23, 2, 26),
+ ),
+ ];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_empty() {
- let references = test_feature(
- BibtexStringReferenceProvider,
- FeatureSpec {
- files: vec![FeatureSpec::file("foo.bib", "")],
- main_file: "foo.bib",
- position: Position::new(0, 0),
- include_declaration: false,
- ..FeatureSpec::default()
- },
- );
- assert!(references.is_empty());
+ #[tokio::test]
+ async fn empty_latex_document() {
+ let actual_refs = FeatureTester::new()
+ .file("main.tex", "")
+ .main("main.tex")
+ .position(0, 0)
+ .test_reference(BibtexStringReferenceProvider)
+ .await;
+
+ assert!(actual_refs.is_empty());
+ }
+
+ #[tokio::test]
+ async fn empty_bibtex_document() {
+ let actual_refs = FeatureTester::new()
+ .file("main.bib", "")
+ .main("main.bib")
+ .position(0, 0)
+ .test_reference(BibtexStringReferenceProvider)
+ .await;
+
+ assert!(actual_refs.is_empty());
}
}
diff --git a/support/texlab/src/reference/latex_label.rs b/support/texlab/src/reference/latex_label.rs
index 424429b60e..16686f6015 100644
--- a/support/texlab/src/reference/latex_label.rs
+++ b/support/texlab/src/reference/latex_label.rs
@@ -1,59 +1,58 @@
-use crate::range::RangeExt;
-use crate::syntax::*;
-use crate::workspace::*;
-use futures_boxed::boxed;
-use lsp_types::{Location, ReferenceParams};
+use crate::{
+ feature::{FeatureProvider, FeatureRequest},
+ protocol::{Location, RangeExt, ReferenceParams},
+ syntax::{latex, LatexLabelKind, SyntaxNode},
+ workspace::DocumentContent,
+};
+use async_trait::async_trait;
-#[derive(Debug, PartialEq, Eq, Clone)]
+#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct LatexLabelReferenceProvider;
+#[async_trait]
impl FeatureProvider for LatexLabelReferenceProvider {
type Params = ReferenceParams;
type Output = Vec<Location>;
- #[boxed]
- async fn execute<'a>(&'a self, request: &'a FeatureRequest<ReferenceParams>) -> Vec<Location> {
- let mut references = Vec::new();
- if let Some(definition) = Self::find_name(request) {
- for document in request.related_documents() {
- if let SyntaxTree::Latex(tree) = &document.tree {
- tree.structure
+ async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
+ let mut refs = Vec::new();
+ if let Some(def) = Self::find_name(req) {
+ for doc in req.related() {
+ if let DocumentContent::Latex(table) = &doc.content {
+ table
.labels
.iter()
- .filter(|label| Self::is_included(request, label))
- .flat_map(LatexLabel::names)
- .filter(|label| label.text() == definition)
- .map(|label| Location::new(document.uri.clone().into(), label.range()))
- .for_each(|location| references.push(location))
+ .filter(|label| Self::is_included(req, label))
+ .flat_map(|label| label.names(&table))
+ .filter(|label| label.text() == def)
+ .map(|label| Location::new(doc.uri.clone().into(), label.range()))
+ .for_each(|location| refs.push(location));
}
}
}
- references
+ refs
}
}
impl LatexLabelReferenceProvider {
- fn find_name(request: &FeatureRequest<ReferenceParams>) -> Option<&str> {
- if let SyntaxTree::Latex(tree) = &request.document().tree {
- tree.structure
+ fn find_name(req: &FeatureRequest<ReferenceParams>) -> Option<&str> {
+ let pos = req.params.text_document_position.position;
+ if let DocumentContent::Latex(table) = &req.current().content {
+ table
.labels
.iter()
- .flat_map(LatexLabel::names)
- .find(|label| {
- label
- .range()
- .contains(request.params.text_document_position.position)
- })
- .map(LatexToken::text)
+ .flat_map(|label| label.names(&table))
+ .find(|label| label.range().contains(pos))
+ .map(latex::Token::text)
} else {
None
}
}
- fn is_included(request: &FeatureRequest<ReferenceParams>, label: &LatexLabel) -> bool {
+ fn is_included(req: &FeatureRequest<ReferenceParams>, label: &latex::Label) -> bool {
match label.kind {
LatexLabelKind::Reference(_) => true,
- LatexLabelKind::Definition => request.params.context.include_declaration,
+ LatexLabelKind::Definition => req.params.context.include_declaration,
}
}
}
@@ -61,120 +60,152 @@ impl LatexLabelReferenceProvider {
#[cfg(test)]
mod tests {
use super::*;
- use crate::range::RangeExt;
- use lsp_types::{Position, Range};
-
- #[test]
- fn test_definition() {
- let references = test_feature(
- LatexLabelReferenceProvider,
- FeatureSpec {
- files: vec![
- FeatureSpec::file("foo.tex", "\\label{foo}"),
- FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"),
- FeatureSpec::file("baz.tex", "\\ref{foo}"),
- ],
- main_file: "foo.tex",
- include_declaration: false,
- position: Position::new(0, 8),
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![Location::new(
- FeatureSpec::uri("bar.tex"),
- Range::new_simple(1, 5, 1, 8)
- )]
- );
+ use crate::{feature::FeatureTester, protocol::Range};
+ use indoc::indoc;
+
+ #[tokio::test]
+ async fn definition() {
+ let actual_refs = FeatureTester::new()
+ .file("foo.tex", r#"\label{foo}"#)
+ .file(
+ "bar.tex",
+ indoc!(
+ r#"
+ \input{foo.tex}
+ \ref{foo}
+ "#
+ ),
+ )
+ .file("baz.tex", r#"\ref{foo}"#)
+ .main("foo.tex")
+ .position(0, 8)
+ .test_reference(LatexLabelReferenceProvider)
+ .await;
+
+ let expected_refs = vec![Location::new(
+ FeatureTester::uri("bar.tex").into(),
+ Range::new_simple(1, 5, 1, 8),
+ )];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_definition_include_declaration() {
- let references = test_feature(
- LatexLabelReferenceProvider,
- FeatureSpec {
- files: vec![
- FeatureSpec::file("foo.tex", "\\label{foo}"),
- FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"),
- FeatureSpec::file("baz.tex", "\\ref{foo}"),
- ],
- main_file: "foo.tex",
- include_declaration: true,
- position: Position::new(0, 8),
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![
- Location::new(FeatureSpec::uri("foo.tex"), Range::new_simple(0, 7, 0, 10)),
- Location::new(FeatureSpec::uri("bar.tex"), Range::new_simple(1, 5, 1, 8)),
- ]
- );
+ #[tokio::test]
+ async fn definition_include_declaration() {
+ let actual_refs = FeatureTester::new()
+ .file("foo.tex", r#"\label{foo}"#)
+ .file(
+ "bar.tex",
+ indoc!(
+ r#"
+ \input{foo.tex}
+ \ref{foo}
+ "#
+ ),
+ )
+ .file("baz.tex", r#"\ref{foo}"#)
+ .main("foo.tex")
+ .position(0, 8)
+ .include_declaration()
+ .test_reference(LatexLabelReferenceProvider)
+ .await;
+
+ let expected_refs = vec![
+ Location::new(
+ FeatureTester::uri("foo.tex").into(),
+ Range::new_simple(0, 7, 0, 10),
+ ),
+ Location::new(
+ FeatureTester::uri("bar.tex").into(),
+ Range::new_simple(1, 5, 1, 8),
+ ),
+ ];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_reference() {
- let references = test_feature(
- LatexLabelReferenceProvider,
- FeatureSpec {
- files: vec![
- FeatureSpec::file("foo.tex", "\\label{foo}"),
- FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"),
- FeatureSpec::file("baz.tex", "\\ref{foo}"),
- ],
- main_file: "bar.tex",
- position: Position::new(1, 7),
- include_declaration: false,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![Location::new(
- FeatureSpec::uri("bar.tex"),
- Range::new_simple(1, 5, 1, 8)
- ),]
- );
+ #[tokio::test]
+ async fn reference() {
+ let actual_refs = FeatureTester::new()
+ .file("foo.tex", r#"\label{foo}"#)
+ .file(
+ "bar.tex",
+ indoc!(
+ r#"
+ \input{foo.tex}
+ \ref{foo}
+ "#
+ ),
+ )
+ .file("baz.tex", r#"\ref{foo}"#)
+ .main("bar.tex")
+ .position(1, 7)
+ .test_reference(LatexLabelReferenceProvider)
+ .await;
+
+ let expected_refs = vec![Location::new(
+ FeatureTester::uri("bar.tex").into(),
+ Range::new_simple(1, 5, 1, 8),
+ )];
+
+ assert_eq!(actual_refs, expected_refs);
}
- #[test]
- fn test_reference_include_declaration() {
- let references = test_feature(
- LatexLabelReferenceProvider,
- FeatureSpec {
- files: vec![
- FeatureSpec::file("foo.tex", "\\label{foo}"),
- FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"),
- FeatureSpec::file("baz.tex", "\\ref{foo}"),
- ],
- main_file: "bar.tex",
- position: Position::new(1, 7),
- include_declaration: true,
- ..FeatureSpec::default()
- },
- );
- assert_eq!(
- references,
- vec![
- Location::new(FeatureSpec::uri("bar.tex"), Range::new_simple(1, 5, 1, 8)),
- Location::new(FeatureSpec::uri("foo.tex"), Range::new_simple(0, 7, 0, 10)),
- ]
- );
+ #[tokio::test]
+ async fn reference_include_declaration() {
+ let actual_refs = FeatureTester::new()
+ .file("foo.tex", r#"\label{foo}"#)
+ .file(
+ "bar.tex",
+ indoc!(
+ r#"
+ \input{foo.tex}
+ \ref{foo}
+ "#
+ ),
+ )
+ .file("baz.tex", r#"\ref{foo}"#)
+ .main("bar.tex")
+ .position(1, 7)
+ .include_declaration()
+ .test_reference(LatexLabelReferenceProvider)
+ .await;
+
+ let expected_refs = vec![
+ Location::new(
+ FeatureTester::uri("bar.tex").into(),
+ Range::new_simple(1, 5, 1, 8),
+ ),
+ Location::new(
+ FeatureTester::uri("foo.tex").into(),
+ Range::new_simple(0, 7, 0, 10),
+ ),
+ ];
+
+ assert_eq!(actual_refs, expected_refs);
+ }
+
+ #[tokio::test]
+ async fn empty_latex_document() {
+ let actual_refs = FeatureTester::new()
+ .file("main.tex", "")
+ .main("main.tex")
+ .position(0, 0)
+ .test_reference(LatexLabelReferenceProvider)
+ .await;
+
+ assert!(actual_refs.is_empty());
}
- #[test]
- fn test_bibtex() {
- let references = test_feature(
- LatexLabelReferenceProvider,
- FeatureSpec {
- files: vec![FeatureSpec::file("foo.bib", "")],
- main_file: "foo.bib",
- position: Position::new(0, 0),
- ..FeatureSpec::default()
- },
- );
- assert!(references.is_empty());
+ #[tokio::test]
+ async fn empty_bibtex_document() {
+ let actual_refs = FeatureTester::new()
+ .file("main.bib", "")
+ .main("main.bib")
+ .position(0, 0)
+ .test_reference(LatexLabelReferenceProvider)
+ .await;
+
+ assert!(actual_refs.is_empty());
}
}
diff --git a/support/texlab/src/reference/mod.rs b/support/texlab/src/reference/mod.rs
index 120807a9f1..fc4379d394 100644
--- a/support/texlab/src/reference/mod.rs
+++ b/support/texlab/src/reference/mod.rs
@@ -2,12 +2,15 @@ mod bibtex_entry;
mod bibtex_string;
mod latex_label;
-use self::bibtex_entry::BibtexEntryReferenceProvider;
-use self::bibtex_string::BibtexStringReferenceProvider;
-use self::latex_label::LatexLabelReferenceProvider;
-use crate::workspace::*;
-use futures_boxed::boxed;
-use lsp_types::{Location, ReferenceParams};
+use self::{
+ bibtex_entry::BibtexEntryReferenceProvider, bibtex_string::BibtexStringReferenceProvider,
+ latex_label::LatexLabelReferenceProvider,
+};
+use crate::{
+ feature::{ConcatProvider, FeatureProvider, FeatureRequest},
+ protocol::{Location, ReferenceParams},
+};
+use async_trait::async_trait;
pub struct ReferenceProvider {
provider: ConcatProvider<ReferenceParams, Location>,
@@ -31,12 +34,12 @@ impl Default for ReferenceProvider {
}
}
+#[async_trait]
impl FeatureProvider for ReferenceProvider {
type Params = ReferenceParams;
type Output = Vec<Location>;
- #[boxed]
- async fn execute<'a>(&'a self, request: &'a FeatureRequest<ReferenceParams>) -> Vec<Location> {
- self.provider.execute(request).await
+ async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
+ self.provider.execute(req).await
}
}