modifiy numbers with keyboard
This commit is contained in:
parent
f38a4e0e5b
commit
e3ad71b4e1
2 changed files with 96 additions and 1 deletions
|
|
@ -331,6 +331,16 @@ exports.commands = [{
|
|||
bindKey: bindKey("Ctrl-/", "Command-/"),
|
||||
exec: function(editor) { editor.toggleCommentLines(); },
|
||||
multiSelectAction: "forEach"
|
||||
}, {
|
||||
name: "modifyNumber+1",
|
||||
bindKey: bindKey("Ctrl-Shift-Up", "Ctrl-Command-Up"),
|
||||
exec: function(editor) { editor.modifyNumber(1); },
|
||||
multiSelectAction: "forEach"
|
||||
}, {
|
||||
name: "modifyNumber-1",
|
||||
bindKey: bindKey("Ctrl-Shift-Down", "Ctrl-Command-Down"),
|
||||
exec: function(editor) { editor.modifyNumber(-1); },
|
||||
multiSelectAction: "forEach"
|
||||
}, {
|
||||
name: "replace",
|
||||
bindKey: bindKey("Ctrl-R", "Command-Option-F"),
|
||||
|
|
|
|||
|
|
@ -1312,6 +1312,91 @@ var Editor = function(renderer, session) {
|
|||
this.session.getMode().toggleCommentLines(state, this.session, rows.first, rows.last);
|
||||
};
|
||||
|
||||
/**
|
||||
* Editor.getNumberAt()
|
||||
*
|
||||
* Works like getTokenAt just that it returns a number
|
||||
**/
|
||||
this.getNumberAt = function( row, column ) {
|
||||
var _numberRx = /[\-]?[0-9]+(?:\.[0-9]+)?/g
|
||||
_numberRx.lastIndex = 0
|
||||
|
||||
var s = this.session.getLine(row)
|
||||
while(_numberRx.lastIndex < column - 1 ){
|
||||
var m = _numberRx.exec(s)
|
||||
if(m.index <= column && m.index+m[0].length >= column){
|
||||
var number = {
|
||||
value: m[0],
|
||||
start: m.index,
|
||||
end: m.index+m[0].length
|
||||
|
||||
}
|
||||
return number
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
/**
|
||||
* Editor.modifyNumber()
|
||||
*
|
||||
* If the character before the cursor is a number, you can increase/descrease it's value with 1 by pressing Ctrl+Cmd+Up/Down (Mac) Ctrl+Shift+Up/Down (Win)
|
||||
**/
|
||||
this.modifyNumber = function( amount ) {
|
||||
var row = this.selection.getCursor().row;
|
||||
var column = this.selection.getCursor().column;
|
||||
|
||||
//get the char before the cursor
|
||||
var charRange = new Range(0,0,0,0);
|
||||
charRange.start.row = row;
|
||||
charRange.end.row = row;
|
||||
charRange.start.column = column-1;
|
||||
charRange.end.column = column;
|
||||
|
||||
var c = this.session.getTextRange(charRange);
|
||||
//if the char is a digit
|
||||
if( !isNaN(parseFloat(c)) && isFinite(c) ) {
|
||||
//get the whole number the digit is part of
|
||||
var nr = this.getNumberAt(row, column);
|
||||
//if number found
|
||||
if( nr ) {
|
||||
var fp = nr.value.indexOf(".") >= 0 ? nr.start + nr.value.indexOf(".") + 1 : nr.end;
|
||||
var decimals = nr.start + nr.value.length - fp;
|
||||
|
||||
var t = parseFloat(nr.value);
|
||||
t *= Math.pow(10, decimals);
|
||||
|
||||
|
||||
if( fp !== nr.end && column < fp){
|
||||
amount *= Math.pow(10, nr.end - column - 1);
|
||||
} else {
|
||||
amount *= Math.pow(10, nr.end - column);
|
||||
}
|
||||
|
||||
t += amount;
|
||||
t /= Math.pow(10, decimals);
|
||||
var nnr = t.toFixed(decimals);
|
||||
|
||||
//update number
|
||||
var replaceRange = new Range(0, 0, 0, 0);
|
||||
replaceRange.start.row = row;
|
||||
replaceRange.end.row = row;
|
||||
replaceRange.start.column = nr.start;
|
||||
replaceRange.end.column = nr.end;
|
||||
this.session.replace(replaceRange, nnr );
|
||||
|
||||
var cursor = column;
|
||||
var offset = nnr.length - nr.value.length;
|
||||
|
||||
|
||||
cursor = Math.max(nr.start +1, cursor + offset);
|
||||
console.log(offset, nr.value.length, nnr.length)
|
||||
//reposition the cursor
|
||||
this.moveCursorTo(row, cursor );
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/** related to: EditSession.remove
|
||||
* Editor.removeLines()
|
||||
*
|
||||
|
|
@ -2126,4 +2211,4 @@ var Editor = function(renderer, session) {
|
|||
|
||||
|
||||
exports.Editor = Editor;
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue