Skip to content

Commit d72c8a4

Browse files
committed
Improved commenting system
Simpler code and now able to easily add new language commenting syntax plus tweaks & fixes
1 parent a24d9fc commit d72c8a4

1 file changed

Lines changed: 53 additions & 34 deletions

File tree

processes/on-editor-load.php

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,54 +62,73 @@
6262
}
6363

6464
// Comment/uncomment line or selected range on keypress
65-
top.ICEcoder.lineCommentToggleSub = function(cM, cursorPos, linePos, lineContent, lCLen, adjustCursor) {
66-
var startLine, endLine, commentChar;
65+
top.ICEcoder.lineCommentToggleSub = function(cM, cursorPos, linePos, lineContent, lCLen) {
66+
var comments, startLine, endLine, commentCH, commentBS, commentBE;
6767

68+
// Language specific commenting
6869
if (["JavaScript","CoffeeScript","PHP","Python","Ruby","CSS","SQL","Erlang","Julia","Java","YAML","C","C++","C#","Go","Lua","Perl","Rust","Sass"].indexOf(top.ICEcoder.caretLocType)>-1) {
70+
71+
comments = {
72+
"JavaScript" : ["// ", "/* ", " */"],
73+
"CoffeeScript" : ["// ", "/* ", " */"],
74+
"PHP" : ["// ", "/* ", " */"],
75+
"Python" : ["# ", "/* ", " */"],
76+
"Ruby" : ["# ", "/* ", " */"],
77+
"CSS" : ["// ", "/* ", " */"],
78+
"SQL" : ["// ", "/* ", " */"],
79+
"Erlang" : ["% ", "/* ", " */"],
80+
"Julia" : ["# ", "/* ", " */"],
81+
"Java" : ["// ", "/* ", " */"],
82+
"YAML" : ["# ", "/* ", " */"],
83+
"C" : ["// ", "/* ", " */"],
84+
"C++" : ["// ", "/* ", " */"],
85+
"C#" : ["// ", "/* ", " */"],
86+
"Go" : ["// ", "/* ", " */"],
87+
"Lua" : ["-- ", "--[[ ", " ]]"],
88+
"Perl" : ["# ", "/* ", " */"],
89+
"Rust" : ["// ", "/* ", " */"],
90+
"Sass" : ["// ", "/* ", " */"]
91+
}
92+
93+
// Identify the single line, block start and block end comment chars
94+
commentCH = comments[top.ICEcoder.caretLocType][0];
95+
commentBS = comments[top.ICEcoder.caretLocType][1];
96+
commentBE = comments[top.ICEcoder.caretLocType][2];
97+
98+
// Block commenting
6999
if (cM.somethingSelected()) {
100+
// Language has no block commenting, so repeating singles are needed
70101
if (["Ruby","Python","Erlang","Julia","YAML","Perl"].indexOf(top.ICEcoder.caretLocType)>-1) {
71-
commentChar = top.ICEcoder.caretLocType == "Erlang" ? "%" : "#";
72102
startLine = cM.getCursor(true).line;
73103
endLine = cM.getCursor().line;
74104
for (var i=startLine; i<=endLine; i++) {
75-
cM.replaceRange(cM.getLine(i).slice(0,1)!=commentChar
76-
? commentChar + cM.getLine(i)
77-
: cM.getLine(i).slice(1,cM.getLine(i).length), {line:i, ch:0}, {line:i, ch:1000000});
105+
cM.replaceRange(cM.getLine(i).slice(0,commentCH.length)!=commentCH
106+
? commentCH + cM.getLine(i)
107+
: cM.getLine(i).slice(commentCH.length,cM.getLine(i).length), {line:i, ch:0}, {line:i, ch:1000000});
78108
}
79-
} else if (["Lua"].indexOf(top.ICEcoder.caretLocType)>-1) {
80-
cM.replaceSelection(cM.getSelection().slice(0,4)!="--[["
81-
? "--[[" + cM.getSelection() + "]]"
82-
: cM.getSelection().slice(4,cM.getSelection().length-2),"around");
109+
// Language has block commenting
83110
} else {
84-
cM.replaceSelection(cM.getSelection().slice(0,2)!="/*"
85-
? "/*" + cM.getSelection() + "*/"
86-
: cM.getSelection().slice(2,cM.getSelection().length-2),"around");
111+
cM.replaceSelection(cM.getSelection().slice(0,commentBS.length)!=commentBS
112+
? commentBS + cM.getSelection() + commentBE
113+
: cM.getSelection().slice(commentBS.length,cM.getSelection().length-commentBE.length),"around");
87114
}
115+
// Single line commenting
88116
} else {
89117
if (["CoffeeScript","CSS","SQL"].indexOf(top.ICEcoder.caretLocType)>-1) {
90-
cM.replaceRange(lineContent.slice(0,2)!="/*"
91-
? "/*" + lineContent + "*/"
92-
: lineContent.slice(2,lCLen).slice(0,lCLen-4), {line: linePos, ch: 0}, {line: linePos, ch: 1000000});
93-
if (lineContent.slice(0,2)=="/*") {adjustCursor = -adjustCursor};
94-
} else if (["Ruby","Python","Erlang","Julia","YAML","Perl"].indexOf(top.ICEcoder.caretLocType)>-1) {
95-
commentChar = top.ICEcoder.caretLocType == "Erlang" ? "%" : "#";
96-
cM.replaceRange(lineContent.slice(0,1)!=commentChar
97-
? commentChar + lineContent
98-
: lineContent.slice(1,lCLen), {line: linePos, ch: 0}, {line: linePos, ch: 1000000});
99-
adjustCursor = 1;
100-
if (lineContent.slice(0,1)==commentChar) {adjustCursor = -adjustCursor};
101-
} else if (["Lua"].indexOf(top.ICEcoder.caretLocType)>-1) {
102-
cM.replaceRange(lineContent.slice(0,2)!="--"
103-
? "--" + lineContent
104-
: lineContent.slice(2,lCLen), {line: linePos, ch: 0}, {line: linePos, ch: 1000000});
105-
if (lineContent.slice(0,2)=="//") {adjustCursor = -adjustCursor};
118+
cM.replaceRange(lineContent.slice(0,commentBS.length)!=commentBS
119+
? commentBS + lineContent + commentBE
120+
: lineContent.slice(commentBS.length,lCLen-commentBE.length), {line: linePos, ch: 0}, {line: linePos, ch: 1000000});
121+
adjustCursor = commentBS.length;
122+
if (lineContent.slice(0,commentBS.length)==commentBS) {adjustCursor = -adjustCursor};
106123
} else {
107-
cM.replaceRange(lineContent.slice(0,2)!="//"
108-
? "//" + lineContent
109-
: lineContent.slice(2,lCLen), {line: linePos, ch: 0}, {line: linePos, ch: 1000000});
110-
if (lineContent.slice(0,2)=="//") {adjustCursor = -adjustCursor};
124+
cM.replaceRange(lineContent.slice(0,commentCH.length)!=commentCH
125+
? commentCH + lineContent
126+
: lineContent.slice(commentCH.length,lCLen), {line: linePos, ch: 0}, {line: linePos, ch: 1000000});
127+
adjustCursor = commentCH.length;
128+
if (lineContent.slice(0,commentCH.length)==commentCH) {adjustCursor = -adjustCursor};
111129
}
112130
}
131+
// HTML style commenting
113132
} else {
114133
if (cM.somethingSelected()) {
115134
cM.replaceSelection(cM.getSelection().slice(0,4)!="<\!--"
@@ -118,7 +137,7 @@
118137
} else {
119138
cM.replaceRange(lineContent.slice(0,4)!="<\!--"
120139
? "<\!--" + lineContent + "//-->"
121-
: lineContent.slice(4,lCLen).slice(0,lCLen-9), {line: linePos, ch: 0}, {line: linePos, ch: 1000000});
140+
: lineContent.slice(4,lCLen-5), {line: linePos, ch: 0}, {line: linePos, ch: 1000000});
122141
adjustCursor = lineContent.slice(0,4)=="<\!--" ? -4 : 4;
123142
}
124143
}

0 commit comments

Comments
 (0)