mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
lsp: Add code action to add missing imports
This commit is contained in:
parent
3bcc9f4cbd
commit
35ee058192
3 changed files with 47 additions and 3 deletions
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue