-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
282 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Configure for Language Servers | ||
|
||
## (Neo)[Vim](https://www.vim.org) | ||
|
||
### [coc.nvim](https://github.com/neoclide/coc.nvim) | ||
|
||
```json | ||
{ | ||
"languageserver": { | ||
"translate": { | ||
"command": "trans", | ||
"args": [ | ||
"--lsp" | ||
], | ||
"filetypes": [ | ||
"text" | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### [vim-lsp](https://github.com/prabirshrestha/vim-lsp) | ||
|
||
```vim | ||
if executable('trans') | ||
augroup lsp | ||
autocmd! | ||
autocmd User lsp_setup call lsp#register_server({ | ||
\ 'name': 'translate', | ||
\ 'cmd': {server_info->['trans', '--lsp']}, | ||
\ 'whitelist': ['text'], | ||
\ }) | ||
augroup END | ||
endif | ||
``` | ||
|
||
## [Neovim](https://neovim.io) | ||
|
||
```lua | ||
vim.api.nvim_create_autocmd({ "BufEnter" }, { | ||
pattern = { "*.txt" }, | ||
callback = function() | ||
vim.lsp.start({ | ||
name = "translate", | ||
cmd = { "trans" "--lsp" } | ||
}) | ||
end, | ||
}) | ||
``` | ||
|
||
## [Emacs](https://www.gnu.org/software/emacs) | ||
|
||
```elisp | ||
(make-lsp-client :new-connection | ||
(lsp-stdio-connection | ||
`(,(executable-find "trans" "--lsp"))) | ||
:activation-fn (lsp-activate-on "*.txt") | ||
:server-id "translate"))) | ||
``` | ||
|
||
## [Sublime](https://www.sublimetext.com) | ||
|
||
```json | ||
{ | ||
"clients": { | ||
"translate": { | ||
"command": [ | ||
"trans", | ||
"--lsp" | ||
], | ||
"enabled": true, | ||
"selector": "source.text" | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env -S pip install -r | ||
|
||
pygls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
r"""Server | ||
========== | ||
""" | ||
import re | ||
from argparse import Namespace | ||
from typing import Any, Tuple | ||
|
||
from lsprotocol.types import ( | ||
TEXT_DOCUMENT_COMPLETION, | ||
TEXT_DOCUMENT_HOVER, | ||
CompletionItem, | ||
CompletionItemKind, | ||
CompletionList, | ||
CompletionParams, | ||
Hover, | ||
MarkupContent, | ||
MarkupKind, | ||
Position, | ||
Range, | ||
TextDocumentPositionParams, | ||
) | ||
from pygls.server import LanguageServer | ||
|
||
from .. import APPNAME, HISTORY_FILE, __version__ | ||
from . import get_processed_result_text | ||
|
||
|
||
class TranslateShellLanguageServer(LanguageServer): | ||
r"""Translate shell language server.""" | ||
|
||
def __init__(self, translate_args: Namespace, *args: Any) -> None: | ||
"""Init. | ||
:param self: | ||
:param translate_args: | ||
:type translate_args: Namespace | ||
:param args: | ||
:type args: Any | ||
:rtype: None | ||
""" | ||
self.args = translate_args | ||
super().__init__(*args) | ||
|
||
@self.feature(TEXT_DOCUMENT_HOVER) | ||
def hover(params: TextDocumentPositionParams) -> Hover | None: | ||
r"""Hover the translated results. | ||
:param params: | ||
:type params: TextDocumentPositionParams | ||
:rtype: Hover | None | ||
""" | ||
word = self._cursor_word( | ||
params.text_document.uri, params.position, True | ||
) | ||
if not word: | ||
return None | ||
self.args.text = word[0] | ||
# ignore processed text | ||
doc = get_processed_result_text(self.args)[0] | ||
if not doc: | ||
return None | ||
return Hover( | ||
contents=MarkupContent(kind=MarkupKind.PlainText, value=doc), | ||
range=word[1], | ||
) | ||
|
||
@self.feature(TEXT_DOCUMENT_COMPLETION) | ||
def completions(params: CompletionParams) -> CompletionList: | ||
r"""Completion history words. | ||
:param params: | ||
:type params: CompletionParams | ||
:rtype: CompletionList | ||
""" | ||
word = self._cursor_word( | ||
params.text_document.uri, params.position, False | ||
) | ||
token = "" if word is None else word[0] | ||
items = [ | ||
CompletionItem( | ||
label=x, | ||
kind=CompletionItemKind.Constant, | ||
insert_text=x, | ||
) | ||
for x in HISTORY_FILE.read_text().splitlines() | ||
if x.startswith(token) | ||
] | ||
return CompletionList(is_incomplete=False, items=items) | ||
|
||
def _cursor_line(self, uri: str, position: Position) -> str: | ||
r"""Cursor line. | ||
:param uri: | ||
:type uri: str | ||
:param position: | ||
:type position: Position | ||
:rtype: str | ||
""" | ||
doc = self.workspace.get_document(uri) | ||
content = doc.source | ||
line = content.split("\n")[position.line] | ||
return str(line) | ||
|
||
def _cursor_word( | ||
self, uri: str, position: Position, include_all: bool = True | ||
) -> Tuple[str, Range] | None: | ||
r"""Cursor word. | ||
:param uri: | ||
:type uri: str | ||
:param position: | ||
:type position: Position | ||
:param include_all: | ||
:type include_all: bool | ||
:rtype: Tuple[str, Range] | None | ||
""" | ||
line = self._cursor_line(uri, position) | ||
cursor = position.character | ||
for m in re.finditer(r"\w+", line): | ||
end = m.end() if include_all else cursor | ||
if m.start() <= cursor <= m.end(): | ||
word = ( | ||
line[m.start() : end], | ||
Range( | ||
start=Position( | ||
line=position.line, character=m.start() | ||
), | ||
end=Position(line=position.line, character=end), | ||
), | ||
) | ||
return word | ||
return None | ||
|
||
|
||
def run(args: Namespace) -> None: | ||
"""Run. | ||
:param args: | ||
:type args: Namespace | ||
:rtype: None | ||
""" | ||
TranslateShellLanguageServer(args, APPNAME, __version__).start_io() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.