Update resolution of options for formatting command in extension (#4064)

VSCode has this detectIndent setting which seems to prefer 4 space by
default and then update the TypeSpec formatter to format that way.
This commit is contained in:
Timothee Guerin 2024-08-12 14:57:59 -07:00 коммит произвёл GitHub
Родитель bd148022e9
Коммит 3775eec3bc
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 59 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---
IDE: Formatting command will use prettier config if provided over the editor's configuration.

Просмотреть файл

@ -31,6 +31,39 @@ When you use the extensions for VS Code or Visual Studio, the tsp formatter beco
If you're working within a TypeSpec file, you can format the document using the default keyboard shortcut for formatting, `alt+shift+F`.
### Configuration - Prettier
If a prettier config (`.prettierrc.yaml`, `.prettierrc.json`, etc.) is present in the project, the formatter will use the configuration from there.
By default this will then use the typespec style guide without any explicit option.
:::note
This only affect the formatting, when using `tab` key to indent it will still use the editor's configuration, so recommend setting one of the configuration below.
:::
### Configuration - VSCode
For VSCode to respect the TypeSpec standard style set the following options style
```json
{
["typespec"]: {
"editor.detectIndentation": false,
"editor.insertSpaces": true,
"editor.tabSize": 2,
}
}
```
### Configuration - EditorConfig
If using `.editorconfig` with the editor config extension
```editorconfig
[*.tsp]
indent_size = 2
indent_style = space
```
## Via prettier
The tsp formatter is essentially a `prettier` plugin. If you already have a `prettier` configuration set up for other languages, it can be quite handy to simply integrate TypeSpec into this existing pipeline.

Просмотреть файл

@ -597,13 +597,30 @@ export function createServer(host: ServerHost): Server {
if (document === undefined) {
return [];
}
const formattedText = await formatTypeSpec(document.getText(), {
const path = await fileService.fileURLToRealPath(params.textDocument.uri);
const prettierConfig = await resolvePrettierConfig(path);
const resolvedConfig = prettierConfig ?? {
tabWidth: params.options.tabSize,
useTabs: !params.options.insertSpaces,
};
log({
level: "info",
message: `Formatting TypeSpec document: ${JSON.stringify({ fileUri: params.textDocument.uri, vscodeOptions: params.options, prettierConfig, resolvedConfig }, null, 2)}`,
});
const formattedText = await formatTypeSpec(document.getText(), resolvedConfig);
return [minimalEdit(document, formattedText)];
}
async function resolvePrettierConfig(path: string) {
try {
// Resolve prettier if it is installed.
const prettier = await import("prettier");
return prettier.resolveConfig(path);
} catch (e) {
return null;
}
}
function minimalEdit(document: TextDocument, string1: string): TextEdit {
const string0 = document.getText();
// length of common prefix