Sublime Text:隐藏所有代码并仅显示注释(带换行符)

时间:2016-06-24 13:45:09

标签: python sublimetext3 sublimetext

之前我在Sublime Text 3中询问了如何hide everything except comments的问题。

r-stein制作了一个方便的插件,可以做到这一点:

import sublime_plugin

class FoldEverythingExceptCommentsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        regions = self.view.find_by_selector("-comment")
        self.view.fold(regions)

以下是一些示例图片:

enter image description here

这是代码最初看起来的样子...... 如果我们使用插件隐藏除评论之外的所有内容,我们就会得到:

enter image description here 正如你所看到的,一切都在一条线上。这可能令人困惑和混乱。 我现在真正想做的是得到这样的东西:

enter image description here

有没有办法编辑插件来实现这个目标?

1 个答案:

答案 0 :(得分:1)

您可以更改以前的插件以保持领先"换行符"和"换行符和缩进"在折叠区域之后:

import sublime
import sublime_plugin


def char_at(view, point):
    return view.substr(sublime.Region(point, point + 1))


def is_space(view, point):
    return char_at(view, point).isspace()


def is_newline(view, point):
    return char_at(view, point) == "\n"


class FoldEverythingExceptCommentsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        regions = view.find_by_selector("-comment")
        fold_regions = []

        for region in regions:
            a, b = region.begin(), region.end()
            # keep new line before the fold
            if is_newline(view, a):
                a += 1
            # keep the indent before next comment
            while is_space(view, b - 1):
                b -= 1
                if is_newline(view, b):
                    break
            # if it is still a valid fold, add it to the list
            if a < b:
                fold_regions.append(sublime.Region(a, b))

        view.fold(fold_regions)