1
1
/**
2
- * @typedef {import('unist').Point } Point
3
- * @typedef {import('unist').Position } UnistPosition
4
2
* @typedef {import('vfile-message').VFileMessage } VFileMessage
5
3
* @typedef {import('unified-engine').Options } EngineOptions
6
4
* @typedef {Pick<
@@ -36,6 +34,7 @@ import {fileURLToPath} from 'node:url'
36
34
import { findUp , pathExists } from 'find-up'
37
35
import { loadPlugin } from 'load-plugin'
38
36
import { engine } from 'unified-engine'
37
+ import { fromPosition } from 'unist-util-lsp'
39
38
import { VFile } from 'vfile'
40
39
import {
41
40
createConnection ,
@@ -52,52 +51,6 @@ import {
52
51
} from 'vscode-languageserver/node.js'
53
52
import { TextDocument } from 'vscode-languageserver-textdocument'
54
53
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
-
101
54
/**
102
55
* Convert a vfile message to a language server protocol diagnostic.
103
56
*
@@ -106,7 +59,9 @@ function unistLocationToLspRange(position) {
106
59
*/
107
60
function vfileMessageToDiagnostic ( message ) {
108
61
const diagnostic = Diagnostic . create (
109
- unistLocationToLspRange ( message . position ) ,
62
+ message . position
63
+ ? fromPosition ( message . position )
64
+ : Range . create ( 0 , 0 , 0 , 0 ) ,
110
65
String ( message . stack || message . reason ) ,
111
66
message . fatal === true
112
67
? DiagnosticSeverity . Error
0 commit comments