|
62 | 62 | } |
63 | 63 |
|
64 | 64 | // 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; |
67 | 67 |
|
| 68 | + // Language specific commenting |
68 | 69 | 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 |
69 | 99 | if (cM.somethingSelected()) { |
| 100 | + // Language has no block commenting, so repeating singles are needed |
70 | 101 | if (["Ruby","Python","Erlang","Julia","YAML","Perl"].indexOf(top.ICEcoder.caretLocType)>-1) { |
71 | | - commentChar = top.ICEcoder.caretLocType == "Erlang" ? "%" : "#"; |
72 | 102 | startLine = cM.getCursor(true).line; |
73 | 103 | endLine = cM.getCursor().line; |
74 | 104 | 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}); |
78 | 108 | } |
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 |
83 | 110 | } 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"); |
87 | 114 | } |
| 115 | + // Single line commenting |
88 | 116 | } else { |
89 | 117 | 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}; |
106 | 123 | } 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}; |
111 | 129 | } |
112 | 130 | } |
| 131 | + // HTML style commenting |
113 | 132 | } else { |
114 | 133 | if (cM.somethingSelected()) { |
115 | 134 | cM.replaceSelection(cM.getSelection().slice(0,4)!="<\!--" |
|
118 | 137 | } else { |
119 | 138 | cM.replaceRange(lineContent.slice(0,4)!="<\!--" |
120 | 139 | ? "<\!--" + 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}); |
122 | 141 | adjustCursor = lineContent.slice(0,4)=="<\!--" ? -4 : 4; |
123 | 142 | } |
124 | 143 | } |
|
0 commit comments