jedi: fix code completion for local _*

This fixes code completion for identifier names starting with "_" in
the local ("__main__") file.
This commit is contained in:
David Lechner
2022-12-28 15:33:33 -06:00
parent 8ee725722f
commit aa72605e99
4 changed files with 43 additions and 3 deletions
+1
View File
@@ -6,6 +6,7 @@
### Fixed
- Fixed code completion for builtin types.
- Fixed code completion for names starting with `_`.
## 1.6.0 - 2022-12-09
+9 -3
View File
@@ -174,6 +174,8 @@ PYBRICKS_TYPING = {
"typing.Mapping.get",
}
MICROPY_NOT_SUPPORTED_DUNDER = {"__doc__", "__package__"}
# Types from monaco editor
@@ -302,10 +304,14 @@ class SignatureHelp(TypedDict):
def _is_pybricks(c: Completion) -> bool:
# filter all "private" names (leading underscore)
if (isinstance(c.name, str)) and c.name.startswith("_"):
return False
if c.name is not None:
if c.name.startswith("_") and c.module_name != "__main__":
return False
if isinstance(c.full_name, str):
if c.name in MICROPY_NOT_SUPPORTED_DUNDER:
return False
if c.full_name is not None:
# this catches things like `from __future__ import annotations`
if c.full_name.startswith("_") and c.module_name != "__main__":
return False
+1
View File
@@ -126,6 +126,7 @@ def test_empty_code():
"yield",
"ZeroDivisionError",
"zip",
"__name__",
]
+32
View File
@@ -0,0 +1,32 @@
import json
from pybricks_jedi import CompletionItem, complete
def test_get_completion_for_private_globals():
code = """
_X = 0
_
"""
completions: list[CompletionItem] = json.loads(complete(code, 4, 2))
assert [c["insertText"] for c in completions] == ["_X", "__name__"]
def test_get_completion_for_private_attributes():
code = """
class X:
def __init__(self):
self.public = 0
self._protected = 0
self.__private = 0
x = X()
x.
"""
completions: list[CompletionItem] = json.loads(complete(code, 10, 3))
assert [c["insertText"] for c in completions] == [
"public",
"_protected",
"__init__",
]