-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathASPComment.py
More file actions
93 lines (70 loc) · 2.73 KB
/
Copy pathASPComment.py
File metadata and controls
93 lines (70 loc) · 2.73 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
import sublime, sublime_plugin
s = sublime.load_settings('ASPComment.sublime-settings')
def isLineUncommented(line):
if (checkFirstChar(line, "'") or (len(line.lstrip()) == 0 and
s.get("comment_empty_lines") == False)):
return 0
return 1
def checkFirstChar(line, char, isToggle = False):
line = line.lstrip()
if len(line) > 0:
if line[0] == char:
return 1
return 0
def addComment(line):
# IF the first character isn't a single quote OR double quotes are allowed
# AND the line is not empty or empty line are allowed
# THEN coment the line.
if ((not checkFirstChar(line, "'") or s.get("allow_double_comments")) and
(len(line.lstrip()) > 0 or s.get("comment_empty_lines"))):
if s.get("comment_after_indent"):
# preserve indentation by adding quote between leading whitespace
# and rest of line
line = line[:len(line)-len(line.lstrip())] + "'" + line.lstrip()
else:
line = "'" + line
return line
def removeComment(line):
# IF the first character is a single quote
# THEN remove it
if checkFirstChar(line, "'"):
line = line.replace("'", "", 1)
return line;
class AspCommentAddCommentCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = sublime.active_window().active_view()
edit = view.begin_edit()
# Loop through each line in the selection
for region in view.sel():
# Add a single quote in front of each line
newtext = '\n'.join(map(lambda line: addComment(line), view.substr(region).splitlines()))
view.replace(edit, region, newtext)
view.end_edit(edit)
class AspCommentRemoveCommentCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = sublime.active_window().active_view()
edit = view.begin_edit()
# Loop through each line in the selection
for region in view.sel():
# Remove the first character in front of each line if it is a single quote
newtext = '\n'.join(map(lambda line: removeComment(line), view.substr(region).splitlines()))
view.replace(edit, region, newtext)
view.end_edit(edit)
class AspCommentToggleCommentCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = sublime.active_window().active_view()
#edit = view.begin_edit()
# loop through each line in the selection
for region in view.sel():
if region.empty():
# Toggle the current line
line = view.substr(view.line(region))
view.replace(edit, view.line(region), (addComment if isLineUncommented(line) else removeComment)(line))
else:
# Toggle the current selection
lines = view.substr(region).split('\n')
uncommentedLines = map(lambda line: int(isLineUncommented(line)), lines)
newRegion = ('\n'.join(map(addComment if sum(uncommentedLines) > 0 else removeComment,
lines)))
view.replace(edit, region, newRegion)
#view.end_edit(edit)