diff --git a/blueprintcompiler/completions.py b/blueprintcompiler/completions.py index f1c17b6..a4e86b9 100644 --- a/blueprintcompiler/completions.py +++ b/blueprintcompiler/completions.py @@ -23,7 +23,7 @@ from . import annotations, gir, language from .ast_utils import AstNode from .completions_utils import * from .language.types import ClassName -from .lsp_utils import Completion, CompletionItemKind +from .lsp_utils import Completion, CompletionItemKind, TextEdit, get_docs_section from .parser import SKIP_TOKENS from .tokenizer import Token, TokenType @@ -55,7 +55,7 @@ def _complete( token_idx -= 1 for completer in ast_node.completers: - yield from completer(prev_tokens, next_token, ast_node, lsp) + yield from completer(prev_tokens, next_token, ast_node, lsp, idx) def complete( @@ -90,20 +90,94 @@ def using_gtk(_ctx: CompletionContext): ) +@completer([language.UI]) +def using(ctx: CompletionContext): + imported_namespaces = set( + [import_.namespace for import_ in ctx.ast_node.root.using] + ) + + # Import statements must be before any content + for i in ctx.ast_node.root.children: + if not isinstance(i, language.GtkDirective) and not isinstance( + i, language.Import + ): + if ctx.index >= i.range.end: + return + + for ns, version in gir.get_available_namespaces(): + if ns not in imported_namespaces and ns != "Gtk": + yield Completion( + f"using {ns} {version}", + CompletionItemKind.Module, + text=f"using {ns} {version};", + sort_text=get_sort_key(CompletionPriority.NAMESPACE, ns), + ) + + +@completer([language.UI]) +def translation_domain(ctx: CompletionContext): + if ctx.ast_node.root.translation_domain is not None: + return + + # Translation domain must be after the import statements but before any content + for i in ctx.ast_node.root.children: + if isinstance(i, language.Import): + if ctx.index <= i.range.start: + return + elif not isinstance(i, language.GtkDirective): + if ctx.index >= i.range.end: + return + + yield Completion( + "translation-domain", + CompletionItemKind.Keyword, + sort_text=get_sort_key(CompletionPriority.KEYWORD, "translation-domain"), + snippet='translation-domain "$0";', + docs=get_docs_section("Syntax TranslationDomain"), + ) + + +def _available_namespace_completions(ctx: CompletionContext): + imported_namespaces = set( + [import_.namespace for import_ in ctx.ast_node.root.using] + ) + + for ns, version in gir.get_available_namespaces(): + if ns not in imported_namespaces and ns != "Gtk": + yield Completion( + ns, + CompletionItemKind.Module, + text=ns + ".", + sort_text=get_sort_key(CompletionPriority.IMPORT_NAMESPACE, ns), + signature=f" using {ns} {version}", + additional_text_edits=[ + TextEdit( + ctx.ast_node.root.import_range(ns), f"\nusing {ns} {version};" + ) + ], + ) + + @completer( applies_in=[language.UI, language.ObjectContent, language.Template], matches=new_statement_patterns, ) def namespace(ctx: CompletionContext): yield Completion("Gtk", CompletionItemKind.Module, text="Gtk.") + for ns in ctx.ast_node.root.children[language.Import]: if ns.gir_namespace is not None: yield Completion( ns.gir_namespace.name, CompletionItemKind.Module, text=ns.gir_namespace.name + ".", + sort_text=get_sort_key( + CompletionPriority.NAMESPACE, ns.gir_namespace.name + ), ) + yield from _available_namespace_completions(ctx) + @completer( applies_in=[language.UI, language.ObjectContent, language.Template], @@ -123,6 +197,7 @@ def object_completer(ctx: CompletionContext): yield Completion( c.name, CompletionItemKind.Class, + sort_text=get_sort_key(CompletionPriority.CLASS, c.name), snippet=snippet, docs=c.doc, detail=c.detail, @@ -144,6 +219,7 @@ def gtk_object_completer(ctx: CompletionContext): yield Completion( c.name, CompletionItemKind.Class, + sort_text=get_sort_key(CompletionPriority.CLASS, c.name), snippet=snippet, docs=c.doc, detail=c.detail, @@ -158,38 +234,12 @@ def property_completer(ctx: CompletionContext): assert isinstance(ctx.ast_node, language.ObjectContent) if ctx.ast_node.gir_class and hasattr(ctx.ast_node.gir_class, "properties"): for prop_name, prop in ctx.ast_node.gir_class.properties.items(): - if str(ctx.next_token) == ":": - snippet = prop_name - elif ( - isinstance(prop.type, gir.BoolType) - and ctx.client_supports_completion_choice - ): - snippet = f"{prop_name}: ${{1|true,false|}};" - elif isinstance(prop.type, gir.StringType): - snippet = ( - f'{prop_name}: _("$0");' - if annotations.is_property_translated(prop) - else f'{prop_name}: "$0";' - ) - elif ( - isinstance(prop.type, gir.Enumeration) - and len(prop.type.members) <= 10 - and ctx.client_supports_completion_choice - ): - choices = ",".join(prop.type.members.keys()) - snippet = f"{prop_name}: ${{1|{choices}|}};" - elif prop.type.full_name == "Gtk.Expression": - snippet = f"{prop_name}: expr $0;" - else: - snippet = f"{prop_name}: $0;" - - yield Completion( + yield get_property_completion( prop_name, - CompletionItemKind.Property, - sort_text=f"0 {prop_name}", - snippet=snippet, - docs=prop.doc, - detail=prop.detail, + prop, + ctx, + annotations.is_property_translated(prop), + prop.doc, ) @@ -198,6 +248,15 @@ def property_completer(ctx: CompletionContext): matches=[[(TokenType.IDENT, None), (TokenType.OP, ":")]], ) def prop_value_completer(ctx: CompletionContext): + if isinstance(ctx.ast_node, language.Property): + yield Completion( + "bind", + CompletionItemKind.Keyword, + snippet="bind $0", + docs=get_docs_section("Syntax Binding"), + sort_text=get_sort_key(CompletionPriority.KEYWORD, "bind"), + ) + assert isinstance(ctx.ast_node, language.Property) or isinstance( ctx.ast_node, language.A11yProperty ) @@ -210,11 +269,61 @@ def prop_value_completer(ctx: CompletionContext): CompletionItemKind.EnumMember, docs=member.doc, detail=member.detail, + sort_text=get_sort_key(CompletionPriority.ENUM_MEMBER, name), ) elif isinstance(vt.value_type, gir.BoolType): - yield Completion("true", CompletionItemKind.Constant) - yield Completion("false", CompletionItemKind.Constant) + yield Completion( + "true", + CompletionItemKind.Constant, + sort_text=get_sort_key(CompletionPriority.ENUM_MEMBER, "true"), + ) + yield Completion( + "false", + CompletionItemKind.Constant, + sort_text=get_sort_key(CompletionPriority.ENUM_MEMBER, "false"), + ) + + elif isinstance(vt.value_type, gir.Class) or isinstance( + vt.value_type, gir.Interface + ): + yield Completion( + "null", + CompletionItemKind.Constant, + sort_text=get_sort_key(CompletionPriority.KEYWORD, "null"), + ) + + for id, obj in ctx.ast_node.root.context[language.ScopeCtx].objects.items(): + if obj.gir_class is not None and obj.gir_class.assignable_to( + vt.value_type + ): + yield Completion( + id, + CompletionItemKind.Variable, + signature=" " + obj.signature, + sort_text=get_sort_key(CompletionPriority.NAMED_OBJECT, id), + ) + + if isinstance(ctx.ast_node, language.Property): + yield from _available_namespace_completions(ctx) + + for ns in ctx.ast_node.root.gir.namespaces.values(): + for c in ns.classes.values(): + if not c.abstract and c.assignable_to(vt.value_type): + name = ( + c.name if ns.name == "Gtk" else ns.name + "." + c.name + ) + snippet = name + if str(ctx.next_token) != "{": + snippet += " {\n $0\n}" + yield Completion( + name, + CompletionItemKind.Class, + sort_text=get_sort_key(CompletionPriority.CLASS, name), + snippet=snippet, + detail=c.detail, + docs=c.doc, + ) @completer( @@ -244,7 +353,7 @@ def signal_completer(ctx: CompletionContext): yield Completion( signal_name, CompletionItemKind.Event, - sort_text=f"1 {signal_name}", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, signal_name), snippet=snippet, docs=signal.doc, detail=signal.detail, diff --git a/blueprintcompiler/completions_utils.py b/blueprintcompiler/completions_utils.py index effb152..bfca55a 100644 --- a/blueprintcompiler/completions_utils.py +++ b/blueprintcompiler/completions_utils.py @@ -20,18 +20,36 @@ import typing as T from dataclasses import dataclass +from enum import Enum +from . import gir from .ast_utils import AstNode -from .lsp_utils import Completion +from .lsp_utils import Completion, CompletionItemKind from .tokenizer import Token, TokenType +class CompletionPriority(Enum): + ENUM_MEMBER = "00" + NAMED_OBJECT = "01" + OBJECT_MEMBER = "02" + CLASS = "03" + NAMESPACE = "04" + KEYWORD = "05" + # An available namespace that hasn't been imported yet + IMPORT_NAMESPACE = "99" + + +def get_sort_key(priority: CompletionPriority, name: str): + return f"{priority.value} {name}" + + @dataclass class CompletionContext: client_supports_completion_choice: bool ast_node: AstNode match_variables: T.List[str] next_token: Token + index: int new_statement_patterns = [ @@ -44,7 +62,9 @@ new_statement_patterns = [ def completer(applies_in: T.List, matches: T.List = [], applies_in_subclass=None): def decorator(func: T.Callable[[CompletionContext], T.Generator[Completion]]): - def inner(prev_tokens: T.List[Token], next_token: Token, ast_node, lsp): + def inner( + prev_tokens: T.List[Token], next_token: Token, ast_node, lsp, idx: int + ): # For completers that apply in ObjectContent nodes, we can further # check that the object is the right class if applies_in_subclass is not None: @@ -82,6 +102,7 @@ def completer(applies_in: T.List, matches: T.List = [], applies_in_subclass=None ast_node=ast_node, match_variables=match_variables, next_token=next_token, + index=idx, ) yield from func(context) @@ -90,3 +111,37 @@ def completer(applies_in: T.List, matches: T.List = [], applies_in_subclass=None return inner return decorator + + +def get_property_completion( + name: str, + type: gir.GirType, + ctx: CompletionContext, + translated: bool, + doc: str, +) -> Completion: + if str(ctx.next_token) == ":": + snippet = name + elif isinstance(type, gir.BoolType) and ctx.client_supports_completion_choice: + snippet = f"{name}: ${{1|true,false|}};" + elif isinstance(type, gir.StringType): + snippet = f'{name}: _("$0");' if translated else f'{name}: "$0";' + elif ( + isinstance(type, gir.Enumeration) + and len(type.members) <= 10 + and ctx.client_supports_completion_choice + ): + choices = ",".join(type.members.keys()) + snippet = f"{name}: ${{1|{choices}|}};" + elif type.full_name == "Gtk.Expression": + snippet = f"{name}: expr $0;" + else: + snippet = f"{name}: $0;" + + return Completion( + name, + CompletionItemKind.Property, + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, name), + snippet=snippet, + docs=doc, + ) diff --git a/blueprintcompiler/formatter.py b/blueprintcompiler/formatter.py index 60d87b4..f438675 100644 --- a/blueprintcompiler/formatter.py +++ b/blueprintcompiler/formatter.py @@ -193,6 +193,9 @@ def format(data, tab_size=2, insert_space=True): elif prev_line_type in require_extra_newline: newlines = 2 + current_line = "\n".join( + [line.rstrip() for line in current_line.split("\n")] + ) commit_current_line(LineType.COMMENT, newlines_before=newlines) else: # pragma: no cover diff --git a/blueprintcompiler/language/adw_response_dialog.py b/blueprintcompiler/language/adw_response_dialog.py index 516c69a..c621df0 100644 --- a/blueprintcompiler/language/adw_response_dialog.py +++ b/blueprintcompiler/language/adw_response_dialog.py @@ -156,7 +156,10 @@ def complete_adw_message_dialog(_ctx: CompletionContext): ) def complete_adw_alert_dialog(_ctx: CompletionContext): yield Completion( - "responses", CompletionItemKind.Keyword, snippet="responses [\n\t$0\n]" + "responses", + CompletionItemKind.Keyword, + snippet="responses [\n\t$0\n]", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "responses"), ) diff --git a/blueprintcompiler/language/gobject_property.py b/blueprintcompiler/language/gobject_property.py index 50a7512..67f2555 100644 --- a/blueprintcompiler/language/gobject_property.py +++ b/blueprintcompiler/language/gobject_property.py @@ -26,7 +26,11 @@ from .values import ArrayValue, ExprValue, ObjectValue, Value class Property(AstNode): grammar = Statement( - UseIdent("name"), ":", AnyOf(Binding, ExprValue, ObjectValue, Value, ArrayValue) + UseIdent("name"), + ":", + AnyOf(Binding, ExprValue, ObjectValue, Value, ArrayValue).expected( + "property value" + ), ) @property diff --git a/blueprintcompiler/language/gtk_a11y.py b/blueprintcompiler/language/gtk_a11y.py index 417eaec..7f90b6e 100644 --- a/blueprintcompiler/language/gtk_a11y.py +++ b/blueprintcompiler/language/gtk_a11y.py @@ -25,7 +25,7 @@ from .gobject_object import ObjectContent, validate_parent_type from .values import Value -def get_property_types(gir): +def get_property_types(gir: gir.GirContext) -> T.Dict[str, T.Optional[GirType]]: # from return { "autocomplete": gir.get_type("AccessibleAutocomplete", "Gtk"), @@ -50,7 +50,7 @@ def get_property_types(gir): } -def get_relation_types(gir): +def get_relation_types(gir: gir.GirContext) -> T.Dict[str, T.Optional[GirType]]: # from widget = gir.get_type("Widget", "Gtk") return { @@ -75,7 +75,7 @@ def get_relation_types(gir): } -def get_state_types(gir): +def get_state_types(gir: gir.GirContext) -> T.Dict[str, T.Optional[GirType]]: # from return { "busy": BoolType(), @@ -89,6 +89,20 @@ def get_state_types(gir): } +TRANSLATED = set( + [ + "description", + "help-text", + "label", + "placeholder", + "role-description", + "value-text", + "col-index-text", + "row-index-text", + ] +) + + def get_types(gir): return { **get_property_types(gir), @@ -121,7 +135,9 @@ class A11yProperty(AstNode): grammar = Statement( UseIdent("name"), ":", - AnyOf(Value, ["[", UseLiteral("list_form", True), Delimited(Value, ","), "]"]), + AnyOf( + Value, ["[", UseLiteral("list_form", True), Delimited(Value, ","), "]"] + ).expected("value"), ) @property @@ -234,7 +250,10 @@ class ExtAccessibility(AstNode): ) def a11y_completer(_ctx: CompletionContext): yield Completion( - "accessibility", CompletionItemKind.Snippet, snippet="accessibility {\n $0\n}" + "accessibility", + CompletionItemKind.Snippet, + snippet="accessibility {\n $0\n}", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "accessibility"), ) @@ -242,12 +261,14 @@ def a11y_completer(_ctx: CompletionContext): applies_in=[ExtAccessibility], matches=new_statement_patterns, ) -def a11y_name_completer(ctx: CompletionContext): +def a11y_property_completer(ctx: CompletionContext): for name, type in get_types(ctx.ast_node.root.gir).items(): - yield Completion( + yield get_property_completion( name, - CompletionItemKind.Property, - docs=_get_docs(ctx.ast_node.root.gir, type.name), + type, + ctx, + name in TRANSLATED, + _get_docs(ctx.ast_node.root.gir, name), ) diff --git a/blueprintcompiler/language/gtk_combo_box_text.py b/blueprintcompiler/language/gtk_combo_box_text.py index e162cca..5a7a892 100644 --- a/blueprintcompiler/language/gtk_combo_box_text.py +++ b/blueprintcompiler/language/gtk_combo_box_text.py @@ -95,7 +95,12 @@ class ExtComboBoxItems(AstNode): matches=new_statement_patterns, ) def items_completer(_ctx: CompletionContext): - yield Completion("items", CompletionItemKind.Snippet, snippet="items [$0]") + yield Completion( + "items", + CompletionItemKind.Snippet, + snippet="items [$0]", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "items"), + ) @decompiler("items", parent_type="Gtk.ComboBoxText") diff --git a/blueprintcompiler/language/gtk_file_filter.py b/blueprintcompiler/language/gtk_file_filter.py index 754130a..d0e53d2 100644 --- a/blueprintcompiler/language/gtk_file_filter.py +++ b/blueprintcompiler/language/gtk_file_filter.py @@ -103,10 +103,23 @@ ext_file_filter_suffixes = create_node("suffixes", "suffix") ) def file_filter_completer(_ctx: CompletionContext): yield Completion( - "mime-types", CompletionItemKind.Snippet, snippet='mime-types ["$0"]' + "mime-types", + CompletionItemKind.Snippet, + snippet='mime-types ["$0"]', + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "mime-types"), + ) + yield Completion( + "patterns", + CompletionItemKind.Snippet, + snippet='patterns ["$0"]', + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "patterns"), + ) + yield Completion( + "suffixes", + CompletionItemKind.Snippet, + snippet='suffixes ["$0"]', + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "suffixes"), ) - yield Completion("patterns", CompletionItemKind.Snippet, snippet='patterns ["$0"]') - yield Completion("suffixes", CompletionItemKind.Snippet, snippet='suffixes ["$0"]') @decompiler("mime-types") diff --git a/blueprintcompiler/language/gtk_layout.py b/blueprintcompiler/language/gtk_layout.py index 901c40d..63bc0f6 100644 --- a/blueprintcompiler/language/gtk_layout.py +++ b/blueprintcompiler/language/gtk_layout.py @@ -94,7 +94,12 @@ class ExtLayout(AstNode): matches=new_statement_patterns, ) def layout_completer(_ctx: CompletionContext): - yield Completion("layout", CompletionItemKind.Snippet, snippet="layout {\n $0\n}") + yield Completion( + "layout", + CompletionItemKind.Snippet, + snippet="layout {\n $0\n}", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "layout"), + ) @decompiler("layout") diff --git a/blueprintcompiler/language/gtk_menu.py b/blueprintcompiler/language/gtk_menu.py index d5bf4fb..ed7ede8 100644 --- a/blueprintcompiler/language/gtk_menu.py +++ b/blueprintcompiler/language/gtk_menu.py @@ -253,21 +253,48 @@ def menu_completer(_ctx: CompletionContext): ) def menu_content_completer(_ctx: CompletionContext): yield Completion( - "submenu", CompletionItemKind.Snippet, snippet="submenu {\n $0\n}" + "submenu", + CompletionItemKind.Snippet, + snippet="submenu {\n $0\n}", + sort_text=get_sort_key(CompletionPriority.CLASS, "1 submenu"), ) yield Completion( - "section", CompletionItemKind.Snippet, snippet="section {\n $0\n}" + "section", + CompletionItemKind.Snippet, + snippet="section {\n $0\n}", + sort_text=get_sort_key(CompletionPriority.CLASS, "1 section"), + ) + yield Completion( + "item", + CompletionItemKind.Snippet, + snippet="item {\n $0\n}", + sort_text=get_sort_key(CompletionPriority.CLASS, "1 item"), ) - yield Completion("item", CompletionItemKind.Snippet, snippet="item {\n $0\n}") yield Completion( "item (shorthand)", CompletionItemKind.Snippet, snippet='item (_("${1:Label}"), "${2:action-name}", "${3:icon-name}")', + sort_text=get_sort_key(CompletionPriority.CLASS, "0 item (shorthand)"), ) - yield Completion("label", CompletionItemKind.Snippet, snippet="label: $0;") - yield Completion("action", CompletionItemKind.Snippet, snippet='action: "$0";') - yield Completion("icon", CompletionItemKind.Snippet, snippet='icon: "$0";') + yield Completion( + "label", + CompletionItemKind.Snippet, + snippet="label: $0;", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "label"), + ) + yield Completion( + "action", + CompletionItemKind.Snippet, + snippet='action: "$0";', + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "action"), + ) + yield Completion( + "icon", + CompletionItemKind.Snippet, + snippet='icon: "$0";', + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "icon"), + ) @decompiler("menu") diff --git a/blueprintcompiler/language/gtk_scale.py b/blueprintcompiler/language/gtk_scale.py index 2615a67..21089a4 100644 --- a/blueprintcompiler/language/gtk_scale.py +++ b/blueprintcompiler/language/gtk_scale.py @@ -138,7 +138,12 @@ class ExtScaleMarks(AstNode): matches=new_statement_patterns, ) def complete_marks(_ctx: CompletionContext): - yield Completion("marks", CompletionItemKind.Keyword, snippet="marks [\n\t$0\n]") + yield Completion( + "marks", + CompletionItemKind.Keyword, + snippet="marks [\n\t$0\n]", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "marks"), + ) @completer( diff --git a/blueprintcompiler/language/gtk_size_group.py b/blueprintcompiler/language/gtk_size_group.py index 3505a06..d30eef9 100644 --- a/blueprintcompiler/language/gtk_size_group.py +++ b/blueprintcompiler/language/gtk_size_group.py @@ -105,7 +105,12 @@ class ExtSizeGroupWidgets(AstNode): matches=new_statement_patterns, ) def size_group_completer(_ctx: CompletionContext): - yield Completion("widgets", CompletionItemKind.Snippet, snippet="widgets [$0]") + yield Completion( + "widgets", + CompletionItemKind.Snippet, + snippet="widgets [$0]", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "widgets"), + ) @decompiler("widgets") diff --git a/blueprintcompiler/language/gtk_string_list.py b/blueprintcompiler/language/gtk_string_list.py index 0e6c00a..a4fa3b5 100644 --- a/blueprintcompiler/language/gtk_string_list.py +++ b/blueprintcompiler/language/gtk_string_list.py @@ -76,7 +76,12 @@ class ExtStringListStrings(AstNode): matches=new_statement_patterns, ) def strings_completer(_ctx: CompletionContext): - yield Completion("strings", CompletionItemKind.Snippet, snippet="strings [$0]") + yield Completion( + "strings", + CompletionItemKind.Snippet, + snippet="strings [$0]", + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "strings"), + ) @decompiler("items", parent_type="Gtk.StringList") diff --git a/blueprintcompiler/language/gtk_styles.py b/blueprintcompiler/language/gtk_styles.py index 0cdea0b..7c9252c 100644 --- a/blueprintcompiler/language/gtk_styles.py +++ b/blueprintcompiler/language/gtk_styles.py @@ -81,7 +81,12 @@ class ExtStyles(AstNode): matches=new_statement_patterns, ) def style_completer(_ctx: CompletionContext): - yield Completion("styles", CompletionItemKind.Keyword, snippet='styles ["$0"]') + yield Completion( + "styles", + CompletionItemKind.Keyword, + snippet='styles ["$0"]', + sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "styles"), + ) @decompiler("style") diff --git a/blueprintcompiler/language/ui.py b/blueprintcompiler/language/ui.py index d55a22a..896c0f7 100644 --- a/blueprintcompiler/language/ui.py +++ b/blueprintcompiler/language/ui.py @@ -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 diff --git a/blueprintcompiler/lsp_utils.py b/blueprintcompiler/lsp_utils.py index b938181..2a4380a 100644 --- a/blueprintcompiler/lsp_utils.py +++ b/blueprintcompiler/lsp_utils.py @@ -87,6 +87,7 @@ class Completion: text: T.Optional[str] = None snippet: T.Optional[str] = None detail: T.Optional[str] = None + additional_text_edits: T.Optional[T.List["TextEdit"]] = None def to_json(self, snippets: bool): insert_text = self.text or self.label @@ -114,6 +115,11 @@ class Completion: "insertText": insert_text, "insertTextFormat": insert_text_format, "detail": self.detail if self.detail else None, + "additionalTextEdits": ( + [edit.to_json() for edit in self.additional_text_edits] + if self.additional_text_edits + else None + ), } return {k: v for k, v in result.items() if v is not None} diff --git a/tests/formatting/comment_in.blp b/tests/formatting/comment_in.blp index 32a907c..88b825a 100644 --- a/tests/formatting/comment_in.blp +++ b/tests/formatting/comment_in.blp @@ -1,2 +1,4 @@ using Gtk 4.0; -//comment \ No newline at end of file +//comment +// Trailing whitespace: +// diff --git a/tests/formatting/comment_out.blp b/tests/formatting/comment_out.blp index d5dca95..91e647a 100644 --- a/tests/formatting/comment_out.blp +++ b/tests/formatting/comment_out.blp @@ -1,2 +1,4 @@ using Gtk 4.0; // comment +// Trailing whitespace: +//