Improve formatting of function autocompletion labels in Rust

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-02-02 18:43:55 +01:00
parent 8d7815456c
commit 8149bcbb13
7 changed files with 63 additions and 21 deletions

View file

@ -119,6 +119,7 @@ pub struct Diagnostic {
pub struct Completion<T> {
pub old_range: Range<T>,
pub new_text: String,
pub label: Option<String>,
pub lsp_completion: lsp::CompletionItem,
}
@ -203,6 +204,7 @@ pub trait File {
&self,
buffer_id: u64,
position: Anchor,
language: Option<Arc<Language>>,
cx: &mut MutableAppContext,
) -> Task<Result<Vec<Completion<Anchor>>>>;
@ -286,6 +288,7 @@ impl File for FakeFile {
&self,
_: u64,
_: Anchor,
_: Option<Arc<Language>>,
_: &mut MutableAppContext,
) -> Task<Result<Vec<Completion<Anchor>>>> {
Task::ready(Ok(Default::default()))
@ -1800,10 +1803,11 @@ impl Buffer {
} else {
return Task::ready(Ok(Default::default()));
};
let language = self.language.clone();
if let Some(file) = file.as_local() {
let server = if let Some(lang) = self.language_server.as_ref() {
lang.server.clone()
let server = if let Some(language_server) = self.language_server.as_ref() {
language_server.server.clone()
} else {
return Task::ready(Ok(Default::default()));
};
@ -1850,6 +1854,7 @@ impl Buffer {
Some(Completion {
old_range: this.anchor_before(old_range.start)..this.anchor_after(old_range.end),
new_text,
label: language.as_ref().and_then(|l| l.label_for_completion(&lsp_completion)),
lsp_completion,
})
} else {
@ -1859,7 +1864,12 @@ impl Buffer {
})
})
} else {
file.completions(self.remote_id(), self.anchor_before(position), cx.as_mut())
file.completions(
self.remote_id(),
self.anchor_before(position),
language,
cx.as_mut(),
)
}
}
@ -2668,7 +2678,7 @@ impl Default for Diagnostic {
impl<T> Completion<T> {
pub fn label(&self) -> &str {
&self.lsp_completion.label
self.label.as_deref().unwrap_or(&self.lsp_completion.label)
}
pub fn filter_range(&self) -> Range<usize> {

View file

@ -43,8 +43,11 @@ pub trait ToLspPosition {
fn to_lsp_position(self) -> lsp::Position;
}
pub trait DiagnosticProcessor: 'static + Send + Sync {
pub trait LspPostProcessor: 'static + Send + Sync {
fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
fn label_for_completion(&self, _completion: &lsp::CompletionItem) -> Option<String> {
None
}
}
#[derive(Default, Deserialize)]
@ -77,7 +80,7 @@ pub struct BracketPair {
pub struct Language {
pub(crate) config: LanguageConfig,
pub(crate) grammar: Option<Arc<Grammar>>,
pub(crate) diagnostic_processor: Option<Box<dyn DiagnosticProcessor>>,
pub(crate) lsp_post_processor: Option<Box<dyn LspPostProcessor>>,
}
pub struct Grammar {
@ -144,7 +147,7 @@ impl Language {
highlight_map: Default::default(),
})
}),
diagnostic_processor: None,
lsp_post_processor: None,
}
}
@ -188,8 +191,8 @@ impl Language {
Ok(self)
}
pub fn with_diagnostics_processor(mut self, processor: impl DiagnosticProcessor) -> Self {
self.diagnostic_processor = Some(Box::new(processor));
pub fn with_lsp_post_processor(mut self, processor: impl LspPostProcessor) -> Self {
self.lsp_post_processor = Some(Box::new(processor));
self
}
@ -241,11 +244,17 @@ impl Language {
}
pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
if let Some(processor) = self.diagnostic_processor.as_ref() {
if let Some(processor) = self.lsp_post_processor.as_ref() {
processor.process_diagnostics(diagnostics);
}
}
pub fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option<String> {
self.lsp_post_processor
.as_ref()
.and_then(|p| p.label_for_completion(completion))
}
pub fn brackets(&self) -> &[BracketPair] {
&self.config.brackets
}

View file

@ -1,4 +1,4 @@
use crate::{diagnostic_set::DiagnosticEntry, Completion, Diagnostic, Operation};
use crate::{diagnostic_set::DiagnosticEntry, Completion, Diagnostic, Language, Operation};
use anyhow::{anyhow, Result};
use clock::ReplicaId;
use collections::HashSet;
@ -387,7 +387,10 @@ pub fn serialize_completion(completion: &Completion<Anchor>) -> proto::Completio
}
}
pub fn deserialize_completion(completion: proto::Completion) -> Result<Completion<Anchor>> {
pub fn deserialize_completion(
completion: proto::Completion,
language: Option<&Arc<Language>>,
) -> Result<Completion<Anchor>> {
let old_start = completion
.old_start
.and_then(deserialize_anchor)
@ -396,9 +399,11 @@ pub fn deserialize_completion(completion: proto::Completion) -> Result<Completio
.old_end
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid old end"))?;
let lsp_completion = serde_json::from_slice(&completion.lsp_completion)?;
Ok(Completion {
old_range: old_start..old_end,
new_text: completion.new_text,
lsp_completion: serde_json::from_slice(&completion.lsp_completion)?,
label: language.and_then(|l| l.label_for_completion(&lsp_completion)),
lsp_completion,
})
}