Skip to content

Commit

Permalink
Merge pull request #1524 from ajaxorg/fix/complete-in-middle
Browse files Browse the repository at this point in the history
Fix inserting key in middle of variable has weird interaction
  • Loading branch information
Zef Hemel committed May 15, 2012
2 parents 60bbc15 + 8c2f0cb commit a03063e
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions plugins-client/ext.language/complete.js
Expand Up @@ -11,7 +11,7 @@ var dom = require("ace/lib/dom");
var keyhandler = require("ext/language/keyhandler");

var lang = require("ace/lib/lang");
var ID_REGEX = /[a-zA-Z_0-9\$]/;
var ID_REGEX = /[a-zA-Z_0-9\$\_]/;

var oldCommandKey, oldOnTextInput;

Expand All @@ -38,15 +38,29 @@ function retrievePreceedingIdentifier(text, pos) {
return buf.reverse().join("");
}

function retrieveFollowingIdentifier(text, pos) {
var buf = [];
for (var i = pos; i < text.length; i++) {
if (ID_REGEX.test(text[i]))
buf.push(text[i]);
else
break;
}
return buf;
}

/**
* Replaces the preceeding identifier (`prefix`) with `newText`, where ^^
* indicates the cursor position after the replacement
* indicates the cursor position after the replacement.
* If the prefix is already followed by an identifier substring, that string
* is deleted.
*/
function replaceText(editor, prefix, newText) {
var pos = editor.getCursorPosition();
var line = editor.getSession().getLine(pos.row);
var doc = editor.getSession().getDocument();

// Ensure cursor marker
if (newText.indexOf("^^") === -1)
newText += "^^";

Expand All @@ -56,6 +70,8 @@ function replaceText(editor, prefix, newText) {

var prefixWhitespace = line.substring(0, i);

var postfix = retrieveFollowingIdentifier(line, pos.column) || "";

// Pad the text to be inserted
var paddedLines = newText.split("\n").join("\n" + prefixWhitespace);
var splitPaddedLines = paddedLines.split("\n");
Expand All @@ -68,7 +84,7 @@ function replaceText(editor, prefix, newText) {
// Remove cursor marker
paddedLines = paddedLines.replace("^^", "");

doc.removeInLine(pos.row, pos.column - prefix.length, pos.column);
doc.removeInLine(pos.row, pos.column - prefix.length, pos.column + postfix.length);
doc.insert({row: pos.row, column: pos.column - prefix.length}, paddedLines);
editor.moveCursorTo(pos.row + rowOffset, pos.column + colOffset - prefix.length);
}
Expand Down

0 comments on commit a03063e

Please sign in to comment.