# tree-sitter-lsp
[](https://tree-sitter-lsp.readthedocs.io)
[](https://results.pre-commit.ci/latest/github/neomutt/tree-sitter-lsp/main)
[](https://github.com/neomutt/tree-sitter-lsp/actions)
[](https://codecov.io/gh/neomutt/tree-sitter-lsp)
[](https://deepsource.io/gh/neomutt/tree-sitter-lsp)
[](https://github.com/neomutt/tree-sitter-lsp/releases)
[](https://github.com/neomutt/tree-sitter-lsp/releases/latest)
[](https://github.com/neomutt/tree-sitter-lsp/issues)
[](https://github.com/neomutt/tree-sitter-lsp/issues?q=is%3Aissue+is%3Aclosed)
[](https://github.com/neomutt/tree-sitter-lsp/pulls)
[](https://github.com/neomutt/tree-sitter-lsp/pulls?q=is%3Apr+is%3Aclosed)
[](https://github.com/neomutt/tree-sitter-lsp/discussions)
[](https://github.com/neomutt/tree-sitter-lsp/milestones)
[](https://github.com/neomutt/tree-sitter-lsp/network/members)
[](https://github.com/neomutt/tree-sitter-lsp/stargazers)
[](https://github.com/neomutt/tree-sitter-lsp/watchers)
[](https://github.com/neomutt/tree-sitter-lsp/graphs/contributors)
[](https://github.com/neomutt/tree-sitter-lsp/graphs/commit-activity)
[](https://github.com/neomutt/tree-sitter-lsp/commits)
[](https://github.com/neomutt/tree-sitter-lsp/releases/latest)
[](https://github.com/neomutt/tree-sitter-lsp/blob/main/LICENSE)
[](https://github.com/neomutt/tree-sitter-lsp)
[](https://github.com/neomutt/tree-sitter-lsp)
[](https://github.com/neomutt/tree-sitter-lsp)
[](https://github.com/neomutt/tree-sitter-lsp)
[](https://github.com/neomutt/tree-sitter-lsp)
[](https://github.com/neomutt/tree-sitter-lsp)
[](https://pypi.org/project/tree-sitter-lsp/#description)
[](https://pypi.org/project/tree-sitter-lsp/#history)
[](https://pypi.org/project/tree-sitter-lsp/#files)
[](https://pypi.org/project/tree-sitter-lsp/#files)
[](https://pypi.org/project/tree-sitter-lsp/#files)
[](https://pypi.org/project/tree-sitter-lsp/#files)
A core library to support language servers.
I write many language servers and they share some same code so I extract the
shared code to this library.
I've had enough of writing many DSLs in my editor without any LSP support
(completion, hover, ...). So I decide to sacrifice my time to do this work.
## Language servers
- [termux-language-server](https://github.com/termux/termux-language-server/):
for some specific bash scripts:
- [`build.sh`](https://github.com/termux/termux-packages/wiki/Creating-new-package)
- [`PKGBUILD`](https://wiki.archlinux.org/title/PKGBUILD)
- [`*.ebuild`](https://dev.gentoo.org/~zmedico/portage/doc/man/ebuild.5.html)
- ...
- [mutt-language-server](https://github.com/neomutt/mutt-language-server):
for [(neo)mutt](https://github.com/neomutt/neomutt)'s (neo)muttrc
- [More](https://github.com/Freed-Wu?tab=repositories&q=lsp-server)
## What does this library provide
### Schema
A `Trie` to convert a file to a json, then you can use json schema to validate
it to get diagnostics.
Take
[termux-language-server](https://github.com/termux/termux-language-server/) as
an example.
`PKGBUILD`:
```sh
pkgname=hello
pkgver=0.0.1
pkgrel=1
pkgdesc="hello"
arch=(wrong_arch)
license=(GPL3)
build() {
cat <<EOF > hello
#!/usr/bin/env sh
echo hello
EOF
}
package() {
install -D hello -t $pkgdir/usr/bin
}
```
```sh
termux-language-server --convert PKGBUILD
```
```json
{
"pkgname": "hello",
"pkgver": "0.0.1",
"pkgrel": "1",
"pkgdesc": "hello",
"arch": [
"wrong_arch"
],
"license": [
"GPL3"
],
"build": 0,
"package": 0
}
```
So, we can validate the json by [a json schema](https://github.com/termux/termux-language-server/tree/main/src/termux_language_server/assets/json):
<!-- markdownlint-disable MD013 -->
```sh
$ termux-language-server --check PKGBUILD
PKGBUILD:5:7-5:17:error: 'wrong_arch' is not one of ['any', 'pentium4', 'i486', 'i686', 'x86_64', 'x86_64_v3', 'arm', 'armv6h', 'armv7h', 'armv8', 'aarch64']
```
<!-- markdownlint-enable MD013 -->

Sometimes it will be more complicated:
`neomuttrc`:
```neomuttrc
set allow_ansi=yes sleep_time = no ispell = aspell
set query_command = 'mutt_ldap_query.pl %s'
```
```sh
mutt-language-server --convert neomuttrc
```
```json
{
"set": {
"allow_ansi": "yes",
"sleep_time": "no",
"ispell": "aspell",
"query_command": "mutt_ldap_query.pl %s"
}
}
```
```sh
$ mutt-language-server --check neomuttrc
neomuttrc:1:33-1:35:error: 'no' is not of type 'number'
```

We put the result to the json's `.set` not `.` just in order to reserve the
other keys for other usages.
### Finders
Some finders to find the required node in tree-sitter's AST.
Such as, if you want to get the node under the cursor:
```python
@self.feature(TEXT_DOCUMENT_COMPLETION)
def completions(params: CompletionParams) -> CompletionList:
document = self.workspace.get_document(params.text_document.uri)
uni = PositionFinder(params.position, right_equal=True).find(
document.uri, self.trees[document.uri]
)
# ...
```
UNI (Universal Node Identifier) is URI + node.
### Utilities
This library also provides many utility functions. Such as converting man page to
markdown and tokenizing it in order to generate the json schema.
```sh
mutt-language-server --generate-schema neomuttrc
```
<!-- markdownlint-disable MD013 -->
````json
{
"$id": "https://github.com/neomutt/mutt-language-server/blob/main/src/termux_language_server/assets/json/neomuttrc.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"$comment": "Don't edit this file directly! It is generated by `mutt-language-server --generate-schema=neomuttrc`.",
"type": "object",
"properties": {
"account-hook": {
"description": "```neomuttrc\naccount-hook regex command\n```\nThis hook is executed whenever you access a remote mailbox. Useful to adjust configuration settings to different IMAP or POP servers."
},
"$comment": "..."
}
}
````
<!-- markdownlint-enable MD013 -->

## References
- some [Chinese blogs](https://freed-wu.github.io/tag/lsp/) about how I write
these language servers
- [tree-sitter](https://tree-sitter.github.io/tree-sitter/)
- [language server protocol](https://microsoft.github.io/language-server-protocol/specifications/specification-current)
- [json schema](https://json-schema.org/specification)
Raw data
{
"_id": null,
"home_page": "",
"name": "tree-sitter-lsp",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "language server,tree sitter,json schema",
"author": "",
"author_email": "Wu Zhenyu <wuzhenyu@ustc.edu>",
"download_url": "https://files.pythonhosted.org/packages/7c/6b/a1a2ff7d06936ac3103b59fd9b07db65fc9b448d6808ccc12e4c86b6372e/tree-sitter-lsp-0.0.14.tar.gz",
"platform": null,
"description": "# tree-sitter-lsp\n\n[](https://tree-sitter-lsp.readthedocs.io)\n[](https://results.pre-commit.ci/latest/github/neomutt/tree-sitter-lsp/main)\n[](https://github.com/neomutt/tree-sitter-lsp/actions)\n[](https://codecov.io/gh/neomutt/tree-sitter-lsp)\n[](https://deepsource.io/gh/neomutt/tree-sitter-lsp)\n\n[](https://github.com/neomutt/tree-sitter-lsp/releases)\n[](https://github.com/neomutt/tree-sitter-lsp/releases/latest)\n[](https://github.com/neomutt/tree-sitter-lsp/issues)\n[](https://github.com/neomutt/tree-sitter-lsp/issues?q=is%3Aissue+is%3Aclosed)\n[](https://github.com/neomutt/tree-sitter-lsp/pulls)\n[](https://github.com/neomutt/tree-sitter-lsp/pulls?q=is%3Apr+is%3Aclosed)\n[](https://github.com/neomutt/tree-sitter-lsp/discussions)\n[](https://github.com/neomutt/tree-sitter-lsp/milestones)\n[](https://github.com/neomutt/tree-sitter-lsp/network/members)\n[](https://github.com/neomutt/tree-sitter-lsp/stargazers)\n[](https://github.com/neomutt/tree-sitter-lsp/watchers)\n[](https://github.com/neomutt/tree-sitter-lsp/graphs/contributors)\n[](https://github.com/neomutt/tree-sitter-lsp/graphs/commit-activity)\n[](https://github.com/neomutt/tree-sitter-lsp/commits)\n[](https://github.com/neomutt/tree-sitter-lsp/releases/latest)\n\n[](https://github.com/neomutt/tree-sitter-lsp/blob/main/LICENSE)\n[](https://github.com/neomutt/tree-sitter-lsp)\n[](https://github.com/neomutt/tree-sitter-lsp)\n[](https://github.com/neomutt/tree-sitter-lsp)\n[](https://github.com/neomutt/tree-sitter-lsp)\n[](https://github.com/neomutt/tree-sitter-lsp)\n[](https://github.com/neomutt/tree-sitter-lsp)\n\n[](https://pypi.org/project/tree-sitter-lsp/#description)\n[](https://pypi.org/project/tree-sitter-lsp/#history)\n[](https://pypi.org/project/tree-sitter-lsp/#files)\n[](https://pypi.org/project/tree-sitter-lsp/#files)\n[](https://pypi.org/project/tree-sitter-lsp/#files)\n[](https://pypi.org/project/tree-sitter-lsp/#files)\n\nA core library to support language servers.\n\nI write many language servers and they share some same code so I extract the\nshared code to this library.\n\nI've had enough of writing many DSLs in my editor without any LSP support\n(completion, hover, ...). So I decide to sacrifice my time to do this work.\n\n## Language servers\n\n- [termux-language-server](https://github.com/termux/termux-language-server/):\n for some specific bash scripts:\n - [`build.sh`](https://github.com/termux/termux-packages/wiki/Creating-new-package)\n - [`PKGBUILD`](https://wiki.archlinux.org/title/PKGBUILD)\n - [`*.ebuild`](https://dev.gentoo.org/~zmedico/portage/doc/man/ebuild.5.html)\n - ...\n- [mutt-language-server](https://github.com/neomutt/mutt-language-server):\n for [(neo)mutt](https://github.com/neomutt/neomutt)'s (neo)muttrc\n- [More](https://github.com/Freed-Wu?tab=repositories&q=lsp-server)\n\n## What does this library provide\n\n### Schema\n\nA `Trie` to convert a file to a json, then you can use json schema to validate\nit to get diagnostics.\n\nTake\n[termux-language-server](https://github.com/termux/termux-language-server/) as\nan example.\n\n`PKGBUILD`:\n\n```sh\npkgname=hello\npkgver=0.0.1\npkgrel=1\npkgdesc=\"hello\"\narch=(wrong_arch)\nlicense=(GPL3)\n\nbuild() {\n cat <<EOF > hello\n#!/usr/bin/env sh\necho hello\nEOF\n}\n\npackage() {\n install -D hello -t $pkgdir/usr/bin\n}\n```\n\n```sh\ntermux-language-server --convert PKGBUILD\n```\n\n```json\n{\n \"pkgname\": \"hello\",\n \"pkgver\": \"0.0.1\",\n \"pkgrel\": \"1\",\n \"pkgdesc\": \"hello\",\n \"arch\": [\n \"wrong_arch\"\n ],\n \"license\": [\n \"GPL3\"\n ],\n \"build\": 0,\n \"package\": 0\n}\n```\n\nSo, we can validate the json by [a json schema](https://github.com/termux/termux-language-server/tree/main/src/termux_language_server/assets/json):\n\n<!-- markdownlint-disable MD013 -->\n\n```sh\n$ termux-language-server --check PKGBUILD\nPKGBUILD:5:7-5:17:error: 'wrong_arch' is not one of ['any', 'pentium4', 'i486', 'i686', 'x86_64', 'x86_64_v3', 'arm', 'armv6h', 'armv7h', 'armv8', 'aarch64']\n```\n\n<!-- markdownlint-enable MD013 -->\n\n\n\nSometimes it will be more complicated:\n\n`neomuttrc`:\n\n```neomuttrc\nset allow_ansi=yes sleep_time = no ispell = aspell\nset query_command = 'mutt_ldap_query.pl %s'\n```\n\n```sh\nmutt-language-server --convert neomuttrc\n```\n\n```json\n{\n \"set\": {\n \"allow_ansi\": \"yes\",\n \"sleep_time\": \"no\",\n \"ispell\": \"aspell\",\n \"query_command\": \"mutt_ldap_query.pl %s\"\n }\n}\n```\n\n```sh\n$ mutt-language-server --check neomuttrc\nneomuttrc:1:33-1:35:error: 'no' is not of type 'number'\n```\n\n\n\nWe put the result to the json's `.set` not `.` just in order to reserve the\nother keys for other usages.\n\n### Finders\n\nSome finders to find the required node in tree-sitter's AST.\nSuch as, if you want to get the node under the cursor:\n\n```python\n@self.feature(TEXT_DOCUMENT_COMPLETION)\ndef completions(params: CompletionParams) -> CompletionList:\n document = self.workspace.get_document(params.text_document.uri)\n uni = PositionFinder(params.position, right_equal=True).find(\n document.uri, self.trees[document.uri]\n )\n # ...\n```\n\nUNI (Universal Node Identifier) is URI + node.\n\n### Utilities\n\nThis library also provides many utility functions. Such as converting man page to\nmarkdown and tokenizing it in order to generate the json schema.\n\n```sh\nmutt-language-server --generate-schema neomuttrc\n```\n\n<!-- markdownlint-disable MD013 -->\n\n````json\n{\n \"$id\": \"https://github.com/neomutt/mutt-language-server/blob/main/src/termux_language_server/assets/json/neomuttrc.json\",\n \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n \"$comment\": \"Don't edit this file directly! It is generated by `mutt-language-server --generate-schema=neomuttrc`.\",\n \"type\": \"object\",\n \"properties\": {\n \"account-hook\": {\n \"description\": \"```neomuttrc\\naccount-hook regex command\\n```\\nThis hook is executed whenever you access a remote mailbox. Useful to adjust configuration settings to different IMAP or POP servers.\"\n },\n \"$comment\": \"...\"\n }\n}\n````\n\n<!-- markdownlint-enable MD013 -->\n\n\n\n## References\n\n- some [Chinese blogs](https://freed-wu.github.io/tag/lsp/) about how I write\n these language servers\n- [tree-sitter](https://tree-sitter.github.io/tree-sitter/)\n- [language server protocol](https://microsoft.github.io/language-server-protocol/specifications/specification-current)\n- [json schema](https://json-schema.org/specification)\n",
"bugtrack_url": null,
"license": "GPL v3",
"summary": "a library to create language servers",
"version": "0.0.14",
"project_urls": {
"Bug Report": "https://github.com/neomutt/tree-sitter-lsp/issues",
"Download": "https://github.com/neomutt/tree-sitter-lsp/releases",
"Homepage": "https://tree-sitter-lsp.readthedocs.io",
"Source": "https://github.com/neomutt/tree-sitter-lsp"
},
"split_keywords": [
"language server",
"tree sitter",
"json schema"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "69010a0656e441bf94c4352bec9b9ed946eaf1759262ab6f1a20a516c2def84c",
"md5": "b0a91d9b0192f2d0c9f94f1b6ba66ac8",
"sha256": "9a5d67a965bc0f5b323c487d2e0f57db262120da34e096282d0fb05bf060a021"
},
"downloads": -1,
"filename": "tree_sitter_lsp-0.0.14-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b0a91d9b0192f2d0c9f94f1b6ba66ac8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 30543,
"upload_time": "2024-02-19T12:52:59",
"upload_time_iso_8601": "2024-02-19T12:52:59.926472Z",
"url": "https://files.pythonhosted.org/packages/69/01/0a0656e441bf94c4352bec9b9ed946eaf1759262ab6f1a20a516c2def84c/tree_sitter_lsp-0.0.14-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7c6ba1a2ff7d06936ac3103b59fd9b07db65fc9b448d6808ccc12e4c86b6372e",
"md5": "7b68d467079cd5629bb47471844dcbab",
"sha256": "e7c96b65de35480f6fd997afa856bb035d9565e95028f8e13767ddf6e78e0517"
},
"downloads": -1,
"filename": "tree-sitter-lsp-0.0.14.tar.gz",
"has_sig": false,
"md5_digest": "7b68d467079cd5629bb47471844dcbab",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 37413,
"upload_time": "2024-02-19T12:53:02",
"upload_time_iso_8601": "2024-02-19T12:53:02.128474Z",
"url": "https://files.pythonhosted.org/packages/7c/6b/a1a2ff7d06936ac3103b59fd9b07db65fc9b448d6808ccc12e4c86b6372e/tree-sitter-lsp-0.0.14.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-19 12:53:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "neomutt",
"github_project": "tree-sitter-lsp",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "tree-sitter-lsp"
}