mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-07 16:29:07 -04:00
Compare commits
5 commits
1f75d819aa
...
af1e234a5a
Author | SHA1 | Date | |
---|---|---|---|
|
af1e234a5a | ||
|
5b0f662478 | ||
|
3efb2f9f4e | ||
|
a75adc7d1b | ||
|
f894301571 |
7 changed files with 294 additions and 16 deletions
191
blueprintcompiler/annotations.py
Normal file
191
blueprintcompiler/annotations.py
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
# annotations.py
|
||||||
|
#
|
||||||
|
# Copyright 2024 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
|
||||||
|
|
||||||
|
# Extra information about types in common libraries that's used for things like completions.
|
||||||
|
|
||||||
|
import typing as T
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from . import gir
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Annotation:
|
||||||
|
translatable_properties: T.List[str]
|
||||||
|
|
||||||
|
|
||||||
|
def is_property_translated(property: gir.Property):
|
||||||
|
ns = property.get_containing(gir.Namespace)
|
||||||
|
ns_name = ns.name + "-" + ns.version
|
||||||
|
if annotation := _ANNOTATIONS.get(ns_name):
|
||||||
|
assert property.container is not None
|
||||||
|
return (
|
||||||
|
property.container.name + ":" + property.name
|
||||||
|
in annotation.translatable_properties
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
_ANNOTATIONS = {
|
||||||
|
"Gtk-4.0": Annotation(
|
||||||
|
translatable_properties=[
|
||||||
|
"AboutDialog:comments",
|
||||||
|
"AboutDialog:translator-credits",
|
||||||
|
"AboutDialog:website-label",
|
||||||
|
"AlertDialog:detail",
|
||||||
|
"AlertDialog:message",
|
||||||
|
"AppChooserButton:heading",
|
||||||
|
"AppChooserDialog:heading",
|
||||||
|
"AppChooserWidget:default-text",
|
||||||
|
"AssistantPage:title",
|
||||||
|
"Button:label",
|
||||||
|
"CellRendererText:markup",
|
||||||
|
"CellRendererText:placeholder-text",
|
||||||
|
"CellRendererText:text",
|
||||||
|
"CheckButton:label",
|
||||||
|
"ColorButton:title",
|
||||||
|
"ColorDialog:title",
|
||||||
|
"ColumnViewColumn:title",
|
||||||
|
"ColumnViewRow:accessible-description",
|
||||||
|
"ColumnViewRow:accessible-label",
|
||||||
|
"Entry:placeholder-text",
|
||||||
|
"Entry:primary-icon-tooltip-markup",
|
||||||
|
"Entry:primary-icon-tooltip-text",
|
||||||
|
"Entry:secondary-icon-tooltip-markup",
|
||||||
|
"Entry:secondary-icon-tooltip-text",
|
||||||
|
"EntryBuffer:text",
|
||||||
|
"Expander:label",
|
||||||
|
"FileChooserNative:accept-label",
|
||||||
|
"FileChooserNative:cancel-label",
|
||||||
|
"FileChooserWidget:subtitle",
|
||||||
|
"FileDialog:accept-label",
|
||||||
|
"FileDialog:title",
|
||||||
|
"FileDialog:initial-name",
|
||||||
|
"FileFilter:name",
|
||||||
|
"FontButton:title",
|
||||||
|
"FontDialog:title",
|
||||||
|
"Frame:label",
|
||||||
|
"Inscription:markup",
|
||||||
|
"Inscription:text",
|
||||||
|
"Label:label",
|
||||||
|
"ListItem:accessible-description",
|
||||||
|
"ListItem:accessible-label",
|
||||||
|
"LockButton:text-lock",
|
||||||
|
"LockButton:text-unlock",
|
||||||
|
"LockButton:tooltip-lock",
|
||||||
|
"LockButton:tooltip-not-authorized",
|
||||||
|
"LockButton:tooltip-unlock",
|
||||||
|
"MenuButton:label",
|
||||||
|
"MessageDialog:secondary-text",
|
||||||
|
"MessageDialog:text",
|
||||||
|
"NativeDialog:title",
|
||||||
|
"NotebookPage:menu-label",
|
||||||
|
"NotebookPage:tab-label",
|
||||||
|
"PasswordEntry:placeholder-text",
|
||||||
|
"Picture:alternative-text",
|
||||||
|
"PrintDialog:accept-label",
|
||||||
|
"PrintDialog:title",
|
||||||
|
"Printer:name",
|
||||||
|
"PrintJob:title",
|
||||||
|
"PrintOperation:custom-tab-label",
|
||||||
|
"PrintOperation:export-filename",
|
||||||
|
"PrintOperation:job-name",
|
||||||
|
"ProgressBar:text",
|
||||||
|
"SearchEntry:placeholder-text",
|
||||||
|
"ShortcutLabel:disabled-text",
|
||||||
|
"ShortcutsGroup:title",
|
||||||
|
"ShortcutsSection:title",
|
||||||
|
"ShortcutsShortcut:title",
|
||||||
|
"ShortcutsShortcut:subtitle",
|
||||||
|
"StackPage:title",
|
||||||
|
"Text:placeholder-text",
|
||||||
|
"TextBuffer:text",
|
||||||
|
"TreeViewColumn:title",
|
||||||
|
"Widget:tooltip-markup",
|
||||||
|
"Widget:tooltip-text",
|
||||||
|
"Window:title",
|
||||||
|
"Editable:text",
|
||||||
|
"FontChooser:preview-text",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
"Adw-1": Annotation(
|
||||||
|
translatable_properties=[
|
||||||
|
"AboutDialog:comments",
|
||||||
|
"AboutDialog:translator-credits",
|
||||||
|
"AboutWindow:comments",
|
||||||
|
"AboutWindow:translator-credits",
|
||||||
|
"ActionRow:subtitle",
|
||||||
|
"ActionRow:title",
|
||||||
|
"AlertDialog:body",
|
||||||
|
"AlertDialog:heading",
|
||||||
|
"Avatar:text",
|
||||||
|
"Banner:button-label",
|
||||||
|
"Banner:title",
|
||||||
|
"ButtonContent:label",
|
||||||
|
"Dialog:title",
|
||||||
|
"ExpanderRow:subtitle",
|
||||||
|
"MessageDialog:body",
|
||||||
|
"MessageDialog:heading",
|
||||||
|
"NavigationPage:title",
|
||||||
|
"PreferencesGroup:description",
|
||||||
|
"PreferencesGroup:title",
|
||||||
|
"PreferencesPage:description",
|
||||||
|
"PreferencesPage:title",
|
||||||
|
"PreferencesRow:title",
|
||||||
|
"SplitButton:dropdown-tooltip",
|
||||||
|
"SplitButton:label",
|
||||||
|
"StatusPage:description",
|
||||||
|
"StatusPage:title",
|
||||||
|
"TabPage:indicator-tooltip",
|
||||||
|
"TabPage:keyword",
|
||||||
|
"TabPage:title",
|
||||||
|
"Toast:button-label",
|
||||||
|
"Toast:title",
|
||||||
|
"ViewStackPage:title",
|
||||||
|
"ViewSwitcherTitle:subtitle",
|
||||||
|
"ViewSwitcherTitle:title",
|
||||||
|
"WindowTitle:subtitle",
|
||||||
|
"WindowTitle:title",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
"Shumate-1.0": Annotation(
|
||||||
|
translatable_properties=[
|
||||||
|
"License:extra-text",
|
||||||
|
"MapSource:license",
|
||||||
|
"MapSource:name",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
"GtkSource-5": Annotation(
|
||||||
|
translatable_properties=[
|
||||||
|
"CompletionCell:markup",
|
||||||
|
"CompletionCell:text",
|
||||||
|
"CompletionSnippets:title",
|
||||||
|
"CompletionWords:title",
|
||||||
|
"GutterRendererText:markup",
|
||||||
|
"GutterRendererText:text",
|
||||||
|
"SearchSettings:search-text",
|
||||||
|
"Snippet:description",
|
||||||
|
"Snippet:name",
|
||||||
|
"SnippetChunk:tooltip-text",
|
||||||
|
"StyleScheme:description",
|
||||||
|
"StyleScheme:name",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
}
|
|
@ -20,7 +20,7 @@
|
||||||
import sys
|
import sys
|
||||||
import typing as T
|
import typing as T
|
||||||
|
|
||||||
from . import gir, language
|
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
|
||||||
|
@ -154,11 +154,17 @@ def property_completer(lsp, ast_node, match_variables):
|
||||||
detail=prop.detail,
|
detail=prop.detail,
|
||||||
)
|
)
|
||||||
elif isinstance(prop.type, gir.StringType):
|
elif isinstance(prop.type, gir.StringType):
|
||||||
|
snippet = (
|
||||||
|
f'{prop_name}: _("$0");'
|
||||||
|
if annotations.is_property_translated(prop)
|
||||||
|
else f'{prop_name}: "$0";'
|
||||||
|
)
|
||||||
|
|
||||||
yield Completion(
|
yield Completion(
|
||||||
prop_name,
|
prop_name,
|
||||||
CompletionItemKind.Property,
|
CompletionItemKind.Property,
|
||||||
sort_text=f"0 {prop_name}",
|
sort_text=f"0 {prop_name}",
|
||||||
snippet=f'{prop_name}: "$0";',
|
snippet=snippet,
|
||||||
docs=prop.doc,
|
docs=prop.doc,
|
||||||
detail=prop.detail,
|
detail=prop.detail,
|
||||||
)
|
)
|
||||||
|
|
|
@ -22,12 +22,12 @@ from .binding import Binding
|
||||||
from .common import *
|
from .common import *
|
||||||
from .contexts import ValueTypeCtx
|
from .contexts import ValueTypeCtx
|
||||||
from .gtkbuilder_template import Template
|
from .gtkbuilder_template import Template
|
||||||
from .values import ArrayValue, ObjectValue, Value
|
from .values import ArrayValue, ObjectValue, Value, VariantValue
|
||||||
|
|
||||||
|
|
||||||
class Property(AstNode):
|
class Property(AstNode):
|
||||||
grammar = Statement(
|
grammar = Statement(
|
||||||
UseIdent("name"), ":", AnyOf(Binding, ObjectValue, Value, ArrayValue)
|
UseIdent("name"), ":", AnyOf(Binding, VariantValue, ObjectValue, Value, ArrayValue)
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -35,7 +35,7 @@ class Property(AstNode):
|
||||||
return self.tokens["name"]
|
return self.tokens["name"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self) -> T.Union[Binding, ObjectValue, Value, ArrayValue]:
|
def value(self) -> T.Union[Binding, VariantValue, ObjectValue, Value, ArrayValue]:
|
||||||
return self.children[0]
|
return self.children[0]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
import typing as T
|
import typing as T
|
||||||
|
|
||||||
from blueprintcompiler.language.values import StringValue
|
from blueprintcompiler.language.values import StringValue, VariantValue
|
||||||
|
|
||||||
from .common import *
|
from .common import *
|
||||||
from .contexts import ValueTypeCtx
|
from .contexts import ValueTypeCtx
|
||||||
|
@ -98,8 +98,12 @@ class MenuAttribute(AstNode):
|
||||||
return self.tokens["name"]
|
return self.tokens["name"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self) -> StringValue:
|
def value(self) -> StringValue | VariantValue:
|
||||||
return self.children[StringValue][0]
|
if len(self.children[StringValue]) > 0:
|
||||||
|
return self.children[StringValue][0]
|
||||||
|
elif len(self.children[VariantValue]) > 0:
|
||||||
|
return self.children[VariantValue][0]
|
||||||
|
raise CompilerBugError()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def document_symbol(self) -> DocumentSymbol:
|
def document_symbol(self) -> DocumentSymbol:
|
||||||
|
@ -133,7 +137,7 @@ menu_attribute = Group(
|
||||||
[
|
[
|
||||||
UseIdent("name"),
|
UseIdent("name"),
|
||||||
":",
|
":",
|
||||||
Err(StringValue, "Expected string or translated string"),
|
Err(AnyOf(StringValue, VariantValue), "Expected string or translated string"),
|
||||||
Match(";").expected(),
|
Match(";").expected(),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
@ -371,6 +371,44 @@ class IdentLiteral(AstNode):
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
class VariantValue(AstNode):
|
||||||
|
grammar = [
|
||||||
|
"variant",
|
||||||
|
"<",
|
||||||
|
UseQuoted("type"),
|
||||||
|
">",
|
||||||
|
"(",
|
||||||
|
UseQuoted("value"),
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_type(self) -> str:
|
||||||
|
return self.tokens["type"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_value(self) -> str:
|
||||||
|
return self.tokens["value"]
|
||||||
|
|
||||||
|
@validate()
|
||||||
|
def validate_for_type(self) -> None:
|
||||||
|
expected_type = self.context[ValueTypeCtx].value_type
|
||||||
|
if expected_type is None:
|
||||||
|
pass
|
||||||
|
elif (
|
||||||
|
isinstance(expected_type, gir.IntType)
|
||||||
|
or isinstance(expected_type, gir.UIntType)
|
||||||
|
or isinstance(expected_type, gir.FloatType)
|
||||||
|
or isinstance(expected_type, gir.FloatType)
|
||||||
|
):
|
||||||
|
raise CompileError(f"Cannot convert variant to number")
|
||||||
|
elif isinstance(expected_type, gir.StringType):
|
||||||
|
raise CompileError("Cannot convert variant to string")
|
||||||
|
elif isinstance(expected_type, gir.Boxed) and expected_type.full_name == "GLib.Variant":
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise CompileError(f"Cannot convert variant into {expected_type.full_name}")
|
||||||
|
pass
|
||||||
|
|
||||||
class Literal(AstNode):
|
class Literal(AstNode):
|
||||||
grammar = AnyOf(
|
grammar = AnyOf(
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import typing as T
|
import typing as T
|
||||||
|
|
||||||
|
from blueprintcompiler.language.values import VariantValue
|
||||||
|
|
||||||
from ...language import *
|
from ...language import *
|
||||||
from .. import OutputFormat
|
from .. import OutputFormat
|
||||||
from .xml_emitter import XmlEmitter
|
from .xml_emitter import XmlEmitter
|
||||||
|
@ -83,13 +85,23 @@ class XmlOutput(OutputFormat):
|
||||||
if isinstance(child, Menu):
|
if isinstance(child, Menu):
|
||||||
self._emit_menu(child, xml)
|
self._emit_menu(child, xml)
|
||||||
elif isinstance(child, MenuAttribute):
|
elif isinstance(child, MenuAttribute):
|
||||||
xml.start_tag(
|
if isinstance(child.value, StringValue):
|
||||||
"attribute",
|
xml.start_tag(
|
||||||
name=child.name,
|
"attribute",
|
||||||
**self._translated_string_attrs(child.value.child),
|
name=child.name,
|
||||||
)
|
**self._translated_string_attrs(child.value.child),
|
||||||
xml.put_text(child.value.string)
|
)
|
||||||
xml.end_tag()
|
xml.put_text(child.value.string)
|
||||||
|
xml.end_tag()
|
||||||
|
elif isinstance(child.value, VariantValue):
|
||||||
|
xml.start_tag(
|
||||||
|
"attribute",
|
||||||
|
name=child.name,
|
||||||
|
type=child.value.var_type,
|
||||||
|
)
|
||||||
|
xml.put_text(child.value.var_value)
|
||||||
|
xml.end_tag()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise CompilerBugError()
|
raise CompilerBugError()
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
|
@ -148,6 +160,11 @@ class XmlOutput(OutputFormat):
|
||||||
self._emit_value(values[-1], xml)
|
self._emit_value(values[-1], xml)
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
|
|
||||||
|
elif isinstance(value, VariantValue):
|
||||||
|
xml.start_tag("property", **props, type=value.var_type)
|
||||||
|
xml.put_text(value.var_value)
|
||||||
|
xml.end_tag()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise CompilerBugError()
|
raise CompilerBugError()
|
||||||
|
|
||||||
|
@ -205,6 +222,8 @@ class XmlOutput(OutputFormat):
|
||||||
xml.put_text(self._object_id(value, value.ident))
|
xml.put_text(self._object_id(value, value.ident))
|
||||||
elif isinstance(value, TypeLiteral):
|
elif isinstance(value, TypeLiteral):
|
||||||
xml.put_text(value.type_name.glib_type_name)
|
xml.put_text(value.type_name.glib_type_name)
|
||||||
|
elif isinstance(value, VariantValue):
|
||||||
|
xml.put_text(value.value)
|
||||||
else:
|
else:
|
||||||
if isinstance(value.value, float) and value.value == int(value.value):
|
if isinstance(value.value, float) and value.value == int(value.value):
|
||||||
xml.put_text(int(value.value))
|
xml.put_text(int(value.value))
|
||||||
|
@ -284,6 +303,10 @@ class XmlOutput(OutputFormat):
|
||||||
xml.start_tag(tag, **attrs)
|
xml.start_tag(tag, **attrs)
|
||||||
xml.put_text(value.child.value)
|
xml.put_text(value.child.value)
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
|
elif isinstance(value.child, VariantValue):
|
||||||
|
xml.start_tag(tag, **attrs, type=value.child.var_type)
|
||||||
|
xml.put_text(value.child.var_value)
|
||||||
|
xml.end_tag()
|
||||||
else:
|
else:
|
||||||
xml.start_tag(tag, **attrs)
|
xml.start_tag(tag, **attrs)
|
||||||
self._emit_value(value, xml)
|
self._emit_value(value, xml)
|
||||||
|
|
16
variant-test.blp
Normal file
16
variant-test.blp
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
using Gtk 4.0;
|
||||||
|
|
||||||
|
menu root {
|
||||||
|
submenu {
|
||||||
|
name: "one";
|
||||||
|
item {
|
||||||
|
action: "app.foo_bar";
|
||||||
|
target: variant<"s">("\"one\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Button {
|
||||||
|
action-name: "app.shave_yak";
|
||||||
|
action-target: variant<"y">("8");
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue