Skip to content

Commit 2854c38

Browse files
authored
Change custom request types to LSP commands (#473)
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.
1 parent 284608b commit 2854c38

12 files changed

Lines changed: 437 additions & 276 deletions

File tree

.changeset/large-shoes-applaud.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@mdx-js/language-service': minor
3+
'@mdx-js/language-server': minor
4+
---
5+
6+
Convert the custom MDX syntax toggle request types into LSP commands.

packages/language-server/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,66 @@ This language server supports all features supported by
6565
[`volar-service-typescript`][volar-service-typescript], plus some additional
6666
features specific to MDX.
6767

68+
#### Commands
69+
70+
The language server supports the following [LSP commands][]:
71+
72+
##### `mdx.toggleDelete`
73+
74+
Toggle delete syntax at the cursor position.
75+
It uses the `workspace/applyEdit` command to apply edits.
76+
77+
###### Arguments
78+
79+
* `uri` — The URI of the document to apply changes to.
80+
* `range` — The current selection range of the user.
81+
82+
###### Returns
83+
84+
`null`
85+
86+
##### `mdx.toggleEmphasis`
87+
88+
Toggle emphasis syntax at the cursor position.
89+
It uses the `workspace/applyEdit` command to apply edits.
90+
91+
###### Arguments
92+
93+
* `uri` — The URI of the document to apply changes to.
94+
* `range` — The current selection range of the user.
95+
96+
###### Returns
97+
98+
`null`
99+
100+
##### `mdx.toggleInlineCode`
101+
102+
Toggle inline code syntax at the cursor position.
103+
It uses the `workspace/applyEdit` command to apply edits.
104+
105+
###### Arguments
106+
107+
* `uri` — The URI of the document to apply changes to.
108+
* `range` — The current selection range of the user.
109+
110+
###### Returns
111+
112+
`null`
113+
114+
##### `mdx.toggleStrong`
115+
116+
Toggle strong syntax at the cursor position.
117+
It uses the `workspace/applyEdit` command to apply edits.
118+
119+
###### Arguments
120+
121+
* `uri` — The URI of the document to apply changes to.
122+
* `range` — The current selection range of the user.
123+
124+
###### Returns
125+
126+
`null`
127+
68128
### Initialize Options
69129

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

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

334+
[lsp commands]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#command
335+
274336
[mdx]: https://mdxjs.com
275337

276338
[mit]: LICENSE

packages/language-server/index.js

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env node
22

33
/**
4-
* @import {Commands} from '@mdx-js/language-service'
54
* @import {PluggableList, Plugin} from 'unified'
65
*/
76

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

3129
process.title = 'mdx-language-server'
3230

@@ -68,7 +66,7 @@ connection.onInitialize(async (parameters) => {
6866
return context.env.getConfiguration?.('mdx.validate')
6967
}
7068
}),
71-
createMdxServicePlugin()
69+
createMdxServicePlugin(connection.workspace)
7270
]
7371

7472
if (tsEnabled) {
@@ -123,26 +121,6 @@ connection.onInitialize(async (parameters) => {
123121
}
124122
})
125123

126-
connection.onRequest('mdx/toggleDelete', async (parameters) => {
127-
const commands = await getCommands(parameters.uri)
128-
return commands.toggleDelete(parameters)
129-
})
130-
131-
connection.onRequest('mdx/toggleEmphasis', async (parameters) => {
132-
const commands = await getCommands(parameters.uri)
133-
return commands.toggleEmphasis(parameters)
134-
})
135-
136-
connection.onRequest('mdx/toggleInlineCode', async (parameters) => {
137-
const commands = await getCommands(parameters.uri)
138-
return commands.toggleInlineCode(parameters)
139-
})
140-
141-
connection.onRequest('mdx/toggleStrong', async (parameters) => {
142-
const commands = await getCommands(parameters.uri)
143-
return commands.toggleStrong(parameters)
144-
})
145-
146124
connection.onInitialized(() => {
147125
const extensions = ['mdx']
148126
if (tsEnabled) {
@@ -164,12 +142,3 @@ connection.onInitialized(() => {
164142
})
165143

166144
connection.listen()
167-
168-
/**
169-
* @param {string} uri
170-
* @returns {Promise<Commands>}
171-
*/
172-
async function getCommands(uri) {
173-
const service = await server.project.getLanguageService(URI.parse(uri))
174-
return service.context.inject('mdxCommands')
175-
}

packages/language-server/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
"remark-frontmatter": "^5.0.0",
3939
"remark-gfm": "^4.0.0",
4040
"volar-service-markdown": "0.0.61",
41-
"volar-service-typescript": "0.0.61",
42-
"vscode-uri": "^3.0.0"
41+
"volar-service-typescript": "0.0.61"
4342
},
4443
"devDependencies": {
4544
"@types/node": "^22.0.0",
4645
"@volar/test-utils": "~2.4.0",
47-
"unified": "^11.0.0"
46+
"unified": "^11.0.0",
47+
"vscode-uri": "^3.0.0"
4848
}
4949
}

packages/language-server/test/initialize.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ test('initialize', async () => {
5555
},
5656
documentRangeFormattingProvider: true,
5757
documentSymbolProvider: true,
58+
executeCommandProvider: {
59+
commands: [
60+
'mdx.toggleDelete',
61+
'mdx.toggleEmphasis',
62+
'mdx.toggleInlineCode',
63+
'mdx.toggleStrong'
64+
]
65+
},
5866
experimental: {
5967
autoInsertionProvider: {
6068
configurationSections: [

0 commit comments

Comments
 (0)