获取Sublime Text 3插件的正确缩进级别的位置

时间:2016-08-25 21:10:30

标签: sublimetext3 sublime-text-plugin

我试图创建一个Sublime Text 3插件,该插件应该在正确的缩进级别的所选行插入文本。

任何人都知道如何实现这一目标?

这就是我到目前为止:

class HelloWorldCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for pos in self.view.sel():
            line = self.view.line(pos)
            # Get Correct indentation level and pass in instead of line.begin()
            self.view.insert(edit, line.begin(), "Hello world;\n")

1 个答案:

答案 0 :(得分:1)

我通常只是这样做并使用python进行缩进:

class HelloWorldCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for sel in self.view.sel():
            line = self.view.line(sel)
            line_str = self.view.substr(line)
            indent = len(line_str) - len(line_str.lstrip())
            bol = line.begin() + indent
            self.view.insert(edit, bol, "Hello world;\n")

如果要保留缩进,可以将最后一行更改为:

self.view.insert(edit, bol, "Hello world;\n" + line_str[:indent])