Skip to content

Commit

Permalink
Add support for legacy python.linting.cwd setting (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig authored Jan 19, 2023
1 parent 5cf75d5 commit 62fd1d3
Show file tree
Hide file tree
Showing 14 changed files with 531 additions and 469 deletions.
2 changes: 1 addition & 1 deletion bundled/tool/_debug_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def update_sys_path(path_to_add: str) -> None:
# line and set breakpoints as appropriate.
debugpy.breakpoint()

SERVER_PATH = os.fspath(pathlib.Path(__file__).parent / "server.py")
SERVER_PATH = os.fspath(pathlib.Path(__file__).parent / "lsp_server.py")
# NOTE: Set breakpoint in `server.py` before continuing.
runpy.run_path(SERVER_PATH, run_name="__main__")
4 changes: 2 additions & 2 deletions bundled/tool/jsonrpc.py → bundled/tool/lsp_jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import BinaryIO, Dict, Optional, Sequence, Union

CONTENT_LENGTH = "Content-Length: "
RUNNER_SCRIPT = str(pathlib.Path(__file__).parent / "runner.py")
RUNNER_SCRIPT = str(pathlib.Path(__file__).parent / "lsp_runner.py")


def to_str(text) -> str:
Expand Down Expand Up @@ -132,7 +132,6 @@ def __init__(self):
self._lock = threading.Lock()
self._thread_pool = ThreadPoolExecutor(10)

@atexit.register
def stop_all_processes(self):
"""Send exit command to all processes and shutdown transport."""
for i in self._rpc.values():
Expand Down Expand Up @@ -174,6 +173,7 @@ def get_json_rpc(self, workspace: str) -> JsonRpc:


_process_manager = ProcessManager()
atexit.register(_process_manager.stop_all_processes)


def _get_json_rpc(workspace: str) -> Union[JsonRpc, None]:
Expand Down
4 changes: 2 additions & 2 deletions bundled/tool/runner.py → bundled/tool/lsp_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def update_sys_path(path_to_add: str, strategy: str) -> None:
)


import jsonrpc
import utils
import lsp_jsonrpc as jsonrpc
import lsp_utils as utils

RPC = jsonrpc.create_json_rpc(sys.stdin.buffer, sys.stdout.buffer)

Expand Down
30 changes: 17 additions & 13 deletions bundled/tool/server.py → bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ def update_sys_path(path_to_add: str, strategy: str) -> None:
# **********************************************************
# Imports needed for the language server goes below this.
# **********************************************************
import jsonrpc
import utils
from pygls import lsp, protocol, server, uris, workspace
import lsp_jsonrpc as jsonrpc
import lsp_utils as utils
from lsprotocol import types as lsp
from pygls import protocol, server, uris, workspace

WORKSPACE_SETTINGS = {}
RUNNER = pathlib.Path(__file__).parent / "runner.py"
RUNNER = pathlib.Path(__file__).parent / "lsp_runner.py"

MAX_WORKERS = 5
LSP_SERVER = server.LanguageServer(max_workers=MAX_WORKERS)
LSP_SERVER = server.LanguageServer(
name="flake8-server", version="v0.1.0", max_workers=MAX_WORKERS
)


# **********************************************************
Expand Down Expand Up @@ -216,7 +219,7 @@ def solutions(


@LSP_SERVER.feature(
lsp.CODE_ACTION,
lsp.TEXT_DOCUMENT_CODE_ACTION,
lsp.CodeActionOptions(code_action_kinds=[lsp.CodeActionKind.QuickFix]),
)
def code_action(params: lsp.CodeActionParams) -> List[lsp.CodeAction]:
Expand Down Expand Up @@ -270,9 +273,9 @@ def _create_workspace_edits(
return lsp.WorkspaceEdit(
document_changes=[
lsp.TextDocumentEdit(
text_document=lsp.VersionedTextDocumentIdentifier(
text_document=lsp.OptionalVersionedTextDocumentIdentifier(
uri=document.uri,
version=0 if document.version is None else document.version,
version=document.version,
),
edits=results,
)
Expand Down Expand Up @@ -303,13 +306,13 @@ def initialize(params: lsp.InitializeParams) -> None:

if isinstance(LSP_SERVER.lsp, protocol.LanguageServerProtocol):
if any(setting["logLevel"] == "debug" for setting in settings):
LSP_SERVER.lsp.trace = lsp.Trace.Verbose
LSP_SERVER.lsp.trace = lsp.TraceValues.Verbose
elif any(
setting["logLevel"] in ["error", "warn", "info"] for setting in settings
):
LSP_SERVER.lsp.trace = lsp.Trace.Messages
LSP_SERVER.lsp.trace = lsp.TraceValues.Messages
else:
LSP_SERVER.lsp.trace = lsp.Trace.Off
LSP_SERVER.lsp.trace = lsp.TraceValues.Off
_log_version_info()


Expand Down Expand Up @@ -365,6 +368,7 @@ def _update_workspace_settings(settings):
if not settings:
key = os.getcwd()
WORKSPACE_SETTINGS[key] = {
"cwd": key,
"workspaceFS": key,
"workspace": uris.from_fs_path(key),
"logLevel": "error",
Expand Down Expand Up @@ -432,7 +436,7 @@ def _run_tool_on_document(
settings = copy.deepcopy(_get_settings_by_document(document))

code_workspace = settings["workspaceFS"]
cwd = settings["workspaceFS"]
cwd = settings["cwd"]

use_path = False
use_rpc = False
Expand Down Expand Up @@ -514,7 +518,7 @@ def _run_tool_on_document(
def _run_tool(extra_args: Sequence[str], settings: Dict[str, Any]) -> utils.RunResult:
"""Runs tool."""
code_workspace = settings["workspaceFS"]
cwd = settings["workspaceFS"]
cwd = settings["cwd"]

use_path = False
use_rpc = False
Expand Down
File renamed without changes.
Loading

0 comments on commit 62fd1d3

Please sign in to comment.