adding multiple list items from a macro
keith-hall opened this issue · comments
Hi,
I'd really like some help, probably I'm missing something obvious, but hopefully any advice you give here could also help others as clueless as me - apologies in advance if any of my terminology is woefully incorrect :)
I have created a simple macro that returns a list of rules, let's call it Packages/User/macros/multi.py
:
from YAMLMacros.lib.syntax import meta, expect, pop_on, stack, rule
def multiple_items(prefix, count):
return [rule(match = prefix + str(index)) for index in range(0, count)]
I want to be able to append the rules created from this macro into an existing list of rules. To be concrete, let's say I want to extend the XML
syntax definition, in particular to add some rules at the top of the tag-stuff
context:
%YAML 1.2
%TAG ! tag:yaml-macros:YAMLMacros.lib.extend,YAMLMacros.lib.include,User.macros.multi:
---
!apply
- !include_resource XML.sublime-syntax
- !merge
name: New Name
scope: text.xml.example
contexts: !merge
tag-stuff: !prepend
- match: 'example1'
- match: 'example2'
- !multiple_items [test, 3]
Currently, building this produces:
# ...
tag-stuff:
- match: example1
- match: example2
- - match: test0
- match: test1
- match: test2
# existing tag-stuff items...
but I want it to produce:
# ...
tag-stuff:
- match: example1
- match: example2
- match: test0
- match: test1
- match: test2
# existing tag-stuff items...
what do I need to do to achieve that? I thought maybe all
would help to flatten it, so I tried:
%YAML 1.2
%TAG ! tag:yaml-macros:YAMLMacros.lib.extend,YAMLMacros.lib.include,User.macros.multi:
---
!apply
- !include_resource XML.sublime-syntax
- !merge
name: New Name
scope: text.xml.example
contexts: !merge
tag-stuff: !prepend
- !all
- - match: 'example1'
- match: 'example2'
- !multiple_items [test, 3]
but it gives a traceback in the ST console (i.e. it isn't caught and displayed in the build system output):
ruamel.yaml.representer.RepresenterError: cannot represent an object: <YAMLMacros.lib.extend.All object at 0x000000000521FFD0>
clearly I'm clueless as to what I'm doing - I've tried it in various different ways, and with !merge
and others in case it would help, but no joy...
The easy way to do this is with include
:
tag-stuff: !prepend
- match: 'example1'
- match: 'example2'
- include: other-stuff
other-stuff: !multiple_items [test, 3]
The extend
library also exports a flatten
macro. However, because the contents of tag-stuff
already have a !prepend
tag, it would be a bit awkward to apply both macros:
tag-stuff: !prepend
- !flatten
- match: 'example1'
- match: 'example2'
- !multiple_items [test, 3]
Ideally, it would be nice if include
would accept an anonymous context as an alternative to a context name. Then, we could do this:
tag-stuff: !prepend
- match: 'example1'
- match: 'example2'
- include: !multiple_items [test, 3]
This would require support on Sublime's part.
EDIT: Fixed the third example.
Using flatten
and prepend
together doesn't seem to work as expected - I just end up with - match: example1
prepended, and not example2
or test0
etc.
But your idea of using include
is brilliant and works perfectly, thanks!