From 2289d5fd4f4e0cf8adedc5c8ee66e2c349d8890b Mon Sep 17 00:00:00 2001 From: Matthew McCune Date: Wed, 8 Dec 2021 17:35:57 -0800 Subject: [PATCH 1/5] Add default script language completion --- packages/language-server/src/ls-config.ts | 4 +++- .../src/plugins/typescript/TypeScriptPlugin.ts | 2 +- .../typescript/features/CompletionProvider.ts | 8 ++++++-- .../typescript/features/CodeActionsProvider.test.ts | 3 ++- .../typescript/features/CompletionProvider.test.ts | 2 +- .../plugins/typescript/features/preferences.test.ts | 12 ++++++------ packages/svelte-vscode/package.json | 7 +++++++ 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/language-server/src/ls-config.ts b/packages/language-server/src/ls-config.ts index e819297b9..69152a1ff 100644 --- a/packages/language-server/src/ls-config.ts +++ b/packages/language-server/src/ls-config.ts @@ -61,7 +61,8 @@ const defaultLSConfig: LSConfig = { completions: { enable: true }, hover: { enable: true }, codeActions: { enable: true }, - selectionRange: { enable: true } + selectionRange: { enable: true }, + defaultScriptLanguage: 'none' } }; @@ -198,6 +199,7 @@ export interface LSSvelteConfig { selectionRange: { enable: boolean; }; + defaultScriptLanguage: 'none' | 'ts'; } /** diff --git a/packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts b/packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts index 898334ba4..bed46b52a 100644 --- a/packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts +++ b/packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts @@ -97,7 +97,7 @@ export class TypeScriptPlugin constructor(configManager: LSConfigManager, lsAndTsDocResolver: LSAndTSDocResolver) { this.configManager = configManager; this.lsAndTsDocResolver = lsAndTsDocResolver; - this.completionProvider = new CompletionsProviderImpl(this.lsAndTsDocResolver); + this.completionProvider = new CompletionsProviderImpl(this.lsAndTsDocResolver, this.configManager); this.codeActionsProvider = new CodeActionsProviderImpl( this.lsAndTsDocResolver, this.completionProvider, diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index 01c35244a..5f6546aba 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -21,6 +21,7 @@ import { mapRangeToOriginal, toRange } from '../../../lib/documents'; +import { LSConfigManager } from '../../../ls-config'; import { flatten, getRegExpMatches, isNotNullOrUndefined, pathToUrl } from '../../../utils'; import { AppCompletionItem, AppCompletionList, CompletionsProvider } from '../../interfaces'; import { ComponentPartInfo } from '../ComponentInfoProvider'; @@ -50,7 +51,7 @@ type LastCompletion = { }; export class CompletionsProviderImpl implements CompletionsProvider { - constructor(private readonly lsAndTsDocResolver: LSAndTSDocResolver) {} + constructor(private readonly lsAndTsDocResolver: LSAndTSDocResolver, private readonly configManager: LSConfigManager) {} /** * The language service throws an error if the character is not a valid trigger character. @@ -530,9 +531,12 @@ export class CompletionsProviderImpl implements CompletionsProvider${ts.sys.newLine}${change.newText}${ts.sys.newLine}` + `${ts.sys.newLine}${change.newText}${ts.sys.newLine}` ); } diff --git a/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts b/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts index 77d2364af..3a396dc07 100644 --- a/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts @@ -40,7 +40,8 @@ describe('CodeActionsProvider', () => { [pathToUrl(testDir)], new LSConfigManager() ); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, + new LSConfigManager()); const provider = new CodeActionsProviderImpl( lsAndTsDocResolver, completionProvider, 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 6930c9d4d..373091097 100644 --- a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts @@ -41,7 +41,7 @@ describe('CompletionProviderImpl', () => { [pathToUrl(testDir)], new LSConfigManager() ); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); const filePath = join(testFilesDir, filename); const document = docManager.openDocument({ uri: pathToUrl(filePath), diff --git a/packages/language-server/test/plugins/typescript/features/preferences.test.ts b/packages/language-server/test/plugins/typescript/features/preferences.test.ts index 79f481fe3..88bfe1587 100644 --- a/packages/language-server/test/plugins/typescript/features/preferences.test.ts +++ b/packages/language-server/test/plugins/typescript/features/preferences.test.ts @@ -64,7 +64,7 @@ describe('ts user preferences', () => { it('provides auto import completion according to preferences', async () => { const { docManager, document } = setup('code-action.svelte'); const lsAndTsDocResolver = createLSAndTSDocResolver(docManager); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); const completions = await completionProvider.getCompletions( document, @@ -84,7 +84,7 @@ describe('ts user preferences', () => { ) { const { docManager, document } = setup(filename); const lsAndTsDocResolver = createLSAndTSDocResolver(docManager); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); const codeActionProvider = new CodeActionsProviderImpl( lsAndTsDocResolver, completionProvider, @@ -122,7 +122,7 @@ describe('ts user preferences', () => { includeCompletionsForImportStatements: undefined } }); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); const completions = await completionProvider.getCompletions( document, @@ -151,7 +151,7 @@ describe('ts user preferences', () => { it('provides auto import for svelte component when importModuleSpecifierEnding is js', async () => { const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); const completions = await completionProvider.getCompletions( document, @@ -166,7 +166,7 @@ describe('ts user preferences', () => { it('provides auto import for context="module" export when importModuleSpecifierEnding is js', async () => { const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); const completions = await completionProvider.getCompletions( document, @@ -185,7 +185,7 @@ describe('ts user preferences', () => { const range = Range.create(Position.create(4, 1), Position.create(4, 8)); const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); const codeActionProvider = new CodeActionsProviderImpl( lsAndTsDocResolver, completionProvider, diff --git a/packages/svelte-vscode/package.json b/packages/svelte-vscode/package.json index 5b3e7b5d5..07a654260 100644 --- a/packages/svelte-vscode/package.json +++ b/packages/svelte-vscode/package.json @@ -382,6 +382,13 @@ "default": true, "title": "Svelte: Rename", "description": "Enable rename/move Svelte files functionality" + }, + "svelte.plugin.svelte.defaultScriptLanguage": { + "type": "string", + "default": "none", + "title": "Svelte: Default Script Language", + "description": "The default language to use when generating new script tags", + "enum": ["none", "ts"] } } }, From bb61515a0eb4ed0ff396208f741d81097f0213c4 Mon Sep 17 00:00:00 2001 From: Matthew McCune Date: Wed, 8 Dec 2021 18:12:03 -0800 Subject: [PATCH 2/5] Add defaultScriptLanguage to README --- packages/language-server/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/language-server/README.md b/packages/language-server/README.md index 35ae32b7f..527aab47d 100644 --- a/packages/language-server/README.md +++ b/packages/language-server/README.md @@ -297,6 +297,10 @@ Enable code actions for Svelte. _Default_: `true` Enable selection range for Svelte. _Default_: `true` +##### `svelte.plugin.svelte.defaultScriptLanguage` + +The default language to use when generating new script tags in Svelte. _Default_: `none` + ## Credits - [James Birtles](https://github.com/jamesbirtles) for creating the foundation which this language server is built on From d8382144c7e106dd57b6eee95620c2c7c25e998f Mon Sep 17 00:00:00 2001 From: Matthew McCune Date: Wed, 8 Dec 2021 18:17:18 -0800 Subject: [PATCH 3/5] Handle falsy defualtScriptLang values --- .../src/plugins/typescript/features/CompletionProvider.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index 5f6546aba..bc0f4feb5 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -532,7 +532,8 @@ export class CompletionsProviderImpl implements CompletionsProvider Date: Wed, 8 Dec 2021 18:51:13 -0800 Subject: [PATCH 4/5] Fix linting errors --- .../plugins/typescript/TypeScriptPlugin.ts | 5 +++- .../typescript/features/CompletionProvider.ts | 5 +++- .../features/CodeActionsProvider.test.ts | 6 ++-- .../features/CompletionProvider.test.ts | 5 +++- .../typescript/features/preferences.test.ts | 30 +++++++++++++++---- packages/svelte-vscode/package.json | 5 +++- 6 files changed, 44 insertions(+), 12 deletions(-) diff --git a/packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts b/packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts index bed46b52a..3387db5db 100644 --- a/packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts +++ b/packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts @@ -97,7 +97,10 @@ export class TypeScriptPlugin constructor(configManager: LSConfigManager, lsAndTsDocResolver: LSAndTSDocResolver) { this.configManager = configManager; this.lsAndTsDocResolver = lsAndTsDocResolver; - this.completionProvider = new CompletionsProviderImpl(this.lsAndTsDocResolver, this.configManager); + this.completionProvider = new CompletionsProviderImpl( + this.lsAndTsDocResolver, + this.configManager + ); this.codeActionsProvider = new CodeActionsProviderImpl( this.lsAndTsDocResolver, this.completionProvider, diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index bc0f4feb5..dbf5553b4 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -51,7 +51,10 @@ type LastCompletion = { }; export class CompletionsProviderImpl implements CompletionsProvider { - constructor(private readonly lsAndTsDocResolver: LSAndTSDocResolver, private readonly configManager: LSConfigManager) {} + constructor( + private readonly lsAndTsDocResolver: LSAndTSDocResolver, + private readonly configManager: LSConfigManager + ) {} /** * The language service throws an error if the character is not a valid trigger character. diff --git a/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts b/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts index 3a396dc07..158dad5da 100644 --- a/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts @@ -40,8 +40,10 @@ describe('CodeActionsProvider', () => { [pathToUrl(testDir)], new LSConfigManager() ); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, - new LSConfigManager()); + const completionProvider = new CompletionsProviderImpl( + lsAndTsDocResolver, + new LSConfigManager() + ); const provider = new CodeActionsProviderImpl( lsAndTsDocResolver, completionProvider, 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 373091097..d43ddec29 100644 --- a/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts @@ -41,7 +41,10 @@ describe('CompletionProviderImpl', () => { [pathToUrl(testDir)], new LSConfigManager() ); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); + const completionProvider = new CompletionsProviderImpl( + lsAndTsDocResolver, + new LSConfigManager() + ); const filePath = join(testFilesDir, filename); const document = docManager.openDocument({ uri: pathToUrl(filePath), diff --git a/packages/language-server/test/plugins/typescript/features/preferences.test.ts b/packages/language-server/test/plugins/typescript/features/preferences.test.ts index 88bfe1587..be3bc15f7 100644 --- a/packages/language-server/test/plugins/typescript/features/preferences.test.ts +++ b/packages/language-server/test/plugins/typescript/features/preferences.test.ts @@ -64,7 +64,10 @@ describe('ts user preferences', () => { it('provides auto import completion according to preferences', async () => { const { docManager, document } = setup('code-action.svelte'); const lsAndTsDocResolver = createLSAndTSDocResolver(docManager); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); + const completionProvider = new CompletionsProviderImpl( + lsAndTsDocResolver, + new LSConfigManager() + ); const completions = await completionProvider.getCompletions( document, @@ -84,7 +87,10 @@ describe('ts user preferences', () => { ) { const { docManager, document } = setup(filename); const lsAndTsDocResolver = createLSAndTSDocResolver(docManager); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); + const completionProvider = new CompletionsProviderImpl( + lsAndTsDocResolver, + new LSConfigManager() + ); const codeActionProvider = new CodeActionsProviderImpl( lsAndTsDocResolver, completionProvider, @@ -122,7 +128,10 @@ describe('ts user preferences', () => { includeCompletionsForImportStatements: undefined } }); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); + const completionProvider = new CompletionsProviderImpl( + lsAndTsDocResolver, + new LSConfigManager() + ); const completions = await completionProvider.getCompletions( document, @@ -151,7 +160,10 @@ describe('ts user preferences', () => { it('provides auto import for svelte component when importModuleSpecifierEnding is js', async () => { const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); + const completionProvider = new CompletionsProviderImpl( + lsAndTsDocResolver, + new LSConfigManager() + ); const completions = await completionProvider.getCompletions( document, @@ -166,7 +178,10 @@ describe('ts user preferences', () => { it('provides auto import for context="module" export when importModuleSpecifierEnding is js', async () => { const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); + const completionProvider = new CompletionsProviderImpl( + lsAndTsDocResolver, + new LSConfigManager() + ); const completions = await completionProvider.getCompletions( document, @@ -185,7 +200,10 @@ describe('ts user preferences', () => { const range = Range.create(Position.create(4, 1), Position.create(4, 8)); const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); - const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, new LSConfigManager()); + const completionProvider = new CompletionsProviderImpl( + lsAndTsDocResolver, + new LSConfigManager() + ); const codeActionProvider = new CodeActionsProviderImpl( lsAndTsDocResolver, completionProvider, diff --git a/packages/svelte-vscode/package.json b/packages/svelte-vscode/package.json index 07a654260..3609bf75b 100644 --- a/packages/svelte-vscode/package.json +++ b/packages/svelte-vscode/package.json @@ -388,7 +388,10 @@ "default": "none", "title": "Svelte: Default Script Language", "description": "The default language to use when generating new script tags", - "enum": ["none", "ts"] + "enum": [ + "none", + "ts" + ] } } }, From 0f081f249951f47fde2d67baa15d679ae5908464 Mon Sep 17 00:00:00 2001 From: Matthew McCune Date: Thu, 9 Dec 2021 11:51:54 -0800 Subject: [PATCH 5/5] Revert handling falsy values for defaultScriptLang --- .../src/plugins/typescript/features/CompletionProvider.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index dbf5553b4..eab3a8d66 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -535,8 +535,7 @@ export class CompletionsProviderImpl implements CompletionsProvider