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 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..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.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..eab3a8d66 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,10 @@ 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 +534,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..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,7 +40,10 @@ 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..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); + 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..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); + 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); + 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); + 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); + 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); + 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); + 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..3609bf75b 100644 --- a/packages/svelte-vscode/package.json +++ b/packages/svelte-vscode/package.json @@ -382,6 +382,16 @@ "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" + ] } } },