mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
completions: Add translation-domain completer
This commit is contained in:
parent
3d0593bc2b
commit
f5cef37db8
2 changed files with 29 additions and 3 deletions
|
@ -23,7 +23,7 @@ from . import annotations, gir, language
|
||||||
from .ast_utils import AstNode
|
from .ast_utils import AstNode
|
||||||
from .completions_utils import *
|
from .completions_utils import *
|
||||||
from .language.types import ClassName
|
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 .parser import SKIP_TOKENS
|
||||||
from .tokenizer import Token, TokenType
|
from .tokenizer import Token, TokenType
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ def _complete(
|
||||||
token_idx -= 1
|
token_idx -= 1
|
||||||
|
|
||||||
for completer in ast_node.completers:
|
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(
|
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(
|
@completer(
|
||||||
applies_in=[language.UI, language.ObjectContent, language.Template],
|
applies_in=[language.UI, language.ObjectContent, language.Template],
|
||||||
matches=new_statement_patterns,
|
matches=new_statement_patterns,
|
||||||
|
|
|
@ -32,6 +32,7 @@ class CompletionContext:
|
||||||
ast_node: AstNode
|
ast_node: AstNode
|
||||||
match_variables: T.List[str]
|
match_variables: T.List[str]
|
||||||
next_token: Token
|
next_token: Token
|
||||||
|
index: int
|
||||||
|
|
||||||
|
|
||||||
new_statement_patterns = [
|
new_statement_patterns = [
|
||||||
|
@ -44,7 +45,9 @@ new_statement_patterns = [
|
||||||
|
|
||||||
def completer(applies_in: T.List, matches: T.List = [], applies_in_subclass=None):
|
def completer(applies_in: T.List, matches: T.List = [], applies_in_subclass=None):
|
||||||
def decorator(func: T.Callable[[CompletionContext], T.Generator[Completion]]):
|
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
|
# For completers that apply in ObjectContent nodes, we can further
|
||||||
# check that the object is the right class
|
# check that the object is the right class
|
||||||
if applies_in_subclass is not None:
|
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,
|
ast_node=ast_node,
|
||||||
match_variables=match_variables,
|
match_variables=match_variables,
|
||||||
next_token=next_token,
|
next_token=next_token,
|
||||||
|
index=idx,
|
||||||
)
|
)
|
||||||
yield from func(context)
|
yield from func(context)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue