From f5cef37db85391490b52870628dfc1a6dfbe216f Mon Sep 17 00:00:00 2001 From: James Westman Date: Sat, 4 Jan 2025 16:23:42 -0600 Subject: [PATCH] completions: Add translation-domain completer --- blueprintcompiler/completions.py | 26 ++++++++++++++++++++++++-- blueprintcompiler/completions_utils.py | 6 +++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/blueprintcompiler/completions.py b/blueprintcompiler/completions.py index 60cadad..cdd8d7d 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, TextEdit +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,6 +90,28 @@ def using_gtk(_ctx: CompletionContext): ) +@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, + snippet='translation-domain "$0";', + docs=get_docs_section("Syntax TranslationDomain"), + ) + + @completer( applies_in=[language.UI, language.ObjectContent, language.Template], matches=new_statement_patterns, diff --git a/blueprintcompiler/completions_utils.py b/blueprintcompiler/completions_utils.py index effb152..eb0bb79 100644 --- a/blueprintcompiler/completions_utils.py +++ b/blueprintcompiler/completions_utils.py @@ -32,6 +32,7 @@ class CompletionContext: ast_node: AstNode match_variables: T.List[str] next_token: Token + index: int new_statement_patterns = [ @@ -44,7 +45,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 +85,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)