change toggleCommentLines behavior to match sublime text

This commit is contained in:
nightwing 2013-02-18 10:56:02 +04:00
commit 528a103149

View file

@ -3,7 +3,7 @@
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
@ -14,7 +14,7 @@
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* 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
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -32,6 +32,7 @@ define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var lang = require("ace/lib/lang");
var TextMode = require("./text").Mode;
var Tokenizer = require("../tokenizer").Tokenizer;
var JavaScriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules;
@ -51,32 +52,41 @@ oop.inherits(Mode, TextMode);
(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) {
var outdent = true;
var re = /^(\s*)\/\//;
for (var i=startRow; i<= endRow; i++) {
if (!re.test(doc.getLine(i))) {
outdent = false;
break;
}
function isCommented(str) {
return regexpStart.test(str);
}
if (outdent) {
var deleteRange = new Range(0, 0, 0, 0);
for (var i=startRow; i<= endRow; i++)
{
var removeComment = true;
var minSpace = Infinity;
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 m = line.match(re);
deleteRange.start.row = i;
deleteRange.end.row = i;
deleteRange.end.column = m[0].length;
doc.replace(deleteRange, m[1]);
var m = line.match(regexpStart);
doc.removeInLine(i, indentations[i], m[0].length);
}
} else {
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") {
return indent;
}
if (state == "start" || state == "no_regex") {
var match = line.match(/^.*(?:\bcase\b.*\:|[\{\(\[])\s*$/);
if (match) {
@ -119,7 +129,7 @@ oop.inherits(Mode, TextMode);
this.autoOutdent = function(state, doc, row) {
this.$outdent.autoOutdent(doc, row);
};
this.createWorker = function(session) {
var worker = new WorkerClient(["ace"], "ace/mode/javascript_worker", "JavaScriptWorker");
worker.attachToDocument(session.getDocument());
@ -127,11 +137,11 @@ oop.inherits(Mode, TextMode);
worker.on("jslint", function(results) {
session.setAnnotations(results.data);
});
worker.on("terminate", function() {
session.clearAnnotations();
});
return worker;
};