Skip to content

Commit

Permalink
Fix type definition function and starting to add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yeger00 committed Dec 9, 2023
1 parent 61f0bf6 commit 954220f
Show file tree
Hide file tree
Showing 12 changed files with 1,033 additions and 143 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ lint:

test:
poetry install --no-interaction --no-root --with tests
poetry run pytest
poetry run pytest $(PYTEST_FLAGS)

publish:
poetry install --no-interaction --no-root --with publish
Expand Down
80 changes: 0 additions & 80 deletions classes.dot

This file was deleted.

17 changes: 12 additions & 5 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@ pip install pylspclient
```

# Contributing
In order to contribute you need to make sure your PR passes all the [Test Package](https://github.com/yeger00/pylspclient/blob/main/.github/workflows/test-pkg.yml) steps. You can run it locally as well:
In order to contribute you need to make sure your PR passes all the [Test Package](https://github.com/yeger00/pylspclient/blob/main/.github/workflows/test-pkg.yml) steps.
All the development related commands are wrap with Makefile just for easy access from both the development environment and from the workflows. You can see that most of the commands are only 1 line and you can run them directly.
You can run it locally as well:

## Run the tests
```
pip install -e .
pip install -r requirements.test.txt
pytest test
make test
```

## Run the linter
```
ruff check .
make lint
```

## New release
```
make bump
make build
make publish
```

# License
Expand Down
2 changes: 1 addition & 1 deletion pylspclient/lsp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def typeDefinition(self, textDocument, position):
:param TextDocumentItem textDocument: The text document.
:param Position position: The position inside the text document.
"""
result_dict = self.lsp_endpoint.call_method("textDocument/definition", textDocument=textDocument, position=position)
result_dict = self.lsp_endpoint.call_method("textDocument/typeDefinition", textDocument=textDocument, position=position)
return [lsp_structs.Location(**result) for result in result_dict]


Expand Down
36 changes: 0 additions & 36 deletions setup.py

This file was deleted.

8 changes: 8 additions & 0 deletions tests/test-workspace/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#from __future__ import absolute_import

__all__ = []

from pylspclient.json_rpc_endpoint import JsonRpcEndpoint
from pylspclient.lsp_client import LspClient
from pylspclient.lsp_endpoint import LspEndpoint
from pylspclient import lsp_structs
93 changes: 93 additions & 0 deletions tests/test-workspace/json_rpc_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from __future__ import print_function
import json
from pylspclient import lsp_structs
import threading

JSON_RPC_REQ_FORMAT = "Content-Length: {json_string_len}\r\n\r\n{json_string}"
LEN_HEADER = "Content-Length: "
TYPE_HEADER = "Content-Type: "


# TODO: add content-type


class MyEncoder(json.JSONEncoder):
"""
Encodes an object in JSON
"""
def default(self, o): # pylint: disable=E0202
return o.__dict__


class JsonRpcEndpoint(object):
'''
Thread safe JSON RPC endpoint implementation. Responsible to recieve and send JSON RPC messages, as described in the
protocol. More information can be found: https://www.jsonrpc.org/
'''
def __init__(self, stdin, stdout):
self.stdin = stdin
self.stdout = stdout
self.read_lock = threading.Lock()
self.write_lock = threading.Lock()

@staticmethod
def __add_header(json_string):
'''
Adds a header for the given json string
:param str json_string: The string
:return: the string with the header
'''
return JSON_RPC_REQ_FORMAT.format(json_string_len=len(json_string), json_string=json_string)


def send_request(self, message):
'''
Sends the given message.
:param dict message: The message to send.
'''
json_string = json.dumps(message, cls=MyEncoder)
jsonrpc_req = self.__add_header(json_string)
with self.write_lock:
self.stdin.write(jsonrpc_req.encode())
self.stdin.flush()


def recv_response(self):
'''
Recives a message.
:return: a message
'''
with self.read_lock:
message_size = None
while True:
#read header
line = self.stdout.readline()
if not line:
# server quit
return None
line = line.decode("utf-8")
if not line.endswith("\r\n"):
raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: missing newline")
#remove the "\r\n"
line = line[:-2]
if line == "":
# done with the headers
break
elif line.startswith(LEN_HEADER):
line = line[len(LEN_HEADER):]
if not line.isdigit():
raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: size is not int")
message_size = int(line)
elif line.startswith(TYPE_HEADER):
# nothing todo with type for now.
pass
else:
raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: unkown header")
if not message_size:
raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: missing size")

jsonrpc_res = self.stdout.read(message_size).decode("utf-8")
return json.loads(jsonrpc_res)
Loading

0 comments on commit 954220f

Please sign in to comment.