Skip to content

Commit

Permalink
Change custom request types to LSP commands (#473)
Browse files Browse the repository at this point in the history
LSP commands are the intended way to implement custom behaviour. This also uses the `workspace/applyEdit` command to apply the edits, so clients have to implement less custom logic to support this.
  • Loading branch information
remcohaszing authored Sep 8, 2024
1 parent 284608b commit 2854c38
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 276 deletions.
6 changes: 6 additions & 0 deletions .changeset/large-shoes-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@mdx-js/language-service': minor
'@mdx-js/language-server': minor
---

Convert the custom MDX syntax toggle request types into LSP commands.
62 changes: 62 additions & 0 deletions packages/language-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,66 @@ This language server supports all features supported by
[`volar-service-typescript`][volar-service-typescript], plus some additional
features specific to MDX.

#### Commands

The language server supports the following [LSP commands][]:

##### `mdx.toggleDelete`

Toggle delete syntax at the cursor position.
It uses the `workspace/applyEdit` command to apply edits.

###### Arguments

* `uri` — The URI of the document to apply changes to.
* `range` — The current selection range of the user.

###### Returns

`null`

##### `mdx.toggleEmphasis`

Toggle emphasis syntax at the cursor position.
It uses the `workspace/applyEdit` command to apply edits.

###### Arguments

* `uri` — The URI of the document to apply changes to.
* `range` — The current selection range of the user.

###### Returns

`null`

##### `mdx.toggleInlineCode`

Toggle inline code syntax at the cursor position.
It uses the `workspace/applyEdit` command to apply edits.

###### Arguments

* `uri` — The URI of the document to apply changes to.
* `range` — The current selection range of the user.

###### Returns

`null`

##### `mdx.toggleStrong`

Toggle strong syntax at the cursor position.
It uses the `workspace/applyEdit` command to apply edits.

###### Arguments

* `uri` — The URI of the document to apply changes to.
* `range` — The current selection range of the user.

###### Returns

`null`

### Initialize Options

MDX language server supports the following LSP initialization options:
Expand Down Expand Up @@ -271,6 +331,8 @@ Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.m

[lsp]: https://microsoft.github.io/language-server-protocol

[lsp commands]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#command

[mdx]: https://mdxjs.com

[mit]: LICENSE
Expand Down
33 changes: 1 addition & 32 deletions packages/language-server/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node

/**
* @import {Commands} from '@mdx-js/language-service'
* @import {PluggableList, Plugin} from 'unified'
*/

Expand All @@ -26,7 +25,6 @@ import remarkGfm from 'remark-gfm'
import {create as createMarkdownServicePlugin} from 'volar-service-markdown'
import {create as createTypeScriptServicePlugin} from 'volar-service-typescript'
import {create as createTypeScriptSyntacticServicePlugin} from 'volar-service-typescript/lib/plugins/syntactic.js'
import {URI} from 'vscode-uri'

process.title = 'mdx-language-server'

Expand Down Expand Up @@ -68,7 +66,7 @@ connection.onInitialize(async (parameters) => {
return context.env.getConfiguration?.('mdx.validate')
}
}),
createMdxServicePlugin()
createMdxServicePlugin(connection.workspace)
]

if (tsEnabled) {
Expand Down Expand Up @@ -123,26 +121,6 @@ connection.onInitialize(async (parameters) => {
}
})

connection.onRequest('mdx/toggleDelete', async (parameters) => {
const commands = await getCommands(parameters.uri)
return commands.toggleDelete(parameters)
})

connection.onRequest('mdx/toggleEmphasis', async (parameters) => {
const commands = await getCommands(parameters.uri)
return commands.toggleEmphasis(parameters)
})

connection.onRequest('mdx/toggleInlineCode', async (parameters) => {
const commands = await getCommands(parameters.uri)
return commands.toggleInlineCode(parameters)
})

connection.onRequest('mdx/toggleStrong', async (parameters) => {
const commands = await getCommands(parameters.uri)
return commands.toggleStrong(parameters)
})

connection.onInitialized(() => {
const extensions = ['mdx']
if (tsEnabled) {
Expand All @@ -164,12 +142,3 @@ connection.onInitialized(() => {
})

connection.listen()

/**
* @param {string} uri
* @returns {Promise<Commands>}
*/
async function getCommands(uri) {
const service = await server.project.getLanguageService(URI.parse(uri))
return service.context.inject('mdxCommands')
}
6 changes: 3 additions & 3 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0",
"volar-service-markdown": "0.0.61",
"volar-service-typescript": "0.0.61",
"vscode-uri": "^3.0.0"
"volar-service-typescript": "0.0.61"
},
"devDependencies": {
"@types/node": "^22.0.0",
"@volar/test-utils": "~2.4.0",
"unified": "^11.0.0"
"unified": "^11.0.0",
"vscode-uri": "^3.0.0"
}
}
8 changes: 8 additions & 0 deletions packages/language-server/test/initialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ test('initialize', async () => {
},
documentRangeFormattingProvider: true,
documentSymbolProvider: true,
executeCommandProvider: {
commands: [
'mdx.toggleDelete',
'mdx.toggleEmphasis',
'mdx.toggleInlineCode',
'mdx.toggleStrong'
]
},
experimental: {
autoInsertionProvider: {
configurationSections: [
Expand Down
Loading

0 comments on commit 2854c38

Please sign in to comment.