Skip to content

Commit f17040d

Browse files
authored
(fix) Format: Fall back to VSCode editor config (#553)
For tab size and whether or not to use spaces #539
1 parent ce3679e commit f17040d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

packages/language-server/src/plugins/svelte/SveltePlugin.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ export class SveltePlugin
3535
CodeActionsProvider {
3636
private docManager = new Map<Document, SvelteDocument>();
3737

38-
constructor(private configManager: LSConfigManager, private prettierConfig: any) {}
38+
constructor(
39+
private configManager: LSConfigManager,
40+
private prettierConfig: any,
41+
private editorConfig?: any,
42+
) {}
3943

4044
async getDiagnostics(document: Document): Promise<Diagnostic[]> {
4145
if (!this.featureEnabled('diagnostics')) {
@@ -67,7 +71,13 @@ export class SveltePlugin
6771
const prettier = importPrettier(filePath);
6872
// Try resolving the config through prettier and fall back to possible editor config
6973
const config =
70-
(await prettier.resolveConfig(filePath, { editorconfig: true })) || this.prettierConfig;
74+
(await prettier.resolveConfig(filePath, { editorconfig: true })) ||
75+
this.prettierConfig ||
76+
// Be defensive here because IDEs other than VSCode might not have these settings
77+
(this.editorConfig && this.editorConfig.tabSize && {
78+
tabWidth: this.editorConfig.tabSize,
79+
useTabs: !this.editorConfig.insertSpaces,
80+
});
7181
// Take .prettierignore into account
7282
const fileInfo = await prettier.getFileInfo(filePath, {
7383
ignorePath: this.prettierConfig?.ignorePath ?? '.prettierignore',

packages/language-server/src/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export function startServer(options?: LSOptions) {
9999
(sveltePlugin = new SveltePlugin(
100100
configManager,
101101
evt.initializationOptions?.prettierConfig || {},
102+
evt.initializationOptions?.editorConfig, // deliberatly don't fall back to empty object
102103
)),
103104
);
104105
pluginHost.register(new HTMLPlugin(docManager, configManager));

packages/svelte-vscode/src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function activate(context: ExtensionContext) {
9494
initializationOptions: {
9595
config: workspace.getConfiguration('svelte.plugin'),
9696
prettierConfig: workspace.getConfiguration('prettier'),
97+
editorConfig: workspace.getConfiguration('editor', { languageId: 'svelte' }),
9798
dontFilterIncompleteCompletions: true, // VSCode filters client side and is smarter at it than us
9899
},
99100
};

0 commit comments

Comments
 (0)