Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/language-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/language-server/src/ls-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const defaultLSConfig: LSConfig = {
completions: { enable: true },
hover: { enable: true },
codeActions: { enable: true },
selectionRange: { enable: true }
selectionRange: { enable: true },
defaultScriptLanguage: 'none'
}
};

Expand Down Expand Up @@ -198,6 +199,7 @@ export interface LSSvelteConfig {
selectionRange: {
enable: boolean;
};
defaultScriptLanguage: 'none' | 'ts';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -50,7 +51,10 @@ type LastCompletion = {
};

export class CompletionsProviderImpl implements CompletionsProvider<CompletionEntryWithIdentifer> {
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.
Expand Down Expand Up @@ -530,9 +534,12 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
const scriptTagInfo = fragment.scriptInfo || fragment.moduleScriptInfo;
if (!scriptTagInfo) {
// no script tag defined yet, add it.
const lang = this.configManager.getConfig().svelte.defaultScriptLanguage;
const scriptLang = lang === 'none' ? '' : ` lang="${lang}"`;

return TextEdit.replace(
beginOfDocumentRange,
`<script>${ts.sys.newLine}${change.newText}</script>${ts.sys.newLine}`
`<script${scriptLang}>${ts.sys.newLine}${change.newText}</script>${ts.sys.newLine}`
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(<any>{
uri: pathToUrl(filePath),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@
"default": true,
"title": "Svelte: Rename",
"description": "Enable rename/move Svelte files functionality"
},
"svelte.plugin.svelte.defaultScriptLanguage": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put this inside completions? Like "svelte.plugin.svelte.completions.defaultScriptLanguage"? @jasonlyu123 what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. It is also being used in the code action. if we want to further organize it. Maybe the name needs to be more generic. Maybe something like suggest or preferences?

Copy link
Member

@dummdidumm dummdidumm Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, it's also part of the code action. In that case, never mind 😄 My fear is that people would mistake this as something like "if I set this I don't need to write <script lang="ts"> at all" (like the deprecated and discouraged default language of svelte preprocess).

"type": "string",
"default": "none",
"title": "Svelte: Default Script Language",
"description": "The default language to use when generating new script tags",
"enum": [
"none",
"ts"
]
}
}
},
Expand Down