Skip to content

Commit 979b4eb

Browse files
authored
Refactor to use unist-util-lsp
Closes GH-56. Reviewed-by: Titus Wormer <[email protected]>
1 parent 242bdef commit 979b4eb

File tree

2 files changed

+5
-50
lines changed

2 files changed

+5
-50
lines changed

lib/index.js

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/**
2-
* @typedef {import('unist').Point} Point
3-
* @typedef {import('unist').Position} UnistPosition
42
* @typedef {import('vfile-message').VFileMessage} VFileMessage
53
* @typedef {import('unified-engine').Options} EngineOptions
64
* @typedef {Pick<
@@ -36,6 +34,7 @@ import {fileURLToPath} from 'node:url'
3634
import {findUp, pathExists} from 'find-up'
3735
import {loadPlugin} from 'load-plugin'
3836
import {engine} from 'unified-engine'
37+
import {fromPosition} from 'unist-util-lsp'
3938
import {VFile} from 'vfile'
4039
import {
4140
createConnection,
@@ -52,52 +51,6 @@ import {
5251
} from 'vscode-languageserver/node.js'
5352
import {TextDocument} from 'vscode-languageserver-textdocument'
5453

55-
/**
56-
* Convert a unist point to a language server protocol position.
57-
*
58-
* @param {Point} point
59-
* @returns {Position}
60-
*/
61-
function unistPointToLspPosition(point) {
62-
return Position.create(point.line - 1, point.column - 1)
63-
}
64-
65-
/**
66-
* @param {Point|null|undefined} point
67-
* @returns {boolean}
68-
*/
69-
function isValidUnistPoint(point) {
70-
return Boolean(
71-
point && Number.isInteger(point.line) && Number.isInteger(point.column)
72-
)
73-
}
74-
75-
/**
76-
* Convert a unist position to a language server protocol range.
77-
*
78-
* If no position is given, a range is returned which represents the beginning
79-
* of the document.
80-
*
81-
* @param {UnistPosition|null|undefined} position
82-
* @returns {Range}
83-
*/
84-
function unistLocationToLspRange(position) {
85-
if (position) {
86-
const end = isValidUnistPoint(position.end)
87-
? unistPointToLspPosition(position.end)
88-
: undefined
89-
const start = isValidUnistPoint(position.start)
90-
? unistPointToLspPosition(position.start)
91-
: end
92-
93-
if (start) {
94-
return Range.create(start, end || start)
95-
}
96-
}
97-
98-
return Range.create(0, 0, 0, 0)
99-
}
100-
10154
/**
10255
* Convert a vfile message to a language server protocol diagnostic.
10356
*
@@ -106,7 +59,9 @@ function unistLocationToLspRange(position) {
10659
*/
10760
function vfileMessageToDiagnostic(message) {
10861
const diagnostic = Diagnostic.create(
109-
unistLocationToLspRange(message.position),
62+
message.position
63+
? fromPosition(message.position)
64+
: Range.create(0, 0, 0, 0),
11065
String(message.stack || message.reason),
11166
message.fatal === true
11267
? DiagnosticSeverity.Error

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
"index.d.ts"
3434
],
3535
"dependencies": {
36-
"@types/unist": "^2.0.0",
3736
"find-up": "^6.0.0",
3837
"load-plugin": "^5.0.0",
3938
"unified-engine": "^10.0.0",
39+
"unist-util-lsp": "^1.0.0",
4040
"vfile": "^5.0.0",
4141
"vfile-message": "^3.0.0",
4242
"vscode-languageserver": "^8.0.0",

0 commit comments

Comments
 (0)