ehuss / Sublime-Wrap-Plus

Enhanced "wrap lines" command for Sublime Text 2 or 3.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tripple slash...

sean- opened this issue · comments

Wrapping lines like:

/// First line of a doxygen C++ comment
/// And the second line of a doxy comment

Should work, but instead it wraps it as:

/// First line of a doxygen C++ comment /
// And the second line of a doxy comment

It's like the pattern match is for only two slashes, not three. Seems like an easy fix, but I haven't looked in to it.

Hi,

I've search to do what you want and it's not as easy as it sounds...

The comment mark is guessed for the current file (& current line) using Sublime Text API:

    def _determine_comment_style(self):
        # I'm not exactly sure why this function needs a point.  It seems to
        # return the same value regardless of location for the stuff I've
        # tried.
        (self._lc, self._bc) = comment.build_comment_data(self.view, 0)

(this code is here: https://github.com/ehuss/Sublime-Wrap-Plus/blob/master/wrap_plus.py#L432)

In this code, the comment property is an import from Sublime Text API:

try:
    import Default.comment as comment
except ImportError:
    import comment

(https://github.com/ehuss/Sublime-Wrap-Plus/blob/master/wrap_plus.py#L6)

According to Sublime Text documentation (http://www.sublimetext.com/docs/3/porting_guide.html), this import Default.comment will import the Packages/Default/Comment.py file.
Here is the definition of the build_comment_data function from this file:

def build_comment_data(view, pt):
    shell_vars = view.meta_info("shellVariables", pt)
    if not shell_vars:
        return ([], [])

    # transform the list of dicts into a single dict
    all_vars = {}
    for v in shell_vars:
        if 'name' in v and 'value' in v:
            all_vars[v['name']] = v['value']

    line_comments = []
    block_comments = []

    # transform the dict into a single array of valid comments
    suffixes = [""] + ["_" + str(i) for i in range(1, 10)]
    for suffix in suffixes:
        start = all_vars.setdefault("TM_COMMENT_START" + suffix)
        end = all_vars.setdefault("TM_COMMENT_END" + suffix)
        mode = all_vars.setdefault("TM_COMMENT_MODE" + suffix)
        disable_indent = all_vars.setdefault("TM_COMMENT_DISABLE_INDENT" + suffix)

        if start and end:
            block_comments.append((start, end, disable_indent == 'yes'))
            block_comments.append((start.strip(), end.strip(), disable_indent == 'yes'))
        elif start:
            line_comments.append((start, disable_indent == 'yes'))
            line_comments.append((start.strip(), disable_indent == 'yes'))

    return (line_comments, block_comments)

Simply done, this code retrieve the shellVariables value for the current view, then look for some variables like: TM_COMMENT_START, TM_COMMENT_END, TM_COMMENT_MODE and TM_COMMENT_DISABLE_INDENT, and additionally those variable with prefixes like TM_COMMENT_START_1, TM_COMMENT_START_2 ... up to TM_COMMENT_START_10.

Knowing that, if we can define our own TM_COMMENT_START_xx values, it would be possible to handle the case of the triple / ///. And guess what ?! We can define them ! According to https://github.com/SublimeText/Modelines, the shellVariables is defined inside all *.tmPreferences files, such as Packages/Python/Miscellaneous.tmPreferences:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>name</key>
    <string>Comments</string>
    <key>scope</key>
    <string>source.js, source.json</string>
    <key>settings</key>
    <dict>
        <key>shellVariables</key>
        <array>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_START</string>
                <key>value</key>
                <string>// </string>
            </dict>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_START_2</string>
                <key>value</key>
                <string>/*</string>
            </dict>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_END_2</string>
                <key>value</key>
                <string>*/</string>
            </dict>
        </array>
    </dict>
    <key>uuid</key>
    <string>A67A8BD9-A951-406F-9175-018DD4B52FD1</string>
</dict>
</plist>

TL;DR;

Then, you can create a Comments.tmPreferences file in your Packages/User folder (use the command Browse Packages from the Command Palette) with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>name</key>
    <string>Comments</string>
    <key>scope</key>
    <string>source.js, source.json</string>
    <key>settings</key>
    <dict>
        <key>shellVariables</key>
        <array>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_START_5</string>
                <key>value</key>
                <string>/// </string>
            </dict>
        </array>
    </dict>
    <key>uuid</key>
    <string>A67A8BD9-A951-406F-9175-018DD4B52FD1</string>
</dict>
</plist>

What's important in this file is the key name as TM_COMMENT_START and TM_COMMENT_START_1 are already used. TM_COMMENT_START_5 looks like a good choice (it works for me). In the value you can put whatever you want, and you might want to change the source.js, source.json value if you want this additional comment start mark to be active in files other than JavaScript/JSON files.

Hope this will help !