Skip to content

Commit

Permalink
Update resolution of options for formatting command in extension (#4064)
Browse files Browse the repository at this point in the history
VSCode has this detectIndent setting which seems to prefer 4 space by
default and then update the TypeSpec formatter to format that way.
  • Loading branch information
timotheeguerin authored Aug 12, 2024
1 parent bd14802 commit 3775eec
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions docs/handbook/formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 18 additions & 1 deletion packages/compiler/src/server/serverlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3775eec

Please sign in to comment.