Skip to content

Commit

Permalink
refactor: consistent formatting options in language service plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 12, 2024
1 parent d6051d4 commit 61d10ed
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 57 deletions.
17 changes: 8 additions & 9 deletions packages/css/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CodeAction, Diagnostic, Disposable, DocumentSelector, LocationLink, Result, ServiceContext, ServicePlugin, ServicePluginInstance } from '@volar/language-service';
import type { CodeAction, Diagnostic, Disposable, DocumentSelector, FormattingOptions, LocationLink, Result, ServiceContext, ServicePlugin, ServicePluginInstance } from '@volar/language-service';
import * as css from 'vscode-css-languageservice';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { URI, Utils } from 'vscode-uri';
Expand Down Expand Up @@ -36,8 +36,11 @@ export function create({
isFormattingEnabled = async (document, context) => {
return await context.env.getConfiguration?.(document.languageId + '.format.enable') ?? true;
},
getFormatConfiguration = async (document, context) => {
return await context.env.getConfiguration?.(document.languageId + '.format');
getFormattingOptions = async (document, options, context) => {
return {
...options,
...await context.env.getConfiguration?.(document.languageId + '.format'),
};
},
getLanguageSettings = async (document, context) => {
return await context.env.getConfiguration?.(document.languageId);
Expand Down Expand Up @@ -75,7 +78,7 @@ export function create({
useDefaultDataProvider?: boolean;
getDocumentContext?(context: ServiceContext): css.DocumentContext;
isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
getFormatConfiguration?(document: TextDocument, context: ServiceContext): Result<css.CSSFormatConfiguration | undefined>;
getFormattingOptions?(document: TextDocument, options: FormattingOptions, context: ServiceContext): Result<css.CSSFormatConfiguration>;
getLanguageSettings?(document: TextDocument, context: ServiceContext): Result<css.LanguageSettings | undefined>;
getCustomData?(context: ServiceContext): Result<css.ICSSDataProvider[]>;
onDidChangeCustomData?(listener: () => void, context: ServiceContext): Disposable;
Expand Down Expand Up @@ -219,11 +222,7 @@ export function create({
return;
}

const formatSettings = await getFormatConfiguration(document, context);
const formatOptions: css.CSSFormatConfiguration = {
...options,
...formatSettings,
};
const formatOptions = await getFormattingOptions(document, options, context);

let formatDocument = document;
let prefixes = [];
Expand Down
32 changes: 16 additions & 16 deletions packages/html/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Disposable, DocumentSelector, Result, ServiceContext, ServicePlugin, ServicePluginInstance } from '@volar/language-service';
import type { Disposable, DocumentSelector, FormattingOptions, Result, ServiceContext, ServicePlugin, ServicePluginInstance } from '@volar/language-service';
import * as html from 'vscode-html-languageservice';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { URI, Utils } from 'vscode-uri';
Expand Down Expand Up @@ -42,8 +42,19 @@ export function create({
isAutoClosingTagsEnabled = async (_document, context) => {
return await context.env.getConfiguration?.('html.autoClosingTags') ?? true;
},
getFormattingOptions = async (_document, context) => {
return await context.env.getConfiguration?.('html.format');
getFormattingOptions = async (_document, options, context) => {
const formatSettings: html.FormattingOptions = {
...options,
endWithNewline: options.insertFinalNewline ? true : options.trimFinalNewlines ? false : undefined,
...await context.env.getConfiguration?.('html.format'),
};
// https://github.com/microsoft/vscode/blob/a8f73340be02966c3816a2f23cb7e446a3a7cb9b/extensions/html-language-features/server/src/modes/htmlMode.ts#L47-L51
if (formatSettings.contentUnformatted) {
formatSettings.contentUnformatted = formatSettings.contentUnformatted + ',script';
} else {
formatSettings.contentUnformatted = 'script';
}
return formatSettings;
},
getCompletionConfiguration = async (_document, context) => {
return await context.env.getConfiguration?.('html.completion');
Expand Down Expand Up @@ -84,7 +95,7 @@ export function create({
isAutoCreateQuotesEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
isAutoClosingTagsEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
getDocumentContext?(context: ServiceContext): html.DocumentContext;
getFormattingOptions?(document: TextDocument, context: ServiceContext): Result<html.HTMLFormatConfiguration | undefined>;
getFormattingOptions?(document: TextDocument, options: FormattingOptions, context: ServiceContext): Result<html.HTMLFormatConfiguration>;
getCompletionConfiguration?(document: TextDocument, context: ServiceContext): Result<html.CompletionConfiguration | undefined>;
getHoverSettings?(document: TextDocument, context: ServiceContext): Result<html.HoverSettings | undefined>;
getCustomData?(context: ServiceContext): Result<html.IHTMLDataProvider[]>;
Expand Down Expand Up @@ -211,18 +222,7 @@ export function create({
};
}

const formatSettings: html.HTMLFormatConfiguration = {
...options,
endWithNewline: options.insertFinalNewline ? true : options.trimFinalNewlines ? false : undefined,
...await getFormattingOptions(document, context),
};

// https://github.com/microsoft/vscode/blob/a8f73340be02966c3816a2f23cb7e446a3a7cb9b/extensions/html-language-features/server/src/modes/htmlMode.ts#L47-L51
if (formatSettings.contentUnformatted) {
formatSettings.contentUnformatted = formatSettings.contentUnformatted + ',script';
} else {
formatSettings.contentUnformatted = 'script';
}
const formatSettings = await getFormattingOptions(document, options, context);

let formatDocument = document;
let prefixes = [];
Expand Down
18 changes: 9 additions & 9 deletions packages/json/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ServicePlugin, ServicePluginInstance, DocumentSelector, ServiceContext, Disposable, Result } from '@volar/language-service';
import type { ServicePlugin, ServicePluginInstance, DocumentSelector, ServiceContext, Disposable, Result, FormattingOptions } from '@volar/language-service';
import * as json from 'vscode-json-languageservice';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { URI, Utils } from 'vscode-uri';
Expand Down Expand Up @@ -28,8 +28,11 @@ export function create({
isFormattingEnabled = async (_document, context) => {
return await context.env.getConfiguration?.('json.format.enable') ?? true;
},
getFormattingOptions = async (_document, context) => {
return await context.env.getConfiguration?.('json.format');
getFormattingOptions = async (_document, options, context) => {
return {
...options,
...await context.env.getConfiguration?.('json.format'),
};
},
getLanguageSettings = async context => {
const languageSettings: json.LanguageSettings = {};
Expand Down Expand Up @@ -68,7 +71,7 @@ export function create({
documentSelector?: DocumentSelector;
getWorkspaceContextService?(context: ServiceContext): json.WorkspaceContextService;
isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
getFormattingOptions?(document: TextDocument, context: ServiceContext): Result<json.FormattingOptions | undefined>;
getFormattingOptions?(document: TextDocument, options: FormattingOptions, context: ServiceContext): Result<json.FormattingOptions>;
getLanguageSettings?(context: ServiceContext): Result<json.LanguageSettings>;
getDocumentLanguageSettings?(document: TextDocument, context: ServiceContext): Result<json.DocumentLanguageSettings | undefined>;
onDidChangeLanguageSettings?(listener: () => void, context: ServiceContext): Disposable;
Expand Down Expand Up @@ -172,12 +175,9 @@ export function create({
return;
}

const formatOptions = await getFormattingOptions(document, context);
const formatOptions = await getFormattingOptions(document, options, context);

return jsonLs.format(document, range, {
...options,
...formatOptions,
});
return jsonLs.format(document, range, formatOptions);
});
},
};
Expand Down
22 changes: 13 additions & 9 deletions packages/prettyhtml/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import * as prettyhtml from '@starptech/prettyhtml';
import type { DocumentSelector, Result, ServiceContext, ServicePlugin, ServicePluginInstance, TextDocument } from '@volar/language-service';
import type { DocumentSelector, FormattingOptions, Result, ServiceContext, ServicePlugin, ServicePluginInstance, TextDocument } from '@volar/language-service';

export type FormattingOptions = Parameters<typeof prettyhtml>[1];
export type PrettyhtmlFormattingOptions = Parameters<typeof prettyhtml>[1];

export function create({
documentSelector = ['html'],
isFormattingEnabled = () => true,
getFormattingOptions = () => ({}),
getFormattingOptions = (_document, options) => {
return {
tabWidth: options.tabSize,
useTabs: !options.insertSpaces,
};
},
}: {
documentSelector?: DocumentSelector;
isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
getFormattingOptions?(document: TextDocument, context: ServiceContext): Result<FormattingOptions>;
getFormattingOptions?(document: TextDocument, options: FormattingOptions, context: ServiceContext): Result<PrettyhtmlFormattingOptions>;
} = {}): ServicePlugin {
return {
name: 'prettyhtml',
Expand All @@ -25,11 +30,10 @@ export function create({
return;

const oldRangeText = document.getText(range);
const newRangeText = prettyhtml(oldRangeText, {
tabWidth: options.tabSize,
useTabs: !options.insertSpaces,
...await getFormattingOptions(document, context),
}).contents;
const newRangeText = prettyhtml(
oldRangeText,
await getFormattingOptions(document, options, context),
).contents;

if (newRangeText === oldRangeText)
return [];
Expand Down
17 changes: 10 additions & 7 deletions packages/pug-beautify/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type { DocumentSelector, Result, ServiceContext, ServicePlugin, ServicePluginInstance, TextDocument } from '@volar/language-service';
import type { DocumentSelector, FormattingOptions, Result, ServiceContext, ServicePlugin, ServicePluginInstance, TextDocument } from '@volar/language-service';

export function create({
documentSelector = ['jade'],
isFormattingEnabled = () => true,
getFormattingOptions = (_document, options) => {
return {
tab_size: options.tabSize,
fill_tab: !options.insertSpaces,
};
},
}: {
documentSelector?: DocumentSelector;
isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
getFormattingOptions?(document: TextDocument, options: FormattingOptions, context: ServiceContext): Result<{}>;
} = {}): ServicePlugin {
return {
name: 'pug-beautify',
Expand All @@ -20,7 +27,6 @@ export function create({
return;

const pugCode = document.getText(range);

// fix https://github.com/johnsoncodehk/volar/issues/304
if (pugCode.trim() === '')
return;
Expand All @@ -30,11 +36,8 @@ export function create({
const suffixesLength = pugCode.length - pugCode.trimEnd().length;
const prefixes = pugCode.slice(0, prefixesLength);
const suffixes = pugCode.slice(pugCode.length - suffixesLength);

let newText: string = pugBeautify(pugCode, {
tab_size: options.tabSize,
fill_tab: !options.insertSpaces,
});
const formatOptions = await getFormattingOptions(document, options, context);
const newText: string = pugBeautify(pugCode, formatOptions);

return [{
range,
Expand Down
11 changes: 4 additions & 7 deletions packages/sass-formatter/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { DocumentSelector, Result, ServiceContext, ServicePlugin, ServicePluginInstance, TextDocument } from '@volar/language-service';
import type { DocumentSelector, FormattingOptions, Result, ServiceContext, ServicePlugin, ServicePluginInstance, TextDocument } from '@volar/language-service';
import { SassFormatter, SassFormatterConfig } from 'sass-formatter';

export function create({
documentSelector = ['sass'],
isFormattingEnabled = () => true,
getFormatterConfig,
getFormatterConfig = (_document, options) => options,
}: {
documentSelector?: DocumentSelector;
isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
getFormatterConfig?(document: TextDocument): Result<SassFormatterConfig>;
getFormatterConfig?(document: TextDocument, options: FormattingOptions, context: ServiceContext): Result<Partial<SassFormatterConfig>>;
} = {}): ServicePlugin {
return {
name: 'sass-formatter',
Expand All @@ -22,10 +22,7 @@ export function create({
if (!await isFormattingEnabled(document, context))
return;

const config = {
...options,
...getFormatterConfig ? await getFormatterConfig(document) : {},
};
const config = await getFormatterConfig(document, options, context);

// don't set when options.insertSpaces is false to avoid sass-formatter internal judge bug
if (config.insertSpaces)
Expand Down

0 comments on commit 61d10ed

Please sign in to comment.