Skip to content

Commit

Permalink
feat(lsp): include source in auto import completion label (denoland#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Sep 16, 2023
1 parent 430b63c commit d13e6e6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,7 @@ impl Inner {
&self.config.workspace_settings().suggest,
&specifier,
position,
self,
);
Some(results)
} else {
Expand Down
29 changes: 29 additions & 0 deletions cli/lsp/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,7 @@ impl CompletionInfo {
settings: &config::CompletionSettings,
specifier: &ModuleSpecifier,
position: u32,
language_server: &language_server::Inner,
) -> lsp::CompletionResponse {
let items = self
.entries
Expand All @@ -2614,6 +2615,7 @@ impl CompletionInfo {
settings,
specifier,
position,
language_server,
)
})
.collect();
Expand Down Expand Up @@ -2808,8 +2810,10 @@ impl CompletionEntry {
settings: &config::CompletionSettings,
specifier: &ModuleSpecifier,
position: u32,
language_server: &language_server::Inner,
) -> lsp::CompletionItem {
let mut label = self.name.clone();
let mut label_details: Option<lsp::CompletionItemLabelDetails> = None;
let mut kind: Option<lsp::CompletionItemKind> =
Some(self.kind.clone().into());

Expand Down Expand Up @@ -2865,6 +2869,30 @@ impl CompletionEntry {
}
}

if let Some(source) = &self.source {
let mut source = source.clone();
if let Some(data) = &self.data {
if let Ok(import_data) =
serde_json::from_value::<CompletionEntryDataImport>(data.clone())
{
if let Ok(import_specifier) =
normalize_specifier(import_data.file_name)
{
if let Some(new_module_specifier) = language_server
.get_ts_response_import_mapper()
.check_specifier(&import_specifier, specifier)
.or_else(|| relative_specifier(specifier, &import_specifier))
{
source = new_module_specifier;
}
}
}
}
label_details
.get_or_insert_with(Default::default)
.description = Some(source);
}

let text_edit =
if let (Some(text_span), Some(new_text)) = (range, &insert_text) {
let range = text_span.to_range(line_index);
Expand All @@ -2889,6 +2917,7 @@ impl CompletionEntry {

lsp::CompletionItem {
label,
label_details,
kind,
sort_text,
preselect,
Expand Down
21 changes: 21 additions & 0 deletions cli/tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5212,6 +5212,9 @@ fn lsp_completions_auto_import() {

let req = json!({
"label": "foo",
"labelDetails": {
"description": "./b.ts",
},
"kind": 6,
"sortText": "￿16",
"commitCharacters": [
Expand Down Expand Up @@ -5243,6 +5246,9 @@ fn lsp_completions_auto_import() {
res,
json!({
"label": "foo",
"labelDetails": {
"description": "./b.ts",
},
"kind": 6,
"detail": "const foo: \"foo\"",
"documentation": {
Expand Down Expand Up @@ -5323,6 +5329,9 @@ fn lsp_npm_completions_auto_import_and_quick_fix_no_import_map() {
res,
json!({
"label": "getClient",
"labelDetails": {
"description": "npm:@denotest/types-exports-subpaths@1/client",
},
"kind": 3,
"detail": "function getClient(): 5",
"documentation": {
Expand Down Expand Up @@ -5435,6 +5444,9 @@ fn lsp_npm_completions_auto_import_and_quick_fix_no_import_map() {
res,
json!({
"label": "chalk",
"labelDetails": {
"description": "npm:chalk@5.0",
},
"kind": 6,
"sortText": "￿16",
"additionalTextEdits": [
Expand Down Expand Up @@ -5635,6 +5647,9 @@ fn lsp_completions_auto_import_and_quick_fix_with_import_map() {
res,
json!({
"label": "getClient",
"labelDetails": {
"description": "types-exports-subpaths/client",
},
"kind": 3,
"detail": "function getClient(): 5",
"documentation": {
Expand Down Expand Up @@ -5747,6 +5762,9 @@ fn lsp_completions_auto_import_and_quick_fix_with_import_map() {
res,
json!({
"label": "chalk",
"labelDetails": {
"description": "chalk",
},
"kind": 6,
"sortText": "￿16",
"additionalTextEdits": [
Expand Down Expand Up @@ -5854,6 +5872,9 @@ fn lsp_completions_auto_import_and_quick_fix_with_import_map() {
res,
json!({
"label": "printHello",
"labelDetails": {
"description": "print_hello",
},
"kind": 3,
"sortText": "￿16",
"additionalTextEdits": [
Expand Down

0 comments on commit d13e6e6

Please sign in to comment.