rainbow icon indicating copy to clipboard operation
rainbow copied to clipboard

include

Open petervaro opened this issue 11 years ago • 3 comments

It also would be nice to have 'include': '$self' and 'include': '#partial_definition' like in tmLanguage syntax definition files.

For example, in python there are keyword arguments or in python 3 there are function annotations. It would be kewl, to catch the variable names, and highlight them, while anything that comes after the equal sign and before a comma or a closing bracket \s*=.+(?=,|[\n)]) will be matched by the whole definition.

Is there a way right now to include the whole regex for matching or some kind of hack to accomplish this?

petervaro avatar Jul 22 '13 22:07 petervaro

You can't include partial definitions, but you can highlight a block using another language syntax which you can define.

For example for embedded php in html

Rainbow.extend('html', [
    {
        'name': 'source.php.embedded',
        'matches': {
            2: {
                'language': 'php'
            }
        },
        'pattern': /<\?(php)?([\s\S]*?)(\?>)/gm
    }
], true);

You can define a partial block yourself such as

Rainbow.extend('python.specific-thing', rules, true);

Then in another block set the language to be python.specific-thing to highlight using the rules defined there. I haven't really tested this too much but it should work. I will see about the $self and #partial stuff. I have seen them before in tmLanguage files.

ccampbell avatar Jul 22 '13 22:07 ccampbell

Will try this language thingy, if this is working, the partial part is almost done, with a tiny little headache -- I have to save all the small snippets in a separated file. In tmLanguage at the root dictionary there is two main objects: patterns and repository. The patterns is what you have right now, and the repository is a dictionary of dictionaries, where you can define smaller parts, just as in patterns, but their key value will be their reference names.

{
    'patterns':
    [
        {
            'pattern': /\((.+)\s*=\s*(.+)\)/g,
            'matches':
            {
                1: {'include': '#word_like_names'},
                2: {'include': '#floats'}
            }
        }
    ],
    'repository':
    {
        'word_like_names':
        {
            'name': 'support.type.some_lang'
            'pattern': /[A-z_]\w*/g
        },
        'floats':
        {
            'name': 'constant.numeric.float.some_lang'
            'pattern': /\d+.\d+/g
        }
    }
}

You can include one repository item into another one, so you can create "groups" as well, if needed.

What I did above is a stupid example, how I imagine this thing in your system, since you don't have begin and end, so I just imagine, that when you catch "everything", you tell what do you want to catch in everything.

petervaro avatar Jul 22 '13 23:07 petervaro

Although the $self is a big problem, that can not be solved with language nor with include.. The 1: {'include': '$self'} statement means, that everything inside the regex defeinition will be captured here. For example:

{
    'patterns':
    [
        {
            'pattern': /\b(lambda)\s+([A-z_]\w*)\s*(=)\s*(.+)/g,
            'matches':
            {
                1: 'storage.type.function.anonymous.python'
                2: 'variable.parameter.function.python',
                3: 'keyword.operator.assignment.python'
                4: {'include': '$self'}
            }
        }
    ]
}

So if there is another lambda expression in the keyword argument assignment which also has one keyword argument, then it will be captured.

petervaro avatar Jul 22 '13 23:07 petervaro