Skip to content

Remove TypeScript moving parts #512

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions packages/language-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,6 @@ It uses the `workspace/applyEdit` command to apply edits.

`null`

### Initialize Options

MDX language server supports the following LSP initialization options:

* `typescript.enabled` (`boolean`, default: `false`) —
If true, enable TypeScript.
* `typescript.tsdk` (`string`, required) —
The path from which to load TypeScript.
* `locale` (`string`, optional) —
The locale to use for TypeScript error messages.

### Configuration

MDX language server supports the following LSP configuration options:
Expand Down
115 changes: 8 additions & 107 deletions packages/language-server/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
#!/usr/bin/env node

/**
* @import {VirtualCodePlugin} from '@mdx-js/language-service'
* @import {PluggableList} from 'unified'
*/

import assert from 'node:assert'
import {createRequire} from 'node:module'
import path from 'node:path'
import process from 'node:process'
import {
createMdxLanguagePlugin,
createMdxServicePlugin,
resolvePlugins
} from '@mdx-js/language-service'
import {
createConnection,
createServer,
createTypeScriptProject,
loadTsdkByPath
createSimpleProject
} from '@volar/language-server/node.js'
import remarkFrontmatter from 'remark-frontmatter'
import remarkGfm from 'remark-gfm'
import typescript from 'typescript'
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'

process.title = 'mdx-language-server'
Expand All @@ -32,116 +26,23 @@ process.title = 'mdx-language-server'
const defaultPlugins = [[remarkFrontmatter, ['toml', 'yaml']], remarkGfm]
const connection = createConnection()
const server = createServer(connection)
let tsEnabled = false

connection.onInitialize(async (parameters) => {
const tsdk = parameters.initializationOptions?.typescript?.tsdk
tsEnabled = Boolean(parameters.initializationOptions?.typescript?.enabled)
assert.ok(
typeof tsdk === 'string',
'Missing initialization option typescript.tsdk'
)

const {typescript, diagnosticMessages} = loadTsdkByPath(
tsdk,
parameters.locale
)

return server.initialize(
parameters,
createTypeScriptProject(
typescript,
diagnosticMessages,
({configFileName}) => ({
languagePlugins: getLanguagePlugins(configFileName)
})
),
getLanguageServicePlugins()
)

function getLanguageServicePlugins() {
const plugins = [
createSimpleProject([createMdxLanguagePlugin(defaultPlugins)]),
[
createMarkdownServicePlugin({
getDiagnosticOptions(document, context) {
return context.env.getConfiguration?.('mdx.validate')
}
}),
createMdxServicePlugin(connection.workspace)
createMdxServicePlugin(connection.workspace),
createTypeScriptSyntacticServicePlugin(typescript)
]

if (tsEnabled) {
plugins.push(...createTypeScriptServicePlugin(typescript, {}))
} else {
plugins.push(createTypeScriptSyntacticServicePlugin(typescript))
}

return plugins
}

/**
* @param {string | undefined} tsconfig
*/
function getLanguagePlugins(tsconfig) {
/** @type {PluggableList | undefined} */
let remarkPlugins
/** @type {VirtualCodePlugin[] | undefined} */
let virtualCodePlugins
let checkMdx = false
let jsxImportSource = 'react'

if (tsconfig) {
const cwd = path.dirname(tsconfig)
const configSourceFile = typescript.readJsonConfigFile(
tsconfig,
typescript.sys.readFile
)
const commandLine = typescript.parseJsonSourceFileConfigFileContent(
configSourceFile,
typescript.sys,
cwd,
undefined,
tsconfig
)

const require = createRequire(tsconfig)

;[remarkPlugins, virtualCodePlugins] = resolvePlugins(
commandLine.raw?.mdx,
(name) => require(name).default
)
checkMdx = Boolean(commandLine.raw?.mdx?.checkMdx)
jsxImportSource = commandLine.options.jsxImportSource || jsxImportSource
}

return [
createMdxLanguagePlugin(
remarkPlugins || defaultPlugins,
virtualCodePlugins,
checkMdx,
jsxImportSource
)
]
}
)
})

connection.onInitialized(() => {
const extensions = ['mdx']
if (tsEnabled) {
extensions.push(
'cjs',
'cts',
'js',
'jsx',
'json',
'mjs',
'mts',
'ts',
'tsx'
)
}

server.initialized()
server.fileWatcher.watchFiles([`**/*.{${extensions.join(',')}}`])
})
connection.onInitialized(server.initialized)

connection.listen()
3 changes: 3 additions & 0 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"volar-service-markdown": "0.0.64",
"volar-service-typescript": "0.0.64"
},
"peerDependencies": {
"typescript": "*"
},
"devDependencies": {
"@types/node": "^22.0.0",
"@volar/test-utils": "~2.4.0",
Expand Down
8 changes: 1 addition & 7 deletions packages/vscode-mdx/src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import * as languageServerProtocol from '@volar/language-server/protocol.js'
import {
activateAutoInsertion,
activateDocumentDropEdit,
createLabsInfo,
getTsdk
createLabsInfo
} from '@volar/vscode'
import {
extensions,
Expand Down Expand Up @@ -42,8 +41,6 @@ let disposable
export async function activate(context) {
extensions.getExtension('vscode.typescript-language-features')?.activate()

const {tsdk} = (await getTsdk(context)) ?? {tsdk: ''}

client = new LanguageClient(
'MDX',
{
Expand All @@ -52,9 +49,6 @@ export async function activate(context) {
},
{
documentSelector: [{language: 'mdx'}],
initializationOptions: {
typescript: {tsdk}
},
markdown: {
isTrusted: true,
supportHtml: true
Expand Down
Loading