completions: Add translation-domain completer

This commit is contained in:
James Westman 2025-01-04 16:23:42 -06:00
parent 3d0593bc2b
commit f5cef37db8
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
2 changed files with 29 additions and 3 deletions

View file

@ -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,

View file

@ -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)