【问题标题】:Get position of the correct indentation level for a Sublime Text 3 plugin获取 Sublime Text 3 插件的正确缩进级别的位置
【发布时间】:2016-08-25 21:10:30
【问题描述】:

我正在尝试创建一个 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")

【问题讨论】:

    标签: sublimetext3 sublime-text-plugin


    【解决方案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])
    

    【讨论】:

      猜你喜欢
      • 2015-12-11
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      相关资源
      最近更新 更多