Merge pull request #472 from nightwing/pullreq

more bugfix, add togglefold to default commands
This commit is contained in:
Fabian Jakobs 2011-10-21 01:23:51 -07:00
commit 2715ec90a3
9 changed files with 174 additions and 129 deletions

View file

@ -548,95 +548,6 @@ exports.launch = function(env) {
} }
}); });
canon.addCommand({
name: "fold",
bindKey: {
win: "Alt-L",
mac: "Alt-L",
sender: "editor"
},
exec: function(env) {
toggleFold(env, false);
}
});
canon.addCommand({
name: "unfold",
bindKey: {
win: "Alt-Shift-L",
mac: "Alt-Shift-L",
sender: "editor"
},
exec: function(env) {
toggleFold(env, true);
}
});
function isCommentRow(row) {
var session = env.editor.session;
var token;
var tokens = session.getTokens(row, row)[0].tokens;
var c = 0;
for (var i = 0; i < tokens.length; i++) {
token = tokens[i];
if (/^comment/.test(token.type)) {
return c;
} else if (!/^text/.test(token.type)) {
return false;
}
c += token.value.length;
}
return false;
}
function toggleFold(env, tryToUnfold) {
var session = env.editor.session;
var selection = env.editor.selection;
var range = selection.getRange();
var addFold;
if(range.isEmpty()) {
var br = session.findMatchingBracket(range.start);
var fold = session.getFoldAt(range.start.row, range.start.column);
var column;
if (fold) {
session.expandFold(fold);
selection.setSelectionRange(fold.range);
} else if (br) {
if (range.compare(br.row, br.column) == 1)
range.end = br;
else
range.start = br;
addFold = true;
} else if ((column = isCommentRow(range.start.row)) !== false) {
var firstCommentRow = range.start.row;
var lastCommentRow = range.start.row;
var t;
while ((t = isCommentRow(firstCommentRow - 1)) !== false) {
firstCommentRow --;
column = t;
}
while (isCommentRow(lastCommentRow + 1) !== false) {
lastCommentRow ++;
}
range.start.row = firstCommentRow;
range.start.column = column + 2;
range.end.row = lastCommentRow;
range.end.column = session.getLine(lastCommentRow).length - 1;
addFold = true;
}
} else {
addFold = true;
}
if (addFold) {
var placeHolder = session.getTextRange(range);
if(placeHolder.length < 3)
return;
placeHolder = placeHolder.trim().substring(0, 3).replace(' ','','g') + "...";
session.addFold(placeHolder, range);
}
}
}; };
var themes = {}; var themes = {};

View file

@ -365,4 +365,33 @@ canon.addCommand({
exec: function(env, args, request) { env.editor.transposeLetters(); } exec: function(env, args, request) { env.editor.transposeLetters(); }
}); });
canon.addCommand({
name: "fold",
bindKey: bindKey("Alt-L", "Alt-L"),
exec: function(env) {
env.editor.session.toggleFold(false);
}
});
canon.addCommand({
name: "unfold",
bindKey: bindKey("Alt-Shift-L", "Alt-Shift-L"),
exec: function(env) {
env.editor.session.toggleFold(true);
}
});
canon.addCommand({
name: "foldall",
bindKey: bindKey("Alt-Shift-0", "Alt-Shift-0"),
exec: function(env) {
env.editor.session.foldAll();
}
});
canon.addCommand({
name: "unfoldall",
bindKey: bindKey("Alt-Shift-0", "Alt-Shift-0"),
exec: function(env) {
env.editor.session.unFoldAll();
}
});
}); });

View file

