diff --git a/lib/index.js b/lib/index.js index c6bbc80..8085a99 100644 --- a/lib/index.js +++ b/lib/index.js @@ -40,7 +40,7 @@ import path from 'node:path' import {PassThrough} from 'node:stream' -import {fileURLToPath} from 'node:url' +import {fileURLToPath, pathToFileURL} from 'node:url' import {findUp, pathExists} from 'find-up' import {loadPlugin} from 'load-plugin' import {engine} from 'unified-engine' @@ -79,8 +79,8 @@ function vfileMessageToDiagnostic(message) { message.fatal === true ? DiagnosticSeverity.Error : message.fatal === false - ? DiagnosticSeverity.Warning - : DiagnosticSeverity.Information, + ? DiagnosticSeverity.Warning + : DiagnosticSeverity.Information, message.ruleId || undefined, message.source || undefined ) @@ -196,7 +196,7 @@ export function createUnifiedLanguageServer({ try { processor = /** @type {EngineOptions['processor']} */ ( await loadPlugin(processorName, { - cwd, + from: pathToFileURL(cwd + '/'), key: processorSpecifier }) ) @@ -290,15 +290,17 @@ export function createUnifiedLanguageServer({ let cwd if (workspaces.size === 0) { cwd = await findUp( - async (dir) => { - const pkgExists = await pathExists(path.join(dir, 'package.json')) - if (pkgExists) { - return dir + async (directory) => { + const packageExists = await pathExists( + path.join(directory, 'package.json') + ) + if (packageExists) { + return directory } - const gitExists = await pathExists(path.join(dir, '.git')) + const gitExists = await pathExists(path.join(directory, '.git')) if (gitExists) { - return dir + return directory } }, { diff --git a/package.json b/package.json index 42a50e1..beff3f9 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ ], "dependencies": { "find-up": "^6.0.0", - "load-plugin": "^5.0.0", + "load-plugin": "^6.0.0", "unified-engine": "^11.0.0", "unist-util-lsp": "^2.0.0", "vfile": "^6.0.0", @@ -42,15 +42,15 @@ }, "devDependencies": { "@types/node": "^20.0.0", - "c8": "^8.0.0", + "c8": "^9.0.0", "prettier": "^3.0.0", "remark": "^15.0.0", - "remark-cli": "^11.0.0", - "remark-preset-wooorm": "^9.0.0", + "remark-cli": "^12.0.0", + "remark-preset-wooorm": "^10.0.0", "type-coverage": "^2.0.0", "typescript": "^5.0.0", "unified": "^11.0.0", - "xo": "^0.56.0" + "xo": "^0.58.0" }, "scripts": { "prepack": "npm run build", diff --git a/readme.md b/readme.md index 7a5370b..4db9c02 100644 --- a/readme.md +++ b/readme.md @@ -12,22 +12,22 @@ Create a **[language server][]** based on **[unified][]** ecosystems. ## Contents -* [What is this?](#what-is-this) -* [When should I use this?](#when-should-i-use-this) -* [Install](#install) -* [Use](#use) -* [API](#api) - * [`createUnifiedLanguageServer(options)`](#createunifiedlanguageserveroptions) -* [Examples](#examples) -* [Types](#types) -* [Language Server features](#language-server-features) - * [Watching files](#watching-files) - * [Requests](#requests) - * [Configuration](#configuration) -* [Compatibility](#compatibility) -* [Related](#related) -* [Contribute](#contribute) -* [License](#license) +* [What is this?](#what-is-this) +* [When should I use this?](#when-should-i-use-this) +* [Install](#install) +* [Use](#use) +* [API](#api) + * [`createUnifiedLanguageServer(options)`](#createunifiedlanguageserveroptions) +* [Examples](#examples) +* [Types](#types) +* [Language Server features](#language-server-features) + * [Watching files](#watching-files) + * [Requests](#requests) + * [Configuration](#configuration) +* [Compatibility](#compatibility) +* [Related](#related) +* [Contribute](#contribute) +* [License](#license) ## What is this? @@ -35,10 +35,10 @@ This package exports a function which can be used to create a [language server][] based on [unified][] processors. It can do the following: -* format documents based on a unified processor -* validate documents based on a unified processor -* support configuration files (such as `.remarkrc`) using - [`unified-engine`][unified-engine] +* format documents based on a unified processor +* validate documents based on a unified processor +* support configuration files (such as `.remarkrc`) using + [`unified-engine`][unified-engine] **unified** is a project that validates and transforms content with abstract syntax trees (ASTs). @@ -58,7 +58,7 @@ various editors. ## Install -This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). +This package is [ESM only][]. In Node.js (version 16.0+), install with [npm][]: ```sh @@ -162,11 +162,11 @@ Name of configuration files to load (`string`, optional). For examples, see the following projects: -* `redot-language-server` - (coming soon) -* `rehype-language-server` - (coming soon) -* [`remark-language-server`](https://github.com/remarkjs/remark-language-server) +* `redot-language-server` + (coming soon) +* `rehype-language-server` + (coming soon) +* [`remark-language-server`](https://github.com/remarkjs/remark-language-server) ## Types @@ -187,39 +187,39 @@ change was made. Language servers created using this package implement the following language server features: -* `textDocument/codeAction` - — the language server implements code actions based on the `expected` field - on reported messages. - A code action can either insert, replace, or delete text based on the range - of the message and the expected value. -* `textDocument/didChange` - — when a document is changed by the client, the language server processes it - using a unified pipeline. - Any messages collected are published to the client using - `textDocument/publishDiagnostics`. -* `textDocument/didClose` - — when a document is closed by the client, the language server resets - diagnostics by publishing an empty array using - `textDocument/publishDiagnostics`. -* `textDocument/didOpen` - — when a document is opened by the client, the language server processes it - using a unified pipeline. - Any messages collected are published to the client using - `textDocument/publishDiagnostics`. -* `textDocument/formatting` - — when document formatting is requested by the client, the language server - processes it using a unified pipeline. - The stringified result is returned. -* `workspace/didChangeWatchedFiles` and `workspace/didChangeWorkspaceFolders` - — when the client signals a watched file or workspace has changed, the - language server processes all open files using a unified pipeline. - Any messages collected are published to the client using - `textDocument/publishDiagnostics`. +* `textDocument/codeAction` + — the language server implements code actions based on the `expected` field + on reported messages. + A code action can either insert, replace, or delete text based on the range + of the message and the expected value. +* `textDocument/didChange` + — when a document is changed by the client, the language server processes it + using a unified pipeline. + Any messages collected are published to the client using + `textDocument/publishDiagnostics`. +* `textDocument/didClose` + — when a document is closed by the client, the language server resets + diagnostics by publishing an empty array using + `textDocument/publishDiagnostics`. +* `textDocument/didOpen` + — when a document is opened by the client, the language server processes it + using a unified pipeline. + Any messages collected are published to the client using + `textDocument/publishDiagnostics`. +* `textDocument/formatting` + — when document formatting is requested by the client, the language server + processes it using a unified pipeline. + The stringified result is returned. +* `workspace/didChangeWatchedFiles` and `workspace/didChangeWorkspaceFolders` + — when the client signals a watched file or workspace has changed, the + language server processes all open files using a unified pipeline. + Any messages collected are published to the client using + `textDocument/publishDiagnostics`. ### Configuration -* `requireConfig` (default: `false`) - — If true, files will only be checked if a configuration file is present. +* `requireConfig` (default: `false`) + — If true, files will only be checked if a configuration file is present. ## Compatibility @@ -234,10 +234,10 @@ It should work anywhere where LSP 3.6.0 or later is implemented. ## Related -* [`unified`](https://github.com/unifiedjs/unified) - — create pipeline for working with syntax trees -* [`unified-args`](https://github.com/unifiedjs/unified-args) - — create a CLI for a unified pipeline +* [`unified`](https://github.com/unifiedjs/unified) + — create pipeline for working with syntax trees +* [`unified-args`](https://github.com/unifiedjs/unified-args) + — create a CLI for a unified pipeline ## Contribute @@ -267,6 +267,8 @@ abide by its terms. [downloads]: https://www.npmjs.com/package/unified-language-server +[esm only]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c + [size-badge]: https://img.shields.io/bundlephobia/minzip/unified-language-server.svg [size]: https://bundlephobia.com/result?p=unified-language-server diff --git a/test/index.js b/test/index.js index d33ce89..d7a2a2a 100644 --- a/test/index.js +++ b/test/index.js @@ -981,7 +981,7 @@ function cleanStack(stack, max) { * @param relativeCwd The cwd to use for the process relative to this test file. */ function startLanguageServer(serverFilePath, relativeCwd = './') { - const bin = fileURLToPath(new URL(serverFilePath, import.meta.url)) + const binary = fileURLToPath(new URL(serverFilePath, import.meta.url)) const cwd = new URL(relativeCwd, import.meta.url) // Using ipc is useful for debugging. This allows logging in the language @@ -989,20 +989,23 @@ function startLanguageServer(serverFilePath, relativeCwd = './') { // Enabling this breaks code coverage // https://github.com/bcoe/c8/issues/189 if (process.argv.includes('--ipc')) { - const proc = spawn('node', [bin, '--node-ipc'], { + const serverProcess = spawn('node', [binary, '--node-ipc'], { cwd, stdio: [null, 'inherit', 'inherit', 'ipc'] }) connection = createProtocolConnection( - new IPCMessageReader(proc), - new IPCMessageWriter(proc) + new IPCMessageReader(serverProcess), + new IPCMessageWriter(serverProcess) ) connection.onDispose(() => { - proc.kill() + serverProcess.kill() }) } else { - const proc = spawn('node', [bin, '--stdio'], {cwd}) - connection = createProtocolConnection(proc.stdout, proc.stdin) + const serverProcess = spawn('node', [binary, '--stdio'], {cwd}) + connection = createProtocolConnection( + serverProcess.stdout, + serverProcess.stdin + ) } connection.onDispose(() => {