Skip to content

Commit

Permalink
Merge pull request #216 from galaxyproject/migrate_pygls_1.0.1
Browse files Browse the repository at this point in the history
Migrate to pygls v1
  • Loading branch information
davelopez committed Feb 25, 2023
2 parents 5e99f03 + 0287a3f commit 34cf7de
Show file tree
Hide file tree
Showing 39 changed files with 202 additions and 143 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/client-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python-version: ['3.8', '3.9', '3.10', '3.11']
runs-on: ${{ matrix.os }}
defaults:
run:
Expand All @@ -22,11 +23,11 @@ jobs:
- name: Set up Python for local environment
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: ${{ matrix.python-version }}
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
- run: npm install
- name: Run integration tests
run: xvfb-run -a npm run test:e2e
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/server-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ on:

jobs:
build:
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
runs-on: ubuntu-latest
defaults:
run:
Expand All @@ -24,7 +27,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
12 changes: 6 additions & 6 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use strict";

import * as net from "net";
import { ExtensionContext, window, IndentAction, LanguageConfiguration, languages, ExtensionMode } from "vscode";
import { ExtensionContext, ExtensionMode, IndentAction, LanguageConfiguration, languages, window } from "vscode";
import { LanguageClient, LanguageClientOptions, ServerOptions } from "vscode-languageclient/node";
import { installLanguageServer } from "./setup";
import { Constants } from "./constants";
import { setupCommands } from "./commands";
import { setupPlanemo } from "./planemo/main";
import { Constants } from "./constants";
import { DefaultConfigurationFactory } from "./planemo/configuration";
import { setupPlanemo } from "./planemo/main";
import { setupProviders } from "./providers/setup";
import { installLanguageServer } from "./setup";

let client: LanguageClient;

