forked from akof1314/Sublime-CoolFormat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoolformat.py
182 lines (168 loc) · 4.78 KB
/
Coolformat.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import sublime, sublime_plugin
from ctypes import *
import os
__file__ = os.path.normpath(os.path.abspath(__file__))
__path__ = os.path.dirname(__file__)
class CoolformatCommand(sublime_plugin.TextCommand):
def run(self, edit, action = 'quickFormat'):
if action == 'quickFormat':
self.doFormatSafe(edit, False)
elif action == 'selectedFormat':
self.doFormatSafe(edit, True)
else:
self.showSettings()
def doFormatSafe(self, edit, selected):
try:
self.doFormat(edit, selected)
except Exception as e:
print(str(e))
sublime.message_dialog('Cannot format this file!')
def doFormat(self, edit, selected):
self.loadCFDll()
if self.DoFormatter == None:
return
view = self.view
line_eol = self.getCFEol()
lang = self.getCFLang()
if selected:
regions = []
for sel in view.sel():
initIndent = self.getInitIndent(min(sel.a, sel.b))
region = sublime.Region(
view.line(min(sel.a, sel.b)).a, # line start of first line
view.line(max(sel.a, sel.b)).b # line end of last line
)
code = view.substr(region)
formatted_code = self.getFormattedCode(code, lang, line_eol, initIndent)
view.replace(edit, region, formatted_code)
if sel.a <= sel.b:
regions.append(sublime.Region(region.a, region.a + len(formatted_code)))
else:
regions.append(sublime.Region(region.a + len(formatted_code), region.a))
view.sel().clear()
[view.sel().add(region) for region in regions]
else:
region = sublime.Region(0, view.size())
code = view.substr(region)
formatted_code = self.getFormattedCode(code, lang, line_eol, None)
view.replace(edit, region, formatted_code)
def showSettings(self):
self.view.window().open_file(__path__ + '/CoolFormatLib/CoolFormatConfig.cfconfig')
"""
self.loadCFDll()
if self.ShowSettings:
self.ShowSettings()
"""
def getInitIndent(self, point):
line_region = self.view.line(point)
code = self.view.substr(line_region)
initIndent = ''
for _, ch in enumerate(code):
if ch != ' ' and ch != '\t':
break
initIndent = initIndent + ch
return initIndent
def getCFLang(self):
lang = self.view.settings().get('syntax') # eg Packages/C++/C++.tmLanguage
lang = lang[9:lang.find('/', 9)]
return Synlanguage.lang_dict.get(lang, -1)
def getCFEol(self):
line_eol = self.view.line_endings()
return '\n'
"""
if line_eol == 'Windows':
return '\r\n'
elif line_eol == 'Unix':
return '\n'
else:
return '\r'
"""
def getFormattedCode(self, code, lang, line_eol, initIndent):
if self.DoFormatter:
sizeTextOut = c_int()
sizeMsgOut = c_int()
strTextIn = c_char_p(code.encode('utf-8'))
strLineEol = c_char_p(line_eol.encode('utf-8'))
strInitIndent = c_char_p(initIndent.encode('utf-8')) if initIndent else None
if self.DoFormatter(lang, strTextIn, None, byref(sizeTextOut), None, byref(sizeMsgOut), 0, strLineEol, strInitIndent):
strTextOut = create_string_buffer(sizeTextOut.value + 1)
strMsgOut = create_string_buffer(sizeMsgOut.value + 1)
if self.DoFormatter(lang, strTextIn, strTextOut, byref(sizeTextOut), strMsgOut, byref(sizeMsgOut), 0, strLineEol, strInitIndent):
self.showOutput(strMsgOut.value.decode('utf-8'))
return strTextOut.value.decode('utf-8')
return code
def showOutput(self, msg):
if len(msg) != 0:
self.view.window().run_command("show_panel", {"panel": "console"})
print('=========== CoolFormat Output Begin ===========')
print(msg)
print('=========== CoolFormat Output End ===========')
def loadCFDll(self):
if self.hInstCF == None:
platform_name = sublime.platform()
if platform_name == 'windows':
dll_name = '/CoolFormatLib.dll'
elif platform_name == 'osx':
dll_name = '/libCoolFormatLib.dylib'
else:
dll_name = '/libCoolFormatLib.so'
self.hInstCF = cdll.LoadLibrary(__path__ + '/CoolFormatLib/cf_' + platform_name + '_' + sublime.arch() + dll_name)
if self.hInstCF:
self.DoFormatter = self.hInstCF.DoFormatter
self.ShowSettings = self.hInstCF.ShowSettings
else:
sublime.message_dialog('Cannot load CoolFormatLib library!')
hInstCF = None
DoFormatter = None
ShowSettings = None
class Synlanguage:
(
SYN_ACTIONSCRIPT,
SYN_ADA,
SYN_ASM,
SYN_ASP,
SYN_AUTOHOTKEY,
SYN_AUTOIT,
SYN_BATCH,
SYN_COBOL,
SYN_CPP,
SYN_CS,
SYN_CSS,
SYN_D,
SYN_FORTRAN,
SYN_HASKELL,
SYN_HTML,
SYN_INI,
SYN_JAVA,
SYN_JAVASCRIPT,
SYN_JSON,
SYN_JSP,
SYN_LISP,
SYN_LUA,
SYN_NORMALTEXT,
SYN_OBJECTIVEC,
SYN_PASCAL,
SYN_PERL,
SYN_PHP,
SYN_PYTHON,
SYN_RUBY,
SYN_SQL,
SYN_VB,
SYN_VERILOG,
SYN_VHDL,
SYN_XML
) = range(0, 34)
lang_dict = {
'C': SYN_CPP,
'C++':SYN_CPP,
'C#':SYN_CS,
'CSS':SYN_CSS,
'HTML':SYN_HTML,
'Java':SYN_JAVA,
'JavaScript':SYN_JAVASCRIPT,
'JSON':SYN_JSON,
'Objective-C':SYN_OBJECTIVEC,
'Objective-C++':SYN_OBJECTIVEC,
'PHP':SYN_PHP,
'SQL':SYN_SQL,
'XML':SYN_XML }