Permalink
Browse files

Fixed issue in interactive actions in multi-cursor mode

  • Loading branch information...
1 parent 1f20fdf commit 08870fb78831a2a80521b77deae7051e5e2231b9 @sergeche sergeche committed May 11, 2014
Showing with 110 additions and 100 deletions.
  1. +30 −14 editor.js
  2. +34 −55 interactive.js
  3. +2 −1 keymap.json
  4. +4 −5 main.js
  5. +40 −25 prompt.js
View
@@ -1,7 +1,7 @@
/**
* Emmet Editor interface implementation for Brackets.
* Interface is optimized for multiple cursor usage: authors
- * should run acttion multiple times and update `selectionIndex`
+ * should run acttion multiple times and update `_selection.index`
* property on each iteration.
*/
define(function(require, exports, module) {
@@ -20,7 +20,7 @@ define(function(require, exports, module) {
* @param {Editor} editor Brackets editor instance
* @return {String}
*/
- function normalize(text, editor) {
+ function normalize(text) {
var indentation = '\t';
if (!Editor.getUseTabChar()) {
indentation = '';
@@ -36,6 +36,13 @@ define(function(require, exports, module) {
});
}
+ function visualize(str) {
+ return str
+ .replace(/\t/g, '\\t')
+ .replace(/\n/g, '\\n')
+ .replace(/\s/g, '\\s');
+ }
+
return {
editor: null,
modeMap: {
@@ -69,20 +76,21 @@ define(function(require, exports, module) {
* Executes given function for every selection
* @param {Function} fn
*/
- exec: function(fn) {
+ exec: function(fn, skipSelSet) {
var sel = this._selection;
var ix = sel.bufferRanges.length - 1;
var success = true;
- sel.saved = new Array(sel.bufferRanges.length);
+ sel.saved = [];
while (ix >= 0) {
sel.index = ix;
- if (fn(sel.index) === false) {
+ if (fn(ix, sel.indexRanges[ix], sel.bufferRanges[ix]) === false) {
success = false;
break;
}
+ ix--;
}
- if (success && sel.saved.length > 1) {
+ if (!skipSelSet && success && sel.saved.length > 1) {
this.editor.setSelections(sel.saved);
}
},
@@ -146,12 +154,17 @@ define(function(require, exports, module) {
createSelection: function(start, end) {
end = end || start;
- var sels = this._selection.bufferRanges;
- sels[this._selection.index] = {
- start: this._posFromIndex(start),
- end: this._posFromIndex(end)
- };
- this.editor.setSelections(sels);
+ this.editor.setSelection(
+ this._posFromIndex(start),
+ this._posFromIndex(end)
+ );
+
+ // var sels = this._selection.bufferRanges;
+ // sels[this._selection.index] = {
+ // start: this._posFromIndex(start),
+ // end: this._posFromIndex(end)
+ // };
+ // this.editor.setSelections(sels);
},
/**
@@ -188,10 +201,13 @@ define(function(require, exports, module) {
if (typeof start == 'undefined') {
start = 0;
}
+
+ value = normalize(value);
// indent new value
if (!noIndent) {
- value = utils.padString(value, utils.getLinePaddingFromPosition(this.getContent(), start));
+ var pad = utils.getLinePaddingFromPosition(this.getContent(), start);
+ value = utils.padString(value, pad);
}
// find new caret position
@@ -219,7 +235,7 @@ define(function(require, exports, module) {
},
getSyntax: function() {
- var sel = this.editor.getSelections()[this.selectionIndex];
+ var sel = this.editor.getSelections()[this._selection.index];
var mode = this.editor.getModeForRange(sel.start, sel.end).name;
return this.modeMap[mode] || mode;
},
View
@@ -23,10 +23,9 @@ define(function(require, exports, module) {
*/
function selectionContext(editor, info) {
info = info || editorUtils.outputInfo(editor);
- return editor.selectionList().map(function(sel, i) {
- editor.selectionIndex = i;
-
- var r = range(editor.getSelectionRange());
+ var result = [];
+ editor.exec(function(i, sel) {
+ var r = range(sel);
var tag = htmlMatcher.tag(info.content, r.start);
if (!r.length() && tag) {
// no selection, use tag pair
@@ -39,16 +38,19 @@ define(function(require, exports, module) {
caret: r.start,
syntax: info.syntax,
profile: info.profile || null,
- counter: editor.selectionIndex + 1,
+ counter: i + 1,
contextNode: actionUtils.captureContext(editor, r.start)
};
if (r.length()) {
- out.pastedContent = utils.escapeText(r.substring(info.content));
+ var pasted = utils.escapeText(r.substring(info.content));
+ out.pastedContent = editorUtils.unindent(editor, pasted);
}
- return out;
- });
+ result[i] = out;
+ }, true);
+
+ return result;
}
function updateFinalCarets(selCtx, fromIndex, delta) {
@@ -63,7 +65,7 @@ define(function(require, exports, module) {
/**
* Returns current caret position for given editor
- * @param {Editor} editor Atom editor instance
+ * @param {Editor} editor Brackets editor instance
* @return {Point} Character position in editor
*/
function getCaret(editor) {
@@ -86,28 +88,6 @@ define(function(require, exports, module) {
}
}
-
- function updateCarets(selCtx, fromIndex, delta) {
- for (var i = fromIndex + 1, il = selCtx.length; i < il; i++) {
- selCtx[i].caret += delta;
- }
- }
-
- function resetCarets(selCtx) {
- selCtx.forEach(function(ctx) {
- ctx.caret = ctx.selection.start;
- });
- }
-
- function restore(editor, selCtx) {
- if (selCtx) {
- for (var i = selCtx.length - 1; i >= 0; i--) {
- editor.selectionIndex = i;
- editor.setCaretPos(selCtx[i].caret);
- }
- }
- }
-
return {
run: function(cmd, editor) {
if (cmd === 'wrap_with_abbreviation') {
@@ -125,15 +105,15 @@ define(function(require, exports, module) {
expandAbbreviation: function(editor) {
var info = editorUtils.outputInfo(editor);
- var selCtx = editor.selectionList().map(function(sel, i) {
- editor.selectionIndex = i;
- var r = range(editor.getSelectionRange());
- return {
+ var selCtx = [];
+ editor.exec(function(i, sel) {
+ var r = range(sel);
+ selCtx[i] = {
selection: r,
caret: r.start,
syntax: info.syntax,
profile: info.profile || null,
- counter: editor.selectionIndex + 1,
+ counter: i + 1,
contextNode: actionUtils.captureContext(editor, r.start)
};
});
@@ -162,9 +142,10 @@ define(function(require, exports, module) {
}
} catch (e) {
console.error(e);
+ result = ctx.pastedContent;
}
- editor.selectionIndex = i;
+ editor._selection.index = i;
replaced = editor.replaceContent(result, ctx.selection.start, ctx.selection.end);
ctx.finalCaret = getCaret(editor.editor);
updateFinalCarets(selCtx, i, lineDelta(ctx.pastedContent, replaced));
@@ -186,18 +167,21 @@ define(function(require, exports, module) {
label: 'Enter Abbreviation',
editor: editor.editor,
update: function(abbr) {
- resetCarets(selCtx);
- var tag, replaced, delta;
+ abbr = abbr.trim();
+ var tag, replaced;
+ var didUpdate = false;
for (var i = selCtx.length - 1, ctx; i >= 0; i--) {
ctx = selCtx[i];
tag = null;
- try {
- tag = updateTag.getUpdatedTag(abbr, {match: ctx.tag}, info.content, {
- counter: ctx.counter
- });
- } catch (e) {
- console.error(e);
+ if (abbr) {
+ try {
+ tag = updateTag.getUpdatedTag(abbr, {match: ctx.tag}, info.content, {
+ counter: ctx.counter
+ });
+ } catch (e) {
+ console.error(e);
+ }
}
if (!tag) {
@@ -218,22 +202,17 @@ define(function(require, exports, module) {
});
}
- delta = 0;
- editor.selectionIndex = i;
replaced.forEach(function(data) {
+ didUpdate = true;
editor.replaceContent(data.content, data.start, data.end);
- ctx.caret = data.start;
- delta += data.content.length - data.end + data.start;
+ ctx.finalCaret = editor._posFromIndex(data.start);
});
-
- updateCarets(selCtx, i, delta);
}
+
+ return didUpdate;
},
confirm: function() {
- restore(editor, selCtx);
- },
- cancel: function() {
- restore(editor, selCtx);
+ setFinalCarets(selCtx, editor.editor);
}
});
}
View
@@ -27,5 +27,6 @@
"select_previous_item": "Shift-Ctrl-,",
"reflect_css_value": "Shift-Ctrl-Alt-R",
- "insert_formatted_line_break": "Enter"
+ "insert_formatted_line_break": "Enter",
+ "interactive_expand_abbreviation": "Ctrl-Shift-Alt-Enter"
}
View
@@ -72,11 +72,9 @@ define(function(require, exports, module) {
editor.setup(bracketsEditor);
bracketsEditor.document.batchOperation(function() {
- var selections = editor.selectionList();
- for (var i = 0, il = selections.length; i < il; i++) {
- editor.selectionIndex = i;
+ editor.exec(function() {
runAction(action, df);
- }
+ });
});
return df.resolve().promise();
};
@@ -165,6 +163,8 @@ define(function(require, exports, module) {
* Register special, interactive versions of some Emmet actions
*/
function registerInteractiveCommands(menu) {
+ actions.add('interactive_expand_abbreviation', function() {}, 'Expand Abbreviation (interactive)');
+
['wrap_with_abbreviation', 'update_tag', 'interactive_expand_abbreviation'].forEach(function(cmd) {
var action = actions.get(cmd);
CommandManager.register(actionLabel(action, cmd), 'io.emmet.' + cmd, function() {
@@ -173,7 +173,6 @@ define(function(require, exports, module) {
});
});
- menu.addMenuItem('io.emmet.interactive_expand_abbreviation', 'Alt-Enter');
}
function actionLabel(action, fallback) {
Oops, something went wrong.

0 comments on commit 08870fb

Please sign in to comment.