From bb9a8a90e3ae55cf92c5c13701efc1007ecd18f8 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 17 May 2021 17:05:16 +0200 Subject: [PATCH 1/5] (fix) better import completion locations Add import to module script if trigger in there, same for instance script #927 --- .../features/CodeActionsProvider.ts | 3 +- .../typescript/features/CompletionProvider.ts | 20 ++++--- .../src/plugins/typescript/utils.ts | 4 +- .../features/CompletionProvider.test.ts | 56 ++++++++++++++++++- .../completions/importcompletions8.svelte | 7 +++ 5 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions8.svelte diff --git a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts index 2aa6f3771..7f52a50dc 100644 --- a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts @@ -189,8 +189,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider { fragment, edit, true, - isInTag(range.start, document.scriptInfo) || - isInTag(range.start, document.moduleScriptInfo) + range.start ); } diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index fb96067a7..a84bfa8b0 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -317,8 +317,7 @@ export class CompletionsProviderImpl implements CompletionsProvider this.codeActionChangeToTextEdit( @@ -363,7 +362,7 @@ export class CompletionsProviderImpl implements CompletionsProvider { return pathToUrl(join(testFilesDir, file)); }; -describe('CompletionProviderImpl', () => { +describe.only('CompletionProviderImpl', () => { function setup(filename: string) { const docManager = new DocumentManager( (textDocument) => new Document(textDocument.uri, textDocument.text) @@ -524,6 +524,60 @@ describe('CompletionProviderImpl', () => { ); }); + it('resolve auto import completion in instance script (instance and module script present)', async () => { + const { completionProvider, document } = setup('importcompletions8.svelte'); + + const completions = await completionProvider.getCompletions( + document, + Position.create(5, 7) + ); + document.version++; + + const item = completions?.items.find((item) => item.label === 'onMount'); + const { additionalTextEdits, detail } = await completionProvider.resolveCompletion( + document, + item! + ); + + assert.strictEqual( + harmonizeNewLines(additionalTextEdits![0]?.newText), + // " instead of ' because VSCode uses " by default when there are no other imports indicating otherwise + `${newLine}import { onMount } from "svelte";${newLine}${newLine}` + ); + + assert.deepEqual( + additionalTextEdits![0]?.range, + Range.create(Position.create(4, 8), Position.create(4, 8)) + ); + }); + + it('resolve auto import completion in module script (instance and module script present)', async () => { + const { completionProvider, document } = setup('importcompletions8.svelte'); + + const completions = await completionProvider.getCompletions( + document, + Position.create(1, 7) + ); + document.version++; + + const item = completions?.items.find((item) => item.label === 'onMount'); + const { additionalTextEdits, detail } = await completionProvider.resolveCompletion( + document, + item! + ); + + assert.strictEqual( + harmonizeNewLines(additionalTextEdits![0]?.newText), + // " instead of ' because VSCode uses " by default when there are no other imports indicating otherwise + `${newLine}import { onMount } from "svelte";${newLine}${newLine}` + ); + + assert.deepEqual( + additionalTextEdits![0]?.range, + Range.create(Position.create(0, 25), Position.create(0, 25)) + ); + }); + async function openFileToBeImported( docManager: DocumentManager, completionProvider: CompletionsProviderImpl, diff --git a/packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions8.svelte b/packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions8.svelte new file mode 100644 index 000000000..7c7d536de --- /dev/null +++ b/packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions8.svelte @@ -0,0 +1,7 @@ + + + From 361dbb7a7a369761213f0354e0437ca91ca3d9dd Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 18 May 2021 08:31:16 +0200 Subject: [PATCH 2/5] file rename --- .../plugins/typescript/features/CompletionProvider.test.ts | 4 ++-- .../{importcompletions8.svelte => importcompletions9.svelte} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename packages/language-server/test/plugins/typescript/testfiles/completions/{importcompletions8.svelte => importcompletions9.svelte} (100%) diff --git a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts index b75e932c6..bee3576d1 100644 --- a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts @@ -525,7 +525,7 @@ describe.only('CompletionProviderImpl', () => { }); it('resolve auto import completion in instance script (instance and module script present)', async () => { - const { completionProvider, document } = setup('importcompletions8.svelte'); + const { completionProvider, document } = setup('importcompletions9.svelte'); const completions = await completionProvider.getCompletions( document, @@ -552,7 +552,7 @@ describe.only('CompletionProviderImpl', () => { }); it('resolve auto import completion in module script (instance and module script present)', async () => { - const { completionProvider, document } = setup('importcompletions8.svelte'); + const { completionProvider, document } = setup('importcompletions9.svelte'); const completions = await completionProvider.getCompletions( document, diff --git a/packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions8.svelte b/packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions9.svelte similarity index 100% rename from packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions8.svelte rename to packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions9.svelte From 44d5efc874077b4acda3088219edc8f4a93c39dc Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 18 May 2021 08:34:33 +0200 Subject: [PATCH 3/5] file --- .../test/plugins/typescript/features/CompletionProvider.test.ts | 2 +- .../{importCompletion8.svelte => importcompletions8.svelte} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/language-server/test/plugins/typescript/testfiles/completions/{importCompletion8.svelte => importcompletions8.svelte} (100%) diff --git a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts index bee3576d1..6eb9f6ef6 100644 --- a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts @@ -710,7 +710,7 @@ describe.only('CompletionProviderImpl', () => { }); it('resolve auto completion in correct place when already imported in module script', async () => { - const { completionProvider, document } = setup('importCompletion8.svelte'); + const { completionProvider, document } = setup('importcompletions8.svelte'); const completions = await completionProvider.getCompletions( document, diff --git a/packages/language-server/test/plugins/typescript/testfiles/completions/importCompletion8.svelte b/packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions8.svelte similarity index 100% rename from packages/language-server/test/plugins/typescript/testfiles/completions/importCompletion8.svelte rename to packages/language-server/test/plugins/typescript/testfiles/completions/importcompletions8.svelte From 7c11189fcf10ac94d2017f6865e836ebc36ca12c Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 18 May 2021 08:37:20 +0200 Subject: [PATCH 4/5] lint --- .../typescript/features/CompletionProvider.test.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts index 6eb9f6ef6..ebc3c4c46 100644 --- a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts @@ -534,10 +534,7 @@ describe.only('CompletionProviderImpl', () => { document.version++; const item = completions?.items.find((item) => item.label === 'onMount'); - const { additionalTextEdits, detail } = await completionProvider.resolveCompletion( - document, - item! - ); + const { additionalTextEdits } = await completionProvider.resolveCompletion(document, item!); assert.strictEqual( harmonizeNewLines(additionalTextEdits![0]?.newText), @@ -561,10 +558,7 @@ describe.only('CompletionProviderImpl', () => { document.version++; const item = completions?.items.find((item) => item.label === 'onMount'); - const { additionalTextEdits, detail } = await completionProvider.resolveCompletion( - document, - item! - ); + const { additionalTextEdits } = await completionProvider.resolveCompletion(document, item!); assert.strictEqual( harmonizeNewLines(additionalTextEdits![0]?.newText), From afad6b458a8874607b5fbbae20d4bac7414c317f Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 18 May 2021 08:42:31 +0200 Subject: [PATCH 5/5] lint --- .../plugins/typescript/features/CodeActionsProvider.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts index 7f52a50dc..9eb90ab20 100644 --- a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts @@ -1,3 +1,4 @@ +import ts from 'typescript'; import { CodeAction, CodeActionContext, @@ -10,18 +11,15 @@ import { } from 'vscode-languageserver'; import { Document, - mapRangeToOriginal, + getLineAtPosition, isRangeInTag, - isInTag, - getLineAtPosition + mapRangeToOriginal } from '../../../lib/documents'; -import { pathToUrl, flatten, isNotNullOrUndefined, modifyLines, getIndent } from '../../../utils'; +import { flatten, getIndent, isNotNullOrUndefined, modifyLines, pathToUrl } from '../../../utils'; import { CodeActionsProvider } from '../../interfaces'; import { SnapshotFragment, SvelteSnapshotFragment } from '../DocumentSnapshot'; import { LSAndTSDocResolver } from '../LSAndTSDocResolver'; import { convertRange } from '../utils'; - -import ts from 'typescript'; import { CompletionsProviderImpl } from './CompletionProvider'; import { isNoTextSpanInGeneratedCode, SnapshotFragmentMap } from './utils';