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

@ -883,6 +883,7 @@ impl MultiBuffer {
completion.old_range.end,
),
new_text: completion.new_text,
label: completion.label,
lsp_completion: completion.lsp_completion,
})
.collect()
@ -939,6 +940,7 @@ impl MultiBuffer {
old_range: completion.old_range.start.text_anchor
..completion.old_range.end.text_anchor,
new_text: completion.new_text,
label: completion.label,
lsp_completion: completion.lsp_completion,
},
true,

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,
})
}

View file

@ -1752,11 +1752,13 @@ impl Project {
.get(&sender_id)
.and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id).cloned())
.ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))?;
let language = buffer.read(cx).language();
let completion = language::proto::deserialize_completion(
envelope
.payload
.completion
.ok_or_else(|| anyhow!("invalid position"))?,
language,
)?;
cx.spawn(|_, mut cx| async move {
match buffer

View file

@ -14,7 +14,9 @@ use gpui::{
executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
Task,
};
use language::{Anchor, Buffer, Completion, DiagnosticEntry, Operation, PointUtf16, Rope};
use language::{
Anchor, Buffer, Completion, DiagnosticEntry, Language, Operation, PointUtf16, Rope,
};
use lazy_static::lazy_static;
use parking_lot::Mutex;
use postage::{
@ -1425,6 +1427,7 @@ impl language::File for File {
&self,
buffer_id: u64,
position: Anchor,
language: Option<Arc<Language>>,
cx: &mut MutableAppContext,
) -> Task<Result<Vec<Completion<Anchor>>>> {
let worktree = self.worktree.read(cx);
@ -1448,7 +1451,9 @@ impl language::File for File {
response
.completions
.into_iter()
.map(language::proto::deserialize_completion)
.map(|completion| {
language::proto::deserialize_completion(completion, language.as_ref())
})
.collect()
})
}

View file

@ -9,9 +9,9 @@ use std::{str, sync::Arc};
#[folder = "languages"]
struct LanguageDir;
struct RustDiagnosticProcessor;
struct RustPostProcessor;
impl DiagnosticProcessor for RustDiagnosticProcessor {
impl LspPostProcessor for RustPostProcessor {
fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
lazy_static! {
static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`$").unwrap();
@ -31,6 +31,15 @@ impl DiagnosticProcessor for RustDiagnosticProcessor {
}
}
}
fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option<String> {
let detail = completion.detail.as_ref()?;
if detail.starts_with("fn(") {
Some(completion.label.replace("(…)", &detail[2..]))
} else {
None
}
}
}
pub fn build_language_registry() -> LanguageRegistry {
@ -52,7 +61,7 @@ fn rust() -> Language {
.unwrap()
.with_outline_query(load_query("rust/outline.scm").as_ref())
.unwrap()
.with_diagnostics_processor(RustDiagnosticProcessor)
.with_lsp_post_processor(RustPostProcessor)
}
fn markdown() -> Language {
@ -72,9 +81,9 @@ fn load_query(path: &str) -> Cow<'static, str> {
#[cfg(test)]
mod tests {
use language::DiagnosticProcessor;
use language::LspPostProcessor;
use super::RustDiagnosticProcessor;
use super::RustPostProcessor;
#[test]
fn test_process_rust_diagnostics() {
@ -100,7 +109,7 @@ mod tests {
},
],
};
RustDiagnosticProcessor.process_diagnostics(&mut params);
RustPostProcessor.process_diagnostics(&mut params);
assert_eq!(params.diagnostics[0].message, "use of moved value `a`");