@@ -39,7 +39,6 @@ define(function (require, exports, module) {
3939 StringUtils = require ( "utils/StringUtils" ) ,
4040 TokenUtils = require ( "utils/TokenUtils" ) ;
4141
42-
4342 /**
4443 * List of constants
4544 */
@@ -55,14 +54,15 @@ define(function (require, exports, module) {
5554 * @param {!number } endLine - valid line inside the document
5655 * @return {boolean } true if there is at least one uncommented line
5756 */
58- function _containsUncommented ( editor , startLine , endLine ) {
57+ function _containsUncommented ( editor , startLine , endLine , prefix ) {
58+ var lineExp = new RegExp ( "^\\s*" + StringUtils . regexEscape ( prefix ) ) ;
5959 var containsUncommented = false ;
6060 var i ;
6161 var line ;
6262 for ( i = startLine ; i <= endLine ; i ++ ) {
6363 line = editor . document . getLine ( i ) ;
6464 // A line is commented out if it starts with 0-N whitespace chars, then "//"
65- if ( ! line . match ( / ^ \s * \/ \/ / ) && line . match ( / \S / ) ) {
65+ if ( ! line . match ( lineExp ) && line . match ( / \S / ) ) {
6666 containsUncommented = true ;
6767 break ;
6868 }
@@ -75,11 +75,10 @@ define(function (require, exports, module) {
7575 * and cursor position. Applies to currently focused Editor.
7676 *
7777 * If all non-whitespace lines are already commented out, then we uncomment; otherwise we comment
78- * out. Commenting out adds "//" to at column 0 of every line. Uncommenting removes the first "//"
78+ * out. Commenting out adds the prefix at column 0 of every line. Uncommenting removes the first prefix
7979 * on each line (if any - empty lines might not have one).
8080 */
81- function lineCommentSlashSlash ( editor ) {
82-
81+ function lineCommentPrefix ( editor , prefix ) {
8382 var doc = editor . document ;
8483 var sel = editor . getSelection ( ) ;
8584 var startLine = sel . start . line ;
@@ -96,7 +95,7 @@ define(function (require, exports, module) {
9695 // Decide if we're commenting vs. un-commenting
9796 // Are there any non-blank lines that aren't commented out? (We ignore blank lines because
9897 // some editors like Sublime don't comment them out)
99- var containsUncommented = _containsUncommented ( editor , startLine , endLine ) ;
98+ var containsUncommented = _containsUncommented ( editor , startLine , endLine , prefix ) ;
10099 var i ;
101100 var line ;
102101 var updateSelection = false ;
@@ -107,7 +106,7 @@ define(function (require, exports, module) {
107106 if ( containsUncommented ) {
108107 // Comment out - prepend "//" to each line
109108 for ( i = startLine ; i <= endLine ; i ++ ) {
110- doc . replaceRange ( "//" , { line : i , ch : 0 } ) ;
109+ doc . replaceRange ( prefix , { line : i , ch : 0 } ) ;
111110 }
112111
113112 // Make sure selection includes "//" that was added at start of range
@@ -119,9 +118,9 @@ define(function (require, exports, module) {
119118 // Uncomment - remove first "//" on each line (if any)
120119 for ( i = startLine ; i <= endLine ; i ++ ) {
121120 line = doc . getLine ( i ) ;
122- var commentI = line . indexOf ( "//" ) ;
121+ var commentI = line . indexOf ( prefix ) ;
123122 if ( commentI !== - 1 ) {
124- doc . replaceRange ( "" , { line : i , ch : commentI } , { line : i , ch : commentI + 2 } ) ;
123+ doc . replaceRange ( "" , { line : i , ch : commentI } , { line : i , ch : commentI + prefix . length } ) ;
125124 }
126125 }
127126 }
@@ -205,11 +204,11 @@ define(function (require, exports, module) {
205204 * the lines in the selection are line-commented.
206205 *
207206 * @param {!Editor } editor
208- * @param {!String } prefix
209- * @param {!String } suffix
210- * @param {boolean= } slashComment - true if the mode also supports "//" comments
207+ * @param {!string } prefix, e.g. "<!--"
208+ * @param {!string } suffix, e.g. "-->"
209+ * @param {?string } linePrefix, e.g. "//"
211210 */
212- function blockCommentPrefixSuffix ( editor , prefix , suffix , slashComment ) {
211+ function blockCommentPrefixSuffix ( editor , prefix , suffix , linePrefix ) {
213212
214213 var doc = editor . document ,
215214 sel = editor . getSelection ( ) ,
@@ -218,7 +217,7 @@ define(function (require, exports, module) {
218217 endCtx = TokenUtils . getInitialContext ( editor . _codeMirror , { line : sel . end . line , ch : sel . end . ch } ) ,
219218 prefixExp = new RegExp ( "^" + StringUtils . regexEscape ( prefix ) , "g" ) ,
220219 suffixExp = new RegExp ( StringUtils . regexEscape ( suffix ) + "$" , "g" ) ,
221- lineExp = new RegExp ( "^\/\/" ) ,
220+ lineExp = linePrefix ? new RegExp ( "^" + StringUtils . regexEscape ( linePrefix ) ) : null ,
222221 prefixPos = null ,
223222 suffixPos = null ,
224223 canComment = false ,
@@ -234,7 +233,7 @@ define(function (require, exports, module) {
234233 }
235234
236235 // Check if we should just do a line uncomment (if all lines in the selection are commented).
237- if ( slashComment && ( ctx . token . string . match ( lineExp ) || endCtx . token . string . match ( lineExp ) ) ) {
236+ if ( lineExp && ( ctx . token . string . match ( lineExp ) || endCtx . token . string . match ( lineExp ) ) ) {
238237 var startCtxIndex = editor . indexFromPos ( { line : ctx . pos . line , ch : ctx . token . start } ) ;
239238 var endCtxIndex = editor . indexFromPos ( { line : endCtx . pos . line , ch : endCtx . token . start + endCtx . token . string . length } ) ;
240239
@@ -256,7 +255,7 @@ define(function (require, exports, module) {
256255 }
257256
258257 // Find if all the lines are line-commented.
259- if ( ! _containsUncommented ( editor , sel . start . line , endLine ) ) {
258+ if ( ! _containsUncommented ( editor , sel . start . line , endLine , linePrefix ) ) {
260259 lineUncomment = true ;
261260
262261 // Block-comment in all the other cases
@@ -328,7 +327,7 @@ define(function (require, exports, module) {
328327 return ;
329328
330329 } else if ( lineUncomment ) {
331- lineCommentSlashSlash ( editor ) ;
330+ lineCommentPrefix ( editor , linePrefix ) ;
332331
333332 } else {
334333 doc . batchOperation ( function ( ) {
@@ -438,12 +437,11 @@ define(function (require, exports, module) {
438437 result = result && _findNextBlockComment ( ctx , selEnd , prefixExp ) ;
439438
440439 if ( className === "comment" || result || isLineSelection ) {
441- blockCommentPrefixSuffix ( editor , prefix , suffix , false ) ;
442-
440+ blockCommentPrefixSuffix ( editor , prefix , suffix ) ;
443441 } else {
444442 // Set the new selection and comment it
445443 editor . setSelection ( selStart , selEnd ) ;
446- blockCommentPrefixSuffix ( editor , prefix , suffix , false ) ;
444+ blockCommentPrefixSuffix ( editor , prefix , suffix ) ;
447445
448446 // Restore the old selection taking into account the prefix change
449447 if ( isMultipleLine ) {
@@ -468,14 +466,10 @@ define(function (require, exports, module) {
468466 return ;
469467 }
470468
471- var mode = editor . getModeForSelection ( ) ;
469+ var language = editor . getLanguageForSelection ( ) ;
472470
473- if ( mode === "javascript" || mode === "less" ) {
474- blockCommentPrefixSuffix ( editor , "/*" , "*/" , true ) ;
475- } else if ( mode === "css" ) {
476- blockCommentPrefixSuffix ( editor , "/*" , "*/" , false ) ;
477- } else if ( mode === "html" ) {
478- blockCommentPrefixSuffix ( editor , "<!--" , "-->" , false ) ;
471+ if ( language . blockComment ) {
472+ blockCommentPrefixSuffix ( editor , language . blockComment . prefix , language . blockComment . suffix , language . lineComment ? language . lineComment . prefix : null ) ;
479473 }
480474 }
481475
@@ -489,15 +483,12 @@ define(function (require, exports, module) {
489483 return ;
490484 }
491485
492- var mode = editor . getModeForSelection ( ) ;
486+ var language = editor . getLanguageForSelection ( ) ;
493487
494- // Currently we only support languages with "//" commenting
495- if ( mode === "javascript" || mode === "less" ) {
496- lineCommentSlashSlash ( editor ) ;
497- } else if ( mode === "css" ) {
498- lineCommentPrefixSuffix ( editor , "/*" , "*/" ) ;
499- } else if ( mode === "html" ) {
500- lineCommentPrefixSuffix ( editor , "<!--" , "-->" ) ;
488+ if ( language . lineComment ) {
489+ lineCommentPrefix ( editor , language . lineComment . prefix ) ;
490+ } else if ( language . blockComment ) {
491+ lineCommentPrefixSuffix ( editor , language . blockComment . prefix , language . blockComment . suffix ) ;
501492 }
502493 }
503494
@@ -733,7 +724,7 @@ define(function (require, exports, module) {
733724 CommandManager . register ( Strings . CMD_LINE_UP , Commands . EDIT_LINE_UP , moveLineUp ) ;
734725 CommandManager . register ( Strings . CMD_LINE_DOWN , Commands . EDIT_LINE_DOWN , moveLineDown ) ;
735726 CommandManager . register ( Strings . CMD_SELECT_LINE , Commands . EDIT_SELECT_LINE , selectLine ) ;
736-
727+
737728 CommandManager . register ( Strings . CMD_UNDO , Commands . EDIT_UNDO , handleUndo ) ;
738729 CommandManager . register ( Strings . CMD_REDO , Commands . EDIT_REDO , handleRedo ) ;
739730 CommandManager . register ( Strings . CMD_CUT , Commands . EDIT_CUT , ignoreCommand ) ;
0 commit comments