Skip to content

Commit

Permalink
Provide completion for citations
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 9, 2019
1 parent bf97ca5 commit 9c99f45
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ clap = "2.33"
indoc = "0.3.1"
regex = "1.1.6"
url = "1.7.2"
url_serde = "0.2.0"
path-clean = "0.1.0"
itertools = "0.8.0"
walkdir = "2"
19 changes: 16 additions & 3 deletions src/completion/factory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lsp_types::{CompletionItem, CompletionItemKind, InsertTextFormat};
use lsp_types::{CompletionItem, CompletionItemKind, InsertTextFormat, Uri};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::path::Path;
Expand All @@ -20,7 +20,7 @@ impl LatexComponentId {
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub enum CompletionItemData {
Snippet,
Command,
Expand All @@ -36,7 +36,11 @@ pub enum CompletionItemData {
Class,
EntryKind,
FieldName,
Citation,
Citation {
#[serde(with = "url_serde")]
uri: Uri,
key: String,
},
CommandSymbol,
ArgumentSymbol,
}
Expand Down Expand Up @@ -157,3 +161,12 @@ pub fn create_class(name: Cow<'static, str>) -> CompletionItem {
..CompletionItem::default()
}
}

pub fn create_citation(uri: Uri, key: String) -> CompletionItem {
CompletionItem {
label: Cow::from(key.clone()),
kind: Some(CompletionItemKind::Field),
data: Some(CompletionItemData::Citation { uri, key }.into()),
..CompletionItem::default()
}
}
85 changes: 85 additions & 0 deletions src/completion/latex/citation.rs
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());
}
}
1 change: 1 addition & 0 deletions src/completion/latex/mod.rs
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;
Expand Down
5 changes: 4 additions & 1 deletion src/completion/latex/user_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ impl LatexUserCommandCompletionProvider {
.map(|command| &command.name.text()[1..])
.unique()
.map(|name| {
factory::create_command(Cow::from(name.to_owned()), &LatexComponentId::Unknown)
factory::create_command(
Cow::from(name.to_owned()),
&LatexComponentId::Unknown,
)
})
.for_each(|item| items.push(item));
}
Expand Down
2 changes: 2 additions & 0 deletions src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod latex;
mod quality;

use self::latex::begin_command::LatexBeginCommandCompletionProvider;
use self::latex::citation::LatexCitationCompletionProvider;
use self::latex::color::LatexColorCompletionProvider;
use self::latex::color_model::LatexColorModelCompletionProvider;
use self::latex::include::LatexIncludeCompletionProvider;
Expand Down Expand Up @@ -36,6 +37,7 @@ impl CompletionProvider {
LatexColorCompletionProvider,
LatexColorModelCompletionProvider,
LatexLabelCompletionProvider,
LatexCitationCompletionProvider,
LatexIncludeCompletionProvider,
LatexBeginCommandCompletionProvider,
LatexTikzCommandCompletionProvider,
Expand Down

0 comments on commit 9c99f45

Please sign in to comment.