mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
internal: make CompletionItem and SourceChange consistent
Before this PR, SourceChange used a bool and CompletionItem used an enum to signify if edit is a snippet. It makes sense to use the same pattern in both cases. `bool` feels simpler, as there's only one consumer of this API, and all producers are encapsulated anyway (we check the capability at the production site).
This commit is contained in:
parent
2ce88b504c
commit
6e9780c005
4 changed files with 16 additions and 31 deletions
|
@ -97,7 +97,6 @@ pub use ide_assists::{
|
||||||
};
|
};
|
||||||
pub use ide_completion::{
|
pub use ide_completion::{
|
||||||
CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit,
|
CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit,
|
||||||
InsertTextFormat,
|
|
||||||
};
|
};
|
||||||
pub use ide_db::{
|
pub use ide_db::{
|
||||||
base_db::{
|
base_db::{
|
||||||
|
|
|
@ -39,8 +39,7 @@ pub struct CompletionItem {
|
||||||
///
|
///
|
||||||
/// Typically, replaces `source_range` with new identifier.
|
/// Typically, replaces `source_range` with new identifier.
|
||||||
text_edit: TextEdit,
|
text_edit: TextEdit,
|
||||||
|
is_snippet: bool,
|
||||||
insert_text_format: InsertTextFormat,
|
|
||||||
|
|
||||||
/// What item (struct, function, etc) are we completing.
|
/// What item (struct, function, etc) are we completing.
|
||||||
kind: Option<CompletionItemKind>,
|
kind: Option<CompletionItemKind>,
|
||||||
|
@ -272,12 +271,6 @@ pub(crate) enum CompletionKind {
|
||||||
Attribute,
|
Attribute,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
|
||||||
pub enum InsertTextFormat {
|
|
||||||
PlainText,
|
|
||||||
Snippet,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CompletionItem {
|
impl CompletionItem {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
completion_kind: CompletionKind,
|
completion_kind: CompletionKind,
|
||||||
|
@ -290,7 +283,7 @@ impl CompletionItem {
|
||||||
completion_kind,
|
completion_kind,
|
||||||
label,
|
label,
|
||||||
insert_text: None,
|
insert_text: None,
|
||||||
insert_text_format: InsertTextFormat::PlainText,
|
is_snippet: false,
|
||||||
detail: None,
|
detail: None,
|
||||||
documentation: None,
|
documentation: None,
|
||||||
lookup: None,
|
lookup: None,
|
||||||
|
@ -312,13 +305,13 @@ impl CompletionItem {
|
||||||
self.source_range
|
self.source_range
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_text_format(&self) -> InsertTextFormat {
|
|
||||||
self.insert_text_format
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn text_edit(&self) -> &TextEdit {
|
pub fn text_edit(&self) -> &TextEdit {
|
||||||
&self.text_edit
|
&self.text_edit
|
||||||
}
|
}
|
||||||
|
/// Whether `text_edit` is a snippet (contains `$0` markers).
|
||||||
|
pub fn is_snippet(&self) -> bool {
|
||||||
|
self.is_snippet
|
||||||
|
}
|
||||||
|
|
||||||
/// Short one-line additional information, like a type
|
/// Short one-line additional information, like a type
|
||||||
pub fn detail(&self) -> Option<&str> {
|
pub fn detail(&self) -> Option<&str> {
|
||||||
|
@ -396,7 +389,7 @@ pub(crate) struct Builder {
|
||||||
import_to_add: Option<ImportEdit>,
|
import_to_add: Option<ImportEdit>,
|
||||||
label: String,
|
label: String,
|
||||||
insert_text: Option<String>,
|
insert_text: Option<String>,
|
||||||
insert_text_format: InsertTextFormat,
|
is_snippet: bool,
|
||||||
detail: Option<String>,
|
detail: Option<String>,
|
||||||
documentation: Option<Documentation>,
|
documentation: Option<Documentation>,
|
||||||
lookup: Option<String>,
|
lookup: Option<String>,
|
||||||
|
@ -442,8 +435,8 @@ impl Builder {
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
source_range: self.source_range,
|
source_range: self.source_range,
|
||||||
label,
|
label,
|
||||||
insert_text_format: self.insert_text_format,
|
|
||||||
text_edit,
|
text_edit,
|
||||||
|
is_snippet: self.is_snippet,
|
||||||
detail: self.detail,
|
detail: self.detail,
|
||||||
documentation: self.documentation,
|
documentation: self.documentation,
|
||||||
lookup,
|
lookup,
|
||||||
|
@ -473,7 +466,7 @@ impl Builder {
|
||||||
_cap: SnippetCap,
|
_cap: SnippetCap,
|
||||||
snippet: impl Into<String>,
|
snippet: impl Into<String>,
|
||||||
) -> &mut Builder {
|
) -> &mut Builder {
|
||||||
self.insert_text_format = InsertTextFormat::Snippet;
|
self.is_snippet = true;
|
||||||
self.insert_text(snippet)
|
self.insert_text(snippet)
|
||||||
}
|
}
|
||||||
pub(crate) fn kind(&mut self, kind: impl Into<CompletionItemKind>) -> &mut Builder {
|
pub(crate) fn kind(&mut self, kind: impl Into<CompletionItemKind>) -> &mut Builder {
|
||||||
|
@ -485,7 +478,7 @@ impl Builder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub(crate) fn snippet_edit(&mut self, _cap: SnippetCap, edit: TextEdit) -> &mut Builder {
|
pub(crate) fn snippet_edit(&mut self, _cap: SnippetCap, edit: TextEdit) -> &mut Builder {
|
||||||
self.insert_text_format = InsertTextFormat::Snippet;
|
self.is_snippet = true;
|
||||||
self.text_edit(edit)
|
self.text_edit(edit)
|
||||||
}
|
}
|
||||||
pub(crate) fn detail(&mut self, detail: impl Into<String>) -> &mut Builder {
|
pub(crate) fn detail(&mut self, detail: impl Into<String>) -> &mut Builder {
|
||||||
|
|
|
@ -25,7 +25,7 @@ use crate::{completions::Completions, context::CompletionContext, item::Completi
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
config::CompletionConfig,
|
config::CompletionConfig,
|
||||||
item::{CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit, InsertTextFormat},
|
item::{CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit},
|
||||||
};
|
};
|
||||||
|
|
||||||
//FIXME: split the following feature into fine-grained features.
|
//FIXME: split the following feature into fine-grained features.
|
||||||
|
|
|
@ -9,8 +9,8 @@ use ide::{
|
||||||
Annotation, AnnotationKind, Assist, AssistKind, CallInfo, Cancellable, CompletionItem,
|
Annotation, AnnotationKind, Assist, AssistKind, CallInfo, Cancellable, CompletionItem,
|
||||||
CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit,
|
CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit,
|
||||||
Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint,
|
Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint,
|
||||||
InlayKind, InsertTextFormat, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable,
|
InlayKind, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity,
|
||||||
Severity, SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
|
SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use serde_json::to_value;
|
use serde_json::to_value;
|
||||||
|
@ -92,15 +92,6 @@ pub(crate) fn documentation(documentation: Documentation) -> lsp_types::Document
|
||||||
lsp_types::Documentation::MarkupContent(markup_content)
|
lsp_types::Documentation::MarkupContent(markup_content)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn insert_text_format(
|
|
||||||
insert_text_format: InsertTextFormat,
|
|
||||||
) -> lsp_types::InsertTextFormat {
|
|
||||||
match insert_text_format {
|
|
||||||
InsertTextFormat::Snippet => lsp_types::InsertTextFormat::Snippet,
|
|
||||||
InsertTextFormat::PlainText => lsp_types::InsertTextFormat::PlainText,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn completion_item_kind(
|
pub(crate) fn completion_item_kind(
|
||||||
completion_item_kind: CompletionItemKind,
|
completion_item_kind: CompletionItemKind,
|
||||||
) -> lsp_types::CompletionItemKind {
|
) -> lsp_types::CompletionItemKind {
|
||||||
|
@ -278,7 +269,9 @@ fn completion_item(
|
||||||
lsp_item.command = Some(command::trigger_parameter_hints());
|
lsp_item.command = Some(command::trigger_parameter_hints());
|
||||||
}
|
}
|
||||||
|
|
||||||
lsp_item.insert_text_format = Some(insert_text_format(item.insert_text_format()));
|
if item.is_snippet() {
|
||||||
|
lsp_item.insert_text_format = Some(lsp_types::InsertTextFormat::Snippet);
|
||||||
|
}
|
||||||
if enable_imports_on_the_fly {
|
if enable_imports_on_the_fly {
|
||||||
if let Some(import_edit) = item.import_to_add() {
|
if let Some(import_edit) = item.import_to_add() {
|
||||||
let import_path = &import_edit.import.import_path;
|
let import_path = &import_edit.import.import_path;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue