Skip to content

Commit 49aa783

Browse files
authored
Fixes and updates as found and addressed in other extensions (#99)
* Fix filenames to avoid shadowing * Upgrade to latest `pygls` * Get fixes from other extensions * Fix formatting * Fix debug config * Add prettier to recommended extensions. * Use latest language client
1 parent b3e917b commit 49aa783

28 files changed

+742
-879
lines changed

.vscode/extensions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
// See http://go.microsoft.com/fwlink/?LinkId=827846
33
// for the documentation about the extensions.json format
4-
"recommendations": ["dbaeumer.vscode-eslint", "amodio.tsl-problem-matcher"]
4+
"recommendations": ["dbaeumer.vscode-eslint", "amodio.tsl-problem-matcher", "esbenp.prettier-vscode"]
55
}

.vscode/launch.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
1313
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
1414
"preLaunchTask": "npm: watch",
15-
"env": {
16-
"DEBUGPY_ENABLED": "False"
17-
},
1815
"presentation": {
1916
"hidden": false,
2017
"group": "",
@@ -34,7 +31,7 @@
3431
}
3532
},
3633
{
37-
"name": "Python Test Debug",
34+
"name": "Python Config for test explorer (hidden)",
3835
"type": "python",
3936
"request": "launch",
4037
"console": "integratedTerminal",
@@ -47,19 +44,22 @@
4744
}
4845
},
4946
{
50-
"name": "Debug Extension",
47+
"name": "Debug Extension (hidden)",
5148
"type": "extensionHost",
5249
"request": "launch",
5350
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
5451
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
52+
"env": {
53+
"USE_DEBUGPY": "True"
54+
},
5555
"presentation": {
5656
"hidden": true,
5757
"group": "",
5858
"order": 4
5959
}
6060
},
6161
{
62-
"name": "Python Server",
62+
"name": "Python debug server (hidden)",
6363
"type": "python",
6464
"request": "attach",
6565
"listen": { "host": "localhost", "port": 5678 },
@@ -74,7 +74,7 @@
7474
"compounds": [
7575
{
7676
"name": "Debug Extension and Python",
77-
"configurations": ["Python Server", "Debug Extension"],
77+
"configurations": ["Python debug server (hidden)", "Debug Extension (hidden)"],
7878
"stopAll": true,
7979
"preLaunchTask": "npm: watch",
8080
"presentation": {

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
"python.testing.unittestEnabled": false,
1515
"python.testing.pytestEnabled": true,
1616
"python.testing.cwd": "${workspaceFolder}",
17-
"python.analysis.extraPaths": ["bundled/libs"]
17+
"python.analysis.extraPaths": ["bundled/libs"],
18+
"[typescript]": {
19+
"editor.defaultFormatter": "esbenp.prettier-vscode",
20+
"editor.formatOnSave": true
21+
},
1822
}

LICENSE

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
MIT License
22

3-
# TODO: Copyright (c) Microsoft Corporation.
3+
# TODO: If you want to keep using the MIT license, add your name here.
4+
# To change the license, PREPEND it to this file.
5+
6+
Copyright (c) Microsoft Corporation.
47

58
Permission is hereby granted, free of charge, to any person obtaining a copy
69
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ After finishing the getting started part, this template would have added the fol
4040

4141
1. A command `My Tool: Restart Server` (command Id: `mytool.restart`).
4242
1. Following setting:
43-
- `mytool.logLevel`
4443
- `mytool.args`
4544
- `mytool.path`
4645
- `mytool.importStrategy`
@@ -54,11 +53,11 @@ After finishing the getting started part, this template would have added the fol
5453

5554
## Adding features from your tool
5655

57-
Open `bundled/tool/server.py`, here is where you will do most of the changes. Look for `TODO` comments there for more details.
56+
Open `bundled/tool/lsp_server.py`, here is where you will do most of the changes. Look for `TODO` comments there for more details.
5857

5958
Also look for `TODO` in other locations in the entire template:
6059

61-
- `bundled/tool/runner.py` : You may need to update this in some special cases.
60+
- `bundled/tool/lsp_runner.py` : You may need to update this in some special cases.
6261
- `src/test/python_tests/test_server.py` : This is where you will write tests. There are two incomplete examples provided there to get you started.
6362
- All the markdown files in this template have some `TODO` items, be sure to check them out as well. That includes updating the LICENSE file, even if you want to keep it MIT License.
6463

@@ -81,11 +80,11 @@ To debug both TypeScript and Python code use `Debug Extension and Python` debug
8180

8281
To debug only TypeScript code, use `Debug Extension` debug config.
8382

84-
To debug a already running server or in production server, use `Python Attach`, and select the process that is running `server.py`.
83+
To debug a already running server or in production server, use `Python Attach`, and select the process that is running `lsp_server.py`.
8584

8685
## Adding new Settings or Commands
8786

88-
You can add new settings by adding details for the settings in `package.json` file. To pass this configuration to your python tool server (i.e, `server.py`) update the `settings.ts` as need. There are examples of different types of settings in that file that you can base your new settings on.
87+
You can add new settings by adding details for the settings in `package.json` file. To pass this configuration to your python tool server (i.e, `lsp_server.py`) update the `settings.ts` as need. There are examples of different types of settings in that file that you can base your new settings on.
8988

9089
You can follow how `restart` command is implemented in `package.json` and `extension.ts` for how to add commands. You cam also contribute commands from Python via the Language Server Protocol.
9190

@@ -131,11 +130,11 @@ To manually upgrade your local project:
131130

132131
## Troubleshooting
133132

134-
### Changing path or name of `server.py` something else
133+
### Changing path or name of `lsp_server.py` something else
135134

136-
If you want to change the name of `server.py` to something else, you can. Be sure to update `constants.ts` and `src\test\python_tests\lsp_test_client\session.py`.
135+
If you want to change the name of `lsp_server.py` to something else, you can. Be sure to update `constants.ts` and `src\test\python_tests\lsp_test_client\session.py`.
137136

138-
Also make sure that the inserted paths in `server.py` are pointing to the right folders to pick up the dependent packages.
137+
Also make sure that the inserted paths in `lsp_server.py` are pointing to the right folders to pick up the dependent packages.
139138

140139
### Module not found errors
141140

bundled/tool/_debug_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ def update_sys_path(path_to_add: str) -> None:
3434
# line and set breakpoints as appropriate.
3535
debugpy.breakpoint()
3636

37-
SERVER_PATH = os.fspath(pathlib.Path(__file__).parent / "server.py")
38-
# NOTE: Set breakpoint in `server.py` before continuing.
37+
SERVER_PATH = os.fspath(pathlib.Path(__file__).parent / "lsp_server.py")
38+
# NOTE: Set breakpoint in `lsp_server.py` before continuing.
3939
runpy.run_path(SERVER_PATH, run_name="__main__")

bundled/tool/jsonrpc.py renamed to bundled/tool/lsp_jsonrpc.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing import BinaryIO, Dict, Optional, Sequence, Union
1515

1616
CONTENT_LENGTH = "Content-Length: "
17-
RUNNER_SCRIPT = str(pathlib.Path(__file__).parent / "runner.py")
17+
RUNNER_SCRIPT = str(pathlib.Path(__file__).parent / "lsp_runner.py")
1818

1919

2020
def to_str(text) -> str:
@@ -131,7 +131,6 @@ def __init__(self):
131131
self._rpc: Dict[str, JsonRpc] = {}
132132
self._lock = threading.Lock()
133133
self._thread_pool = ThreadPoolExecutor(10)
134-
atexit.register(self.stop_all_processes)
135134

136135
def stop_all_processes(self):
137136
"""Send exit command to all processes and shutdown transport."""
@@ -175,6 +174,7 @@ def get_json_rpc(self, workspace: str) -> JsonRpc:
175174

176175

177176
_process_manager = ProcessManager()
177+
atexit.register(_process_manager.stop_all_processes)
178178

179179

180180
def _get_json_rpc(workspace: str) -> Union[JsonRpc, None]:
@@ -244,11 +244,14 @@ def run_over_json_rpc(
244244
)
245245

246246
if "error" in data:
247+
result = data["result"] if "result" in data else ""
248+
error = data["error"]
249+
247250
if data.get("exception", False):
248-
return RpcRunResult(data["result"], "", data["error"])
249-
return RpcRunResult(data["result"], data["error"])
251+
return RpcRunResult(result, "", error)
252+
return RpcRunResult(result, error)
250253

251-
return RpcRunResult(data["result"], "")
254+
return RpcRunResult(result, "")
252255

253256

254257
def shutdown_json_rpc():

bundled/tool/runner.py renamed to bundled/tool/lsp_runner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def update_sys_path(path_to_add: str, strategy: str) -> None:
3030

3131

3232
# pylint: disable=wrong-import-position,import-error
33-
import jsonrpc
34-
import utils
33+
import lsp_jsonrpc as jsonrpc
34+
import lsp_utils as utils
3535

3636
RPC = jsonrpc.create_json_rpc(sys.stdin.buffer, sys.stdout.buffer)
3737

@@ -55,7 +55,7 @@ def update_sys_path(path_to_add: str, strategy: str) -> None:
5555
# If your tool supports a programmatic API then replace the function below
5656
# with code for your tool. You can also use `utils.run_api` helper, which
5757
# handles changing working directories, managing io streams, etc.
58-
# Also update `_run_tool_on_document` and `_run_tool` functions in `server.py`.
58+
# Also update `_run_tool_on_document` and `_run_tool` functions in `lsp_server.py`.
5959
result = utils.run_module(
6060
module=msg["module"],
6161
argv=msg["argv"],

0 commit comments

Comments
 (0)