mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-12 01:24:17 +00:00
jedi: add update_user_modules() function
This function will be used to fix import completion of user module names in Pybricks Code. Issue: https://github.com/pybricks/support/issues/759
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Added
|
||||
- Added `update_user_modules()` function for filtering on user modules.
|
||||
|
||||
### Fixed
|
||||
- Fixed code completion for builtin types.
|
||||
- Fixed code completion for names starting with `_`.
|
||||
|
||||
@@ -2,6 +2,7 @@ import io
|
||||
import json
|
||||
import re
|
||||
from enum import IntEnum
|
||||
from typing import Iterable
|
||||
|
||||
import docstring_parser
|
||||
import jedi
|
||||
@@ -176,6 +177,8 @@ PYBRICKS_TYPING = {
|
||||
|
||||
MICROPY_NOT_SUPPORTED_DUNDER = {"__doc__", "__package__"}
|
||||
|
||||
user_modules = set()
|
||||
|
||||
# Types from monaco editor
|
||||
|
||||
|
||||
@@ -326,7 +329,7 @@ def _is_pybricks(c: Completion) -> bool:
|
||||
|
||||
# filter out packages/modules that are not included in Pybricks firmware
|
||||
if c.type == "module" or c.type == "namespace":
|
||||
return c.full_name in PYBRICKS_CODE_PACKAGES
|
||||
return c.full_name in PYBRICKS_CODE_PACKAGES or c.full_name in user_modules
|
||||
|
||||
# filter subset of builtins
|
||||
if c.module_name == "builtins" and c.type != "keyword":
|
||||
@@ -541,3 +544,15 @@ def get_signatures(code: str, line: int, column: int) -> str:
|
||||
"""
|
||||
signatures = jedi.Script(code).get_signatures(line, column - 1)
|
||||
return json.dumps(_map_signatures(signatures))
|
||||
|
||||
|
||||
def update_user_modules(names: Iterable[str]) -> None:
|
||||
"""
|
||||
Updates the set of user module names used for filtering.
|
||||
|
||||
Args:
|
||||
names:
|
||||
An iterable of module names.
|
||||
"""
|
||||
user_modules.clear()
|
||||
user_modules.update(names)
|
||||
|
||||
@@ -6,7 +6,9 @@ Tests for correct code completion of import statements.
|
||||
"""
|
||||
|
||||
import json
|
||||
from pybricks_jedi import CompletionItem, complete
|
||||
|
||||
import pytest
|
||||
from pybricks_jedi import CompletionItem, complete, update_user_modules
|
||||
|
||||
|
||||
def test_from():
|
||||
@@ -27,6 +29,32 @@ def test_from():
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_modules():
|
||||
update_user_modules(["jedi", "pytest"])
|
||||
yield
|
||||
update_user_modules([])
|
||||
|
||||
|
||||
def test_from_with_user_modules(user_modules):
|
||||
code = "from "
|
||||
completions: list[CompletionItem] = json.loads(complete(code, 1, len(code) + 1))
|
||||
assert [c["insertText"] for c in completions] == [
|
||||
"jedi",
|
||||
"micropython",
|
||||
"pybricks",
|
||||
"pytest",
|
||||
"uerrno",
|
||||
"uio",
|
||||
"ujson",
|
||||
"umath",
|
||||
"urandom",
|
||||
"uselect",
|
||||
"ustruct",
|
||||
"usys",
|
||||
]
|
||||
|
||||
|
||||
def test_from_pybricks_import():
|
||||
code = "from pybricks import "
|
||||
completions: list[CompletionItem] = json.loads(complete(code, 1, len(code) + 1))
|
||||
|
||||
Reference in New Issue
Block a user