Skip to content

Commit

Permalink
Add functions to convert selection ranges (#2)
Browse files Browse the repository at this point in the history
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
remcohaszing authored Jun 7, 2024
1 parent 5ee16ea commit e9d1b46
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export { fromPosition, toPosition } from './position.js'
export { fromRange, toRange } from './range.js'
export { fromRelatedInformation, toRelatedInformation } from './relatedInformation.js'
export { fromSelectionRange, toSelectionRange } from './selectionRange.js'
export { fromSelectionRanges, toSelectionRanges } from './selectionRanges.js'
export { fromSemanticTokens, toSemanticTokens } from './semanticTokens.js'
export { fromSemanticTokensEdit, toSemanticTokensEdit } from './semanticTokensEdit.js'
export { fromSemanticTokensEdits, toSemanticTokensEdits } from './semanticTokensEdits.js'
Expand Down
4 changes: 4 additions & 0 deletions src/selectionRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { fromRange, toRange } from './range.js'
* The Monaco selection range to convert.
* @returns
* The selection range as an LSP selection range.
* @deprecated
* Use `fromSelectionRanges` instead.
*/
export function fromSelectionRange(
selectionRange: monaco.languages.SelectionRange
Expand All @@ -26,6 +28,8 @@ export function fromSelectionRange(
* The LSP selection range to convert.
* @returns
* The selection range as Monaco editor selection range.
* @deprecated
* Use `toSelectionRanges` instead.
*/
export function toSelectionRange(
selectionRange: lsp.SelectionRange
Expand Down
56 changes: 56 additions & 0 deletions src/selectionRanges.ts
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
}
25 changes: 25 additions & 0 deletions test/selectionRanges.test.ts
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 } }
]
}
)

0 comments on commit e9d1b46

Please sign in to comment.