-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_convert_indentation.py
129 lines (100 loc) · 4.24 KB
/
plugin_convert_indentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from os.path import normpath
from sublime import packages_path, load_resource, decode_value, windows
from sublime_plugin import TextCommand, EventListener
class ConvertIndentationCommand(TextCommand):
"""
Convert indentation to reflect the user's settings.
Run automatically based on _auto_convert_indentation setting.
"""
def run(self, _):
view = self.view
if view.is_read_only():
return
if view.is_loading():
return
settings = view.settings()
tab_size = settings.get("tab_size")
use_spaces = settings.get("translate_tabs_to_spaces")
# detect_indentation updates settings
view.run_command("detect_indentation", {"show_message": False, "threshold": 1})
file_tab_size = settings.get("tab_size")
file_use_spaces = settings.get("translate_tabs_to_spaces")
if file_tab_size == tab_size and file_use_spaces == use_spaces:
return
# spaces -> spaces
if file_use_spaces and use_spaces:
view.run_command("unexpand_tabs", {"set_translate_tabs": False})
settings.set("tab_size", tab_size)
view.run_command("expand_tabs", {"set_translate_tabs": False})
# spaces -> tabs
elif file_use_spaces and not use_spaces:
view.run_command("unexpand_tabs", {"set_translate_tabs": False})
# tabs -> spaces
elif not file_use_spaces and use_spaces:
view.run_command("expand_tabs", {"set_translate_tabs": False})
# restore settings
settings.set("tab_size", tab_size)
settings.set("translate_tabs_to_spaces", use_spaces)
class ConvertIndentationListener(EventListener):
# convert indentation when opening a file
def on_load(self, view):
settings = view.settings()
auto_indent = settings.get("_auto_convert_indentation")
detect_indent = settings.get("detect_indentation")
if not auto_indent:
return
if detect_indent:
return
view.run_command("convert_indentation")
# convert indentation when view changes its settings
def on_post_text_command(self, view, command, args):
if command not in ("set_setting", "toggle_setting"):
return
if args.get("setting") not in ("tab_size", "translate_tabs_to_spaces"):
return
settings = view.settings()
auto_indent = settings.get("_auto_convert_indentation")
if auto_indent:
view.run_command("convert_indentation")
# convert indentation in open files when Preferences.sublime-settings change
def on_post_save(self, view):
settings_path = normpath(packages_path() + "/User/Preferences.sublime-settings")
file_name = view.file_name()
if file_name != settings_path:
return
settings = load_resource("Packages/User/Preferences.sublime-settings")
settings = decode_value(settings)
auto_indent = settings.get("_auto_convert_indentation")
if not auto_indent:
return
tab_size = settings.get("tab_size")
use_spaces = settings.get("translate_tabs_to_spaces")
views = [v for w in windows() for v in w.views()]
for view in views:
view_settings = view.settings()
view_tab_size = view_settings.get("tab_size")
view_use_spaces = view_settings.get("translate_tabs_to_spaces")
if view_tab_size != tab_size or view_use_spaces != use_spaces:
view_settings.set("tab_size", tab_size)
view_settings.set("translate_tabs_to_spaces", use_spaces)
view.run_command("convert_indentation")
# convert indentation when pasting
def on_text_command(self, view, command, args):
if command not in ("paste", "paste_and_indent"):
return
settings = view.settings()
auto_indent = settings.get("_auto_convert_indentation")
if not auto_indent:
return
tab_size = settings.get("tab_size")
use_spaces = settings.get("translate_tabs_to_spaces")
window = view.window()
view = window.new_file()
settings = view.settings()
settings.set("tab_size", tab_size)
settings.set("translate_tabs_to_spaces", use_spaces)
view.run_command("paste")
view.run_command("convert_indentation")
view.run_command("select_all")
view.run_command("cut")
view.close()