Skip to content

Commit

Permalink
Migrating to pygls v1.1 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwist-sgr authored Oct 10, 2023
1 parent ab3be46 commit 770fe74
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .flake8rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 79
11 changes: 5 additions & 6 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ jobs:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest
pip install -r requirements.txt
make install
- name: Lint with pycodestyle and pyflakes
run: |
pycodestyle .
pyflakes .
make flake
- name: Test with pytest
run: |
pytest
make test
2 changes: 2 additions & 0 deletions .pycodestylerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pycodestyle]
max-line-length = 79
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SHELL = /bin/bash
PYTHON = python


.PHONY: clean
clean:
find . -path "*/__pycache__/*" -delete
find . -type d -empty -delete


.PHONY: install
install: clean
$(PYTHON) -m pip install -r requirements.txt


.PHONY: flake flake8
flake flake8:
pycodestyle --config .pycodestylerc .
pyflakes .

.PHONY: test
test:
$(PYTHON) -m pytest
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# NEWS

## 1.17

- Migrating to pygls v1.1

## 1.16

- Use version 0 for not opened files on rename
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Yet another Jedi Python language server
## Requirements

- Python >= 3.6
- pygls >= 0.10.2,<0.11
- Jedi >= 0.18
- pygls >= 1.1, <1.2
- Jedi >= 0.19
- pyflakes ~= 2.2
- pycodestyle ~= 2.5
- yapf ~=0.30
Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.17
4 changes: 2 additions & 2 deletions anakinls/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import logging

from .server import server
from .version import get_version
from .version import __version__

logging.basicConfig(level=logging.INFO)
logging.getLogger('pygls.protocol').setLevel(logging.WARN)
Expand Down Expand Up @@ -56,7 +56,7 @@ def main():
args = parser.parse_args()

