completions: Complete available namespaces

Add completions for namespaces in the typelib path that can be imported.
Accepting the completion automatically adds an import statement.
This commit is contained in:
James Westman 2025-01-04 16:05:27 -06:00
parent 461fe25316
commit 21f138fa83
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
3 changed files with 37 additions and 7 deletions

View file

@ -110,16 +110,22 @@ class UI(AstNode):
and self.template.class_name.glib_type_name == id
)
def import_code_action(self, ns: str, version: str) -> CodeAction:
if len(self.children[Import]):
pos = self.children[Import][-1].range.end
else:
pos = self.children[GtkDirective][0].range.end
def import_range(self, ns: str):
"""Returns a range to insert a new import statement"""
pos = self.children[GtkDirective][0].range.end
# try to insert alphabetically
for import_ in self.children[Import]:
if ns.lower() > import_.namespace.lower():
pos = import_.range.end
return Range(pos, pos, self.group.text)
def import_code_action(self, ns: str, version: str) -> CodeAction:
return CodeAction(
f"Import {ns} {version}",
f"\nusing {ns} {version};",
Range(pos, pos, self.group.text),
self.import_range(ns),
)
@cached_property