From c837a86b33d5e7d0af7704c89ab10b9b94b0f813 Mon Sep 17 00:00:00 2001 From: dece Date: Thu, 1 Dec 2022 16:35:07 +0100 Subject: [PATCH] init --- .gitignore | 2 + italianswirls/server.py | 82 +++++++++++++++++++++++++++++++++++++++++ requirements-dev.txt | 3 ++ requirements.txt | 2 + 4 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 italianswirls/server.py create mode 100644 requirements-dev.txt create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..372c13e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ + diff --git a/italianswirls/server.py b/italianswirls/server.py new file mode 100644 index 0000000..453ac2d --- /dev/null +++ b/italianswirls/server.py @@ -0,0 +1,82 @@ +import logging + +from jedi import Script +from pygls.lsp.methods import COMPLETION, INITIALIZE +from pygls.lsp.types import (CompletionItem, CompletionItemKind, + CompletionList, CompletionOptions, + CompletionParams, InsertTextFormat, Position) +from pygls.server import LanguageServer +from pygls.workspace import Document + +LOG = logging.getLogger() +LOG.setLevel(logging.DEBUG) +LOG.addHandler(logging.FileHandler("/tmp/a.log")) + +LS = LanguageServer('italianswirls', 'v0.0.1') + +JEDI_COMPLETION_TYPE_MAP = { + "module": CompletionItemKind.Module, + "class": CompletionItemKind.Class, + "instance": CompletionItemKind.Variable, + "function": CompletionItemKind.Function, + "param": CompletionItemKind.Variable, + "path": CompletionItemKind.File, + "keyword": CompletionItemKind.Keyword, + "property": CompletionItemKind.Property, + "statement": CompletionItemKind.Variable, +} + + +def get_jedi_script(document: Document) -> Script: + """Get Jedi Script object from this document and project.""" + return Script(code=document.source, path=document.path) + + +def get_jedi_position(position: Position) -> tuple[int, int]: + """Translate pygls's Position to Jedi position (line is 1-based).""" + return position.line + 1, position.character + + +def get_pygls_compl_kind(jedi_compl_type: str) -> CompletionItemKind: + return JEDI_COMPLETION_TYPE_MAP.get( + jedi_compl_type, + CompletionItemKind.Text + ) + + +@LS.feature(INITIALIZE) +async def do_initialize(*args): + LOG.debug("do_initialize 👋") + + +@LS.feature(COMPLETION, CompletionOptions(trigger_characters=["."])) +async def do_completion(server: LanguageServer, params: CompletionParams): + """Return completion items.""" + document_uri = params.text_document.uri + document = server.workspace.get_document(document_uri) + script = get_jedi_script(document) + jedi_position = get_jedi_position(params.position) + jedi_completions = script.complete(*jedi_position) + LOG.debug(f"Completions: {jedi_completions}") + + completion_items = [] + for jedi_completion in jedi_completions: + name = jedi_completion.name + item = CompletionItem( + label=name, + filter_text=name, + kind=get_pygls_compl_kind(jedi_completion.type), + sort_text=name, + insert_text_name=name, + insert_text_format=InsertTextFormat.PlainText, + ) + completion_items.append(item) + + return CompletionList( + is_incomplete=False, + items=completion_items, + ) + + +if __name__ == "__main__": + LS.start_io() diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..9937b8f --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,3 @@ +-r requirements.txt +flake8 +mypy diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..01bc5fc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pygls +jedi ~= 0.18.2