if args.version:
print(inspect.cleandoc(f'''anakinls v{get_version()}
print(inspect.cleandoc(f'''anakinls v{__version__}
Copyright (C) 2020 Andrii Kolomoiets
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
Expand Down
77 changes: 38 additions & 39 deletions anakinls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,12 @@

from yapf.yapflib.yapf_api import FormatCode # type: ignore

from pygls.lsp.methods import (COMPLETION, TEXT_DOCUMENT_DID_CHANGE,
TEXT_DOCUMENT_DID_CLOSE, TEXT_DOCUMENT_DID_OPEN,
HOVER, SIGNATURE_HELP, DEFINITION,
REFERENCES, WORKSPACE_DID_CHANGE_CONFIGURATION,
TEXT_DOCUMENT_WILL_SAVE, TEXT_DOCUMENT_DID_SAVE,
DOCUMENT_SYMBOL, CODE_ACTION, FORMATTING,
RANGE_FORMATTING, RENAME, DOCUMENT_HIGHLIGHT)
from pygls.lsp import types
from lsprotocol import types
from pygls.server import LanguageServer
from pygls.protocol import LanguageServerProtocol
from pygls.protocol import LanguageServerProtocol, lsp_method
from pygls.uris import to_fs_path

from .version import get_version # type: ignore
from .version import __version__ # type: ignore

RE_WORD = re.compile(r'\w*')

Expand Down Expand Up @@ -76,9 +69,10 @@

class AnakinLanguageServerProtocol(LanguageServerProtocol):

def bf_initialize(
@lsp_method(types.INITIALIZE)
def lsp_initialize(
self, params: types.InitializeParams) -> types.InitializeResult:
result = super().bf_initialize(params)
result = super().lsp_initialize(params)
global jediEnvironment
global jediProject
global completionFunction
Expand Down Expand Up @@ -129,15 +123,14 @@ def get_attr(o, *attrs):
else:
hoverFunction = _docstring

# pygls does not currently support serverInfo of LSP v3.15
result.server_info = types.ServerInfo(
name='anakinls',
version=get_version(),
)
return result


server = LanguageServer(protocol_cls=AnakinLanguageServerProtocol)
server = LanguageServer(
name='anakinls',
version=__version__,
protocol_cls=AnakinLanguageServerProtocol
)

scripts: Dict[str, Script] = {}
pycodestyleOptions: Dict[str, Any] = {}
Expand Down Expand Up @@ -172,7 +165,7 @@ def get_attr(o, *attrs):
def get_script(ls: LanguageServer, uri: str, update: bool = False) -> Script:
result = None if update else scripts.get(uri)
if not result:
document = ls.workspace.get_document(uri)
document = ls.workspace.get_text_document(uri)
result = Script(
code=document.source,
path=document.path,
Expand Down Expand Up @@ -395,21 +388,21 @@ def _validate(ls: LanguageServer, uri: str, script: Script = None):
ls.publish_diagnostics(uri, result)


@server.feature(TEXT_DOCUMENT_DID_OPEN)
@server.feature(types.TEXT_DOCUMENT_DID_OPEN)
def did_open(ls: LanguageServer, params: types.DidOpenTextDocumentParams):
if config['diagnostic_on_open']:
_validate(ls, params.text_document.uri)


@server.feature(TEXT_DOCUMENT_DID_CLOSE)
@server.feature(types.TEXT_DOCUMENT_DID_CLOSE)
def did_close(ls: LanguageServer, params: types.DidCloseTextDocumentParams):
try:
del scripts[params.text_document.uri]
except KeyError:
pass


@server.feature(TEXT_DOCUMENT_DID_CHANGE)
@server.feature(types.TEXT_DOCUMENT_DID_CHANGE)
def did_change(ls: LanguageServer, params: types.DidChangeTextDocumentParams):
script = get_script(ls, params.text_document.uri, True)
if config['diagnostic_on_change']:
Expand Down Expand Up @@ -497,7 +490,10 @@ def _completions_snippets(completions: List[Completion],
))


@server.feature(COMPLETION, types.CompletionOptions(trigger_characters=['.']))
@server.feature(
types.TEXT_DOCUMENT_COMPLETION,
types.CompletionOptions(trigger_characters=['.']),
)
def completions(ls: LanguageServer, params: types.CompletionParams):
global completionFunction
script = get_script(ls, params.text_document.uri)
Expand Down Expand Up @@ -543,7 +539,7 @@ def _docstring_markdown(name: Name) -> str:
return f'```\n{doc}\n```'


@server.feature(HOVER)
@server.feature(types.TEXT_DOCUMENT_HOVER)
def hover(ls: LanguageServer,
params: types.TextDocumentPositionParams) -> Optional[types.Hover]:
global hoverFunction
Expand All @@ -560,8 +556,10 @@ def hover(ls: LanguageServer,
return None


@server.feature(SIGNATURE_HELP,
types.SignatureHelpOptions(trigger_characters=['(', ',']))
@server.feature(
types.TEXT_DOCUMENT_SIGNATURE_HELP,
types.SignatureHelpOptions(trigger_characters=['(', ','])
)
def signature_help(
ls: LanguageServer,
params: types.TextDocumentPositionParams
Expand Down Expand Up @@ -614,7 +612,7 @@ def _get_locations(defs: List[Name]) -> List[types.Location]:
]


@server.feature(DEFINITION)
@server.feature(types.TEXT_DOCUMENT_DEFINITION)
def definition(
ls: LanguageServer,
params: types.TextDocumentPositionParams) -> List[types.Location]:
Expand All @@ -623,7 +621,7 @@ def definition(
return _get_locations(defs)


@server.feature(REFERENCES)
@server.feature(types.TEXT_DOCUMENT_REFERENCES)
def references(ls: LanguageServer,
params: types.ReferenceParams) -> List[types.Location]:
script = get_script(ls, params.text_document.uri)
Expand All @@ -632,7 +630,7 @@ def references(ls: LanguageServer,
return _get_locations(refs)


@server.feature(WORKSPACE_DID_CHANGE_CONFIGURATION)
@server.feature(types.WORKSPACE_DID_CHANGE_CONFIGURATION)
def did_change_configuration(ls: LanguageServer,
settings: types.DidChangeConfigurationParams):
if not settings.settings or 'anakinls' not in settings.settings:
Expand Down Expand Up @@ -673,13 +671,14 @@ def did_change_configuration(ls: LanguageServer,
_validate(ls, uri)


@server.feature(TEXT_DOCUMENT_WILL_SAVE)
@server.feature(types.TEXT_DOCUMENT_WILL_SAVE)
def will_save(ls: LanguageServer, params: types.WillSaveTextDocumentParams):
pass


@server.feature(TEXT_DOCUMENT_DID_SAVE,
types.TextDocumentSaveRegistrationOptions(include_text=False))
@server.feature(
types.TEXT_DOCUMENT_DID_SAVE, types.SaveOptions(include_text=False)
)
def did_save(ls: LanguageServer, params: types.DidSaveTextDocumentParams):
if config['diagnostic_on_save']:
_validate(ls, params.text_document.uri)
Expand Down Expand Up @@ -765,7 +764,7 @@ def _symbols():
return list(_symbols())


@server.feature(DOCUMENT_SYMBOL)
@server.feature(types.TEXT_DOCUMENT_DOCUMENT_SYMBOL)
def document_symbol(
ls: LanguageServer, params: types.DocumentSymbolParams
) -> Union[List[types.DocumentSymbol], List[types.SymbolInformation], None]:
Expand Down Expand Up @@ -837,14 +836,14 @@ def _get_document_changes(
result.append(types.TextDocumentEdit(
text_document=types.VersionedTextDocumentIdentifier(
uri=uri,
version=ls.workspace.get_document(uri).version or 0
version=ls.workspace.get_text_document(uri).version or 0
),
edits=text_edits
))
return result


@server.feature(CODE_ACTION, types.CodeActionOptions(
@server.feature(types.TEXT_DOCUMENT_CODE_ACTION, types.CodeActionOptions(
code_action_kinds=[
types.CodeActionKind.RefactorInline,
types.CodeActionKind.RefactorExtract]))
Expand Down Expand Up @@ -881,21 +880,21 @@ def _formatting(
return _get_text_edits(diff)


@server.feature(FORMATTING)
@server.feature(types.TEXT_DOCUMENT_FORMATTING)
def formatting(
ls: LanguageServer, params: types.DocumentFormattingParams
) -> Optional[List[types.TextEdit]]:
return _formatting(ls, params.text_document.uri)


@server.feature(RANGE_FORMATTING)
@server.feature(types.TEXT_DOCUMENT_RANGE_FORMATTING)
def range_formatting(
ls: LanguageServer, params: types.DocumentRangeFormattingParams
) -> Optional[List[types.TextEdit]]:
return _formatting(ls, params.text_document.uri, params.range)


@server.feature(RENAME)
@server.feature(types.TEXT_DOCUMENT_RENAME)
def rename(ls: LanguageServer,
params: types.RenameParams) -> Optional[types.WorkspaceEdit]:
script = get_script(ls, params.text_document.uri)
Expand All @@ -911,7 +910,7 @@ def rename(ls: LanguageServer,
return None


@server.feature(DOCUMENT_HIGHLIGHT)
@server.feature(types.TEXT_DOCUMENT_DOCUMENT_HIGHLIGHT)
def highlight(
ls: LanguageServer, params: types.TextDocumentPositionParams
) -> Optional[List[types.DocumentHighlight]]:
Expand Down
10 changes: 5 additions & 5 deletions anakinls/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import pkg_resources
from pathlib import Path


def get_version() -> str:
"Return the version of anakinls."
r = pkg_resources.require('anakin-language-server')
return r[0].version
__all__ = ['__version__']


__version__ = (Path(__file__).parents[1] / 'VERSION').read_text().strip()
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jedi>=0.18.0
pygls>=0.10.2,<0.11
jedi>=0.19.0
pygls>=1.1,<1.2
pyflakes~=2.2
pycodestyle~=2.5
yapf~=0.30
13 changes: 6 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
from pathlib import Path
from setuptools import setup # type: ignore


with open('README.md', 'r') as f:
long_description = f.read()
from anakinls import version


setup(
name='anakin-language-server',
version='1.16',
version=version.__version__,
author='Andrii Kolomoiets',
author_email='andreyk.mad@gmail.com',
description='Yet another Jedi Python language server',
long_description=long_description,
long_description=Path('README.md').read_text(),
long_description_content_type='text/markdown',
url='https://github.com/muffinmad/anakin-language-server',
packages=['anakinls'],
python_requires='>=3.6',
install_requires=[
'jedi>=0.18.0',
'pygls>=0.10.2,<0.11',
'jedi>=0.19.0',
'pygls>=1.1,<1.2',
'pyflakes~=2.2',
'pycodestyle~=2.5',
'yapf~=0.30'
Expand Down
Loading

0 comments on commit 770fe74

Please sign in to comment.