Add GtkFileFilter properties

This commit is contained in:
James Westman 2021-11-12 09:23:25 -06:00
parent b776163cd7
commit ebfa72d94f
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
13 changed files with 141 additions and 39 deletions

View file

@ -135,37 +135,6 @@ class Import(AstNode):
pass
class Template(AstNode):
@validate("namespace", "class_name")
def gir_class_exists(self):
if not self.tokens["ignore_gir"]:
self.root.gir.validate_class(self.tokens["class_name"], self.tokens["namespace"])
@property
def gir_class(self):
return self.root.gir.get_class(self.tokens["class_name"], self.tokens["namespace"])
@docs("namespace")
def namespace_docs(self):
return self.root.gir.namespaces[self.tokens["namespace"]].doc
@docs("class_name")
def class_docs(self):
if self.gir_class:
return self.gir_class.doc
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("template", **{
"class": self.tokens["name"],
"parent": self.gir_class.glib_type_name if self.gir_class else self.tokens["class_name"],
})
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Object(AstNode):
@validate("namespace")
def gir_ns_exists(self):
@ -210,6 +179,17 @@ class Object(AstNode):
xml.end_tag()
class Template(Object):
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("template", **{
"class": self.tokens["name"],
"parent": self.gir_class.glib_type_name if self.gir_class else self.tokens["class_name"],
})
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Child(AstNode):
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("child", type=self.tokens["child_type"])

View file

@ -64,6 +64,14 @@ class AstNode:
else:
return self.parent.root
def parent_by_type(self, type):
if self.parent is None:
return None
elif isinstance(self.parent, type):
return self.parent
else:
return self.parent.parent_by_type(type)
@lazy_prop
def errors(self):
return list(self._get_errors())

View file

@ -5,7 +5,8 @@ from .gtk_a11y import a11y
from .gtk_menu import menu
from .gtk_styles import styles
from .gtk_layout import layout
from .gtk_file_filter import mime_types, patterns, suffixes
OBJECT_HOOKS = [menu]
OBJECT_CONTENT_HOOKS = [a11y, styles, layout]
OBJECT_CONTENT_HOOKS = [a11y, styles, layout, mime_types, patterns, suffixes]

View file

@ -0,0 +1,81 @@
# gtk_file_filter.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 .. import ast
from ..ast_utils import AstNode
from ..completions_utils import *
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Filters(AstNode):
def emit_xml(self, xml: XmlEmitter):
xml.start_tag(self.tokens["tag_name"])
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class FilterString(AstNode):
def emit_xml(self, xml):
xml.start_tag(self.tokens["tag_name"])
xml.put_text(self.tokens["name"])
xml.end_tag()
def create_node(tag_name: str, singular: str):
return Group(
Filters,
Statement(
Keyword(tag_name),
UseLiteral("tag_name", tag_name),
Op(":"),
Delimited(
Group(
FilterString,
Sequence(
UseQuoted("name"),
UseLiteral("tag_name", singular),
)
),
Comma(),
),
)
)
mime_types = create_node("mime-types", "mime-type")
patterns = create_node("patterns", "pattern")
suffixes = create_node("suffixes", "suffix")
@completer(
applies_in=[ast.ObjectContent],
matches=new_statement_patterns,
)
def file_filter_completer(ast_node, match_variables):
file_filter = ast_node.root.gir.get_type("FileFilter", "Gtk")
if ast_node.gir_class and ast_node.gir_class.assignable_to(file_filter):
yield Completion("mime-types", CompletionItemKind.Snippet, snippet="mime-types \"$0\";")
yield Completion("patterns", CompletionItemKind.Snippet, snippet="patterns \"$0\";")
yield Completion("suffixes", CompletionItemKind.Snippet, snippet="suffixes \"$0\";")

View file

@ -60,12 +60,17 @@ class GirType:
def doc(self):
return None
class BasicType(GirType):
name: str = "unknown type"
def assignable_to(self, other) -> bool:
raise NotImplementedError()
@property
def full_name(self) -> str:
raise NotImplementedError()
class BasicType(GirType):
name: str = "unknown type"
@property
def full_name(self) -> str:
return self.name

View file

@ -91,7 +91,7 @@ class Completion:
"documentation": {
"kind": "markdown",
"value": self.docs,
},
} if self.docs else None,
"deprecated": self.deprecated,
"insertText": insert_text,
"insertTextFormat": insert_text_format,

View file

@ -1 +1 @@
3,25,17,Namespace Gtk does not contain a class called NotARealClass
3,29,13,Namespace Gtk does not contain a class called NotARealClass

View file

@ -1 +1 @@
3,25,14,Gtk.Orientable is not a class
3,29,10,Gtk.Orientable is not a class

View file

@ -1 +1 @@
3,25,21,Namespace Adw was not imported
3,25,3,Namespace Adw was not imported

View file

@ -8,3 +8,4 @@ Gtk.Widget {
}
}
Gtk.Label my_label {}

View file

@ -0,0 +1,8 @@
using Gtk 4.0;
FileFilter {
name: "File Filter Name";
mime-types: "text/plain", "image/ *";
patterns: "*.txt";
suffixes: "png";
}

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkFileFilter">
<property name="name">File Filter Name</property>
<mime-types>
<mime-type>text/plain</mime-type>
<mime-type>image/ *</mime-type>
</mime-types>
<patterns>
<pattern>*.txt</pattern>
</patterns>
<suffixes>
<suffix>png</suffix>
</suffixes>
</object>
</interface>

View file

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