我修改了上面的代码供我自己使用,所以它现在尊重空白。但是我硬编码制表符而不是空格,所以如果你使用空格,你可能需要进一步改变它。它现在还假设您没有选择文本,而是将光标放在要更改为垂直间距的行中间。我将 intro/outro 作为参数保留,因此您也可以将其用于 [] 或 (),尽管在这种情况下,对于正则表达式可能需要更多的转义。
之前:
fields = { 'Team1', 'Team2', 'Player1', 'Player2', 'Tab=Round', 'DateTime_UTC=DateTime', 'HasTime=TimeEntered', 'OverviewPage=Tournament', 'ShownName', 'Winner', 'Stream' },
之后:
fields = {
'Team1',
'Team2',
'Player1',
'Player2',
'Tab=Round',
'DateTime_UTC=DateTime',
'HasTime=TimeEntered',
'OverviewPage=Tournament',
'ShownName',
'Winner',
'Stream',
},
import sublime
import sublime_plugin
import re
class SplitLineCommand(sublime_plugin.TextCommand):
def run(self, edit, sep=",", repl= "\n", intro="{", outro="}"):
view = self.view
find = re.escape(sep + ' ') + '*(?! *$| *\n)'
intro_repl = intro + repl
intro = intro + ' *'
outro_repl_start = sep + repl
outro_repl_end = outro
outro = ',? *' + outro
repl = sep + repl
cursors = view.sel()
if len(cursors) == 1:
cursor = cursors[0]
begin_offset = 0
end_offset = 0
if cursor.empty():
region = view.line(cursor)
content = view.substr(region)
line_str = view.substr(view.line(view.sel()[0]))
tabs = len(line_str) - len(line_str.lstrip())
intro_repl = intro_repl + '\t' * (tabs + 1)
repl = repl + '\t' * (tabs + 1)
outro_repl = outro_repl_start + ('\t' * tabs) + outro_repl_end
content = re.sub(outro, outro_repl, content)
content = re.sub(find, repl, content)
content = re.sub(intro, intro_repl, content)
view.replace(edit, region, content)
cursors.clear()
cursors.add(sublime.Region(region.begin() + begin_offset, region.begin() + len(content) + end_offset))
view.run_command("split_selection_into_lines")