Skip to content

Commit

Permalink
Show BibTeX entry when completing citations
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 12, 2019
1 parent cba8df5 commit 13e8aad
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
23 changes: 14 additions & 9 deletions src/completion/factory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use lsp_types::{CompletionItem, CompletionItemKind, InsertTextFormat, Uri};
use crate::formatting::{BibtexFormatter, BibtexFormattingOptions};
use crate::syntax::bibtex::{BibtexEntry, BibtexToken};
use lsp_types::*;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::path::Path;
Expand Down Expand Up @@ -36,11 +38,7 @@ pub enum CompletionItemData {
Class,
EntryKind,
FieldName,
Citation {
#[serde(with = "url_serde")]
uri: Uri,
key: String,
},
Citation,
CommandSymbol,
ArgumentSymbol,
}
Expand Down Expand Up @@ -162,11 +160,18 @@ pub fn create_class(name: Cow<'static, str>) -> CompletionItem {
}
}

pub fn create_citation(uri: Uri, key: String) -> CompletionItem {
pub fn create_citation(entry: &BibtexEntry, key: &str) -> CompletionItem {
let mut formatter = BibtexFormatter::new(BibtexFormattingOptions::new(2, true, 35));
formatter.format_entry(entry);
let markdown = format!("```bibtex\n{}\n```", formatter.output);
CompletionItem {
label: Cow::from(key.clone()),
label: Cow::from(key.to_owned()),
kind: Some(CompletionItemKind::Field),
data: Some(CompletionItemData::Citation { uri, key }.into()),
data: Some(CompletionItemData::Citation.into()),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: Cow::from(markdown),
})),
..CompletionItem::default()
}
}
5 changes: 1 addition & 4 deletions src/completion/latex/citation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ impl LatexCitationCompletionProvider {
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.push(factory::create_citation(entry, key.text()));
}
}
}
Expand Down
70 changes: 38 additions & 32 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Default for BibtexFormattingOptions {
pub struct BibtexFormatter {
pub options: BibtexFormattingOptions,
indent: String,
output: String,
pub output: String,
}

impl BibtexFormatter {
Expand Down Expand Up @@ -61,39 +61,45 @@ impl BibtexFormatter {
let text = comment.token.text();
self.output.push_str(text);
}
BibtexDeclaration::Preamble(preamble) => {
self.format_token(&preamble.kind);
self.output.push('{');
if let Some(ref content) = preamble.content {
self.format_content(content, self.output.chars().count());
self.output.push('}');
}
}
BibtexDeclaration::String(string) => {
self.format_token(&string.kind);
self.output.push('{');
if let Some(ref name) = string.name {
self.output.push_str(name.text());
self.output.push_str(" = ");
if let Some(ref value) = string.value {
self.format_content(value, self.output.chars().count());
self.output.push('}');
}
}
BibtexDeclaration::Preamble(preamble) => self.format_preamble(preamble),
BibtexDeclaration::String(string) => self.format_string(string),
BibtexDeclaration::Entry(entry) => self.format_entry(entry),
}
}

pub fn format_preamble(&mut self, preamble: &BibtexPreamble) {
self.format_token(&preamble.kind);
self.output.push('{');
if let Some(ref content) = preamble.content {
self.format_content(content, self.output.chars().count());
self.output.push('}');
}
}

pub fn format_string(&mut self, string: &BibtexString) {
self.format_token(&string.kind);
self.output.push('{');
if let Some(ref name) = string.name {
self.output.push_str(name.text());
self.output.push_str(" = ");
if let Some(ref value) = string.value {
self.format_content(value, self.output.chars().count());
self.output.push('}');
}
BibtexDeclaration::Entry(entry) => {
self.format_token(&entry.kind);
self.output.push('{');
if let Some(ref key) = entry.key {
self.output.push_str(key.text());
self.output.push(',');
self.output.push('\n');
for field in &entry.fields {
self.format_field(field);
}
self.output.push('}');
}
}
}

pub fn format_entry(&mut self, entry: &BibtexEntry) {
self.format_token(&entry.kind);
self.output.push('{');
if let Some(ref key) = entry.key {
self.output.push_str(key.text());
self.output.push(',');
self.output.push('\n');
for field in &entry.fields {
self.format_field(field);
}
self.output.push('}');
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/syntax/bibtex/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ impl BibtexEntry {
right,
}
}

pub fn is_comment(&self) -> bool {
self.kind.text().to_lowercase() == "@comment"
}
}

impl SyntaxNode for BibtexEntry {
Expand Down

0 comments on commit 13e8aad

Please sign in to comment.