崇高文本:隐藏所有代码,只显示注释(带换行符)

2024-06-16 14:59:32 发布

您现在位置:Python中文网/ 问答频道 /正文

之前我问了一个关于如何在崇高文本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

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


Tags: 文本importself插件viewplugincommentsclass
1条回答
网友
1楼 · 发布于 2024-06-16 14:59:32

您可以更改以前的插件,使“换行”和“换行和缩进”位于折叠区域之后:

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)

相关问题 更多 >