forked from smartcomments/sublime-smartcomments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartcomments.py
84 lines (74 loc) · 2.63 KB
/
smartcomments.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
import sublime, sublime_plugin
from subprocess import PIPE, Popen
from io import open as io_open
from os import remove
from os.path import dirname as path_dirname
from tempfile import mkstemp
import platform
def get_command():
command = "smartcomments"
if platform.system() == "Windows":
command = "smartcomments.cmd"
return command
def fn_execute(cmd_args=[], cmd=None):
"""
"""
result = None
try:
if cmd:
result = Popen(cmd_args, executable=cmd,
stdout=PIPE, stderr=PIPE).communicate()
else:
result = Popen(cmd_args, stdout=PIPE, stderr=PIPE).communicate()
except TypeError as err:
print("Error {0} ocurrido al ejecutar el comando {1} \
".format(err, cmd))
except FileNotFoundError as err:
print("Error {0} ocurrido al ejecutar el comando {1} \
".format(err, cmd))
return result
class SmartCommentsFolderCommand(sublime_plugin.TextCommand):
"""
"""
def run_(self, edit):
file_name = self.view.file_name()
if not file_name:
return
file_dir = path_dirname(file_name)
result = fn_execute([get_command(), "-g","-t", file_dir])
print(result)
class SmartCommentsFileCommand(sublime_plugin.TextCommand):
"""
"""
def run_(self, edit):
file_name = self.view.file_name()
if not file_name or not str(file_name).endswith("js"):
return
result = fn_execute([get_command(), "-g", "-t", file_name, "-c", "/usr/lib/node_modules/smartcomments"])
print(result)
self.view.run_command('revert')
class SmartCommentsTextCommand(sublime_plugin.TextCommand):
"""
"""
def run_(self, edit):
file_name = self.view.file_name()
if not file_name or not str(file_name).endswith("js"):
return
selection = self.view.sel()
for region in selection:
if not region.empty():
s = self.view.substr(region)
js_tmpfile = mkstemp(suffix='.js', text=True)
with io_open(js_tmpfile[1], 'w+') as tmpf:
tmpf.write(s)
try:
fn_execute([get_command(), "-g", "-t", js_tmpfile[1]])
except:
remove(js_tmpfile[1])
with io_open(js_tmpfile[1], 'r') as tmpf:
file_size = tmpf.seek(0, 2)
tmpf.seek(0, 0)
data = tmpf.read(file_size)
if data != '':
self.view.replace(edit, region, data)
remove(js_tmpfile[1])