Skip to content

Commit

Permalink
Ensure domain directives are discovered
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Dec 8, 2020
1 parent 8fdb42d commit 0d39ef1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/esbonio/changes/31.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**Language Server** Directives that are part of the ``std`` or ``py`` Sphinx domains
will now be included in completion suggestions
1 change: 0 additions & 1 deletion lib/esbonio/esbonio/lsp/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pygls.types import (
CompletionList,
CompletionItem,
CompletionItemKind,
CompletionParams,
Position,
)
Expand Down
22 changes: 19 additions & 3 deletions lib/esbonio/esbonio/lsp/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ def discover_completion_items(rst: RstLanguageServer):
- roles
- directives
"""
# Lookup the directives and roles that have been registered
dirs = {**directives._directive_registry, **directives._directives}
rst.directives = {k: completion_from_directive(k, v) for k, v in dirs.items()}

rst.directives = {
k: completion_from_directive(k, v)
for k, v in discover_directives(rst.app).items()
}

rst.roles = {
k: completion_from_role(k, v) for k, v in discover_roles(rst.app).items()
Expand All @@ -53,6 +55,20 @@ def discover_completion_items(rst: RstLanguageServer):
rst.logger.debug("Discovered %s roles", len(rst.roles))


def discover_directives(app: Sphinx):
"""Discover directives that we can offer as autocomplete suggestions."""
# Lookup the directives and roles that have been registered
dirs = {**directives._directive_registry, **directives._directives}

# Don't forget to include the directives that are stored under Sphinx domains.
# TODO: Implement proper domain handling, focus on std + python for now.
domains = app.registry.domains
std_directives = domains["std"].directives
py_directives = domains["py"].directives

return {**dirs, **std_directives, **py_directives}


def discover_roles(app: Sphinx):
"""Discover roles that we can offer as autocomplete suggestions."""
# Pull out the roles that are available via docutils.
Expand Down
32 changes: 32 additions & 0 deletions lib/esbonio/tests/lsp/test_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from esbonio.lsp.completion import completions
from esbonio.lsp.initialize import (
discover_directives,
discover_roles,
discover_target_types,
discover_targets,
Expand Down Expand Up @@ -189,6 +190,37 @@ def test_role_discovery(sphinx, project, expected, unexpected):
assert name not in roles.keys(), "Unexpected role '{}'".format(name)


@py.test.mark.parametrize(
"project,expected,unexpected",
[
(
"sphinx-default",
[
"figure",
"function",
"glossary",
"image",
"list-table",
"module",
"toctree",
],
["testcode", "autoclass", "automodule"],
)
],
)
def test_directive_discovery(sphinx, project, expected, unexpected):
"""Ensure that we can discover directives to offer as completion suggestions."""

app = sphinx(project)
directives = discover_directives(app)

for name in expected:
assert name in directives.keys(), "Missing expected directive '{}'".format(name)

for name in unexpected:
assert name not in directives.keys(), "Unexpected directive '{}'".format(name)


@py.test.mark.parametrize(
"project,type,kind,expected",
[
Expand Down

0 comments on commit 0d39ef1

Please sign in to comment.