Skip to content

Commit

Permalink
Fixed pygls v1.0 module import error (#868)
Browse files Browse the repository at this point in the history
* Fixed pygls v1.0 module import error

* Clean up msg and add entry to changelog

* Improved version compare

* Updated pyproject.toml

* Updated isort version to 5.12.0

* Fixed mypy issue

* Removed poetry_core version constraint
  • Loading branch information
haiyangToAI authored Feb 8, 2023
1 parent 3dc732a commit f50f1fe
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- pep8-naming

- repo: https://github.com/pycqa/isort
rev: 5.11.4
rev: 5.12.0
hooks:
- id: isort

Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ License
-----
Released: under development

* Bugfix: Fixed pygls version compatibility.

1.2.0
-----
Released: 24.01.2023
Expand Down
20 changes: 18 additions & 2 deletions poetry.lock

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

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pyparsing = "^3.0.9" # 3.0.5 error: https://github.com/pyparsing/pyparsing/issu
pytest-benchmark = "^4.0.0"
memray = "^1.3.1"

# package version compare, in case setuptools not available
packaging = "*"

[tool.pytest.ini_options]
asyncio_mode= "auto"

Expand Down
33 changes: 24 additions & 9 deletions sphinx_needs/lsp/esbonio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,30 @@
from esbonio.lsp.rst import CompletionContext, DefinitionContext, HoverContext
from esbonio.lsp.sphinx import SphinxLanguageServer
from jinja2 import BaseLoader, Environment
from pygls.lsp.types import (
CompletionItem,
CompletionItemKind,
InsertTextFormat,
Location,
Position,
Range,
TextEdit,
)
from packaging import version
from pygls import __version__

if version.parse(__version__) < version.parse("1.0"):
from pygls.lsp.types import (
CompletionItem,
CompletionItemKind,
InsertTextFormat,
Location,
Position,
Range,
TextEdit,
)
else:
from lsprotocol.types import ( # type: ignore[no-redef]
CompletionItem,
CompletionItemKind,
InsertTextFormat,
Location,
Position,
Range,
TextEdit,
)

from sphinx.application import Sphinx

from sphinx_needs.lsp.needs_store import NeedsStore
Expand Down
26 changes: 25 additions & 1 deletion tests/test_lsp/test_lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@

import pytest
import pytest_lsp
from pygls.lsp.types import MarkupContent, MarkupKind, Position, Range
from packaging import version
from pygls import __version__

if version.parse(__version__) < version.parse("1.0"):
from pygls.lsp.types import MarkupContent, MarkupKind, Position, Range
else:
from lsprotocol.types import MarkupContent, MarkupKind, Position, Range

TEST_DOC_ROOT_URI = os.path.join("file://", os.path.abspath(os.path.dirname(__file__)), "doc_example_lsp")
TEST_FILE_URI = os.path.join(TEST_DOC_ROOT_URI, "index.rst")
Expand All @@ -18,6 +24,9 @@ async def client():
pass


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_need_directive_role_completion(client):
# check needs directive completion
Expand Down Expand Up @@ -202,6 +211,9 @@ async def test_need_directive_role_completion(client):
assert need_role_need.data["source_feature"] == "sphinx_needs.lsp.esbonio.NeedlsFeatures"


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_need_auto_generated_id_completion(client):
# check needs option id snippet, auto-generated need IDs, e.g. :id: REQ_e0bafd9b
Expand All @@ -222,6 +234,9 @@ async def test_need_auto_generated_id_completion(client):
assert needs_option_id.kind == 15 # CompletionItemKind.Snippet


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_need_directive_snippets_completion(client):
# check need directive snippets completion
Expand All @@ -244,6 +259,9 @@ async def test_need_directive_snippets_completion(client):
assert need_directive_snippets_req.data["source_feature"] == "sphinx_needs.lsp.esbonio.NeedlsFeatures"


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_need_id_selection_completion(client):
# check need ID selection completion for need type, e.g. :need:`->`
Expand Down Expand Up @@ -275,6 +293,9 @@ async def test_need_id_selection_completion(client):
assert id_selection_need_id.data["source_feature"] == "sphinx_needs.lsp.esbonio.NeedlsFeatures"


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_goto_definition(client):
# check goto defintion results
Expand All @@ -292,6 +313,9 @@ async def test_goto_definition(client):
assert defined_location.range.end.character == 0


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_hover(client):
# check hover results
Expand Down
5 changes: 5 additions & 0 deletions tests/test_lsp/test_lsp_custom_directive_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import pytest
import pytest_lsp
from packaging import version
from pygls import __version__

TEST_DOC_ROOT_URI = os.path.join(
"file://", os.path.abspath(os.path.dirname(__file__)), "doc_lsp_custom_directive_snippets"
Expand Down Expand Up @@ -35,6 +37,9 @@ async def client():
pass


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_lsp_custom_directive_snippets(client):
# check need custom directive snippets completion
Expand Down
8 changes: 8 additions & 0 deletions tests/test_lsp/test_lsp_custom_need_id_generate_from_title.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import pytest
import pytest_lsp
from packaging import version
from pygls import __version__

TEST_DOC_ROOT_URI = os.path.join(
"file://", os.path.abspath(os.path.dirname(__file__)), "doc_lsp_custom_need_id_generate_from_title"
Expand All @@ -19,6 +21,9 @@ async def client():
pass


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_directive_snippets_with_custom_need_id_generate_from_title(client):
need_directive_snippets = await client.completion_request(uri=TEST_FILE_URI, line=10, character=2)
Expand All @@ -36,6 +41,9 @@ async def test_directive_snippets_with_custom_need_id_generate_from_title(client
assert need_directive_snippets_req.insert_text.startswith(" req:: ${1:title}\n\t:id: ${2:TEST_ID_TEST")


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_id_auto_generation_with_custom_id_generate_from_title(client):
need_req_options_result = await client.completion_request(uri=TEST_FILE_URI, line=11, character=3)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_lsp/test_lsp_custom_need_id_generate_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import pytest
import pytest_lsp
from packaging import version
from pygls import __version__

TEST_DOC_ROOT_URI = os.path.join(
"file://", os.path.abspath(os.path.dirname(__file__)), "doc_lsp_custom_need_id_generate_random"
Expand All @@ -19,6 +21,9 @@ async def client():
pass


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_directive_snippets_with_custom_need_id_generate_random(client):
need_directive_snippets = await client.completion_request(uri=TEST_FILE_URI, line=10, character=2)
Expand All @@ -37,6 +42,9 @@ async def test_directive_snippets_with_custom_need_id_generate_random(client):
assert "_Test" in need_directive_snippets_req.insert_text


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_id_auto_generation_with_custom_id_generate_random(client):
need_req_options_result = await client.completion_request(uri=TEST_FILE_URI, line=11, character=3)
Expand Down
26 changes: 25 additions & 1 deletion tests/test_lsp/test_lsp_support_MyST.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@

import pytest
import pytest_lsp
from pygls.lsp.types import MarkupContent, MarkupKind, Position, Range
from packaging import version
from pygls import __version__

if version.parse(__version__) < version.parse("1.0"):
from pygls.lsp.types import MarkupContent, MarkupKind, Position, Range
else:
from lsprotocol.types import MarkupContent, MarkupKind, Position, Range

TEST_DOC_ROOT_URI = os.path.join("file://", os.path.abspath(os.path.dirname(__file__)), "doc_lsp_support_MyST")
TEST_MD_FILE_URI = os.path.join(TEST_DOC_ROOT_URI, "myfile.md")
Expand All @@ -18,6 +24,9 @@ async def client():
pass


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_lsp_goto_definition_support_for_myst(client):
# Check Goto Defintion support for MySt/Markdown file, e.g. myfile.md
Expand Down Expand Up @@ -62,6 +71,9 @@ async def test_lsp_goto_definition_support_for_myst(client):
assert goto_rst_location.range.end.character == 0


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_lsp_hover_support_for_myst(client):
# Check Hover support for MyST/Markdown file
Expand All @@ -84,6 +96,9 @@ async def test_lsp_hover_support_for_myst(client):
)


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_lsp_id_selection_completion_support_for_myst(client):
# Check ID selection completion support for MyST/Markdown file
Expand Down Expand Up @@ -169,6 +184,9 @@ async def test_lsp_id_selection_completion_support_for_myst(client):
assert need_id_md_result.data["source_feature"] == "sphinx_needs.lsp.esbonio.NeedlsFeatures"


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_lsp_need_directive_snippets_completion_for_myst(client):
# Check need directive snippets support for MyST/Markdown
Expand Down Expand Up @@ -242,6 +260,9 @@ async def test_lsp_need_directive_snippets_completion_for_myst(client):
assert needs_directive_spec_md_inside_eval_rst.data["source_feature"] == "sphinx_needs.lsp.esbonio.NeedlsFeatures"


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_lsp_needs_option_id_completion_for_myst(client):
# Needs option id suggestion is the same for MyST/Markdown as for rst/Sphinx file, e.g. :id:
Expand All @@ -263,6 +284,9 @@ async def test_lsp_needs_option_id_completion_for_myst(client):
assert needs_option_id.kind == 15 # CompletionItemKind.Snippet


@pytest.mark.skipif(
version.parse(__version__) >= version.parse("1.0"), reason="Esbonio version >=0.16.0 using pygls >= 1.0 not tested."
)
@pytest.mark.asyncio
async def test_lsp_need_role_need_completion_for_myst(client):
# Need role need support for MyST/Markdown file
Expand Down

0 comments on commit f50f1fe

Please sign in to comment.