@ -173,6 +173,27 @@ var EditSession = function(text, mode) {
return this.bgTokenizer.getTokens(firstRow, lastRow); return this.bgTokenizer.getTokens(firstRow, lastRow);
}; };
this.getTokenAt = function(row, column) {
var tokens = this.bgTokenizer.getTokens(row, row)[0].tokens;
var token, c = 0;
if (column == null) {
i = tokens.length - 1;
c = this.getLine(row).length;
} else {
for (var i = 0; i < tokens.length; i++) {
c += tokens[i].value.length;
if (c >= column)
break;
}
}
token = tokens[i];
if (!token)
return null;
token.index = i;
token.start = c - token.value.length;
return token;
};
this.setUndoManager = function(undoManager) { this.setUndoManager = function(undoManager) {
this.$undoManager = undoManager; this.$undoManager = undoManager;
this.$resetRowCache(0); this.$resetRowCache(0);
@ -1457,7 +1478,7 @@ var EditSession = function(text, mode) {
// clamp row before clamping column, for selection on last line // clamp row before clamping column, for selection on last line
var maxRow = this.getLength() - 1; var maxRow = this.getLength() - 1;
var foldLine = this.getNextFold(docRow); var foldLine = this.getNextFoldLine(docRow);
var foldStart = foldLine ? foldLine.start.row : Infinity; var foldStart = foldLine ? foldLine.start.row : Infinity;
while (row <= screenRow) { while (row <= screenRow) {
@ -1469,7 +1490,7 @@ var EditSession = function(text, mode) {
docRow++; docRow++;
if (docRow > foldStart) { if (docRow > foldStart) {
docRow = foldLine.end.row+1; docRow = foldLine.end.row+1;
foldLine = this.getNextFold(docRow); foldLine = this.getNextFoldLine(docRow, foldLine);
foldStart = foldLine ? foldLine.start.row : Infinity; foldStart = foldLine ? foldLine.start.row : Infinity;
} }
} }
@ -1579,7 +1600,7 @@ var EditSession = function(text, mode) {
} }
var doCache = !rowCache.length || i == rowCache.length; var doCache = !rowCache.length || i == rowCache.length;
var foldLine = this.getNextFold(row); var foldLine = this.getNextFoldLine(row);
var foldStart = foldLine ?foldLine.start.row :Infinity; var foldStart = foldLine ?foldLine.start.row :Infinity;
while (row < docRow) { while (row < docRow) {
@ -1587,7 +1608,7 @@ var EditSession = function(text, mode) {
rowEnd = foldLine.end.row + 1; rowEnd = foldLine.end.row + 1;
if (rowEnd > docRow) if (rowEnd > docRow)
break; break;
foldLine = this.getNextFold(rowEnd); foldLine = this.getNextFoldLine(rowEnd, foldLine);
foldStart = foldLine ?foldLine.start.row :Infinity; foldStart = foldLine ?foldLine.start.row :Infinity;
} }
else { else {

View file

@ -183,7 +183,7 @@ function Folding() {
} }
// returns the fold which starts after or contains docRow // returns the fold which starts after or contains docRow
this.getNextFold = function(docRow, startFoldLine) { this.getNextFoldLine = function(docRow, startFoldLine) {
var foldData = this.$foldData, ans; var foldData = this.$foldData, ans;
var i = 0; var i = 0;
if (startFoldLine) if (startFoldLine)
@ -489,8 +489,85 @@ function Folding() {
return fd; return fd;
}; };
}
this.toggleFold = function(tryToUnfold) {
var selection = this.selection;
var range = selection.getRange();
if (range.isEmpty()) {
var cursor = range.start
var fold = this.getFoldAt(cursor.row, cursor.column);
var bracketPos, column;
if (fold) {
this.expandFold(fold);
return;
} else if (bracketPos = this.findMatchingBracket(cursor)) {
if (range.comparePoint(bracketPos) == 1) {
range.end = bracketPos;
} else {
range.start = bracketPos;
range.start.column++;
range.end.column--;
}
} else if (bracketPos = this.findMatchingBracket({row: cursor.row, column: cursor.column + 1})) {
if (range.comparePoint(bracketPos) == 1)
range.end = bracketPos;
else
range.start = bracketPos;
range.start.column++;
} else {
var token = this.getTokenAt(cursor.row, cursor.column);
if (token && /^comment|string/.test(token.type)) {
var startRow = cursor.row;
var endRow = cursor.row;
var t = token;
while ((t = this.getTokenAt(startRow - 1)) && t.type == token.type) {
startRow --;
token = t;
}
range.start.row = startRow;
range.start.column = token.start + 2;
while ((t = this.getTokenAt(endRow + 1, 0)) && t.type == token.type) {
endRow ++;
token = t;
}
range.end.row = endRow;
range.end.column = token.start + token.value.length - 1;
}
}
} else {
var folds = this.getFoldsInRange(range);
if (tryToUnfold && folds.length) {
this.expandFolds(folds);
return;
} else if (folds.length == 1 ) {
fold = folds[0];
}
}
if (!fold)
fold = this.getFoldAt(range.start.row, range.start.column);
if (fold && fold.range.toString() == range.toString()){
this.expandFold(fold);
return
}
var placeholder = "...";
if (!range.isMultiLine()) {
placeholder = this.getTextRange(range);
if(placeholder.length < 4)
return;
placeholder = placeholder.trim().substring(0, 2) + ".."
}
this.addFold(placeholder, range);
};
}
exports.Folding = Folding; exports.Folding = Folding;
}); });

View file

