diff --git a/index.js b/index.js index d5eafdb..3343eec 100644 --- a/index.js +++ b/index.js @@ -1 +1,7 @@ -export {toPoint, toPosition, fromPoint, fromPosition} from './lib/index.js' +export { + fromPlace, + fromPoint, + fromPosition, + toPoint, + toPosition +} from './lib/index.js' diff --git a/lib/index.js b/lib/index.js index 9d77a9a..648acc0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,6 +5,25 @@ * @typedef {import('vscode-languageserver-types').Range} Range */ +/** + * Convert a unist point or position to an LSP range. + * + * @param {Readonly | Readonly} place + * The unist point or position to convert. + * @returns {Range} + * The LSP range. + */ +export function fromPlace(place) { + if ('line' in place && 'column' in place) { + return { + start: fromPoint(place), + end: fromPoint(place) + } + } + + return fromPosition(place) +} + /** * Turn a unist point into an LSP position. * diff --git a/readme.md b/readme.md index 570546c..ab2b5a1 100644 --- a/readme.md +++ b/readme.md @@ -18,6 +18,7 @@ * [Install](#install) * [Use](#use) * [API](#api) + * [`fromPlace(place)`](#fromplaceplace) * [`fromPoint(point)`](#frompointpoint) * [`fromPosition(unistPosition)`](#frompositionunistposition) * [`toPoint(lspPosition)`](#topointlspposition) @@ -75,7 +76,13 @@ Say we have the following `example.md`: ```js import fs from 'node:fs/promises' import {fromMarkdown} from 'mdast-util-from-markdown' -import {fromPoint, fromPosition, toPoint, toPosition} from 'unist-util-lsp' +import { + fromPlace, + fromPoint, + fromPosition, + toPoint, + toPosition +} from 'unist-util-lsp' const markdown = String(await fs.readFile('example.md')) const mdast = fromMarkdown(markdown) @@ -97,6 +104,14 @@ console.log(startPosition) const startPoint = toPoint(startPosition) console.log(startPoint) + +const fullRange = fromPlace(mdast.position) + +console.log(fullRange) + +const startRange = fromPlace(mdast.position.start) + +console.log(startRange) ``` …now running `node example.js` yields: @@ -110,15 +125,30 @@ console.log(startPoint) { start: { column: 1, line: 1 }, end: { column: 1, line: 2 } } { character: 0, line: 0 } { column: 1, line: 1 } +{ start: { character: 0, line: 0 }, end: { character: 0, line: 1 } } +{ start: { character: 0, line: 0 }, end: { character: 0, line: 0 } } ``` ## API -This package exports the identifiers [`fromPoint`][api-from-point], -[`fromPosition`][api-from-position], [`toPoint`][api-to-point], -and [`toPosition`][api-to-position]. +This package exports the identifiers [`fromPlace`][api-from-place], +[`fromPoint`][api-from-point], [`fromPosition`][api-from-position], +[`toPoint`][api-to-point], and [`toPosition`][api-to-position]. There is no default export. +### `fromPlace(place)` + +Convert a unist point or position to an LSP range. + +###### Parameters + +* `place` ([UnistPoint][point] | [`UnistPosition`][unist-position]) + — the unist point or position to convert + +###### Returns + +The LSP range ([`Range`][range]). + ### `fromPoint(point)` Turn a unist point into an LSP position. @@ -269,6 +299,8 @@ abide by its terms. [range]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#range +[api-from-place]: #fromplaceplace + [api-from-point]: #frompointpoint [api-from-position]: #frompositionunistposition diff --git a/test/index.js b/test/index.js index 885b47e..b69f068 100644 --- a/test/index.js +++ b/test/index.js @@ -1,10 +1,17 @@ import assert from 'node:assert/strict' import test from 'node:test' -import {fromPoint, fromPosition, toPoint, toPosition} from 'unist-util-lsp' +import { + fromPlace, + fromPoint, + fromPosition, + toPoint, + toPosition +} from 'unist-util-lsp' test('unist-util-lsp', async function (t) { await t.test('should expose the public api', async function () { assert.deepEqual(Object.keys(await import('unist-util-lsp')).sort(), [ + 'fromPlace', 'fromPoint', 'fromPosition', 'toPoint', @@ -13,6 +20,28 @@ test('unist-util-lsp', async function (t) { }) }) +test('fromPlace', async function (t) { + await t.test( + 'should convert unist points to LSP positions', + async function () { + assert.deepEqual(fromPlace({line: 43, column: 100}), { + start: {line: 42, character: 99}, + end: {line: 42, character: 99} + }) + } + ) + + await t.test( + 'should convert unist positions to LSP ranges', + async function () { + assert.deepEqual( + fromPlace({start: {line: 1, column: 2}, end: {line: 3, column: 4}}), + {start: {line: 0, character: 1}, end: {line: 2, character: 3}} + ) + } + ) +}) + test('fromPoint', async function (t) { await t.test( 'should convert unist points to LSP positions',