Skip to content

Commit acc5105

Browse files
authored
Merge pull request #37 from yeger00/type-definition-fixes
Fix type definition function and starting to add a test
2 parents 61f0bf6 + 954220f commit acc5105

12 files changed

+1033
-143
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ lint:
1010

1111
test:
1212
poetry install --no-interaction --no-root --with tests
13-
poetry run pytest
13+
poetry run pytest $(PYTEST_FLAGS)
1414

1515
publish:
1616
poetry install --no-interaction --no-root --with publish

classes.dot

-80
This file was deleted.

docs/README.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,25 @@ pip install pylspclient
1616
```
1717

1818
# Contributing
19-
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:
19+
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.
20+
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.
21+
You can run it locally as well:
2022

2123
## Run the tests
2224
```
23-
pip install -e .
24-
pip install -r requirements.test.txt
25-
pytest test
25+
make test
2626
```
2727

2828
## Run the linter
2929
```
30-
ruff check .
30+
make lint
31+
```
32+
33+
## New release
34+
```
35+
make bump
36+
make build
37+
make publish
3138
```
3239

3340
# License

pylspclient/lsp_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def typeDefinition(self, textDocument, position):
117117
:param TextDocumentItem textDocument: The text document.
118118
:param Position position: The position inside the text document.
119119
"""
120-
result_dict = self.lsp_endpoint.call_method("textDocument/definition", textDocument=textDocument, position=position)
120+
result_dict = self.lsp_endpoint.call_method("textDocument/typeDefinition", textDocument=textDocument, position=position)
121121
return [lsp_structs.Location(**result) for result in result_dict]
122122

123123

setup.py

-36
This file was deleted.

tests/test-workspace/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#from __future__ import absolute_import
2+
3+
__all__ = []
4+
5+
from pylspclient.json_rpc_endpoint import JsonRpcEndpoint
6+
from pylspclient.lsp_client import LspClient
7+
from pylspclient.lsp_endpoint import LspEndpoint
8+
from pylspclient import lsp_structs
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from __future__ import print_function
2+
import json
3+
from pylspclient import lsp_structs
4+
import threading
5+
6+
JSON_RPC_REQ_FORMAT = "Content-Length: {json_string_len}\r\n\r\n{json_string}"
7+
LEN_HEADER = "Content-Length: "
8+
TYPE_HEADER = "Content-Type: "
9+
10+
11+
# TODO: add content-type
12+
13+
14+
class MyEncoder(json.JSONEncoder):
15+
"""
16+
Encodes an object in JSON
17+
"""
18+
def default(self, o): # pylint: disable=E0202
19+
return o.__dict__
20+
21+
22+
class JsonRpcEndpoint(object):
23+
'''
24+
Thread safe JSON RPC endpoint implementation. Responsible to recieve and send JSON RPC messages, as described in the
25+
protocol. More information can be found: https://www.jsonrpc.org/
26+
'''
27+
def __init__(self, stdin, stdout):
28+
self.stdin = stdin
29+
self.stdout = stdout
30+
self.read_lock = threading.Lock()
31+
self.write_lock = threading.Lock()
32+
33+
@staticmethod
34+
def __add_header(json_string):
35+
'''
36+
Adds a header for the given json string
37+
38+
:param str json_string: The string
39+
:return: the string with the header
40+
'''
41+
return JSON_RPC_REQ_FORMAT.format(json_string_len=len(json_string), json_string=json_string)
42+
43+
44+
def send_request(self, message):
45+
'''
46+
Sends the given message.
47+
48+
:param dict message: The message to send.
49+
'''
50+
json_string = json.dumps(message, cls=MyEncoder)
51+
jsonrpc_req = self.__add_header(json_string)
52+
with self.write_lock:
53+
self.stdin.write(jsonrpc_req.encode())
54+
self.stdin.flush()
55+
56+
57+
def recv_response(self):
58+
'''
59+
Recives a message.
60+
61+
:return: a message
62+
'''
63+
with self.read_lock:
64+
message_size = None
65+
while True:
66+
#read header
67+
line = self.stdout.readline()
68+
if not line:
69+
# server quit
70+
return None
71+
line = line.decode("utf-8")
72+
if not line.endswith("\r\n"):
73+
raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: missing newline")
74+
#remove the "\r\n"
75+
line = line[:-2]
76+
if line == "":
77+
# done with the headers
78+
break
79+
elif line.startswith(LEN_HEADER):
80+
line = line[len(LEN_HEADER):]
81+
if not line.isdigit():
82+
raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: size is not int")
83+
message_size = int(line)
84+
elif line.startswith(TYPE_HEADER):
85+
# nothing todo with type for now.
86+
pass
87+
else:
88+
raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: unkown header")
89+
if not message_size:
90+
raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: missing size")
91+
92+
jsonrpc_res = self.stdout.read(message_size).decode("utf-8")
93+
return json.loads(jsonrpc_res)

0 commit comments

Comments
 (0)