Skip to content

Commit

Permalink
Refactor to use unist-util-lsp
Browse files Browse the repository at this point in the history
Closes GH-56.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
remcohaszing committed Jun 1, 2023
1 parent 242bdef commit 979b4eb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 50 deletions.
53 changes: 4 additions & 49 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* @typedef {import('unist').Point} Point
* @typedef {import('unist').Position} UnistPosition
* @typedef {import('vfile-message').VFileMessage} VFileMessage
* @typedef {import('unified-engine').Options} EngineOptions
* @typedef {Pick<
Expand Down Expand Up @@ -36,6 +34,7 @@ import {fileURLToPath} from 'node:url'
import {findUp, pathExists} from 'find-up'
import {loadPlugin} from 'load-plugin'
import {engine} from 'unified-engine'
import {fromPosition} from 'unist-util-lsp'
import {VFile} from 'vfile'
import {
createConnection,
Expand All @@ -52,52 +51,6 @@ import {
} from 'vscode-languageserver/node.js'
import {TextDocument} from 'vscode-languageserver-textdocument'

/**
* Convert a unist point to a language server protocol position.
*
* @param {Point} point
* @returns {Position}
*/
function unistPointToLspPosition(point) {
return Position.create(point.line - 1, point.column - 1)
}

/**
* @param {Point|null|undefined} point
* @returns {boolean}
*/
function isValidUnistPoint(point) {
return Boolean(
point && Number.isInteger(point.line) && Number.isInteger(point.column)
)
}

/**
* Convert a unist position to a language server protocol range.
*
* If no position is given, a range is returned which represents the beginning
* of the document.
*
* @param {UnistPosition|null|undefined} position
* @returns {Range}
*/
function unistLocationToLspRange(position) {
if (position) {
const end = isValidUnistPoint(position.end)
? unistPointToLspPosition(position.end)
: undefined
const start = isValidUnistPoint(position.start)
? unistPointToLspPosition(position.start)
: end

if (start) {
return Range.create(start, end || start)
}
}

return Range.create(0, 0, 0, 0)
}

/**
* Convert a vfile message to a language server protocol diagnostic.
*
Expand All @@ -106,7 +59,9 @@ function unistLocationToLspRange(position) {
*/
function vfileMessageToDiagnostic(message) {
const diagnostic = Diagnostic.create(
unistLocationToLspRange(message.position),
message.position
? fromPosition(message.position)
: Range.create(0, 0, 0, 0),
String(message.stack || message.reason),
message.fatal === true
? DiagnosticSeverity.Error
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"index.d.ts"
],
"dependencies": {
"@types/unist": "^2.0.0",
"find-up": "^6.0.0",
"load-plugin": "^5.0.0",
"unified-engine": "^10.0.0",
"unist-util-lsp": "^1.0.0",
"vfile": "^5.0.0",
"vfile-message": "^3.0.0",
"vscode-languageserver": "^8.0.0",
Expand Down

0 comments on commit 979b4eb

Please sign in to comment.