Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virtual_documents customization #416

Merged
merged 8 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## CHANGELOG

### `jupyter-lsp 0.9.3` (???)

- features

- The virtual document folder can be configure with `JP_LSP_VIRTUAL_DIR` or
`LanguageServerManager.virtual_documents_dir`. Its default value is kept
unchanged: _contents.root_dir_ / `.virtual_documents` ([#416])

[#416]: https://github.com/krassowski/jupyterlab-lsp/issues/416

### `@krassowski/jupyterlab-lsp 2.0.9` (???)

- bug fixes
Expand Down
15 changes: 14 additions & 1 deletion docs/Configuring.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@
"`node_modules`."
]
},
{
"source": [
"#### virtual_documents_dir\n",
"\n",
"> default: os.getenv(\"JP_LSP_VIRTUAL_DIR\", \".virtual_documents\")\n",
"\n",
"Path to virtual documents relative to the content manager root directory.\n",
"\n",
"Its default value can be set with `JP_LSP_VIRTUAL_DIR` environment variable and fallback to `.virtual_documents`."
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -211,4 +224,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
16 changes: 15 additions & 1 deletion py_src/jupyter_lsp/manager.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
""" A configurable frontend for stdio-based Language Servers
"""
import os
import traceback
from typing import Dict, Text, Tuple

import entrypoints
from notebook.transutils import _
from traitlets import Bool, Dict as Dict_, Instance, List as List_, default
from traitlets import Bool, Dict as Dict_, Instance, List as List_, Unicode, default

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

virtual_documents_dir = Unicode(
help="""Path to virtual documents relative to the content manager root
directory.

Its default value can be set with JP_LSP_VIRTUAL_DIR and fallback to
'.virtual_documents'.
"""
).tag(config=True)

all_listeners = List_(trait=LoadableCallable).tag(config=True)
server_listeners = List_(trait=LoadableCallable).tag(config=True)
client_listeners = List_(trait=LoadableCallable).tag(config=True)
Expand All @@ -54,6 +64,10 @@ class LanguageServerManager(LanguageServerManagerAPI):
def _default_language_servers(self):
return {}

@default("virtual_documents_dir")
def _default_virtual_documents_dir(self):
return os.getenv("JP_LSP_VIRTUAL_DIR", ".virtual_documents")

def __init__(self, **kwargs):
"""Before starting, perform all necessary configuration"""
super().__init__(**kwargs)
Expand Down
5 changes: 4 additions & 1 deletion py_src/jupyter_lsp/serverextension.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" add language server support to the running jupyter notebook application
"""
import json
from pathlib import Path

import traitlets

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

virtual_documents_uri = root_uri + "/.virtual_documents"
virtual_documents_uri = normalized_uri(
Path(contents.root_dir) / manager.virtual_documents_dir
)
page_config["virtualDocumentsUri"] = virtual_documents_uri
nbapp.log.debug("[lsp] virtualDocumentsUri will be %s", virtual_documents_uri)
else: # pragma: no cover
Expand Down
29 changes: 29 additions & 0 deletions py_src/jupyter_lsp/tests/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os


def test_serverextension_path(app):
import jupyter_lsp

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

assert found_lsp, "apparently didn't install the /lsp/ route"


def test_default_virtual_documents_dir(app):
app.initialize(
["--NotebookApp.nbserver_extensions={'jupyter_lsp.serverextension': True}"]
)
assert app.language_server_manager.virtual_documents_dir == ".virtual_documents"


def test_virtual_documents_dir_config(app):
custom_dir = ".custom_virtual_dir"
app.initialize(
[
"--NotebookApp.nbserver_extensions={'jupyter_lsp.serverextension': True}",
"--NotebookApp.LanguageServerManager.virtual_documents_dir=" + custom_dir,
]
)
assert app.language_server_manager.virtual_documents_dir == custom_dir


def test_virtual_documents_dir_env(app):
os.environ["JP_LSP_VIRTUAL_DIR"] = custom_dir = ".custom_virtual_dir"
app.initialize(
["--NotebookApp.nbserver_extensions={'jupyter_lsp.serverextension': True}"]
)
assert app.language_server_manager.virtual_documents_dir == custom_dir