Вопрос

I'd like to differentiate between Python docstrings and single-line strings in Sublime Text 2. Looking at the Python language definition, I can see this, along with a matching definition for apostrophe-strings that uses the same comment.block.python name.

<dict>
    <key>begin</key>
    <string>^\s*(?=[uU]?[rR]?""")</string>
    <key>end</key>
    <string>(?&lt;=""")</string>
    <key>name</key>
    <string>comment.block.python</string>
...

But when I create a new color rule like this:

<dict>
    <key>name</key>
    <string>Docstring</string>
    <key>scope</key>
    <string>comment.block.python</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#008800</string>
    </dict>
</dict>

Nothing happens; they remain colored the same as single-line strings.

Is it possible to do this? If so, what am I doing wrong?

Это было полезно?

Решение

Maybe change to:

<dict>
    <key>name</key>
    <string>Docstring</string>
    <key>scope</key>
    <string>string.quoted.double.block.python</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#008800</string>
    </dict>
</dict>

I only tested it briefly, but it seemed to work.

Другие советы

I think that Python.tmLanguage has a little bug in it. Here's the fix for docstrings, although i am not sure if it's a nice one:

  • replace the docstrings key with:

    <key>docstrings</key>
    <dict>
        <key>patterns</key>
        <array>
            <dict>
                <key>begin</key>
                <string>^\s*([uU]?[rR]?""")</string>
                <key>end</key>
                <string>(""")</string>
                <key>name</key>
                <string>comment.block.python</string>
            </dict>
            <dict>
                <key>begin</key>
                <string>^\s*(?=[uU]?[rR]?''')</string>
                <key>end</key>
                <string>(?&lt;=''')</string>
                <key>name</key>
                <string>comment.block.python</string>
            </dict>
        </array>
    </dict>
    
  • add a single dict to as a first array of string_quoted_double and string_quoted_single

    <key>string_quoted_double</key>
    <dict>
        <key>patterns</key>
        <array>
            <dict>
                <key>include</key>
                <string>#docstrings</string>
            </dict>
    

This should automatically make docstrings become comments.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top