Sublime Text

修改Sublime的注释格式

2017-02-21  本文已影响735人  Floyda

提出问题

群里有个朋友提出这个问题.

问个问题:
使用 perl 语言开发,
当我使用 command+/ 快捷键完成注释时,
他会自动在代码前面添加一个井号和空格,
问一下我要如何修改可以让他不多加这个空格呢?

我期望的注释风格是:#warn 'hello world';

So, 将解决过程记录下来.

定位问题

...
ignored_packages: ["Vintage"]
pre session restore time: 0.19431
warning: mnemonic v not found in menu caption SVN
startup time: 0.26231
first paint time: 0.26631
first paint time: 0.26731
reloading plugin Default.auto_indent_tag
reloading plugin Default.block
reloading plugin Default.comment # <-- 注意这一行
reloading plugin Default.convert_syntax
reloading plugin Default.copy_path
reloading plugin Default.delete_word
reloading plugin Default.detect_indentation
reloading plugin Default.duplicate_line
...

  1. 解压Default.sublime-package, 修改里面的comment.py, 再打包成Default.sublime-package, 丢回安装目录.
  2. comment.py放到Package的User目录里面.

第一种方法的缺点是麻烦, 而且版本升级的时候, Default.sublime-package有可能会被覆盖.
第二种方法是正解.

解决问题

def advance_to_first_non_white_space_on_line(view, pt):
    ...

def has_non_white_space_on_line(view, pt):
    ...

def build_comment_data(view, pt):
    ...

class ToggleCommentCommand(sublime_plugin.TextCommand):
    def remove_block_comment(self, view, edit, comment_data, region):
        ...

    def remove_line_comment(self, view, edit, comment_data, region):
        ...

    def is_entirely_line_commented(self, view, comment_data, region):
        ...

    def block_comment_region(self, view, edit, block_comment_data, region):
        ...

    def line_comment_region(self, view, edit, line_comment_data, region):
        ...

    def add_comment(self, view, edit, comment_data, prefer_block, region):
        ...

    def run(self, edit, block=False)
        ...

1个Command类, 3个方法.

无论是注释, 还是反注释. 是行注释, 还是块注释.
研究代码之后, 可知, comment_data都是由build_comment_data这个方法构造的.
So, 修改这个函数就OK了.

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)
        disable_indent = all_vars.setdefault("TM_COMMENT_DISABLE_INDENT" + suffix)

        if start and end:
            #[comment by Floyda]block_comments.append((start, end, disable_indent == 'yes'))
            block_comments.append((start.strip() + "[comment by Floyda]", end, disable_indent == 'yes'))
            block_comments.append((start.strip(), end.strip(), disable_indent == 'yes'))
        elif start:
            #[comment by Floyda]line_comments.append((start, disable_indent == 'yes'))
            line_comments.append((start.strip() + "[comment by Floyda]", disable_indent == 'yes'))
            line_comments.append((start.strip(), disable_indent == 'yes'))
    return (line_comments, block_comments)

这样, 我注释过的代码, 别人即使反注释, 也是很蛋疼的 /斜眼笑
So, 群里的朋友的问题也解决了.

最后, 把comment.py丢到Package的User目录.

上一篇下一篇

猜你喜欢

热点阅读