-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to convert selection ranges (#2)
LSP selection ranges define their parent selection range using the `parent` property. Monaco editor on the other hand, defines this ancestry as an array. This means that a singular Monaco editor selection range can’t be represented in LSP. This deprecates the functions `fromSelectionRange` and `toSelectionRange` in favor the plural variants `fromSelectionRanges` and `toSelectionRanges`.
- Loading branch information
1 parent
5ee16ea
commit e9d1b46
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type * as monaco from 'monaco-types' | ||
import type * as lsp from 'vscode-languageserver-protocol' | ||
|
||
import { fromRange, toRange } from './range.js' | ||
|
||
/** | ||
* Convert Monaco editor selection ranges to an LSP selection ranges. | ||
* | ||
* @param selectionRanges | ||
* The Monaco selections range to convert. | ||
* @returns | ||
* The selection ranges as LSP selection range. | ||
*/ | ||
export function fromSelectionRanges( | ||
selectionRanges: monaco.languages.SelectionRange[] | ||
): lsp.SelectionRange | undefined { | ||
let result: lsp.SelectionRange | undefined | ||
|
||
for (let index = selectionRanges.length - 1; index >= 0; index -= 1) { | ||
const parent = result | ||
|
||
result = { | ||
range: fromRange(selectionRanges[index].range) | ||
} | ||
|
||
if (parent) { | ||
result.parent = parent | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Convert an LSP selection range to Monaco editor selection ranges. | ||
* | ||
* @param selectionRange | ||
* The LSP selection range to convert. | ||
* @returns | ||
* The selection range as Monaco editor selection ranges. | ||
*/ | ||
export function toSelectionRanges( | ||
selectionRange: lsp.SelectionRange | undefined | ||
): monaco.languages.SelectionRange[] { | ||
const result: monaco.languages.SelectionRange[] = [] | ||
let current = selectionRange | ||
|
||
while (current) { | ||
result.push({ | ||
range: toRange(current.range) | ||
}) | ||
current = current.parent | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { runTests } from './utils.js' | ||
import { fromSelectionRanges, toSelectionRanges } from '../src/index.js' | ||
|
||
runTests(fromSelectionRanges, toSelectionRanges)( | ||
{ | ||
lsp: { range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } } }, | ||
monaco: [{ range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } }] | ||
}, | ||
{ | ||
lsp: { | ||
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } }, | ||
parent: { | ||
range: { start: { line: 10, character: 10 }, end: { line: 10, character: 10 } }, | ||
parent: { | ||
range: { start: { line: 20, character: 20 }, end: { line: 20, character: 20 } } | ||
} | ||
} | ||
}, | ||
monaco: [ | ||
{ range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } }, | ||
{ range: { startLineNumber: 11, startColumn: 11, endLineNumber: 11, endColumn: 11 } }, | ||
{ range: { startLineNumber: 21, startColumn: 21, endLineNumber: 21, endColumn: 21 } } | ||
] | ||
} | ||
) |