Skip to content

Commit

Permalink
Provide completion for BibTeX commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 13, 2019
1 parent fe22440 commit f64c933
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/completion/bibtex/kernel_command.rs
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);
}
}
1 change: 1 addition & 0 deletions src/completion/bibtex/mod.rs
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;
2 changes: 2 additions & 0 deletions src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod quality;

use self::bibtex::entry_type::BibtexEntryTypeCompletionProvider;
use self::bibtex::field_name::BibtexFieldNameCompletionProvider;
use self::bibtex::kernel_command::BibtexKernelCommandCompletionProvider;
use self::latex::begin_command::LatexBeginCommandCompletionProvider;
use self::latex::citation::LatexCitationCompletionProvider;
use self::latex::color::LatexColorCompletionProvider;
Expand Down Expand Up @@ -36,6 +37,7 @@ impl CompletionProvider {
&request,
BibtexEntryTypeCompletionProvider,
BibtexFieldNameCompletionProvider,
BibtexKernelCommandCompletionProvider,
LatexKernelEnvironmentCompletionProvider,
LatexPgfLibraryCompletionProvider,
LatexTikzLibraryCompletionProvider,
Expand Down

0 comments on commit f64c933

Please sign in to comment.