change toggleCommentLines behavior to match sublime text
This commit is contained in:
parent
b350fe65a4
commit
528a103149
1 changed files with 37 additions and 27 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
*
|
*
|
||||||
* Copyright (c) 2010, Ajax.org B.V.
|
* Copyright (c) 2010, Ajax.org B.V.
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions are met:
|
* modification, are permitted provided that the following conditions are met:
|
||||||
* * Redistributions of source code must retain the above copyright
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
* * Neither the name of Ajax.org B.V. nor the
|
* * Neither the name of Ajax.org B.V. nor the
|
||||||
* names of its contributors may be used to endorse or promote products
|
* names of its contributors may be used to endorse or promote products
|
||||||
* derived from this software without specific prior written permission.
|
* derived from this software without specific prior written permission.
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
|
@ -32,6 +32,7 @@ define(function(require, exports, module) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var oop = require("../lib/oop");
|
var oop = require("../lib/oop");
|
||||||
|
var lang = require("ace/lib/lang");
|
||||||
var TextMode = require("./text").Mode;
|
var TextMode = require("./text").Mode;
|
||||||
var Tokenizer = require("../tokenizer").Tokenizer;
|
var Tokenizer = require("../tokenizer").Tokenizer;
|
||||||
var JavaScriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules;
|
var JavaScriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules;
|
||||||
|
|
@ -51,32 +52,41 @@ oop.inherits(Mode, TextMode);
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
|
this.lineCommentStart = "//";
|
||||||
|
this.toggleCommentLines = function(state, session, startRow, endRow) {
|
||||||
|
var doc = session.doc;
|
||||||
|
var str = this.lineCommentStart;
|
||||||
|
var regexpStart = new RegExp("^\\s*"+lang.escapeRegExp(str) + " ?");
|
||||||
|
|
||||||
this.toggleCommentLines = function(state, doc, startRow, endRow) {
|
function isCommented(str) {
|
||||||
var outdent = true;
|
return regexpStart.test(str);
|
||||||
var re = /^(\s*)\/\//;
|
|
||||||
|
|
||||||
for (var i=startRow; i<= endRow; i++) {
|
|
||||||
if (!re.test(doc.getLine(i))) {
|
|
||||||
outdent = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outdent) {
|
var removeComment = true;
|
||||||
var deleteRange = new Range(0, 0, 0, 0);
|
var minSpace = Infinity;
|
||||||
for (var i=startRow; i<= endRow; i++)
|
var indentations = [];
|
||||||
{
|
|
||||||
|
for (var i = startRow; i <= endRow; i++) {
|
||||||
|
var line = doc.getLine(i);
|
||||||
|
var indent = line.search(/\S|$/);
|
||||||
|
indentations[i] = indent;
|
||||||
|
if (indent < minSpace)
|
||||||
|
minSpace = indent;
|
||||||
|
if (removeComment && !isCommented(line))
|
||||||
|
removeComment = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removeComment) {
|
||||||
|
for (var i = startRow; i <= endRow; i++) {
|
||||||
var line = doc.getLine(i);
|
var line = doc.getLine(i);
|
||||||
var m = line.match(re);
|
var m = line.match(regexpStart);
|
||||||
deleteRange.start.row = i;
|
doc.removeInLine(i, indentations[i], m[0].length);
|
||||||
deleteRange.end.row = i;
|
}
|
||||||
deleteRange.end.column = m[0].length;
|
} else {
|
||||||
doc.replace(deleteRange, m[1]);
|
str += " ";
|
||||||
|
for (var i = startRow; i <= endRow; i++) {
|
||||||
|
doc.insertInLine({row: i, column: minSpace}, str);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
|
||||||
doc.indentRows(startRow, endRow, "//");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -90,7 +100,7 @@ oop.inherits(Mode, TextMode);
|
||||||
if (tokens.length && tokens[tokens.length-1].type == "comment") {
|
if (tokens.length && tokens[tokens.length-1].type == "comment") {
|
||||||
return indent;
|
return indent;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state == "start" || state == "no_regex") {
|
if (state == "start" || state == "no_regex") {
|
||||||
var match = line.match(/^.*(?:\bcase\b.*\:|[\{\(\[])\s*$/);
|
var match = line.match(/^.*(?:\bcase\b.*\:|[\{\(\[])\s*$/);
|
||||||
if (match) {
|
if (match) {
|
||||||
|
|
@ -119,7 +129,7 @@ oop.inherits(Mode, TextMode);
|
||||||
this.autoOutdent = function(state, doc, row) {
|
this.autoOutdent = function(state, doc, row) {
|
||||||
this.$outdent.autoOutdent(doc, row);
|
this.$outdent.autoOutdent(doc, row);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.createWorker = function(session) {
|
this.createWorker = function(session) {
|
||||||
var worker = new WorkerClient(["ace"], "ace/mode/javascript_worker", "JavaScriptWorker");
|
var worker = new WorkerClient(["ace"], "ace/mode/javascript_worker", "JavaScriptWorker");
|
||||||
worker.attachToDocument(session.getDocument());
|
worker.attachToDocument(session.getDocument());
|
||||||
|
|
@ -127,11 +137,11 @@ oop.inherits(Mode, TextMode);
|
||||||
worker.on("jslint", function(results) {
|
worker.on("jslint", function(results) {
|
||||||
session.setAnnotations(results.data);
|
session.setAnnotations(results.data);
|
||||||
});
|
});
|
||||||
|
|
||||||
worker.on("terminate", function() {
|
worker.on("terminate", function() {
|
||||||
session.clearAnnotations();
|
session.clearAnnotations();
|
||||||
});
|
});
|
||||||
|
|
||||||
return worker;
|
return worker;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue