Home Download Buy Blog Forum Support

Python syntax highlighting for docstrings

Python syntax highlighting for docstrings

Postby kporangehat on Wed Oct 17, 2012 9:54 pm

Hey all,

The Python syntax highlighting is pretty awesome and spot on, but it renders docstrings as strings. This may sound right by definition, but I'm finding it very distracting and would rather it render docstrings like comments.

In the code below, I'd like the docstring in """s to render in gray text like the comment in line 7 but without also changing the display of regular strings. I took a look at the language definitions and tried to hack it in, but quickly broke things and had to revert back. I'll go RTFM again and allocate some time to figure this out on my own if need be, but wondering if anyone else has already done this or if I could piggyback of of someone's awesome knowledge ;)

Thanks.
kp

Code: Select all
def foobar(foo=None):
    """Do something foobar.
   
    This sets myvar to the value of foo and then returns the value
    and is generally useless
    """
    # if foo is None, say so.
    if foo is None:
        print "Foo is None. Even more useless."
    myvar = foo
    return myvar


This is how it renders:

python_docstring_syntax.png
python_docstring_syntax.png (25.29 KiB) Viewed 1276 times


[edited to fix image display]
kporangehat
 
Posts: 2
Joined: Wed Oct 17, 2012 9:37 pm

Re: Python syntax highlighting for docstrings

Postby facelessuser on Wed Oct 17, 2012 10:56 pm

Go into your tmTheme that you are using and change the comment section to include the python string scopes.

        <dict>
<key>name</key>
<string>Comment</string>
<key>scope</key>
<string>comment, string.quoted.double.block.python</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#757575</string>
</dict>
</dict>



Then you will get something like this:
def get(settings_obj, key, default=None, callback=None):
"""
Return a Sublime Text plugin setting value

Parameters:
settings_obj - a sublime.Settings object or a dictionary containing
settings
key - the name of the setting
default - the default value to return if the key value is not found.
callback - a callback function that, if provided, will be called with
the found and default values as parameters.

"""
# Parameter validation
if not isinstance(settings_obj, (dict, sublime.Settings)):
raise AttributeError("Invalid settings object")
if not isinstance(key, basestring):
raise AttributeError("Invalid callback function")
if callback != None and not hasattr(callback, '__call__'):
raise AttributeError("Invalid callback function")

setting = settings_obj.get(key, default)
final_val = None
Last edited by facelessuser on Thu Oct 18, 2012 12:14 am, edited 1 time in total.
facelessuser
 
Posts: 1549
Joined: Tue Apr 05, 2011 7:38 pm

Re: Python syntax highlighting for docstrings

Postby facelessuser on Thu Oct 18, 2012 12:13 am

I meant tmTheme. Corrected above.
facelessuser
 
Posts: 1549
Joined: Tue Apr 05, 2011 7:38 pm

Re: Python syntax highlighting for docstrings

Postby kporangehat on Thu Oct 18, 2012 12:45 pm

ahhhhh... thank you @facelessuser! My sanity has returned.
kporangehat
 
Posts: 2
Joined: Wed Oct 17, 2012 9:37 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 12 guests