Sublime Forum

Restructuredtext headline text highlighting

#1

I’m trying to achieve syntax highlighting for the text in restructuredtext section headings in ST3. Here’s a link to an earlier discussion on this: Implement a better reStructuredText syntax highlighting. I’ve tried two approaches.

First, Kiwi Lorenz’s tmLanguage spec reStructuredText Improved which uses the begin/end construction with a backreference.

    <key>begin</key>
    <string>(?x) ^ (?= (\S+\x{00000020})* \S+ $) (?= (?!\d[\.\)]) \w .{2,55} (?&lt;!\.|::|\\|\x{00000020}) $)</string>
    <key>contentName</key>
    <string>markup.heading.headline.or-term.restructuredtext</string>
    <key>end</key>
    <string>(?x) ^ (?=[\x{00000020}\t]+\S) | ^ ( ([=\-`:.'"~^_*+\#])\2{2,55} | \s* | (?=\|\s*$) )$</string>
    <key>endCaptures</key>
    <dict>
        <key>1</key>
        <dict>
            <key>name</key>
            <string>keyword.operator.heading.restructuredtext</string>
        </dict>
    </dict>

The problem using this approach is that while typing regular paragraph text (between 3 and 55 characters), the editor will switch back and forth between ‘markup.heading.headline.or-term.restructuredtext’ and
‘meta.paragraph.restructuredtext’. This is really distracting if heading and paragraph text are highlighted differently. For example, the following text, without a terminating character or space, is highlighted as a heading (bc it’s scoped as a term).

foobar

So, I thought the sublime-syntax push/pop functionality could accomplish header text highlighting, but I have been unsuccessful at modifying the default sublime restructuredtext syntax file Here so that the text in section headings is highlighted. Currently the underline is highlighted, but not the text. The relevant part of the file is the following.

    - match: '(^(=|-|~|`|#|"|\^|\+|\*){3,}$){1,1}?'
      scope: markup.heading.restructuredtext
      captures:
        1: punctuation.definition.heading.restructuredtext

Neither the provided examples in the docs nor looking through recently updated languages (e.g. Go), have helped me get there.

I would appreciate any help/suggestions for how to implement this. I’m open to using either tmLanguage or sublime-syntax.

Thanks,
CL

0 Likes

#2

Since you can only look at one line at a time and cannot change a scope of a line you passed, constructs that rely on contents of a following line to be properly recognized are not possible. Not properly, at least.

Kiwi’s method is basically a hack that tries to recognize a heading by how the line is structured.

1 Like

#3

the same constraint also impacts ST’s scoping of Markdown files, unfortunately.

i.e.

Heading
-----------

there is no way for ST to know that Heading is a heading, so it can only highlight and show the -------- in Goto Symbol. But, fortunately, Markdown offers alternative syntax that ST can recognize:

# Heading

maybe restructuredtext also offers alternative syntax?

A feature request to allow syntax definitions to work with more than one line at a time has been filed here:

1 Like

#4

Thank you both. Yeah, I was trying to use the new sublime-syntax features to achieve the same function as multiline regex. I appreciate you taking the time to respond. I’ll follow the feature request. Should I mark the thread closed?

0 Likes

#5

Just leave it be.

1 Like