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

Generate server methods based on lsprotocol type definitions #489

Merged
merged 9 commits into from
Aug 22, 2024
Merged
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
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@

_pygls_ (pronounced like "pie glass") is a pythonic generic implementation of the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/specification) for use as a foundation for writing your own [Language Servers](https://langserver.org/) in just a few lines of code.

> [!IMPORTANT]
> The next major version, *pygls v2* is ready for testing.
> We encourage all existing server authors to try the [migration guide](https://pygls.readthedocs.io/en/latest/howto/migrate-to-v2.html) and let us know how you get on!

## Quickstart
```python
from pygls.server import LanguageServer
from lsprotocol.types import (
TEXT_DOCUMENT_COMPLETION,
CompletionItem,
CompletionList,
CompletionParams,
)
from pygls.lsp.server import LanguageServer
from lsprotocol import types

server = LanguageServer("example-server", "v0.1")

@server.feature(TEXT_DOCUMENT_COMPLETION)
def completions(params: CompletionParams):
@server.feature(types.TEXT_DOCUMENT_COMPLETION)
def completions(params: types.CompletionParams):
items = []
document = server.workspace.get_document(params.text_document.uri)
document = server.workspace.get_text_document(params.text_document.uri)
current_line = document.lines[params.position.line].strip()
if current_line.endswith("hello."):
items = [
CompletionItem(label="world"),
CompletionItem(label="friend"),
types.CompletionItem(label="world"),
types.CompletionItem(label="friend"),
]
return CompletionList(is_incomplete=False, items=items)
return types.CompletionList(is_incomplete=False, items=items)

server.start_io()
```
Expand Down
1 change: 1 addition & 0 deletions docs/source/howto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ How To Guides
Handle Invalid Data <howto/handle-invalid-data>
Implement Semantic Tokens <howto/implement-semantic-tokens>
Migrate to v1 <howto/migrate-to-v1>
Migrate to v2 <howto/migrate-to-v2>
Use the pygls-playground <howto/use-the-pygls-playground>
366 changes: 366 additions & 0 deletions docs/source/howto/migrate-to-v2.rst

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

*pygls*
=======

Expand Down
5 changes: 4 additions & 1 deletion docs/source/reference/servers.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Servers
=======

.. autoclass:: pygls.server.LanguageServer
.. autoclass:: pygls.lsp.LanguageServer
:members:

.. autoclass:: pygls.progress.Progress
:members:

.. autoclass:: pygls.server.Server
Expand Down
8 changes: 4 additions & 4 deletions docs/source/user-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The code snippet below shows how to start the server in *TCP* mode.

.. code:: python

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer('example-server', 'v0.1')

Expand All @@ -48,7 +48,7 @@ The code snippet below shows how to start the server in *STDIO* mode.

.. code:: python

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer('example-server', 'v0.1')

Expand All @@ -66,7 +66,7 @@ The code snippet below shows how to start the server in *WEBSOCKET* mode.

.. code:: python

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

server = LanguageServer('example-server', 'v0.1')

Expand All @@ -89,7 +89,7 @@ is the minimal setup to setup logging in *pygls*:

import logging

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

logging.basicConfig(filename='pygls.log', filemode='w', level=logging.DEBUG)

Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer
from lsprotocol import types

server = LanguageServer("example-server", "v0.1")
Expand Down
3 changes: 2 additions & 1 deletion examples/servers/code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
This server scans the document for incomplete sums e.g. ``1 + 1 =`` and returns a code
action which, when invoked will fill in the answer.
"""

import re
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer
from lsprotocol.types import (
TEXT_DOCUMENT_CODE_ACTION,
CodeAction,
Expand Down
12 changes: 9 additions & 3 deletions examples/servers/code_lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
front, this example demonstrates how the ``codeLens/resolve`` can be used to defer this
computation until it is actually necessary.
"""

import logging
import re

from lsprotocol import types

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

ADDITION = re.compile(r"^\s*(\d+)\s*\+\s*(\d+)\s*=(?=\s*$)")
server = LanguageServer("code-lens-server", "v1")
Expand Down Expand Up @@ -115,7 +116,8 @@ def evaluate_sum(ls: LanguageServer, args):
answer = arguments["left"] + arguments["right"]
edit = types.TextDocumentEdit(
text_document=types.OptionalVersionedTextDocumentIdentifier(
uri=arguments["uri"], version=document.version
uri=arguments["uri"],
version=document.version,
),
edits=[
types.TextEdit(
Expand All @@ -129,7 +131,11 @@ def evaluate_sum(ls: LanguageServer, args):
)

# Apply the edit.
ls.apply_edit(types.WorkspaceEdit(document_changes=[edit]))
ls.workspace_apply_edit(
types.ApplyWorkspaceEditParams(
edit=types.WorkspaceEdit(document_changes=[edit]),
),
)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion examples/servers/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@
This server implements the requests defined above for CSS's hex color code syntax
(``#000`` and ``#000000``).
"""

import logging
import re

from lsprotocol import types

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

COLOR = re.compile(r"""\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})(?!\w)""")
server = LanguageServer("color-server", "v1")
Expand Down
3 changes: 2 additions & 1 deletion examples/servers/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
expect (fixes welcome!), but it should be enough to demonstrate the expected interaction
between client and server.
"""

import logging
from typing import Dict
from typing import List
Expand All @@ -41,7 +42,7 @@
import attrs
from lsprotocol import types

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer
from pygls.workspace import TextDocument


Expand Down
3 changes: 2 additions & 1 deletion examples/servers/goto.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
This means the choices of what the example server below will return results for are
completely arbitrary.
"""

import logging
import re

from lsprotocol import types

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer
from pygls.workspace import TextDocument

ARGUMENT = re.compile(r"(?P<name>\w+): (?P<type>\w+)")
Expand Down
3 changes: 2 additions & 1 deletion examples/servers/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
displaying a table how the selected date would be formatted in each of the supported
formats.
"""

import logging
from datetime import datetime

from lsprotocol import types

from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

DATE_FORMATS = [
"%H:%M:%S",
Expand Down
3 changes: 2 additions & 1 deletion examples/servers/inlay_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
uses the ``inlayHint/resolve`` to demonstrate how you can defer expensive computations
to when they are required.
"""

import re
from typing import Optional

from lsprotocol import types
from pygls.server import LanguageServer
from pygls.lsp.server import LanguageServer

NUMBER = re.compile(r"\d+")
server = LanguageServer("inlay-hint-server", "v1")
Expand Down
Loading
Loading