-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Client-side on-type formatting support #745
Changes from 17 commits
192de8e
503ba8d
16788ba
7912c4b
1570e22
8141e65
18a7d8b
8169857
e376359
7ff8a26
908b2a3
bec2ea6
d74ebfe
d6ce58f
638d54a
920db1a
7fdfbc5
4fed3f3
51a3acf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -389,29 +389,31 @@ showing automatic folding of both the file header comment and imports when the f | |
|
||
 | ||
|
||
#### Code block provider | ||
### Selection range | ||
|
||
[textDocument/selectionRange](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_selectionRange) is implemented with | ||
the `extendWordSelectionHandler` extension point and used via the IDE's **Extend Selection** (**Ctrl+W** on Windows/Linux; **Opt+Up** on Mac) and **Shrink Selection** (**Ctrl+Shift+W** on Windows/Linux; **Opt+Down** on Mac) actions. | ||
|
||
Here is an example with the [TypeScript Language Server](https://github.com/typescript-language-server/typescript-language-server) showing **Extend/Shrink Selection** using `textDocument/selectionRange`: | ||
|
||
 | ||
|
||
Additionally LSP4IJ registers the an implementation of the `codeBlockProvider` extension point with | ||
### Code block provider | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also updated the code block provider documentation to clearly state that code blocks are derived using selection ranges first and then folding ranges if necessary. |
||
|
||
LSP4IJ registers the an implementation of the `codeBlockProvider` extension point with | ||
[LSPCodeBlockProvider](https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/features/foldingRange/LSPCodeBlockProvider.java) for `TEXT` and `textmate` languages to provide block brace matching and easy navigation to | ||
the beginning/end of the containing block. | ||
the beginning/end of the containing block. For maximum flexibility in the absence of a true AST in the LSP client, code | ||
blocks are derived from `textDocument/selectionRange` first and, failing that, from `textDocument/foldingRange`. | ||
|
||
As with `lang.foldingBuilder`, if you use another language, you will have to declare `codeBlockProvider` with your language. | ||
As with other language-specific EP implementations, if you use another language, you will have to declare | ||
`codeBlockProvider` with your language. | ||
|
||
Below is an example with the [TypeScript Language Server](./user-defined-ls/typescript-language-server.md) showing code | ||
block functionality. The IDE's Presentation Assistant shows the default keyboard shortcuts for each supported operating | ||
system to trigger these actions. | ||
|
||
 | ||
|
||
### Selection range | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it was removed but it's actually still right above the "Code block provider" section. |
||
|
||
[textDocument/selectionRange](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_selectionRange) is implemented with | ||
the `extendWordSelectionHandler` extension point and used via the IDE's **Extend Selection** (**Ctrl+W** on Windows/Linux; **Opt+Up** on Mac) and **Shrink Selection** (**Ctrl+Shift+W** on Windows/Linux; **Opt+Down** on Mac) actions. | ||
|
||
Here is an example with the [TypeScript Language Server](https://github.com/typescript-language-server/typescript-language-server) showing **Extend/Shrink Selection** using `textDocument/selectionRange`: | ||
|
||
 | ||
|
||
### Publish Diagnostics | ||
|
||
[textDocument/publishDiagnostics](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_publishDiagnostics) is implemented with an `externalAnnotator` extension point. As this extension point supports `any` language, it works out-of-the-box. | ||
|
@@ -458,6 +460,67 @@ Here is an example with the [Java Language Server](https://github.com/eclipse-jd | |
|
||
 | ||
|
||
If desired — for example, for those who want to control when formatting is performed — server-side/LSP-based | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While updating the on-type formatting docs for this feature, I added details on how to disable server-side on-type formatting if desired. |
||
on-type formatting can be disabled via client configuration as follows: | ||
|
||
```json | ||
{ | ||
"format": { | ||
"onTypeFormatting": { | ||
"serverSide": { | ||
"enabled": false | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
#### Client-side On-Type Formatting | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I documented client-side on-type formatting extensively including the TypeScript server configuration example and a demo. |
||
|
||
Not all language servers support the `textDocument/onTypeFormatting` feature. To provide an improved editor experience | ||
for users of those that do not, LSP4IJ includes support for _client-side on-type formatting_. Client-side on-type | ||
formatting can be enabled via client configuration with the following settings: | ||
|
||
* `format.onTypeFormatting.clientSide.formatOnCloseBrace` - When set to `true`, formatting is automatically applied when a close brace character is typed. Defaults to `false`. | ||
* `format.onTypeFormatting.clientSide.formatOnCloseBraceCharacters` - Specifies the exact characters that should treated as a close brace character for purposes of client-side on-type formatting. Defaults to the close brace characters for the language, typically `}`, `]`, and `)`. | ||
* `format.onTypeFormatting.clientSide.formatOnCloseBraceScope` - Specifies the scope that should be formatted when a close brace is typed. Valid values are `CODE_BLOCK` and `FILE`. Defaults to `CODE_BLOCK`. `FILE` is most useful for language servers that do not support range formatting or yield incorrect results for range formatting. | ||
* `format.onTypeFormatting.clientSide.formatOnStatementTerminator` - When set to `true`, formatting is automatically applied when a statement terminator character is typed. Defaults to `false`. | ||
* `format.onTypeFormatting.clientSide.formatOnStatementTerminatorCharacters` - Specifies the exact characters that should treated as a statement terminator character for purposes of client-side on-type formatting. Defaults to empty and must be specified if `formatOnStatementTerminator` is enabled. | ||
* `format.onTypeFormatting.clientSide.formatOnStatementTerminatorScope` - Specifies the scope that should be formatted when a statement terminator is typed. Valid values are `STATEMENT`, `CODE_BLOCK` and `FILE`. Defaults to `STATEMENT`. The other values are most useful for language servers that do not support range formatting or yield incorrect results for range formatting. | ||
* `format.onTypeFormatting.clientSide.formatOnCompletionTrigger` - When set to `true`, formatting is automatically applied when a completion trigger character is typed. Defaults to `false`. | ||
* `format.onTypeFormatting.clientSide.formatOnCompletionTriggerCharacters` - Specifies the exact characters that should treated as a completion trigger character for purposes of client-side on-type formatting. Defaults to the completion trigger characters specified by the language server. | ||
* Note that there is no configurable scope for completion trigger-based formatting. Exactly the type completion trigger character is formatted. As above, support for this may vary by language server. | ||
|
||
For example, the [TypeScript Language Server](./user-defined-ls/typescript-language-server.md) does not support server-side on-type formatting, but client-side | ||
on-type formatting is included in its language server configuration template as: | ||
|
||
```json | ||
{ | ||
"format": { | ||
"onTypeFormatting": { | ||
"clientSide": { | ||
"formatOnCloseBrace": true, | ||
"formatOnStatementTerminator": true, | ||
"formatOnStatementTerminatorCharacters": ";", | ||
"formatOnCompletionTrigger": true | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Here is an example for client-side on-type formatting with that configuration showing automatic indentation of a | ||
statement continuation when the completion trigger character `.` is typed and automatic formatting of an entire code | ||
block when the closing brace character `}` is typed for a surrounding conditional statement: | ||
|
||
 | ||
|
||
#### Server-side / Client-side On-Type Formatting Relationship | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also wanted to call out the relationship between server-side and client-side on-type formatting if the former is available in the language server and enabled and the latter is also enabled in config. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks so much for having taken time to write this doc, it is excellent! |
||
|
||
If server-side on-type formatting is supported by the language server and enabled _and_ client-side on-type formatting | ||
is enabled for specific trigger characters, _only client-side on-type formatting will be applied_ when those specific | ||
trigger characters are typed. | ||
|
||
### Show Message | ||
|
||
[window/showMessage](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showMessage) supports Markdown messages and clickable links. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are the new client configuration options for client-side on-type formatting.