Skip to content

Commit

Permalink
Support linked editing ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Dec 8, 2023
1 parent cbac101 commit dc95bef
Show file tree
Hide file tree
Showing 3 changed files with 96 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 @@ -23,6 +23,7 @@ export { fromInlayHint, toInlayHint } from './inlayHint.js'
export { fromInlayHintKind, toInlayHintKind } from './inlayHintKind.js'
export { fromInlayHintLabelPart, toInlayHintLabelPart } from './inlayHintLabelPart.js'
export { fromLink, toLink } from './link.js'
export { fromLinkedEditingRanges, toLinkedEditingRanges } from './linkedEditingRanges.js'
export { fromLocation, toLocation } from './location.js'
export { fromLocationLink, toLocationLink } from './locationLink.js'
export { fromMarkdownString, toMarkdownString } from './markdownString.js'
Expand Down
48 changes: 48 additions & 0 deletions src/linkedEditingRanges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type * as monaco from 'monaco-types'
import type * as ls from 'vscode-languageserver-protocol'

import { fromRange, toRange } from './range.js'

/**
* Convert Monaco editor linked editing ranges to LSP linked editing ranges.
*
* @param linkedEditingRanges
* The Monaco linked editing ranges to convert.
* @returns
* The linked editing ranges as LSP linked editing ranges.
*/
export function fromLinkedEditingRanges(
linkedEditingRanges: monaco.languages.LinkedEditingRanges
): ls.LinkedEditingRanges {
const result: ls.LinkedEditingRanges = {
ranges: linkedEditingRanges.ranges.map(fromRange)
}

if (linkedEditingRanges.wordPattern) {
result.wordPattern = linkedEditingRanges.wordPattern.source
}

return result
}

/**
* Convert LSP linked editing ranges to Monaco editor linked editing ranges.
*
* @param linkedEditingRanges
* The LSP linked editing ranges to convert.
* @returns
* The linked editing ranges Monaco editor linked editing ranges.
*/
export function toLinkedEditingRanges(
linkedEditingRanges: ls.LinkedEditingRanges
): monaco.languages.LinkedEditingRanges {
const result: monaco.languages.LinkedEditingRanges = {
ranges: linkedEditingRanges.ranges.map(toRange)
}

if (linkedEditingRanges.wordPattern != null) {
result.wordPattern = new RegExp(linkedEditingRanges.wordPattern)
}

return result
}
47 changes: 47 additions & 0 deletions test/linkedEditingRanges.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { runTests } from './utils.js'
import { fromLinkedEditingRanges, toLinkedEditingRanges } from '../src/index.js'

runTests(fromLinkedEditingRanges, toLinkedEditingRanges)(
{
lsp: {
ranges: [
{
start: { line: 0, character: 10 },
end: { line: 2, character: 20 }
}
]
},
monaco: {
ranges: [
{
startLineNumber: 1,
startColumn: 11,
endLineNumber: 3,
endColumn: 21
}
]
}
},
{
lsp: {
ranges: [
{
start: { line: 0, character: 10 },
end: { line: 2, character: 20 }
}
],
wordPattern: 'pattern'
},
monaco: {
ranges: [
{
startLineNumber: 1,
startColumn: 11,
endLineNumber: 3,
endColumn: 21
}
],
wordPattern: /pattern/
}
}
)

0 comments on commit dc95bef

Please sign in to comment.