Expand Down Expand Up @@ -76,14 +76,17 @@ function getClientOptions(): LanguageClientOptions {
*/
function connectToLanguageServerTCP(port: number): LanguageClient {
const serverOptions: ServerOptions = () => {
return new Promise((resolve, _reject) => {
return new Promise((resolve, reject) => {
const clientSocket = new net.Socket();
clientSocket.connect(port, "127.0.0.1", () => {
resolve({
reader: clientSocket,
writer: clientSocket,
});
});
clientSocket.on("error", (err) => {
reject(err);
});
});
};

Expand Down
7 changes: 5 additions & 2 deletions server/galaxyls/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
def add_arguments(parser: argparse.ArgumentParser) -> None:
parser.description = "Galaxy Language Server"

parser.add_argument("--tcp", action="store_true", help="Use TCP server instead of stdio")
parser.add_argument("--tcp", action="store_true", help="Use TCP server")
parser.add_argument("--ws", action="store_true", help="Use WebSocket server")
parser.add_argument("--host", default="127.0.0.1", help="Bind to this address")
parser.add_argument("--port", type=int, default=2087, help="Bind to this port")


def main() -> None:
def main():
parser = argparse.ArgumentParser()
add_arguments(parser)
args = parser.parse_args()

if args.tcp:
language_server.start_tcp(args.host, args.port)
elif args.ws:
language_server.start_ws(args.host, args.port)
else:
language_server.start_io()

Expand Down
54 changes: 37 additions & 17 deletions server/galaxyls/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from enum import Enum
from typing import Optional

from pydantic import BaseModel


def to_camel(string: str) -> str:
pascal = "".join(word.capitalize() for word in string.split("_"))
camel = pascal[0].lower() + pascal[1:]
return camel
import attrs


class CompletionMode(str, Enum):
Expand All @@ -15,20 +10,45 @@ class CompletionMode(str, Enum):
DISABLED = "disabled"


class ConfigModel(BaseModel):
class Config:
alias_generator = to_camel
allow_population_by_field_name = True
@attrs.define
class CompletionConfig:
"""Auto-completion feature configuration."""

mode: CompletionMode = attrs.field(default=CompletionMode.AUTO)
auto_close_tags: bool = attrs.field(default=True, alias="autoCloseTags")


class CompletionConfig(ConfigModel):
"""Auto-completion feature configuration."""
@attrs.define
class ServerConfig:
"""Language Server specific configuration."""

silent_install: bool = attrs.field(default=False, alias="silentInstall")


@attrs.define
class PlanemoTestingConfig:
"""Planemo testing configuration."""

enabled: bool = attrs.field(default=True)
auto_test_discover_on_save_enabled: bool = attrs.field(default=True, alias="autoTestDiscoverOnSaveEnabled")
extra_params: str = attrs.field(default="", alias="extraParams")


@attrs.define
class PlanemoConfig:
"""Planemo integration configuration."""

mode: CompletionMode = CompletionMode.AUTO
auto_close_tags: bool = True
enabled: bool = attrs.field(default=False)
binary_path: str = attrs.field(default="planemo", alias="binaryPath")
galaxy_root: Optional[str] = attrs.field(default=None, alias="galaxyRoot")
get_cwd: Optional[str] = attrs.field(default=None, alias="getCwd")
testing: PlanemoTestingConfig = attrs.field(default=PlanemoTestingConfig())


class GalaxyToolsConfiguration(ConfigModel):
@attrs.define
class GalaxyToolsConfiguration:
"""Galaxy Language Server general configuration."""

completion: CompletionConfig = CompletionConfig()
server: ServerConfig = attrs.field(default=ServerConfig())
completion: CompletionConfig = attrs.field(default=CompletionConfig())
planemo: PlanemoConfig = attrs.field(default=PlanemoConfig())
47 changes: 25 additions & 22 deletions server/galaxyls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@
Optional,
)

from pygls.lsp.methods import (
CODE_ACTION,
COMPLETION,
DEFINITION,
FORMATTING,
HOVER,
INITIALIZED,
TEXT_DOCUMENT_DID_CLOSE,
TEXT_DOCUMENT_DID_OPEN,
TEXT_DOCUMENT_DID_SAVE,
WORKSPACE_DID_CHANGE_CONFIGURATION,
)
from pygls.lsp.types import (
from lsprotocol.types import (
CodeAction,
CodeActionKind,
CodeActionOptions,
Expand All @@ -26,20 +14,30 @@
CompletionOptions,
CompletionParams,
ConfigurationItem,
ConfigurationParams,
Diagnostic,
DidChangeConfigurationParams,
DidCloseTextDocumentParams,
DidOpenTextDocumentParams,
DidSaveTextDocumentParams,
DocumentFormattingParams,
Hover,
INITIALIZED,
InitializeParams,
Location,
MessageType,
TEXT_DOCUMENT_CODE_ACTION,
TEXT_DOCUMENT_COMPLETION,
TEXT_DOCUMENT_DEFINITION,
TEXT_DOCUMENT_DID_CLOSE,
TEXT_DOCUMENT_DID_OPEN,
TEXT_DOCUMENT_DID_SAVE,
TEXT_DOCUMENT_FORMATTING,
TEXT_DOCUMENT_HOVER,
TextDocumentIdentifier,
TextDocumentPositionParams,
TextEdit,
WORKSPACE_DID_CHANGE_CONFIGURATION,
WorkspaceConfigurationParams,
)
from pygls.server import LanguageServer
from pygls.workspace import Document
Expand All @@ -63,12 +61,15 @@
)
from galaxyls.utils import deserialize_command_param

GLS_VERSION = "0.9.0"
GLS_NAME = "galaxy-tools-language-server"


class GalaxyToolsLanguageServer(LanguageServer):
"""Galaxy Tools Language Server."""

def __init__(self) -> None:
super().__init__()
super().__init__(name=GLS_NAME, version=GLS_VERSION)
self.service = GalaxyToolLanguageService()
self.configuration: GalaxyToolsConfiguration = GalaxyToolsConfiguration()

Expand All @@ -84,7 +85,9 @@ async def _load_client_config_async(server: GalaxyToolsLanguageServer) -> None:
server (GalaxyToolsLanguageServer): The language server instance.
"""
try:
config = await server.get_configuration_async(ConfigurationParams(items=[ConfigurationItem(section="galaxyTools")]))
config = await server.get_configuration_async(
WorkspaceConfigurationParams(items=[ConfigurationItem(section="galaxyTools")])
)
server.configuration = GalaxyToolsConfiguration(**config[0])
except BaseException as err:
server.show_message_log(f"Error loading configuration: {err}")
Expand All @@ -105,7 +108,7 @@ async def did_change_configuration(server: GalaxyToolsLanguageServer, params: Di
server.show_message("Settings updated")


@language_server.feature(COMPLETION, CompletionOptions(trigger_characters=["<", " "]))
@language_server.feature(TEXT_DOCUMENT_COMPLETION, CompletionOptions(trigger_characters=["<", " "]))
def completions(server: GalaxyToolsLanguageServer, params: CompletionParams) -> Optional[CompletionList]:
"""Returns completion items depending on the current document context."""
if server.configuration.completion.mode == CompletionMode.DISABLED:
Expand All @@ -117,7 +120,7 @@ def completions(server: GalaxyToolsLanguageServer, params: CompletionParams) ->
return None


@language_server.feature(HOVER)
@language_server.feature(TEXT_DOCUMENT_HOVER)
def hover(server: GalaxyToolsLanguageServer, params: TextDocumentPositionParams) -> Optional[Hover]:
"""Displays Markdown documentation for the element under the cursor."""
document = _get_valid_document(server, params.text_document.uri)
Expand All @@ -127,7 +130,7 @@ def hover(server: GalaxyToolsLanguageServer, params: TextDocumentPositionParams)
return None


@language_server.feature(FORMATTING)
@language_server.feature(TEXT_DOCUMENT_FORMATTING)
def formatting(server: GalaxyToolsLanguageServer, params: DocumentFormattingParams) -> Optional[List[TextEdit]]:
"""Formats the whole document using the provided parameters"""
document = _get_valid_document(server, params.text_document.uri)
Expand Down Expand Up @@ -157,18 +160,18 @@ def did_close(server: GalaxyToolsLanguageServer, params: DidCloseTextDocumentPar
server.publish_diagnostics(params.text_document.uri, [])


@language_server.feature(DEFINITION)
@language_server.feature(TEXT_DOCUMENT_DEFINITION)
def definition(server: GalaxyToolsLanguageServer, params: TextDocumentPositionParams) -> Optional[List[Location]]:
"""Provides the location of a symbol definition."""
document = _get_valid_document(server, params.text_document.uri)
if document:
xml_document = _get_xml_document(document)
return server.service.definitions_provider.go_to_definition(xml_document, params.position)
return server.service.go_to_definition(xml_document, params.position)
return None


@language_server.feature(
CODE_ACTION,
TEXT_DOCUMENT_CODE_ACTION,
CodeActionOptions(
code_action_kinds=[
CodeActionKind.RefactorExtract,
Expand Down
2 changes: 1 addition & 1 deletion server/galaxyls/services/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Optional,
)

from pygls.lsp.types import (
from lsprotocol.types import (
CompletionContext,
CompletionItem,
CompletionItemKind,
Expand Down
6 changes: 3 additions & 3 deletions server/galaxyls/services/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Optional,
)

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

from galaxyls.services.tools.constants import MACROS
Expand Down Expand Up @@ -239,8 +239,8 @@ def find_matching_xsd_element(self, node: Optional[XmlSyntaxNode], xsd_tree: Xsd
return xsd_node
return None

def get_range_for_context(self, xml_document: XmlDocument, context: XmlContext) -> Range:
def get_range_for_context(self, xml_document: XmlDocument, context: XmlContext) -> Optional[Range]:
if context.node is None:
return Range()
return None
start_offset, end_offset = context.node.get_offsets(context.offset)
return convert_document_offsets_to_range(xml_document.document, start_offset, end_offset)
2 changes: 1 addition & 1 deletion server/galaxyls/services/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Optional,
)

from pygls.lsp.types import (
from lsprotocol.types import (
Location,
Position,
)
Expand Down
4 changes: 2 additions & 2 deletions server/galaxyls/services/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

from typing import List

from lxml import etree
from pygls.lsp.types import (
from lsprotocol.types import (
DocumentFormattingParams,
Position,
Range,
TextEdit,
)
from lxml import etree

DEFAULT_INDENTATION = " " * 4

Expand Down
Loading

0 comments on commit 34cf7de

Please sign in to comment.