Skip to content
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

Add initial implementation #1

Merged
merged 14 commits into from
Aug 28, 2022
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: main
on:
- pull_request
- push
jobs:
main:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v3
strategy:
matrix:
node:
- lts/fermium
- node
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
*.d.ts
*.log
*.tgz
coverage/
node_modules/
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage/
*.md
Empty file added example.md
Empty file.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
lspPositionToUnistPoint,
lspRangeToUnistPosition,
unistPointToLspPosition,
unistPositionToLspRange
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
} from './lib/index.js'
58 changes: 58 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @typedef {import('unist').Point} Point
* @typedef {import('unist').Position} UnistPosition
* @typedef {import('vscode-languageserver-types').Position} LspPosition
* @typedef {import('vscode-languageserver-types').Range} Range
*/
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

/**
* Convert a unist point to an LSP position.
*
* @param {Point} point The unist point to convert.
* @returns {LspPosition} The point converted to an LSP position.
*/
export function unistPointToLspPosition(point) {
return {
character: point.column - 1,
line: point.line - 1
}
}

/**
* Convert an LSP position to a unist point.
*
* @param {LspPosition} position The LSP position to convert.
* @returns {Point} The position converted to a unist point.
*/
export function lspPositionToUnistPoint(position) {
return {
column: position.character + 1,
line: position.line + 1
}
}

/**
* Convert a unist position to an LSP range.
*
* @param {UnistPosition} position The unist position to convert.
* @returns {Range} The position converted to an LSP range.
*/
export function unistPositionToLspRange(position) {
return {
start: unistPointToLspPosition(position.start),
end: unistPointToLspPosition(position.end)
}
}
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

/**
* Convert an LSP range to a unist position.
*
* @param {Range} range The LSP range to convert.
* @returns {UnistPosition} The range converted to a unist position.
*/
export function lspRangeToUnistPosition(range) {
return {
start: lspPositionToUnistPoint(range.start),
end: lspPositionToUnistPoint(range.end)
}
}
74 changes: 74 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"name": "unist-lsp",
"version": "0.0.0",
"description": "Utility to convert between unist and language server protocol",
"license": "MIT",
"keywords": [
"unist",
"util",
"utility",
"lsp",
"language-server-protocol"
],
"repository": "syntax-tree/unist-lsp",
"bugs": "https://github.com/syntax-tree/unist-lsp/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"author": "Remco Haszing <remcohaszing@gmail.com>",
"contributors": [
"Remco Haszing <remcohaszing@gmail.com>"
],
"sideEffects": false,
"type": "module",
"exports": "./index.js",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0",
"vscode-languageserver-types": "^3.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.52.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "tsc && type-coverage",
"format": "remark . -qfo && prettier -w . --loglevel warn && xo --fix",
"test-api": "node test/index.js",
"test-coverage": "c8 --100 --reporter lcov node test/index.js",
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"prettier": true
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
206 changes: 206 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# unist-lsp

[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

Utility to convert between [unist][] and [language server protocol][].
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

## Contents

* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`unistPointToLspPosition(point)`](#unistpointtolsppositionpoint)
* [`unist-lsp(position)`](#unist-lspposition)
* [`unistPositionToLspRange(position)`](#unistpositiontolsprangeposition)
* [`lspRangeToUnistPosition(range)`](#lsprangetounistpositionrange)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)

## What is this?

This package is a utility that takes converts between [unist][] and
[language server protocol][].
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

## When should I use this?

This project is useful when you want to deal with unist ASTs and a language
server.
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

## Install

This package is [ESM only][esm].
In Node.js (version 14.14+, or 16.0+), install with [npm][]:

```sh
npm install unist-lsp
```

In Deno with [`esm.sh`][esmsh]:

```js
import {
lspPositionToUnistPoint,
lspRangeToUnistPosition,
unistPointToLspPosition,
unistPositionToLspRange,
} from "https://esm.sh/unist-lsp@?0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import {
lspPositionToUnistPoint,
lspRangeToUnistPosition,
unistPointToLspPosition,
unistPositionToLspRange,
} from "https://esm.sh/unist-lsp@?0"
import * as unistLsp from "https://esm.sh/unist-lsp@?0"

As there are so many exports, which are documented below, I believe it is better to not overwhelm users with all the possible options

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with the shorter names? I generally prefer named imports over wildcard imports.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For like 2 it is fine. Maybe 3? But 4 or more feels like a bit much to me.

Not very important to me though.

```

In browsers with [`esm.sh`][esmsh]:

```html
<script type="module">
import {
lspPositionToUnistPoint,
lspRangeToUnistPosition,
unistPointToLspPosition,
unistPositionToLspRange,
} from "https://esm.sh/unist-lsp@?0"
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
</script>
```

## Use

Say we have the following `example.md`:

```markdown
## Hello **World**!
```

…and next to it a module `example.js`:

```js
import {promises as fs} from 'node:fs'
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
import {fromMarkdown} from 'mdast-util-from-markdown'


const markdown = String(await fs.readFile('example.md'))
const mdast = fromMarkdown(markdown)

const range = unistPositionToLspRange(mdast.position)

console.dir(range)
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
```

…now running `node example.js` yields:

```html
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
{ start: { line: 0, character: 0 }, end: { line: 0, character: 19 } }
```

## API

This package exports the identifiers `lspPositionToUnistPoint`, `lspRangeToUnistPosition`, `unistPointToLspPosition`, and `unistPositionToLspRange`
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
There is no default export.

### `unistPointToLspPosition(point)`

Convert a unist point to a language server protocol position.
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

### `unist-lsp(position)`

Convert a language server protocol position to a unist point.

### `unistPositionToLspRange(position)`

Convert a unist position to a language server protocol range.

### `lspRangeToUnistPosition(range)`

Convert a language server protocol range to a unist position.

## Types

This package is fully typed with [TypeScript][].
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

## Compatibility

Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 14.14+, and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.

## Security

remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
## Related

* [`unist`][unist]
— a specification for abstract syntax trees
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
* [`language server protocol`](https://microsoft.github.io/language-server-protocol)
— a protocol for communicating between an editor and a language server
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

## Contribute

See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
started.
See [`support.md`][support] for ways to get help.

This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.

## License

[MIT][license] © [Remco Haszing][author]

<!-- Definitions -->

[build-badge]: https://github.com/syntax-tree/unist-lsp/workflows/main/badge.svg

[build]: https://github.com/syntax-tree/unist-lsp/actions

[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-lsp.svg

[coverage]: https://codecov.io/github/syntax-tree/unist-lsp

[downloads-badge]: https://img.shields.io/npm/dm/unist-lsp.svg

[downloads]: https://www.npmjs.com/package/unist-lsp

[size-badge]: https://img.shields.io/bundlephobia/minzip/unist-lsp.svg

[size]: https://bundlephobia.com/result?p=unist-lsp

[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

[backers-badge]: https://opencollective.com/unified/backers/badge.svg

[collective]: https://opencollective.com/unified

[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg

[chat]: https://github.com/syntax-tree/unist/discussions

[language server protocol]: https://microsoft.github.io/language-server-protocol

[license]: license

[npm]: https://docs.npmjs.com/cli/install

[author]: https://github.com/remcohaszing

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[esmsh]: https://esm.sh

[typescript]: https://www.typescriptlang.org

[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved

[unist]: https://github.com/syntax-tree/unist