-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use crate::completion::factory; | ||
use crate::completion::latex::combinators::LatexCombinators; | ||
use crate::feature::FeatureRequest; | ||
use crate::syntax::bibtex::BibtexDeclaration; | ||
use crate::syntax::latex::CITATION_COMMANDS; | ||
use crate::workspace::SyntaxTree; | ||
use lsp_types::{CompletionItem, CompletionParams}; | ||
|
||
pub struct LatexCitationCompletionProvider; | ||
|
||
impl LatexCitationCompletionProvider { | ||
pub async fn execute(request: &FeatureRequest<CompletionParams>) -> Vec<CompletionItem> { | ||
await!(LatexCombinators::argument( | ||
request, | ||
CITATION_COMMANDS, | ||
0, | ||
async move |_| { | ||
let mut items = Vec::new(); | ||
for document in &request.related_documents { | ||
if let SyntaxTree::Bibtex(tree) = &document.tree { | ||
for declaration in &tree.root.children { | ||
if let BibtexDeclaration::Entry(entry) = declaration { | ||
if let Some(key) = &entry.key { | ||
items.push(factory::create_citation( | ||
document.uri.clone(), | ||
key.text().to_owned(), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
items | ||
} | ||
)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::completion::latex::data::types::LatexComponentDatabase; | ||
use crate::feature::FeatureSpec; | ||
use crate::test_feature; | ||
use lsp_types::Position; | ||
|
||
#[test] | ||
fn test_inside_cite() { | ||
let items = test_feature!( | ||
LatexCitationCompletionProvider, | ||
FeatureSpec { | ||
files: vec![ | ||
FeatureSpec::file("foo.tex", "\\addbibresource{bar.bib}\n\\cite{}"), | ||
FeatureSpec::file("bar.bib", "@article{foo,}"), | ||
FeatureSpec::file("baz.bib", "@article{bar,}") | ||
], | ||
main_file: "foo.tex", | ||
position: Position::new(1, 6), | ||
new_name: "", | ||
component_database: LatexComponentDatabase::default(), | ||
} | ||
); | ||
assert_eq!(items.len(), 1); | ||
assert_eq!("foo", items[0].label); | ||
} | ||
|
||
#[test] | ||
fn test_outside_cite() { | ||
let items = test_feature!( | ||
LatexCitationCompletionProvider, | ||
FeatureSpec { | ||
files: vec![ | ||
FeatureSpec::file("foo.tex", "\\addbibresource{bar.bib}\n\\cite{}"), | ||
FeatureSpec::file("bar.bib", "@article{foo,}"), | ||
FeatureSpec::file("baz.bib", "@article{bar,}") | ||
], | ||
main_file: "foo.tex", | ||
position: Position::new(1, 7), | ||
new_name: "", | ||
component_database: LatexComponentDatabase::default(), | ||
} | ||
); | ||
assert_eq!(items, Vec::new()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod begin_command; | ||
pub mod citation; | ||
pub mod color; | ||
pub mod color_model; | ||
mod combinators; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters