lsp: Add code action to add missing imports

This commit is contained in:
James Westman 2023-07-25 20:02:03 -05:00
parent 3bcc9f4cbd
commit 35ee058192
3 changed files with 47 additions and 3 deletions

View file

@ -55,7 +55,16 @@ class TypeName(AstNode):
@validate("namespace")
def gir_ns_exists(self):
if not self.tokens["extern"]:
self.root.gir.validate_ns(self.tokens["namespace"])
try:
self.root.gir.validate_ns(self.tokens["namespace"])
except CompileError as e:
ns = self.tokens["namespace"]
e.actions = [
self.root.import_code_action(n, version)
for n, version in gir.get_available_namespaces()
if n == ns
]
raise e
@validate()
def deprecated(self) -> None:

View file

@ -99,6 +99,18 @@ 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
return CodeAction(
f"Import {ns} {version}",
f"\nusing {ns} {version};",
Range(pos, pos, self.group.text),
)
@context(ScopeCtx)
def scope_ctx(self) -> ScopeCtx:
return ScopeCtx(node=self)