Skip to content

Commit

Permalink
Add support for passing a processor
Browse files Browse the repository at this point in the history
Closes GH-19.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
remcohaszing committed Dec 30, 2021
1 parent c9896fc commit 0890127
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
22 changes: 17 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
* @typedef {import('unist').Position} UnistPosition
* @typedef {import('vfile-message').VFileMessage} VFileMessage
* @typedef {import('vscode-languageserver').Connection} Connection
* @typedef {Pick<
* @typedef {Partial<Pick<
* import('unified-engine').Options,
* 'ignoreName' | 'packageField' | 'pluginPrefix' | 'plugins' | 'rcName'
* >} Options
* | 'ignoreName'
* | 'packageField'
* | 'pluginPrefix'
* | 'plugins'
* | 'processor'
* | 'rcName'
* >>} Options
*/

import {PassThrough} from 'node:stream'
Expand Down Expand Up @@ -136,7 +141,14 @@ function lspDocumentToVfile(document) {
export function configureUnifiedLanguageServer(
connection,
documents,
{ignoreName, packageField, pluginPrefix, plugins, rcName}
{
ignoreName,
packageField,
pluginPrefix,
plugins,
processor = unified(),
rcName
}
) {
/**
* Process various LSP text documents using unified and send back the
Expand All @@ -156,7 +168,7 @@ export function configureUnifiedLanguageServer(
packageField,
pluginPrefix,
plugins,
processor: unified(),
processor,
quiet: false,
rcName,
silentlyIgnore: true,
Expand Down
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ Create a file names `package.json` with the following content:
"bin": "./index.js",
"type": "module",
"dependencies": {
"unified-language-server"
"remark": "^14.0.0",
"unified-language-server": "^1.0.0"
}
}
```

Then create `index.js` with the following content:

```js
import {remark} from 'remark'
import {createUnifiedLanguageServer} from 'unified-language-server'

process.title = 'remark-language-server'
Expand All @@ -91,7 +93,7 @@ createUnifiedLanguageServer({
ignoreName: '.remarkignore',
packageField: 'remarkConfig',
pluginPrefix: 'remark',
plugins: ['remark-parse', 'remark-stringify'],
processor: remark,
rcName: '.remarkrc'
})
```
Expand Down
39 changes: 38 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/**
* @typedef {import('unified').Processor} Processor
* @typedef {import('unified').Plugin} Plugin
* @typedef {import('./test-plugin').UnifiedTestPluginOptions} UnifiedTestPluginOptions
*/

import {pathToFileURL} from 'node:url'

import {spy, stub} from 'sinon'
import test from 'tape'
import {unified} from 'unified'
import * as exports from 'unified-language-server'
import {
CodeActionKind,
Expand Down Expand Up @@ -49,13 +52,15 @@ function createMockConnection() {
* @param {string} uri
* @param {string} text
* @param {UnifiedTestPluginOptions} [pluginOptions]
* @param {Processor} [processor]
* @param {string} pluginName
* @returns {Promise<import('vscode-languageserver').PublishDiagnosticsParams>}
*/
function getDiagnostic(
uri,
text,
pluginOptions,
processor,
pluginName = './test/test-plugin.js'
) {
const connection = createMockConnection()
Expand All @@ -72,7 +77,8 @@ function getDiagnostic(
})

configureUnifiedLanguageServer(connection, documents, {
plugins: [[pluginName, pluginOptions]]
plugins: [[pluginName, pluginOptions]],
processor
})

onDidChangeContent.firstCall.firstArg({
Expand Down Expand Up @@ -107,6 +113,36 @@ test('onInitialize', (t) => {
t.end()
})

test('Custom processor', async (t) => {
const uri = String(pathToFileURL('test.md'))
const diagnostics = await getDiagnostic(
uri,
'test',
undefined,
unified().use(
/** @type {Plugin} */ (
() => (ast, file) => {
file.message('custom processor')
}
)
)
)

t.deepEquals(diagnostics, {
uri,
version: 0,
diagnostics: [
{
range: {start: {line: 0, character: 0}, end: {line: 0, character: 0}},
message: 'custom processor',
severity: DiagnosticSeverity.Warning
}
]
})

t.end()
})

test('onDocumentFormatting different', async (t) => {
const connection = createMockConnection()
const documents = new TextDocuments(TextDocument)
Expand Down Expand Up @@ -220,6 +256,7 @@ test('onDidChangeContent transformer error', async (t) => {
uri,
'test',
undefined,
undefined,
'unresolved-plugin'
)

Expand Down

0 comments on commit 0890127

Please sign in to comment.