gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:34 24 4
gpt4 key购买 nike

之前我问过一个关于如何hide everything except comments的问题在 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)

以下是一些示例图片:

enter image description here

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

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

enter image description here

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

最佳答案

您可以更改以前的插件以在折叠区域后保留前导“换行符”和“换行符和缩进”:

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)

关于python - Sublime Text : Hide all code and show only comments (with line break),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38014950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com