2016-08-25 3 views
0

私は、正しいインデントレベルで、選択した行にテキストを挿入するSublime Text 3プラグインを作成しようとしています。サブライムテキスト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

私は通常ちょうどこのようにそれを行うと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]) 
関連する問題