Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade PYGLS #6

Open
wants to merge 6 commits into
base: upgrade_js
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,939 changes: 983 additions & 956 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ include = ["salt_lsp/data/states.pickle"]

[tool.poetry.dependencies]
# remove the compat code from salt_lsp/workspace.py once we drop python 3.8 support
python = "^3.8"
pygls = "^0.11.3"
python = "^3.8, <3.12"
pygls = "^1.2"
PyYAML = "^6"

[tool.poetry.dev-dependencies]
pytest = ">=6.2.3"
pytest-cov = ">=2.11.1"
black = ">=21.10b0"
mypy = ">=0.812"
mypy = ">=1.4.1"
pylint = ">=2.7.4"
pyfakefs = ">=4.4.0"
salt = "^3005"
pytest-testinfra = "^6.7.0"
pytest-testinfra = ">=6.7.0"
types-PyYAML = "^6"

[tool.poetry.scripts]
Expand All @@ -38,3 +38,6 @@ xfail_strict = true

[tool.black]
line-length = 79

[tool.isort]
profile = "black"
8 changes: 8 additions & 0 deletions salt_lsp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from importlib import metadata

try:
__version__ = metadata.version(__package__)
except:
__version__ = "dev"

del metadata # avoid polluting the main namespace
20 changes: 10 additions & 10 deletions salt_lsp/document_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
Implementation of the DocumentSymbolsRequest, see:
https://microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol
"""
from dataclasses import dataclass, field

import itertools
from dataclasses import dataclass, field
from typing import Callable, Dict, List, Sequence, TypedDict, cast

from pygls.lsp import types
from lsprotocol import types

from salt_lsp.base_types import CompletionsDict
from salt_lsp.parser import (
AstNode,
Tree,
Optional,
ExtendNode,
IncludeNode,
IncludesNode,
StateParameterNode,
StateCallNode,
StateNode,
Optional,
RequisiteNode,
RequisitesNode,
ExtendNode,
StateCallNode,
StateNode,
StateParameterNode,
Tree,
)
from salt_lsp.base_types import CompletionsDict
from salt_lsp.utils import ast_node_to_range


Expand Down Expand Up @@ -197,7 +198,6 @@ def __call__(self, node: AstNode) -> bool:
def tree_to_document_symbols(
tree: Tree, state_completions: CompletionsDict
) -> List[types.DocumentSymbol]:

res = []

for elem in itertools.chain.from_iterable(
Expand Down
3 changes: 2 additions & 1 deletion salt_lsp/parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Module defining and building an AST from the SLS file.
"""

from __future__ import annotations

import logging
Expand All @@ -9,7 +10,7 @@
from os.path import abspath, dirname, exists, isdir, join
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union, cast

from pygls.lsp import types
from lsprotocol import types
import yaml
from yaml.tokens import BlockEndToken, ScalarToken

Expand Down
35 changes: 19 additions & 16 deletions salt_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
from os.path import basename
from typing import Dict, List, Optional, Sequence, Tuple, Union, cast

