Add GtkComboBoxText items

This commit is contained in:
James Westman 2021-11-12 18:40:26 -06:00
parent f1e1811e1f
commit aa447466c0
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
12 changed files with 137 additions and 17 deletions

View file

@ -455,15 +455,16 @@ class BaseAttribute(AstNode):
""" A helper class for attribute syntax of the form `name: literal_value;`""" """ A helper class for attribute syntax of the form `name: literal_value;`"""
tag_name: str = "" tag_name: str = ""
attr_name: str = "name"
def emit_xml(self, xml: XmlEmitter): def emit_xml(self, xml: XmlEmitter):
value = self.children[Value][0] value = self.children[Value][0]
translatable = isinstance(value, TranslatedStringValue) translatable = isinstance(value, TranslatedStringValue)
xml.start_tag( attrs = {
self.tag_name, self.attr_name: self.tokens["name"],
name=self.tokens["name"], "translatable": "true" if translatable else None
translatable="true" if translatable else None, }
) xml.start_tag(self.tag_name, **attrs)
if translatable: if translatable:
xml.put_text(value.string) xml.put_text(value.string)
else: else:

View file

@ -75,7 +75,8 @@ def using_gtk(ast_node, match_variables):
def namespace(ast_node, match_variables): def namespace(ast_node, match_variables):
yield Completion("Gtk", CompletionItemKind.Module, text="Gtk.") yield Completion("Gtk", CompletionItemKind.Module, text="Gtk.")
for ns in ast_node.root.children[ast.Import]: for ns in ast_node.root.children[ast.Import]:
yield Completion(ns.namespace, CompletionItemKind.Module, text=ns.namespace + ".") if ns.gir_namespace is not None:
yield Completion(ns.gir_namespace.name, CompletionItemKind.Module, text=ns.gir_namespace.name + ".")
@completer( @completer(

View file

@ -40,9 +40,16 @@ def applies_to(*ast_types):
return func return func
return decorator return decorator
def completer(applies_in: T.List, matches: T.List=[]): def completer(applies_in: T.List, matches: T.List=[], applies_in_subclass=None):
def decorator(func): def decorator(func):
def inner(prev_tokens: T.List[Token], ast_node: ast.AstNode): def inner(prev_tokens: T.List[Token], ast_node: ast.AstNode):
# 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:
type = ast_node.root.gir.get_type(applies_in_subclass[1], applies_in_subclass[0])
if ast_node.gir_class and not ast_node.gir_class.assignable_to(type):
return
any_match = len(matches) == 0 any_match = len(matches) == 0
match_variables: T.List[str] = [] match_variables: T.List[str] = []

View file

@ -7,7 +7,8 @@ from .gtk_layout import layout
from .gtk_menu import menu from .gtk_menu import menu
from .gtk_size_group import widgets from .gtk_size_group import widgets
from .gtk_styles import styles from .gtk_styles import styles
from .gtk_combo_box_text import items
OBJECT_HOOKS = [menu] OBJECT_HOOKS = [menu]
OBJECT_CONTENT_HOOKS = [a11y, styles, layout, mime_types, patterns, suffixes, widgets] OBJECT_CONTENT_HOOKS = [a11y, styles, layout, mime_types, patterns, suffixes, widgets, items]

View file

@ -0,0 +1,89 @@
# gtk_combo_box_text.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from ..ast import BaseTypedAttribute
from ..ast_utils import AstNode, validate
from ..completions_utils import *
from ..gir import StringType
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Items(AstNode):
@validate("items")
def container_is_combo_box_text(self):
self.validate_parent_type("Gtk", "ComboBoxText", "combo box items")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("items")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Item(BaseTypedAttribute):
tag_name = "item"
attr_name = "id"
@property
def value_type(self):
return StringType()
item = Group(
Item,
Sequence(
Optional(
Sequence(
UseIdent("name"),
Op(":"),
)
),
value,
)
)
items = Group(
Items,
Statement(
Keyword("items", True),
OpenBracket(),
Delimited(
item,
Comma()
),
CloseBracket(),
)
)
@completer(
applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "ComboBoxText"),
matches=new_statement_patterns,
)
def items_completer(ast_node, match_variables):
yield Completion(
"items", CompletionItemKind.Snippet,
snippet="items [$0];"
)

View file

@ -75,12 +75,11 @@ suffixes = create_node("suffixes", "suffix")
@completer( @completer(
applies_in=[ast.ObjectContent], applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "FileFilter"),
matches=new_statement_patterns, matches=new_statement_patterns,
) )
def file_filter_completer(ast_node, match_variables): def file_filter_completer(ast_node, match_variables):
file_filter = ast_node.root.gir.get_type("FileFilter", "Gtk") yield Completion("mime-types", CompletionItemKind.Snippet, snippet="mime-types [\"$0\"];")
if ast_node.gir_class and ast_node.gir_class.assignable_to(file_filter): yield Completion("patterns", CompletionItemKind.Snippet, snippet="patterns [\"$0\"];")
yield Completion("mime-types", CompletionItemKind.Snippet, snippet="mime-types [\"$0\"];") yield Completion("suffixes", CompletionItemKind.Snippet, snippet="suffixes [\"$0\"];")
yield Completion("patterns", CompletionItemKind.Snippet, snippet="patterns [\"$0\"];")
yield Completion("suffixes", CompletionItemKind.Snippet, snippet="suffixes [\"$0\"];")

View file

@ -70,6 +70,7 @@ layout = Group(
@completer( @completer(
applies_in=[ast.ObjectContent], applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "Widget"),
matches=new_statement_patterns, matches=new_statement_patterns,
) )
def layout_completer(ast_node, match_variables): def layout_completer(ast_node, match_variables):

View file

@ -77,9 +77,8 @@ widgets = Group(
@completer( @completer(
applies_in=[ast.ObjectContent], applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "SizeGroup"),
matches=new_statement_patterns, matches=new_statement_patterns,
) )
def file_filter_completer(ast_node, match_variables): def size_group_completer(ast_node, match_variables):
file_filter = ast_node.root.gir.get_type("SizeGroup", "Gtk") yield Completion("widgets", CompletionItemKind.Snippet, snippet="widgets [$0];")
if ast_node.gir_class and ast_node.gir_class.assignable_to(file_filter):
yield Completion("widgets", CompletionItemKind.Snippet, snippet="widgets [$0];")

View file

@ -63,6 +63,7 @@ styles = Group(
@completer( @completer(
applies_in=[ast.ObjectContent], applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "Widget"),
matches=new_statement_patterns, matches=new_statement_patterns,
) )
def style_completer(ast_node, match_variables): def style_completer(ast_node, match_variables):

View file

@ -0,0 +1,9 @@
using Gtk 4.0;
ComboBoxText {
items [
"Hello, world!",
_("Hello!"),
item_id: "item",
];
}

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkComboBoxText">
<items>
<item>Hello, world!</item>
<item translatable="true">Hello!</item>
<item id="item_id">item</item>
</items>
</object>
</interface>

View file

@ -95,6 +95,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample("accessibility") self.assert_sample("accessibility")
self.assert_sample("binding") self.assert_sample("binding")
self.assert_sample("child_type") self.assert_sample("child_type")
self.assert_sample("combo_box_text")
self.assert_sample("file_filter") self.assert_sample("file_filter")
self.assert_sample("flags") self.assert_sample("flags")
self.assert_sample("id_prop") self.assert_sample("id_prop")