Skip to content

Commit 256d7aa

Browse files
committed
fix(langserver): simplify code for variables and library completions
1 parent 4c2d1f7 commit 256d7aa

File tree

2 files changed

+56
-107
lines changed

2 files changed

+56
-107
lines changed

packages/language_server/src/robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ async def get_libdoc_for_resource_import(
13601360

13611361
async def complete_library_import(
13621362
self, name: Optional[str], base_dir: str = ".", variables: Optional[Dict[str, Any]] = None
1363-
) -> Optional[List[CompleteResult]]:
1363+
) -> List[CompleteResult]:
13641364
return complete_library_import(
13651365
name,
13661366
str(self.folder.to_path()),

packages/language_server/src/robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 55 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,44 +2155,33 @@ def iter_module_names(name: Optional[str] = None) -> Iterator[str]:
21552155
yield e.name
21562156

21572157

2158-
NOT_WANTED_FILE_EXTENSIONS = [".dist-info"]
2158+
NOT_WANTED_DIR_EXTENSIONS = [".dist-info"]
21592159

21602160

21612161
def iter_modules_from_python_path(
21622162
path: Optional[str] = None,
21632163
) -> Iterator[CompleteResult]:
2164-
allow_modules = True if not path or not ("/" in path or os.sep in path) else False
2165-
allow_files = True if not path or "/" in path or os.sep in path else False
2166-
21672164
path = path.replace(".", os.sep) if path is not None and not path.startswith((".", "/", os.sep)) else path
21682165

2169-
needs_init = False
21702166
if path is None:
21712167
paths = sys.path
21722168
else:
21732169
paths = [str(Path(s, path)) for s in sys.path]
2174-
needs_init = True
21752170

21762171
for e in [Path(p) for p in set(paths)]:
21772172
if e.is_dir():
2178-
if needs_init and not (e / "__init__.py").is_file():
2179-
continue
2180-
21812173
for f in e.iterdir():
21822174
if not f.name.startswith(("_", ".")) and (
21832175
f.is_file()
21842176
and f.suffix in ALLOWED_LIBRARY_FILE_EXTENSIONS
21852177
or f.is_dir()
2186-
and f.suffix not in NOT_WANTED_FILE_EXTENSIONS
2178+
and f.suffix not in NOT_WANTED_DIR_EXTENSIONS
21872179
):
21882180
if f.is_dir():
21892181
yield CompleteResult(f.name, CompleteResultKind.MODULE)
21902182

21912183
if f.is_file():
2192-
if allow_modules:
2193-
yield CompleteResult(f.stem, CompleteResultKind.MODULE)
2194-
if allow_files:
2195-
yield CompleteResult(f.name, CompleteResultKind.FILE)
2184+
yield CompleteResult(f.stem, CompleteResultKind.MODULE)
21962185

21972186

21982187
def complete_library_import(
@@ -2201,7 +2190,7 @@ def complete_library_import(
22012190
base_dir: str = ".",
22022191
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
22032192
variables: Optional[Dict[str, Optional[Any]]] = None,
2204-
) -> Optional[List[CompleteResult]]:
2193+
) -> List[CompleteResult]:
22052194
_update_env(working_dir)
22062195

22072196
result: List[CompleteResult] = []
@@ -2225,23 +2214,32 @@ def complete_library_import(
22252214

22262215
if name is None or file_like:
22272216
name_path = Path(name if name else base_dir)
2228-
if name_path.is_absolute():
2229-
path = name_path
2217+
if name and name_path.is_absolute():
2218+
paths = [name_path]
22302219
else:
2231-
path = Path(base_dir, name) if name else Path(base_dir)
2220+
paths = [
2221+
Path(base_dir, name) if name else Path(base_dir),
2222+
*((Path(s) for s in sys.path) if not name else []),
2223+
*((Path(s, name) for s in sys.path) if name and not name.startswith(".") else []),
2224+
]
22322225

2233-
path = path.resolve()
2226+
for p in paths:
2227+
path = p.resolve()
22342228

2235-
if path.exists() and path.is_dir():
2236-
result += [
2237-
CompleteResult(
2238-
str(f.name),
2239-
CompleteResultKind.FILE if f.is_file() else CompleteResultKind.FOLDER,
2240-
)
2241-
for f in path.iterdir()
2242-
if not f.name.startswith(("_", "."))
2243-
and (f.is_dir() or (f.is_file() and f.suffix in ALLOWED_LIBRARY_FILE_EXTENSIONS))
2244-
]
2229+
if path.exists() and path.is_dir():
2230+
result += [
2231+
CompleteResult(
2232+
str(f.name),
2233+
CompleteResultKind.FILE if f.is_file() else CompleteResultKind.FOLDER,
2234+
)
2235+
for f in path.iterdir()
2236+
if not f.name.startswith(("_", "."))
2237+
and (
2238+
(f.is_file() and f.suffix in ALLOWED_LIBRARY_FILE_EXTENSIONS)
2239+
or f.is_dir()
2240+
and f.suffix not in NOT_WANTED_DIR_EXTENSIONS
2241+
)
2242+
]
22452243

22462244
return list(set(result))
22472245

@@ -2261,7 +2259,7 @@ def iter_resources_from_python_path(
22612259
f.is_file()
22622260
and f.suffix in ALLOWED_RESOURCE_FILE_EXTENSIONS
22632261
or f.is_dir()
2264-
and f.suffix not in NOT_WANTED_FILE_EXTENSIONS
2262+
and f.suffix not in NOT_WANTED_DIR_EXTENSIONS
22652263
):
22662264
yield CompleteResult(
22672265
f.name,
@@ -2309,66 +2307,6 @@ def complete_resource_import(
23092307
return list(set(result))
23102308

23112309

2312-
def iter_variables_from_python_path(
2313-
path: Optional[str] = None,
2314-
) -> Iterator[CompleteResult]:
2315-
if get_robot_version() >= (5, 0):
2316-
allow_modules = True if not path or not ("/" in path or os.sep in path) else False
2317-
allow_files = True if not path or "/" in path or os.sep in path else False
2318-
2319-
path = path.replace(".", os.sep) if path is not None and not path.startswith((".", "/", os.sep)) else path
2320-
2321-
needs_init = False
2322-
if path is None:
2323-
paths = sys.path
2324-
else:
2325-
paths = [str(Path(s, path)) for s in sys.path]
2326-
needs_init = True
2327-
2328-
for e in [Path(p) for p in set(paths)]:
2329-
if e.is_dir():
2330-
for f in e.iterdir():
2331-
if needs_init and not (e / "__init__.py").is_file():
2332-
continue
2333-
if not f.name.startswith(("_", ".")) and (
2334-
f.is_file()
2335-
and f.suffix in ALLOWED_VARIABLES_FILE_EXTENSIONS
2336-
or f.is_dir()
2337-
and f.suffix not in NOT_WANTED_FILE_EXTENSIONS
2338-
):
2339-
if f.is_dir():
2340-
yield CompleteResult(f.name, CompleteResultKind.MODULE)
2341-
2342-
if f.is_file():
2343-
if allow_modules and f.suffix.lower() not in [
2344-
".yaml",
2345-
".yml",
2346-
*[".json" if get_robot_version() >= (6, 1) else []],
2347-
]:
2348-
yield CompleteResult(f.stem, CompleteResultKind.VARIABLES_MODULE)
2349-
if allow_files:
2350-
yield CompleteResult(f.name, CompleteResultKind.VARIABLES)
2351-
else:
2352-
if path is None:
2353-
paths = sys.path
2354-
else:
2355-
paths = [str(Path(s, path)) for s in sys.path]
2356-
2357-
for e in [Path(p) for p in set(paths)]:
2358-
if e.is_dir():
2359-
for f in e.iterdir():
2360-
if not f.name.startswith(("_", ".")) and (
2361-
f.is_file()
2362-
and f.suffix in ALLOWED_VARIABLES_FILE_EXTENSIONS
2363-
or f.is_dir()
2364-
and f.suffix not in NOT_WANTED_FILE_EXTENSIONS
2365-
):
2366-
yield CompleteResult(
2367-
f.name,
2368-
CompleteResultKind.VARIABLES if f.is_file() else CompleteResultKind.FOLDER,
2369-
)
2370-
2371-
23722310
def complete_variables_import(
23732311
name: Optional[str],
23742312
working_dir: str = ".",
@@ -2385,29 +2323,40 @@ def complete_variables_import(
23852323

23862324
name = robot_variables.replace_string(name, ignore_errors=True)
23872325

2388-
file_like = is_file_like(name)
2326+
file_like = get_robot_version() < (5, 0) or is_file_like(name)
23892327

2390-
if name is None or not file_like:
2391-
result += list(iter_variables_from_python_path(name))
2328+
if get_robot_version() >= (5, 0) and (name is None or not file_like):
2329+
result += list(iter_modules_from_python_path(name))
23922330

23932331
if name is None or file_like:
23942332
name_path = Path(name if name else base_dir)
2395-
if name_path.is_absolute():
2396-
path = name_path.resolve()
2333+
if name and name_path.is_absolute():
2334+
paths = [name_path]
23972335
else:
2398-
path = Path(base_dir, name if name else base_dir).resolve()
2399-
2400-
if path.exists() and (path.is_dir()):
2401-
result += [
2402-
CompleteResult(
2403-
str(f.name),
2404-
CompleteResultKind.VARIABLES if f.is_file() else CompleteResultKind.FOLDER,
2405-
)
2406-
for f in path.iterdir()
2407-
if not f.name.startswith(("_", "."))
2408-
and (f.is_dir() or (f.is_file() and f.suffix in ALLOWED_VARIABLES_FILE_EXTENSIONS))
2336+
paths = [
2337+
Path(base_dir, name) if name else Path(base_dir),
2338+
*((Path(s) for s in sys.path) if not name else []),
2339+
*((Path(s, name) for s in sys.path) if name and not name.startswith(".") else []),
24092340
]
24102341

2342+
for p in paths:
2343+
path = p.resolve()
2344+
2345+
if path.exists() and path.is_dir():
2346+
result += [
2347+
CompleteResult(
2348+
str(f.name),
2349+
CompleteResultKind.FILE if f.is_file() else CompleteResultKind.FOLDER,
2350+
)
2351+
for f in path.iterdir()
2352+
if not f.name.startswith(("_", "."))
2353+
and (
2354+
(f.is_file() and f.suffix in ALLOWED_VARIABLES_FILE_EXTENSIONS)
2355+
or f.is_dir()
2356+
and f.suffix not in NOT_WANTED_DIR_EXTENSIONS
2357+
)
2358+
]
2359+
24112360
return list(set(result))
24122361

24132362

0 commit comments

Comments
 (0)