from pygls.lsp import types
from pygls.lsp.methods import (
COMPLETION,
INITIALIZE,
TEXT_DOCUMENT_DID_OPEN,
DEFINITION,
DOCUMENT_SYMBOL,
)
from pygls.lsp.types import (
from lsprotocol import types
from lsprotocol.types import (
CompletionItem,
CompletionList,
CompletionOptions,
CompletionParams,
InitializeParams,
INITIALIZE,
TEXT_DOCUMENT_COMPLETION,
TEXT_DOCUMENT_DID_OPEN,
TEXT_DOCUMENT_DEFINITION,
TEXT_DOCUMENT_DOCUMENT_SYMBOL,
)
from pygls.server import LanguageServer

from salt_lsp import __version__
from salt_lsp import utils
from salt_lsp.base_types import StateNameCompletion, SLS_LANGUAGE_ID
from salt_lsp.workspace import SaltLspProto, SlsFileWorkspace
Expand All @@ -41,7 +40,9 @@ class SaltServer(LanguageServer):
LINE_START_REGEX = re.compile(r"^(\s*)\b", re.MULTILINE)

def __init__(self) -> None:
super().__init__(protocol_cls=SaltLspProto)
super().__init__(
name="SaltStack", version=__version__, protocol_cls=SaltLspProto
)

self._state_name_completions: Dict[str, StateNameCompletion] = {}

Expand Down Expand Up @@ -77,7 +78,7 @@ def complete_state_name(
and params.context.trigger_character == "."
)

doc = self.workspace.get_document(params.text_document.uri)
doc = self.workspace.get_text_document(params.text_document.uri)
contents = doc.source
ind = doc.offset_at_position(params.position)
last_match = utils.get_last_element_of_iterator(
Expand Down Expand Up @@ -168,7 +169,8 @@ def initialize(params: InitializeParams) -> None:
server.logger.debug("Replaced workspace with SlsFileWorkspace")

@server.feature(
COMPLETION, CompletionOptions(trigger_characters=["-", "."])
TEXT_DOCUMENT_COMPLETION,
CompletionOptions(trigger_characters=["-", "."]),
)
def completions(
salt_server: SaltServer, params: CompletionParams
Expand Down Expand Up @@ -210,7 +212,7 @@ def completions(
)
return None

@server.feature(DEFINITION)
@server.feature(TEXT_DOCUMENT_DEFINITION)
def goto_definition(
salt_server: SaltServer, params: types.DeclarationParams
) -> Optional[types.Location]:
Expand Down Expand Up @@ -240,15 +242,16 @@ def did_open(
"adding text document '%s' to the workspace",
params.text_document.uri,
)
doc = salt_server.workspace.get_document(params.text_document.uri)
doc = salt_server.workspace.get_text_document(params.text_document.uri)
salt_server.workspace.put_text_document(params.text_document)
return types.TextDocumentItem(
uri=params.text_document.uri,
language_id=SLS_LANGUAGE_ID,
text=params.text_document.text or "",
version=doc.version,
version=doc.version or 0,
)

@server.feature(DOCUMENT_SYMBOL)
@server.feature(TEXT_DOCUMENT_DOCUMENT_SYMBOL)
def document_symbol(
salt_server: SaltServer, params: types.DocumentSymbolParams
) -> Optional[
Expand Down
2 changes: 1 addition & 1 deletion salt_lsp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from urllib.parse import urlparse, ParseResult

from pygls.lsp.types import Position, Range
from lsprotocol.types import Position, Range

from salt_lsp import parser
from salt_lsp.parser import AstNode, Tree
Expand Down
33 changes: 20 additions & 13 deletions salt_lsp/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
contents utilizing the existing Workspace implementation from pygls.

"""
from logging import getLogger, Logger, DEBUG
from pathlib import Path

import sys
from logging import DEBUG, Logger, getLogger
from pathlib import Path
from typing import List, Optional, Union

from pygls.lsp import types
from lsprotocol import types
from pygls.protocol import LanguageServerProtocol
from pygls.workspace import Workspace

from salt_lsp.base_types import CompletionsDict, SLS_LANGUAGE_ID
from salt_lsp.utils import UriDict, FileUri, get_top
from salt_lsp.parser import parse, Tree
from salt_lsp.base_types import SLS_LANGUAGE_ID, CompletionsDict
from salt_lsp.document_symbols import tree_to_document_symbols

from salt_lsp.parser import Tree, parse
from salt_lsp.utils import FileUri, UriDict, get_top

if sys.version_info[1] <= 8:

Expand Down Expand Up @@ -119,7 +119,7 @@ def _resolve_includes(
text_document_uri,
)
with open(inc.path, "r") as inc_file:
self.put_document(
self.put_text_document(
types.TextDocumentItem(
uri=str(inc),
language_id=SLS_LANGUAGE_ID,
Expand All @@ -143,7 +143,7 @@ def _update_document(
) -> None:
self.logger.debug("updating document '%s'", text_document.uri)
uri = text_document.uri
tree = parse(self.get_document(uri).source)
tree = parse(self.get_text_document(uri).source)
self._trees[uri] = tree

self._document_symbols[uri] = tree_to_document_symbols(
Expand All @@ -154,11 +154,10 @@ def _update_document(

def _get_workspace_of_document(self, uri: Union[str, FileUri]) -> FileUri:
for workspace_uri in self._folders:

if is_relative_to(
Path(FileUri(uri).path), Path(FileUri(workspace_uri).path)
):
return workspace_uri
return FileUri(workspace_uri)

return self.root_uri

Expand Down Expand Up @@ -190,6 +189,14 @@ def put_document(self, text_document: types.TextDocumentItem) -> None:
super().put_document(text_document)
self._update_document(text_document)

def put_text_document(
self,
text_document: types.TextDocumentItem,
notebook_uri: Optional[str] = None,
):
super().put_text_document(text_document)
self._update_document(text_document)


class SaltLspProto(LanguageServerProtocol):
"""Custom protocol that replaces the workspace with a SlsFileWorkspace
Expand All @@ -206,9 +213,9 @@ def setup_custom_workspace(self):
"""
if not isinstance(self.workspace, SlsFileWorkspace):
old_ws = self.workspace
self.workspace = SlsFileWorkspace(
self._workspace = SlsFileWorkspace(
self._server._state_name_completions,
old_ws.root_uri,
self._server.sync_kind,
self._server.text_document_sync_kind,
old_ws.folders.values(),
)
22 changes: 13 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import asyncio
from threading import Thread
import os
import time

from pygls.lsp.methods import (
EXIT,
INITIALIZE,
from lsprotocol.types import (
TEXT_DOCUMENT_DID_OPEN,
SHUTDOWN,
)
from pygls.lsp.types import (
EXIT,
INITIALIZE,
ClientCapabilities,
InitializeParams,
DidOpenTextDocumentParams,
Expand Down Expand Up @@ -497,7 +496,9 @@ def salt_client_server():
args=(os.fdopen(csr, "rb"), os.fdopen(scw, "wb")),
)
server_thread.daemon = True
client = LanguageServer(asyncio.new_event_loop())
client = LanguageServer(
name="salt_lps_test", version="v0.0.1", loop=asyncio.new_event_loop()
)
client_thread = Thread(
target=client.start_io,
args=(os.fdopen(scr, "rb"), os.fdopen(csw, "wb")),
Expand All @@ -518,7 +519,7 @@ def salt_client_server():
),
).result(timeout=CALL_TIMEOUT)

assert "capabilities" in response
assert getattr(response, "capabilities")

yield client, server

Expand Down Expand Up @@ -641,7 +642,7 @@ def open_file(
client: LanguageServer, file_path: str, request_timeout: int = 5
) -> None:
with open(file_path) as base_sls:
client.lsp.send_request(
client.lsp.notify(
TEXT_DOCUMENT_DID_OPEN,
DidOpenTextDocumentParams(
text_document=TextDocumentItem(
Expand All @@ -651,4 +652,7 @@ def open_file(
text=base_sls.read(-1),
)
),
).result(timeout=request_timeout)
)
# Client is supposed to wait for the notification that the file has been
# properly added but for the need of the test we take some shortcut
time.sleep(1)
8 changes: 5 additions & 3 deletions tests/test_completions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from types import SimpleNamespace

from lsprotocol.types import TextDocumentItem

from conftest import MODULE_DOCS


Expand All @@ -22,11 +24,11 @@
def test_complete_of_file(salt_client_server, file_name_completer):
_, server = salt_client_server
txt_doc = {
"text_document": SimpleNamespace(
uri="foo.sls", text=TEST_FILE, version=0
"text_document": TextDocumentItem(
uri="foo.sls", text=TEST_FILE, version=0, language_id="sls"
),
}
server.workspace.put_document(txt_doc["text_document"])
server.workspace.put_text_document(txt_doc["text_document"])

completions = server.complete_state_name(
SimpleNamespace(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_document_symbols.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pygls.lsp import types
from lsprotocol import types

from salt_lsp.document_symbols import tree_to_document_symbols
from salt_lsp.parser import parse
Expand Down
2 changes: 1 addition & 1 deletion tests/test_find_ids.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pygls.lsp.types import (
from lsprotocol.types import (
Location,
Range,
Position,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
from salt_lsp.utils import construct_path_to_position

from pygls.lsp.types import (
from lsprotocol.types import (
Position,
)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

import pytest
from pygls.lsp import types
from lsprotocol import types

from salt_lsp.utils import (
ast_node_to_range,
Expand Down
Loading