Skip to content

Add fromPlace #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export {toPoint, toPosition, fromPoint, fromPosition} from './lib/index.js'
export {
fromPlace,
fromPoint,
fromPosition,
toPoint,
toPosition
} from './lib/index.js'
19 changes: 19 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
* @typedef {import('vscode-languageserver-types').Range} Range
*/

/**
* Convert a unist point or position to an LSP range.
*
* @param {Readonly<UnistPosition> | Readonly<Point>} 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.
*
Expand Down
40 changes: 36 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`fromPlace(place)`](#fromplaceplace)
* [`fromPoint(point)`](#frompointpoint)
* [`fromPosition(unistPosition)`](#frompositionunistposition)
* [`toPoint(lspPosition)`](#topointlspposition)
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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',
Expand Down