format code with ruff

This commit is contained in:
dece 2024-08-21 07:57:24 +02:00
parent e5e097f4a2
commit 254bb68c95
3 changed files with 53 additions and 28 deletions

View file

@ -11,8 +11,13 @@ DEFAULT_LOG_FILE = "/tmp/italianswirls.log"
def main(): def main():
argparser = argparse.ArgumentParser(description=DESCRIPTION) argparser = argparse.ArgumentParser(description=DESCRIPTION)
argparser.add_argument("-d", "--debug", nargs="?", const=DEFAULT_LOG_FILE, argparser.add_argument(
help=f"debug log (default: {DEFAULT_LOG_FILE})") "-d",
"--debug",
nargs="?",
const=DEFAULT_LOG_FILE,
help=f"debug log (default: {DEFAULT_LOG_FILE})",
)
args = argparser.parse_args() args = argparser.parse_args()
if debug_log_file := args.debug: if debug_log_file := args.debug:

View file

@ -6,10 +6,18 @@ from typing import Generator, Optional, Union
from jedi.api.classes import Name from jedi.api.classes import Name
from jedi.api.refactoring import Refactoring from jedi.api.refactoring import Refactoring
from lsprotocol.types import (CompletionItemKind, Location, Position, Range, from lsprotocol.types import (
RenameFile, RenameFileOptions, CompletionItemKind,
ResourceOperationKind, TextDocumentEdit, Location,
TextEdit, VersionedTextDocumentIdentifier) Position,
Range,
RenameFile,
RenameFileOptions,
ResourceOperationKind,
TextDocumentEdit,
TextEdit,
VersionedTextDocumentIdentifier,
)
from pygls.workspace import Workspace from pygls.workspace import Workspace
JEDI_COMPLETION_TYPE_MAP = { JEDI_COMPLETION_TYPE_MAP = {
@ -67,10 +75,7 @@ def get_lsp_locations(names: list[Name]) -> list[Location]:
def get_lsp_completion_kind(jedi_compl_type: str) -> CompletionItemKind: def get_lsp_completion_kind(jedi_compl_type: str) -> CompletionItemKind:
"""Return an LSP completion item kind from this Jedi completion type.""" """Return an LSP completion item kind from this Jedi completion type."""
return JEDI_COMPLETION_TYPE_MAP.get( return JEDI_COMPLETION_TYPE_MAP.get(jedi_compl_type, CompletionItemKind.Text)
jedi_compl_type,
CompletionItemKind.Text
)
def _build_line_offsets(text: str) -> list[int]: def _build_line_offsets(text: str) -> list[int]:
@ -119,8 +124,8 @@ def _gen_document_text_edits(
new_code = changed_file.get_new_code() new_code = changed_file.get_new_code()
line_offsets = _build_line_offsets(old_code) line_offsets = _build_line_offsets(old_code)
edit_operations = ( edit_operations = (
opcode for opcode in opcode
difflib.SequenceMatcher(a=old_code, b=new_code).get_opcodes() for opcode in difflib.SequenceMatcher(a=old_code, b=new_code).get_opcodes()
if opcode[0] != "equal" if opcode[0] != "equal"
) )
text_edits: list[TextEdit] = [] text_edits: list[TextEdit] = []
@ -153,5 +158,5 @@ def _gen_document_renames(
kind=ResourceOperationKind.Rename, kind=ResourceOperationKind.Rename,
old_uri=old_name.as_uri(), old_uri=old_name.as_uri(),
new_uri=new_name.as_uri(), new_uri=new_name.as_uri(),
options=RenameFileOptions(ignore_if_exists=True, overwrite=True) options=RenameFileOptions(ignore_if_exists=True, overwrite=True),
) )

View file

@ -3,23 +3,39 @@ from typing import Optional
from jedi import Script from jedi import Script
from jedi.api.refactoring import RefactoringError from jedi.api.refactoring import RefactoringError
from lsprotocol.types import (CompletionItem, CompletionList, CompletionOptions, from lsprotocol.types import (
CompletionParams, DefinitionParams, Hover, CompletionItem,
HoverParams, InsertTextFormat, Location, CompletionList,
ReferenceParams, RenameParams, CompletionOptions,
TextDocumentPositionParams, TypeDefinitionParams, CompletionParams,
WorkspaceEdit, DefinitionParams,
TEXT_DOCUMENT_COMPLETION, TEXT_DOCUMENT_DEFINITION, Hover,
TEXT_DOCUMENT_HOVER, TEXT_DOCUMENT_REFERENCES, HoverParams,
TEXT_DOCUMENT_RENAME, InsertTextFormat,
TEXT_DOCUMENT_TYPE_DEFINITION) Location,
ReferenceParams,
RenameParams,
TextDocumentPositionParams,
TypeDefinitionParams,
WorkspaceEdit,
TEXT_DOCUMENT_COMPLETION,
TEXT_DOCUMENT_DEFINITION,
TEXT_DOCUMENT_HOVER,
TEXT_DOCUMENT_REFERENCES,
TEXT_DOCUMENT_RENAME,
TEXT_DOCUMENT_TYPE_DEFINITION,
)
from pygls.server import LanguageServer from pygls.server import LanguageServer
from pygls.workspace import Document from pygls.workspace import Document
from italianswirls.glue import (gen_document_edits, get_jedi_position, from italianswirls.glue import (
get_lsp_completion_kind, get_lsp_locations) gen_document_edits,
get_jedi_position,
get_lsp_completion_kind,
get_lsp_locations,
)
LS = LanguageServer('italianswirls', 'v0.0.1') LS = LanguageServer("italianswirls", "v0.0.1")
def get_jedi_script(document: Document) -> Script: def get_jedi_script(document: Document) -> Script:
@ -28,8 +44,7 @@ def get_jedi_script(document: Document) -> Script:
def get_jedi_script_from_params( def get_jedi_script_from_params(
params: TextDocumentPositionParams, params: TextDocumentPositionParams, server: LanguageServer
server: LanguageServer
) -> Script: ) -> Script:
"""Get Jedi Script using text document params provided by the client.""" """Get Jedi Script using text document params provided by the client."""
document_uri = params.text_document.uri document_uri = params.text_document.uri