Skip to content

Commit

Permalink
Provide glyph if we have it as a seperate argument
Browse files Browse the repository at this point in the history
Put the glyph in CompletionItem::detail.
Also, don't send images at all if we don't support markdown.
  • Loading branch information
or17191 committed Aug 18, 2019
1 parent 04cc2a5 commit 3486ac5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 65 deletions.
5 changes: 3 additions & 2 deletions src/completion/bibtex/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::completion::factory::{self, LatexComponentId, LatexDocumentation};
use crate::completion::factory::{self, LatexComponentId};
use crate::completion::DATABASE;
use crate::syntax::*;
use crate::workspace::*;
Expand Down Expand Up @@ -30,7 +30,8 @@ impl FeatureProvider for BibtexCommandCompletionProvider {
let item = factory::command(
request,
(&command.name).into(),
LatexDocumentation::from_opt(command.image.as_ref().map(AsRef::as_ref)),
command.image.as_ref().map(AsRef::as_ref),
command.glyph.as_ref().map(AsRef::as_ref),
text_edit,
&component,
);
Expand Down
69 changes: 29 additions & 40 deletions src/completion/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,32 @@ impl<'a> LatexComponentId<'a> {
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum LatexDocumentation {
None,
Glyph(&'static str),
Image(&'static str),
}

impl LatexDocumentation {
pub fn from_opt(opt: Option<&'static str>) -> Self {
match opt {
None => LatexDocumentation::None,
Some(str) => LatexDocumentation::Image(str),
}
}

pub fn documentation(&self, name: &str) -> Option<Documentation> {
match self {
LatexDocumentation::None => None,
LatexDocumentation::Glyph(glyph) =>
Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::PlainText,
value: format!("Glyph: {}", glyph).into(),
})),
LatexDocumentation::Image(image) => Some(image_documentation(&name, image)),
}
}
fn supports_images(request: &FeatureRequest<CompletionParams>) -> bool {
request
.client_capabilities
.text_document
.as_ref()
.and_then(|cap| cap.completion.as_ref())
.and_then(|cap| cap.completion_item.as_ref())
.and_then(|cap| cap.documentation_format.as_ref())
.map_or(true, |formats| formats.contains(&MarkupKind::Markdown))
}

pub fn command(
request: &FeatureRequest<CompletionParams>,
name: Cow<'static, str>,
image: LatexDocumentation,
image: Option<&str>,
glyph: Option<&str>,
text_edit: TextEdit,
component: &LatexComponentId,
) -> CompletionItem {
let detail = glyph.map_or_else(|| component.detail(), |glyph| format!("{}, {}", glyph, component.detail()).into());
CompletionItem {
kind: Some(adjust_kind(request, CompletionItemKind::Function)),
data: Some(CompletionItemData::Command.into()),
documentation: image.documentation(&name),
documentation: image.and_then(|image| image_documentation(&request, &name, image)),
text_edit: Some(text_edit),
..CompletionItem::new_simple(name, component.detail())
..CompletionItem::new_simple(name, detail)
}
}

Expand All @@ -115,7 +100,7 @@ pub fn command_snippet(
CompletionItem {
kind: Some(adjust_kind(request, CompletionItemKind::Snippet)),
data: Some(CompletionItemData::CommandSnippet.into()),
documentation: image.map(|image| image_documentation(&name, image)),
documentation: image.and_then(|image| image_documentation(&request, &name, image)),
insert_text: Some(template.into()),
insert_text_format: Some(InsertTextFormat::Snippet),
..CompletionItem::new_simple(name.into(), component.detail())
Expand Down Expand Up @@ -375,20 +360,24 @@ pub fn argument(
kind: Some(adjust_kind(request, CompletionItemKind::Field)),
data: Some(CompletionItemData::Argument.into()),
text_edit: Some(text_edit),
documentation: image.map(|image| image_documentation(&name, image)),
documentation: image.and_then(|image| image_documentation(&request, &name, image)),
..CompletionItem::default()
}
}

fn image_documentation(name: &str, image: &str) -> Documentation {
Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: format!(
"![{}](data:image/png;base64,{}|width=48,height=48)",
name, image
)
.into(),
})
fn image_documentation(request: &FeatureRequest<CompletionParams>, name: &str, image: &str) -> Option<Documentation> {
if supports_images(request) {
Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: format!(
"![{}](data:image/png;base64,{}|width=48,height=48)",
name, image
)
.into(),
}))
} else {
None
}
}

fn adjust_kind(
Expand Down
24 changes: 3 additions & 21 deletions src/completion/latex/component.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::combinators;
use crate::completion::factory::{self, LatexComponentId, LatexDocumentation};
use crate::completion::factory::{self, LatexComponentId};
use crate::completion::DATABASE;
use crate::workspace::*;
use futures_boxed::boxed;
Expand All @@ -14,14 +14,6 @@ impl FeatureProvider for LatexComponentCommandCompletionProvider {

#[boxed]
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
let supports_images: bool = request
.client_capabilities
.text_document
.as_ref()
.and_then(|cap| cap.completion.as_ref())
.and_then(|cap| cap.completion_item.as_ref())
.and_then(|cap| cap.documentation_format.as_ref())
.map_or(true, |formats| formats.contains(&MarkupKind::Markdown));
combinators::command(request, async move |command| {
let range = command.short_name_range();
let mut items = Vec::new();
Expand All @@ -30,21 +22,11 @@ impl FeatureProvider for LatexComponentCommandCompletionProvider {
let id = LatexComponentId::Component(file_names);
for command in &component.commands {
let text_edit = TextEdit::new(range, (&command.name).into());
let doc: LatexDocumentation = if supports_images {
match &command.image {
None => LatexDocumentation::None,
Some(image) => LatexDocumentation::Image(&image),
}
} else {
match &command.glyph {
None => LatexDocumentation::None,
Some(glyph) => LatexDocumentation::Glyph(&glyph),
}
};
let item = factory::command(
request,
(&command.name).into(),
doc,
command.image.as_ref().map(AsRef::as_ref),
command.glyph.as_ref().map(AsRef::as_ref),
text_edit,
&id,
);
Expand Down
5 changes: 3 additions & 2 deletions src/completion/latex/user.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::combinators;
use crate::completion::factory::{self, LatexComponentId, LatexDocumentation};
use crate::completion::factory::{self, LatexComponentId};
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
Expand Down Expand Up @@ -32,7 +32,8 @@ impl FeatureProvider for LatexUserCommandCompletionProvider {
factory::command(
request,
command.to_owned().into(),
LatexDocumentation::None,
None,
None,
text_edit,
&LatexComponentId::User,
)
Expand Down

0 comments on commit 3486ac5

Please sign in to comment.