@ -103,13 +103,13 @@ var Gutter = function(parentEl) {
var html = []; var html = [];
var i = config.firstRow; var i = config.firstRow;
var lastRow = config.lastRow; var lastRow = config.lastRow;
var fold = this.session.getNextFold(i); var fold = this.session.getNextFoldLine(i);
var foldStart = fold ? fold.start.row : Infinity; var foldStart = fold ? fold.start.row : Infinity;
while (true) { while (true) {
if(i > foldStart) { if(i > foldStart) {
i = fold.end.row + 1; i = fold.end.row + 1;
fold = this.session.getNextFold(i); fold = this.session.getNextFoldLine(i, fold);
foldStart = fold ?fold.start.row :Infinity; foldStart = fold ?fold.start.row :Infinity;
} }
if(i > lastRow) if(i > lastRow)

View file

@ -260,13 +260,13 @@ var Text = function(parentEl) {
this.$renderLinesFragment = function(config, firstRow, lastRow) { this.$renderLinesFragment = function(config, firstRow, lastRow) {
var fragment = this.element.ownerDocument.createDocumentFragment(), var fragment = this.element.ownerDocument.createDocumentFragment(),
row = firstRow, row = firstRow,
fold = this.session.getNextFold(row), fold = this.session.getNextFoldLine(row),
foldStart = fold ?fold.start.row :Infinity; foldStart = fold ?fold.start.row :Infinity;
while (true) { while (true) {
if(row > foldStart) { if(row > foldStart) {
row = fold.end.row+1; row = fold.end.row+1;
fold = this.session.getNextFold(row); fold = this.session.getNextFoldLine(row, fold);
foldStart = fold ?fold.start.row :Infinity; foldStart = fold ?fold.start.row :Infinity;
} }
if(row > lastRow) if(row > lastRow)
@ -307,13 +307,13 @@ var Text = function(parentEl) {
var firstRow = config.firstRow, lastRow = config.lastRow; var firstRow = config.firstRow, lastRow = config.lastRow;
var row = firstRow, var row = firstRow,
fold = this.session.getNextFold(row), fold = this.session.getNextFoldLine(row),
foldStart = fold ?fold.start.row :Infinity; foldStart = fold ?fold.start.row :Infinity;
while (true) { while (true) {
if(row > foldStart) { if(row > foldStart) {
row = fold.end.row+1; row = fold.end.row+1;
fold = this.session.getNextFold(row); fold = this.session.getNextFoldLine(row, fold);
foldStart = fold ?fold.start.row :Infinity; foldStart = fold ?fold.start.row :Infinity;
} }
if(row > lastRow) if(row > lastRow)

View file

@ -206,13 +206,12 @@ var Mode = function() {
for (var key in behaviours) { for (var key in behaviours) {
if (behaviours[key][action]) { if (behaviours[key][action]) {
var ret = behaviours[key][action].apply(this, arguments); var ret = behaviours[key][action].apply(this, arguments);
if (ret !== false) { if (ret) {
return ret; return ret;
} }
} }
} }
} }
return false;
} }
}).call(Mode.prototype); }).call(Mode.prototype);

View file

@ -126,18 +126,18 @@ function DefaultHandlers(editor) {
mousePageY = event.getDocumentY(e); mousePageY = event.getDocumentY(e);
}; };
var onMouseSelectionEnd = function() { var onMouseSelectionEnd = function(e) {
clearInterval(timerId); clearInterval(timerId);
if (state == STATE_UNKNOWN) if (state == STATE_UNKNOWN)
onStartSelect(pos); onStartSelect(pos);
else if (state == STATE_DRAG) else if (state == STATE_DRAG)
onMouseDragSelectionEnd(); onMouseDragSelectionEnd(e);
_self.$clickSelection = null; _self.$clickSelection = null;
state = STATE_UNKNOWN; state = STATE_UNKNOWN;
}; };
var onMouseDragSelectionEnd = function() { var onMouseDragSelectionEnd = function(e) {
dom.removeCssClass(editor.container, "ace_dragging"); dom.removeCssClass(editor.container, "ace_dragging");
editor.session.removeMarker(dragSelectionMarker); editor.session.removeMarker(dragSelectionMarker);
@ -157,7 +157,12 @@ function DefaultHandlers(editor) {
} }
editor.clearSelection(); editor.clearSelection();
if (e && (e.ctrlKey || e.altKey)) {
var session = editor.session;
var newRange = session.insert(dragCursor, session.getTextRange(dragRange));
} else {
var newRange = editor.moveText(dragRange, dragCursor); var newRange = editor.moveText(dragRange, dragCursor);
}
if (!newRange) { if (!newRange) {
dragCursor = null; dragCursor = null;
return; return;

View file

@ -102,9 +102,12 @@ var Range = function(startRow, startColumn, endRow, endColumn) {
} }
} }
this.comparePoint = function(p) {
return this.compare(p.row, p.column);
}
this.containsRange = function(range) { this.containsRange = function(range) {
var cmp = this.compareRange(range); return this.comparePoint(range.start) == 0 && this.comparePoint(range.end) == 0;
return (cmp == -1 || cmp == 0 || cmp == 1);
} }
this.isEnd = function(row, column) { this.isEnd = function(row, column) {