Skip to content

Commit 93c4fcc

Browse files
committed
jedi: add update_user_modules() function
This function will be used to fix import completion of user module names in Pybricks Code. Issue: pybricks/support#759
1 parent aa72605 commit 93c4fcc

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

jedi/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
## Unreleased
66

7+
### Added
8+
- Added `update_user_modules()` function for filtering on user modules.
9+
710
### Fixed
811
- Fixed code completion for builtin types.
912
- Fixed code completion for names starting with `_`.

jedi/src/pybricks_jedi/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import re
44
from enum import IntEnum
5+
from typing import Iterable
56

67
import docstring_parser
78
import jedi
@@ -176,6 +177,8 @@
176177

177178
MICROPY_NOT_SUPPORTED_DUNDER = {"__doc__", "__package__"}
178179

180+
user_modules = set()
181+
179182
# Types from monaco editor
180183

181184

@@ -326,7 +329,7 @@ def _is_pybricks(c: Completion) -> bool:
326329

327330
# filter out packages/modules that are not included in Pybricks firmware
328331
if c.type == "module" or c.type == "namespace":
329-
return c.full_name in PYBRICKS_CODE_PACKAGES
332+
return c.full_name in PYBRICKS_CODE_PACKAGES or c.full_name in user_modules
330333

331334
# filter subset of builtins
332335
if c.module_name == "builtins" and c.type != "keyword":
@@ -541,3 +544,15 @@ def get_signatures(code: str, line: int, column: int) -> str:
541544
"""
542545
signatures = jedi.Script(code).get_signatures(line, column - 1)
543546
return json.dumps(_map_signatures(signatures))
547+
548+
549+
def update_user_modules(names: Iterable[str]) -> None:
550+
"""
551+
Updates the set of user module names used for filtering.
552+
553+
Args:
554+
names:
555+
An iterable of module names.
556+
"""
557+
user_modules.clear()
558+
user_modules.update(names)

jedi/tests/test_complete_import.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"""
77

88
import json
9-
from pybricks_jedi import CompletionItem, complete
9+
10+
import pytest
11+
from pybricks_jedi import CompletionItem, complete, update_user_modules
1012

1113

1214
def test_from():
@@ -27,6 +29,32 @@ def test_from():
2729
]
2830

2931

32+
@pytest.fixture
33+
def user_modules():
34+
update_user_modules(["jedi", "pytest"])
35+
yield
36+
update_user_modules([])
37+
38+
39+
def test_from_with_user_modules(user_modules):
40+
code = "from "
41+
completions: list[CompletionItem] = json.loads(complete(code, 1, len(code) + 1))
42+
assert [c["insertText"] for c in completions] == [
43+
"jedi",
44+
"micropython",
45+
"pybricks",
46+
"pytest",
47+
"uerrno",
48+
"uio",
49+
"ujson",
50+
"umath",
51+
"urandom",
52+
"uselect",
53+
"ustruct",
54+
"usys",
55+
]
56+
57+
3058
def test_from_pybricks_import():
3159
code = "from pybricks import "
3260
completions: list[CompletionItem] = json.loads(complete(code, 1, len(code) + 1))

0 commit comments

Comments
 (0)