, Sublime . , , . , XML/HTML, . Sublime , , .
Sublime Plugins
, tidy , , , , . . , , , , . , , , , .
(Ctrl+n) " ..." "" "Hello World!". ( Python), sublime_plugin.TextCommand. TextCommand / . WindowCommand ApplicationCommand, run.
API , , Sublime, Packages/Default Sublime, - . .
, View, . , TextCommand, , , , , , .
: , <start-tag>, <empty-tag/>, </close-tag> text-node. , , . . , , , . , , .
Python, . , , , . , , , . .
Another thing to take care of is to bind the keys to the plugin command and contribute to the menu items. Probably, perhaps somehow , but the files .sublime-menuand .sublime-commandsby default at least give an idea. Anyway, here is some kind of code. It should be saved as Packages/User/whatever.py, and may be invoked from the Python Console Sublime ( ) as follows: .Ctrl + `view.run_command('guess_indentation')
The code
import sublime
import sublime_plugin
import re
class GuessIndentationCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
start_tag = '<\w+(?:\s+[^>\/]+)*\s*>'
node_patterns = [start_tag,
start_tag[:-1]+'\/\s*>',
'<\/\s?\w+\s?>',
'[^>\s][^<>]*[^<\s]']
patterns = '(?:{0})'.format('|'.join(node_patterns))
indentors = re.compile('[ \t]*({0})'.format('|'.join(node_patterns[:1])))
unindentors=re.compile('[ \t]*({0})'.format(node_patterns[2]))
for region in view.sel():
if not region.empty():
selection = view.substr(region)
expanded = []
for line in selection.split('\n'):
elements = re.findall(patterns, line)
if len(elements)>0:
expanded += elements
else:
expanded.append(line)
indent=0
indented = []
for line in expanded:
match = unindentors.match(line)
if match:
indent = max(0, indent-1)
indented.append('\t'*indent+line)
if match:
continue
match = indentors.match(line)
if match:
indent+=1
view.replace(edit, region, '\n'.join(indented))