-
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.
Provide completion for BibTeX commands
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
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,104 @@ | ||
use crate::completion::factory; | ||
use crate::completion::factory::LatexComponentId; | ||
use crate::completion::latex::kernel_primitives::KERNEL_COMMANDS; | ||
use crate::feature::FeatureRequest; | ||
use crate::syntax::bibtex::{BibtexFinder, BibtexNode, BibtexVisitor}; | ||
use crate::syntax::text::SyntaxNode; | ||
use crate::syntax::SyntaxTree; | ||
use lsp_types::{CompletionItem, CompletionParams}; | ||
use std::borrow::Cow; | ||
|
||
pub struct BibtexKernelCommandCompletionProvider; | ||
|
||
impl BibtexKernelCommandCompletionProvider { | ||
pub async fn execute(request: &FeatureRequest<CompletionParams>) -> Vec<CompletionItem> { | ||
if let SyntaxTree::Bibtex(tree) = &request.document.tree { | ||
let mut finder = BibtexFinder::new(request.params.position); | ||
finder.visit_root(&tree.root); | ||
if let Some(BibtexNode::Command(command)) = finder.results.last() { | ||
if command.token.range().contains(request.params.position) | ||
&& command.token.start().character != request.params.position.character | ||
{ | ||
return Self::generate_items(); | ||
} | ||
} | ||
} | ||
Vec::new() | ||
} | ||
|
||
fn generate_items() -> Vec<CompletionItem> { | ||
KERNEL_COMMANDS | ||
.iter() | ||
.map(|command| factory::create_command(Cow::from(*command), &LatexComponentId::Kernel)) | ||
.collect() | ||
} | ||
} | ||
|
||
#[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_command() { | ||
let items = test_feature!( | ||
BibtexKernelCommandCompletionProvider, | ||
FeatureSpec { | ||
files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar=\n\\}")], | ||
main_file: "foo.bib", | ||
position: Position::new(1, 1), | ||
new_name: "", | ||
component_database: LatexComponentDatabase::default(), | ||
} | ||
); | ||
assert_eq!(items.len() > 0, true); | ||
} | ||
|
||
#[test] | ||
fn test_start_of_command() { | ||
let items = test_feature!( | ||
BibtexKernelCommandCompletionProvider, | ||
FeatureSpec { | ||
files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar=\n\\}")], | ||
main_file: "foo.bib", | ||
position: Position::new(1, 0), | ||
new_name: "", | ||
component_database: LatexComponentDatabase::default(), | ||
} | ||
); | ||
assert_eq!(items.len() == 0, true); | ||
} | ||
|
||
#[test] | ||
fn test_inside_text() { | ||
let items = test_feature!( | ||
BibtexKernelCommandCompletionProvider, | ||
FeatureSpec { | ||
files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar=\n}")], | ||
main_file: "foo.bib", | ||
position: Position::new(1, 0), | ||
new_name: "", | ||
component_database: LatexComponentDatabase::default(), | ||
} | ||
); | ||
assert_eq!(items.len() == 0, true); | ||
} | ||
|
||
#[test] | ||
fn test_latex() { | ||
let items = test_feature!( | ||
BibtexKernelCommandCompletionProvider, | ||
FeatureSpec { | ||
files: vec![FeatureSpec::file("foo.tex", "\\")], | ||
main_file: "foo.tex", | ||
position: Position::new(0, 1), | ||
new_name: "", | ||
component_database: LatexComponentDatabase::default(), | ||
} | ||
); | ||
assert_eq!(items.len() == 0, true); | ||
} | ||
} |
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,2 +1,3 @@ | ||
pub mod entry_type; | ||
pub mod field_name; | ||
pub mod kernel_command; |
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