-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPrettify.py
More file actions
511 lines (248 loc) · 8.15 KB
/
CPrettify.py
File metadata and controls
511 lines (248 loc) · 8.15 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import sublime
import sublime_plugin
import subprocess
import os
import shlex
from shutil import copy
# os.system can't be used here since it creates new cmd window
#arguments containing the code
#initiate load settings settings
#so can be access from anywhere
setting = None
flag = 0
user_setting = None
user_config = False
#For restoring purposes.
file_ = None
chkflag = None
def init(view):
global setting
global user_setting
global file_
global chkflag
#custom settings file for package. handling view.settings() in the fuction getSettings().
setting = sublime.load_settings("CPrettify.sublime-settings")
user_setting = sublime.load_settings("Preferences.sublime-settings")
file_ = getSettings(view,'config_file')
chkflag = getSettings(view,'user_config_file')
#checking in view.settings() first
if file_ is None:
file_ = setting.get('config_file')
if chkflag is None:
chkflag = setting.get('user_config_file')
#function: getSettings()
#Description: Accessing the view's setting object,(project specific settings).
#returns: returns view's setting object.
def getSettings(view,arg):
return view.settings().get(arg)
def execute(args):
info = None
if os.name == 'nt':
info=subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#building file path
packageDir=sublime.packages_path()
libDir=os.path.join('CPrettify','lib')
fDir=os.path.join(packageDir,libDir)
fileDir=os.path.join(fDir,'ben.cfg')
#test file for debug only
tfile=os.path.join(fDir,"abc.c")
#check if the file exists:
if os.path.isfile(fileDir):
#for dbug only
print("config file exists!")
cmd = [dirSetup(),"-c",fileDir,"--no-backup",tfile]
pR=p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,startupinfo = info)
output , stderr = p.communicate()
print(output)
print(stderr)
def restore_config():
global file_
file = get_path();
if file_ is None:
#error
#TODO
#raise error/exception to sublime here
print("no config file found in that name")
return
original_config = os.path.join(file,file_)
original_config = original_config
backup_config = os.path.join(file,file_+'.bk')
copy(backup_config,original_config)
def get_path():
#building file path
packageDir=sublime.packages_path()
libDir=os.path.join('CPrettify','lib')
fDir=os.path.join(packageDir,libDir)
return fDir
def userFoldercheck():
packageDir=sublime.packages_path()
dirFolder=os.path.join(packageDir,'User','CPrettify')
if not os.path.isdir(dirFolder):
#TODO
#change to makedirs
os.mkdir(dirFolder)
cfgfile=os.path.join(dirFolder,'user.cfg')
return os.path.isfile(cfgfile)
def execute_(view,edit,region):
global flag
global file_
global chkflag
info = None
file_cfg = None
if os.name == 'nt':
info=subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#getting config from default settings
#"https://www.sublimetext.com/docs/3/api_reference.html#sublime.Settings"
#check if user has own cfg
isCustomCfg = eval(chkflag)
if isCustomCfg:
packageDir=sublime.packages_path()
file_cfg=os.path.join(packageDir,'User','CPrettify','user.cfg')
config_ = file_
if config_ is None:
#error
sublime.status_message("No config file found.. reinstall plugin")
return
#for debug only
if isCustomCfg is False:
print('using ' + config_ +' file..')
else:
print('using ' + file_cfg + ' file')
if not userFoldercheck():
sublime.status_message("Not provided user config file")
flag = 1
return
packageDir=sublime.packages_path()
libDir=os.path.join('CPrettify','lib')
fDir=os.path.join(packageDir,libDir)
fileDir=os.path.join(fDir, config_)
#test file for debug only
#tfile=os.path.join(fDir,"abc.c")
#check if config file exists in lib folder:
if os.path.isfile(fileDir):
#debug if occurs
#for dbug only
print("config file exists!")
else:
flag =1
sublime.status_message("Provided Default config file doesnt exist")
return
cmd = [dirSetup(),"-c",fileDir,"-l","c"]
if user_config and isCustomCfg:
cmd = [dirSetup(),"-c",file_cfg,"-l","c"]
print('overriding provided files and using custom file')
#TO DO
#handle errors..which is quite a lot.
pR=p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,startupinfo = info)
#view class "sublime.View" "https://www.sublimetext.com/docs/3/api_reference.html#sublime.View"
#EROR FIX # 'str' does not support the buffer interface
#reference for ueing stdin as input
#communicate() returns a tuple (stdoutdata, stderrdata).
#docs https://docs.python.org/2/library/subprocess.html
output , stderr = p.communicate(input = (view.substr(region).encode("utf-8")))
#Todo
#branch out on problems
#getting error from config file
braceserror = setting.get('unmatched_braces');
if braceserror in str(stderr):
#dont replace the intended teest for further infi check logs
#remove this after debugging
print(stderr)
#TODO - change blid assumption
sublime.status_message("unmatched braces problem")
#set global flag
flag = 1
return
view.replace(edit, region, output.decode("utf-8"))
def dirSetup():
packageDir=sublime.packages_path()
libDir=os.path.join('CPrettify','lib')
fDir=os.path.join(packageDir,libDir)
bPath = os.path.join(fDir,'uncrustify.exe')
print(bPath)
return bPath
#etempargs
#debugging and initial version
def tArgs():
#return "-c ben.cfg --no-backup abc.c";
return "-l c -o"
def argsSetup(args):
return shlex.split(args)
class CprettifyCommand(sublime_plugin.TextCommand):
def run(self, edit):
#self.view.insert(edit, 0, "#Prettified by CPrettify")
genVar=runSetup()
execute(genVar)
#Menu entry for sublime
#cprettify_file
class CprettifyFileCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
global flag
#process settings, configurations
init(self.view)
if not userFoldercheck():
print('No user File Detected')
global user_config
user_config = False
else:
user_config = True
#setup directories and variables
#generating folder path
#doc reference future chnages to api
#"https://www.sublimetext.com/docs/3/api_reference.html#sublime.View"
if(sublime.View.substr is ''):
sublime.status_message("This is empty!!!")
return
#failproofing
#region api reference for sublime text 3
#https://www.sublimetext.com/docs/3/api_reference.html#sublime.Region
#check if the region of the file is empty
#constructor
if(sublime.Region(0,self.view.size()).empty()):
#window api ST3
#https://www.sublimetext.com/docs/3/api_reference.html#sublime.Window
sublime.status_message("This is empty!!")
return
#go ahead and proecessing the file
#checking the file name
exp = self.view.file_name()
extension = os.path.splitext(exp)[-1].lower()
if extension != ".c":
sublime.status_message("This is not a c program.. save the file in .c extension")
return
execute_(self.view,edit,sublime.Region(0,self.view.size()))
if flag is 0:
sublime.status_message("Done.. Foramting")
class CprettifyOnlySelectionCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
exp = self.view.file_name()
#for settings
init(self.view)
if not userFoldercheck():
print('No user File Detected')
#pattern = re.compile('c$')
#print(bool(pattern.match(exp)))
#re takes up unnecessary resources
#alternative
extension = os.path.splitext(exp)[-1].lower()
if extension != ".c":
sublime.status_message("This is not a c program.. save the file in .c extension")
return
for region in self.view.sel():
#view can be empty
#some views is and some view are not
#Don't run.. Stop execution
#process non empty views
if sublime.Region.empty(region):
sublime.status_message("Nothing Selected")
return
execute_(self.view,edit,region)
#check flag before going further
if flag is 0:
sublime.status_message("Done.. Foramting")
class CprettifyRestoreConfigCommand(sublime_plugin.TextCommand):
def run(self, edit):
init(self.view)
restore_config()