From d72c8a46d596666c56fa1d7d90da42ae5fa5777c Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Tue, 18 Nov 2014 13:22:32 +0000 Subject: [PATCH] Improved commenting system Simpler code and now able to easily add new language commenting syntax plus tweaks & fixes --- processes/on-editor-load.php | 87 ++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/processes/on-editor-load.php b/processes/on-editor-load.php index e6d549b..6928e45 100644 --- a/processes/on-editor-load.php +++ b/processes/on-editor-load.php @@ -62,54 +62,73 @@ top.ICEcoder.switchMode = function(mode) { } // Comment/uncomment line or selected range on keypress -top.ICEcoder.lineCommentToggleSub = function(cM, cursorPos, linePos, lineContent, lCLen, adjustCursor) { - var startLine, endLine, commentChar; +top.ICEcoder.lineCommentToggleSub = function(cM, cursorPos, linePos, lineContent, lCLen) { + var comments, startLine, endLine, commentCH, commentBS, commentBE; + // Language specific commenting 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) { + + comments = { + "JavaScript" : ["// ", "/* ", " */"], + "CoffeeScript" : ["// ", "/* ", " */"], + "PHP" : ["// ", "/* ", " */"], + "Python" : ["# ", "/* ", " */"], + "Ruby" : ["# ", "/* ", " */"], + "CSS" : ["// ", "/* ", " */"], + "SQL" : ["// ", "/* ", " */"], + "Erlang" : ["% ", "/* ", " */"], + "Julia" : ["# ", "/* ", " */"], + "Java" : ["// ", "/* ", " */"], + "YAML" : ["# ", "/* ", " */"], + "C" : ["// ", "/* ", " */"], + "C++" : ["// ", "/* ", " */"], + "C#" : ["// ", "/* ", " */"], + "Go" : ["// ", "/* ", " */"], + "Lua" : ["-- ", "--[[ ", " ]]"], + "Perl" : ["# ", "/* ", " */"], + "Rust" : ["// ", "/* ", " */"], + "Sass" : ["// ", "/* ", " */"] + } + + // Identify the single line, block start and block end comment chars + commentCH = comments[top.ICEcoder.caretLocType][0]; + commentBS = comments[top.ICEcoder.caretLocType][1]; + commentBE = comments[top.ICEcoder.caretLocType][2]; + + // Block commenting if (cM.somethingSelected()) { + // Language has no block commenting, so repeating singles are needed if (["Ruby","Python","Erlang","Julia","YAML","Perl"].indexOf(top.ICEcoder.caretLocType)>-1) { - commentChar = top.ICEcoder.caretLocType == "Erlang" ? "%" : "#"; startLine = cM.getCursor(true).line; endLine = cM.getCursor().line; for (var i=startLine; i<=endLine; i++) { - cM.replaceRange(cM.getLine(i).slice(0,1)!=commentChar - ? commentChar + cM.getLine(i) - : cM.getLine(i).slice(1,cM.getLine(i).length), {line:i, ch:0}, {line:i, ch:1000000}); + cM.replaceRange(cM.getLine(i).slice(0,commentCH.length)!=commentCH + ? commentCH + cM.getLine(i) + : cM.getLine(i).slice(commentCH.length,cM.getLine(i).length), {line:i, ch:0}, {line:i, ch:1000000}); } - } else if (["Lua"].indexOf(top.ICEcoder.caretLocType)>-1) { - cM.replaceSelection(cM.getSelection().slice(0,4)!="--[[" - ? "--[[" + cM.getSelection() + "]]" - : cM.getSelection().slice(4,cM.getSelection().length-2),"around"); + // Language has block commenting } else { - cM.replaceSelection(cM.getSelection().slice(0,2)!="/*" - ? "/*" + cM.getSelection() + "*/" - : cM.getSelection().slice(2,cM.getSelection().length-2),"around"); + cM.replaceSelection(cM.getSelection().slice(0,commentBS.length)!=commentBS + ? commentBS + cM.getSelection() + commentBE + : cM.getSelection().slice(commentBS.length,cM.getSelection().length-commentBE.length),"around"); } + // Single line commenting } else { if (["CoffeeScript","CSS","SQL"].indexOf(top.ICEcoder.caretLocType)>-1) { - cM.replaceRange(lineContent.slice(0,2)!="/*" - ? "/*" + lineContent + "*/" - : lineContent.slice(2,lCLen).slice(0,lCLen-4), {line: linePos, ch: 0}, {line: linePos, ch: 1000000}); - if (lineContent.slice(0,2)=="/*") {adjustCursor = -adjustCursor}; - } else if (["Ruby","Python","Erlang","Julia","YAML","Perl"].indexOf(top.ICEcoder.caretLocType)>-1) { - commentChar = top.ICEcoder.caretLocType == "Erlang" ? "%" : "#"; - cM.replaceRange(lineContent.slice(0,1)!=commentChar - ? commentChar + lineContent - : lineContent.slice(1,lCLen), {line: linePos, ch: 0}, {line: linePos, ch: 1000000}); - adjustCursor = 1; - if (lineContent.slice(0,1)==commentChar) {adjustCursor = -adjustCursor}; - } else if (["Lua"].indexOf(top.ICEcoder.caretLocType)>-1) { - cM.replaceRange(lineContent.slice(0,2)!="--" - ? "--" + lineContent - : lineContent.slice(2,lCLen), {line: linePos, ch: 0}, {line: linePos, ch: 1000000}); - if (lineContent.slice(0,2)=="//") {adjustCursor = -adjustCursor}; + cM.replaceRange(lineContent.slice(0,commentBS.length)!=commentBS + ? commentBS + lineContent + commentBE + : lineContent.slice(commentBS.length,lCLen-commentBE.length), {line: linePos, ch: 0}, {line: linePos, ch: 1000000}); + adjustCursor = commentBS.length; + if (lineContent.slice(0,commentBS.length)==commentBS) {adjustCursor = -adjustCursor}; } else { - cM.replaceRange(lineContent.slice(0,2)!="//" - ? "//" + lineContent - : lineContent.slice(2,lCLen), {line: linePos, ch: 0}, {line: linePos, ch: 1000000}); - if (lineContent.slice(0,2)=="//") {adjustCursor = -adjustCursor}; + cM.replaceRange(lineContent.slice(0,commentCH.length)!=commentCH + ? commentCH + lineContent + : lineContent.slice(commentCH.length,lCLen), {line: linePos, ch: 0}, {line: linePos, ch: 1000000}); + adjustCursor = commentCH.length; + if (lineContent.slice(0,commentCH.length)==commentCH) {adjustCursor = -adjustCursor}; } } + // HTML style commenting } else { if (cM.somethingSelected()) { cM.replaceSelection(cM.getSelection().slice(0,4)!="<\!--" @@ -118,7 +137,7 @@ top.ICEcoder.lineCommentToggleSub = function(cM, cursorPos, linePos, lineContent } else { cM.replaceRange(lineContent.slice(0,4)!="<\!--" ? "<\!--" + lineContent + "//-->" - : lineContent.slice(4,lCLen).slice(0,lCLen-9), {line: linePos, ch: 0}, {line: linePos, ch: 1000000}); + : lineContent.slice(4,lCLen-5), {line: linePos, ch: 0}, {line: linePos, ch: 1000000}); adjustCursor = lineContent.slice(0,4)=="<\!--" ? -4 : 4; } }