Skip to content

Commit 11ace76

Browse files
authored
virtual_documents customization (#416)
* virtual_documents customization Fixes #353 * Update doc and changelog * Fix prettier * Apply review suggestions * Remove os * Flake8 correction * Restore os * Remove os
1 parent 4830b98 commit 11ace76

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
## CHANGELOG
22

3+
### `jupyter-lsp 0.9.3` (???)
4+
5+
- features
6+
7+
- The virtual document folder can be configure with `JP_LSP_VIRTUAL_DIR` or
8+
`LanguageServerManager.virtual_documents_dir`. Its default value is kept
9+
unchanged: _contents.root_dir_ / `.virtual_documents` ([#416])
10+
11+
[#416]: https://github.com/krassowski/jupyterlab-lsp/issues/416
12+
313
### `@krassowski/jupyterlab-lsp 2.0.9` (???)
414

515
- bug fixes

docs/Configuring.ipynb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@
174174
"`node_modules`."
175175
]
176176
},
177+
{
178+
"source": [
179+
"#### virtual_documents_dir\n",
180+
"\n",
181+
"> default: os.getenv(\"JP_LSP_VIRTUAL_DIR\", \".virtual_documents\")\n",
182+
"\n",
183+
"Path to virtual documents relative to the content manager root directory.\n",
184+
"\n",
185+
"Its default value can be set with `JP_LSP_VIRTUAL_DIR` environment variable and fallback to `.virtual_documents`."
186+
],
187+
"cell_type": "markdown",
188+
"metadata": {}
189+
},
177190
{
178191
"cell_type": "markdown",
179192
"metadata": {},
@@ -211,4 +224,4 @@
211224
},
212225
"nbformat": 4,
213226
"nbformat_minor": 4
214-
}
227+
}

py_src/jupyter_lsp/manager.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
""" A configurable frontend for stdio-based Language Servers
22
"""
3+
import os
34
import traceback
45
from typing import Dict, Text, Tuple
56

67
import entrypoints
78
from notebook.transutils import _
8-
from traitlets import Bool, Dict as Dict_, Instance, List as List_, default
9+
from traitlets import Bool, Dict as Dict_, Instance, List as List_, Unicode, default
910

1011
from .constants import (
1112
EP_LISTENER_ALL_V1,
@@ -46,6 +47,15 @@ class LanguageServerManager(LanguageServerManagerAPI):
4647
help="sessions keyed by language server name",
4748
) # type: Dict[Tuple[Text], LanguageServerSession]
4849

50+
virtual_documents_dir = Unicode(
51+
help="""Path to virtual documents relative to the content manager root
52+
directory.
53+
54+
Its default value can be set with JP_LSP_VIRTUAL_DIR and fallback to
55+
'.virtual_documents'.
56+
"""
57+
).tag(config=True)
58+
4959
all_listeners = List_(trait=LoadableCallable).tag(config=True)
5060
server_listeners = List_(trait=LoadableCallable).tag(config=True)
5161
client_listeners = List_(trait=LoadableCallable).tag(config=True)
@@ -54,6 +64,10 @@ class LanguageServerManager(LanguageServerManagerAPI):
5464
def _default_language_servers(self):
5565
return {}
5666

67+
@default("virtual_documents_dir")
68+
def _default_virtual_documents_dir(self):
69+
return os.getenv("JP_LSP_VIRTUAL_DIR", ".virtual_documents")
70+
5771
def __init__(self, **kwargs):
5872
"""Before starting, perform all necessary configuration"""
5973
super().__init__(**kwargs)

py_src/jupyter_lsp/serverextension.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" add language server support to the running jupyter notebook application
22
"""
33
import json
4+
from pathlib import Path
45

56
import traitlets
67

@@ -25,7 +26,9 @@ def load_jupyter_server_extension(nbapp):
2526
page_config["rootUri"] = root_uri
2627
nbapp.log.debug("[lsp] rootUri will be %s", root_uri)
2728

28-
virtual_documents_uri = root_uri + "/.virtual_documents"
29+
virtual_documents_uri = normalized_uri(
30+
Path(contents.root_dir) / manager.virtual_documents_dir
31+
)
2932
page_config["virtualDocumentsUri"] = virtual_documents_uri
3033
nbapp.log.debug("[lsp] virtualDocumentsUri will be %s", virtual_documents_uri)
3134
else: # pragma: no cover

py_src/jupyter_lsp/tests/test_extension.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
3+
14
def test_serverextension_path(app):
25
import jupyter_lsp
36

@@ -18,3 +21,29 @@ def test_serverextension(app):
1821
found_lsp = True
1922

2023
assert found_lsp, "apparently didn't install the /lsp/ route"
24+
25+
26+
def test_default_virtual_documents_dir(app):
27+
app.initialize(
28+
["--NotebookApp.nbserver_extensions={'jupyter_lsp.serverextension': True}"]
29+
)
30+
assert app.language_server_manager.virtual_documents_dir == ".virtual_documents"
31+
32+
33+
def test_virtual_documents_dir_config(app):
34+
custom_dir = ".custom_virtual_dir"
35+
app.initialize(
36+
[
37+
"--NotebookApp.nbserver_extensions={'jupyter_lsp.serverextension': True}",
38+
"--NotebookApp.LanguageServerManager.virtual_documents_dir=" + custom_dir,
39+
]
40+
)
41+
assert app.language_server_manager.virtual_documents_dir == custom_dir
42+
43+
44+
def test_virtual_documents_dir_env(app):
45+
os.environ["JP_LSP_VIRTUAL_DIR"] = custom_dir = ".custom_virtual_dir"
46+
app.initialize(
47+
["--NotebookApp.nbserver_extensions={'jupyter_lsp.serverextension': True}"]
48+
)
49+
assert app.language_server_manager.virtual_documents_dir == custom_dir

0 commit comments

Comments
 (0)