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

Sublime Text: Hide all code and show only comments (with line break)

之前我问了一个关于如何在 Sublime Text 3 中 的问题。

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)

以下是一些示例图片:

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

如您所见,一切都落在一条线上。这可能会令人困惑和混乱。 我现在真正想做的是得到这样的东西:

有没有办法通过编辑插件来实现这一点?

您可以更改之前的插件以在折叠区域之后保持领先 "newlines" 和 "newlines and the indent":

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)