Reputation: 4180
I'm trying to create a Sublime Text 3 plugin which should insert text at the selected line(s) at the correct indentation level.
Anyone knows how to achieve this?
This is what I have thus far:
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")
Upvotes: 0
Views: 102
Reputation: 4847
I usually just do it like this and get the indent with 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")
If you want to keep the indentation you can change the last line to:
self.view.insert(edit, bol, "Hello world;\n" + line_str[:indent])
Upvotes: 1