From e3ad71b4e157ac828aa6c43975c42049839eaf1e Mon Sep 17 00:00:00 2001 From: Vlad Zinculescu Date: Mon, 22 Oct 2012 18:37:23 +0200 Subject: [PATCH 01/10] modifiy numbers with keyboard --- lib/ace/commands/default_commands.js | 10 ++++ lib/ace/editor.js | 87 +++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index 9da2dd1d..458d5582 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -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"), diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 937e0e73..190e0d86 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -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; -}); \ No newline at end of file +}); From 9c3e6c5cce7c29070d6c630150863ba2e6a343ed Mon Sep 17 00:00:00 2001 From: Vlad Zinculescu Date: Mon, 22 Oct 2012 18:42:05 +0200 Subject: [PATCH 02/10] reformat the code --- lib/ace/editor.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 190e0d86..5c34e33d 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -1341,7 +1341,7 @@ var Editor = function(renderer, session) { * * 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 ) { + this.modifyNumber = function(amount) { var row = this.selection.getCursor().row; var column = this.selection.getCursor().column; @@ -1354,11 +1354,11 @@ var Editor = function(renderer, session) { var c = this.session.getTextRange(charRange); //if the char is a digit - if( !isNaN(parseFloat(c)) && isFinite(c) ) { + 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 ) { + if(nr) { var fp = nr.value.indexOf(".") >= 0 ? nr.start + nr.value.indexOf(".") + 1 : nr.end; var decimals = nr.start + nr.value.length - fp; @@ -1366,7 +1366,7 @@ var Editor = function(renderer, session) { t *= Math.pow(10, decimals); - if( fp !== nr.end && column < fp){ + if(fp !== nr.end && column < fp){ amount *= Math.pow(10, nr.end - column - 1); } else { amount *= Math.pow(10, nr.end - column); @@ -1382,16 +1382,10 @@ var Editor = function(renderer, session) { replaceRange.end.row = row; replaceRange.start.column = nr.start; replaceRange.end.column = nr.end; - this.session.replace(replaceRange, nnr ); + 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 ); + this.moveCursorTo(row, Math.max(nr.start +1, column + nnr.length - nr.value.length)); } } From 82c07a5f68da4b1bfb59f7ebddfd6a07b8eea598 Mon Sep 17 00:00:00 2001 From: Vlad Zinculescu Date: Tue, 23 Oct 2012 10:49:52 +0200 Subject: [PATCH 03/10] update keyboard shortcut for modifyNumber --- lib/ace/commands/default_commands.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index 458d5582..b833f0ca 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -333,12 +333,12 @@ exports.commands = [{ multiSelectAction: "forEach" }, { name: "modifyNumber+1", - bindKey: bindKey("Ctrl-Shift-Up", "Ctrl-Command-Up"), + bindKey: bindKey("Alt-Shift-Up", "Alt-Shift-Up"), exec: function(editor) { editor.modifyNumber(1); }, multiSelectAction: "forEach" }, { name: "modifyNumber-1", - bindKey: bindKey("Ctrl-Shift-Down", "Ctrl-Command-Down"), + bindKey: bindKey("Alt-Shift-Down", "Alt-Shift-Down"), exec: function(editor) { editor.modifyNumber(-1); }, multiSelectAction: "forEach" }, { From 66f46cc6ef8a05ae84d4eb7964eaca9b91efe5e8 Mon Sep 17 00:00:00 2001 From: Vlad Zinculescu Date: Tue, 23 Oct 2012 10:54:27 +0200 Subject: [PATCH 04/10] update description of modifyNumber --- lib/ace/editor.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 5c34e33d..1ac6e2c2 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -1339,7 +1339,8 @@ var Editor = function(renderer, session) { /** * 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) + * 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 will update the whole number the digit is part of. **/ this.modifyNumber = function(amount) { var row = this.selection.getCursor().row; From 073ee0cbf6e85b7d98e29f41ea697e6396f9b936 Mon Sep 17 00:00:00 2001 From: Vlad Zinculescu Date: Tue, 23 Oct 2012 10:55:19 +0200 Subject: [PATCH 05/10] update description of modifyNumber --- lib/ace/editor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 1ac6e2c2..318844a3 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -1339,7 +1339,7 @@ var Editor = function(renderer, session) { /** * 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). + * If the character before the cursor is a number, you can increase/descrease it's value with 1 by pressing Alt+Shift+Up/Down. * This will update the whole number the digit is part of. **/ this.modifyNumber = function(amount) { From 048c88cf5e31a466a3f9d86544511e93b22f9d64 Mon Sep 17 00:00:00 2001 From: Vlad Zinculescu Date: Tue, 23 Oct 2012 12:17:25 +0200 Subject: [PATCH 06/10] update keybinding for modifyNumber on windows --- lib/ace/commands/default_commands.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index b833f0ca..ec85f61c 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -333,12 +333,12 @@ exports.commands = [{ multiSelectAction: "forEach" }, { name: "modifyNumber+1", - bindKey: bindKey("Alt-Shift-Up", "Alt-Shift-Up"), + bindKey: bindKey("Ctrl-Shift-Up", "Alt-Shift-Up"), exec: function(editor) { editor.modifyNumber(1); }, multiSelectAction: "forEach" }, { name: "modifyNumber-1", - bindKey: bindKey("Alt-Shift-Down", "Alt-Shift-Down"), + bindKey: bindKey("Ctrl-Shift-Down", "Alt-Shift-Down"), exec: function(editor) { editor.modifyNumber(-1); }, multiSelectAction: "forEach" }, { From 43c74f415fe875f4675889bbadda2d93444bdb7c Mon Sep 17 00:00:00 2001 From: Garen Torikian Date: Tue, 23 Oct 2012 11:00:13 -0700 Subject: [PATCH 07/10] Add tenth place, fix doc comments --- lib/ace/commands/default_commands.js | 10 ++++++++++ lib/ace/editor.js | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index ec85f61c..f7c96538 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -341,6 +341,16 @@ exports.commands = [{ bindKey: bindKey("Ctrl-Shift-Down", "Alt-Shift-Down"), exec: function(editor) { editor.modifyNumber(-1); }, multiSelectAction: "forEach" +}, { + name: "modifyNumber+10", + bindKey: bindKey("Ctrl-Alt-Shift-Up", "Alt-Command-Shift-Up"), + exec: function(editor) { editor.modifyNumber(10); }, + multiSelectAction: "forEach" +}, { + name: "modifyNumber-10", + bindKey: bindKey("Ctrl-Alt-Shift-Down", "Alt-Command-Shift-Down"), + exec: function(editor) { editor.modifyNumber(-10); }, + multiSelectAction: "forEach" }, { name: "replace", bindKey: bindKey("Ctrl-R", "Command-Option-F"), diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 318844a3..85fb5a18 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -1304,7 +1304,7 @@ var Editor = function(renderer, session) { /** * Editor.toggleCommentLines() * - * Given the currently selected range, this function either comments all lines or uncomments all lines (depending on whether it's commented or not). + * Given the currently selected range, this function either comments all the lines, or uncomments all of them. **/ this.toggleCommentLines = function() { var state = this.session.getState(this.getCursorPosition().row); @@ -1313,9 +1313,9 @@ var Editor = function(renderer, session) { }; /** - * Editor.getNumberAt() + * Editor.getNumberAt() -> Number * - * Works like getTokenAt just that it returns a number + * Works like [[Editor.getTokenAt]], excepts it returns a number. **/ this.getNumberAt = function( row, column ) { var _numberRx = /[\-]?[0-9]+(?:\.[0-9]+)?/g @@ -1339,8 +1339,8 @@ var Editor = function(renderer, session) { /** * Editor.modifyNumber() * - * If the character before the cursor is a number, you can increase/descrease it's value with 1 by pressing Alt+Shift+Up/Down. - * This will update the whole number the digit is part of. + * If the character before the cursor is a number, you can increase/or decrease its value by one (by pressing Ctrl+Shift+Up/Down), or + * 10 (by pressing Ctrl+Alt+Shift+Up/Down). **/ this.modifyNumber = function(amount) { var row = this.selection.getCursor().row; From 23f7fc465859783a928d407abcfec4d56a7472e6 Mon Sep 17 00:00:00 2001 From: Garen Torikian Date: Tue, 23 Oct 2012 11:42:22 -0700 Subject: [PATCH 08/10] Remove +10, change command name --- lib/ace/commands/default_commands.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index f7c96538..8c33fc42 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -332,25 +332,15 @@ exports.commands = [{ exec: function(editor) { editor.toggleCommentLines(); }, multiSelectAction: "forEach" }, { - name: "modifyNumber+1", + name: "modifyNumberUp", bindKey: bindKey("Ctrl-Shift-Up", "Alt-Shift-Up"), exec: function(editor) { editor.modifyNumber(1); }, multiSelectAction: "forEach" }, { - name: "modifyNumber-1", + name: "modifyNumberDown", bindKey: bindKey("Ctrl-Shift-Down", "Alt-Shift-Down"), exec: function(editor) { editor.modifyNumber(-1); }, multiSelectAction: "forEach" -}, { - name: "modifyNumber+10", - bindKey: bindKey("Ctrl-Alt-Shift-Up", "Alt-Command-Shift-Up"), - exec: function(editor) { editor.modifyNumber(10); }, - multiSelectAction: "forEach" -}, { - name: "modifyNumber-10", - bindKey: bindKey("Ctrl-Alt-Shift-Down", "Alt-Command-Shift-Down"), - exec: function(editor) { editor.modifyNumber(-10); }, - multiSelectAction: "forEach" }, { name: "replace", bindKey: bindKey("Ctrl-R", "Command-Option-F"), From aca8b9a73b5b74e1b588f9bc52343026f43512a1 Mon Sep 17 00:00:00 2001 From: Garen Torikian Date: Tue, 23 Oct 2012 11:46:36 -0700 Subject: [PATCH 09/10] Fix doc comment --- lib/ace/editor.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 85fb5a18..1551473c 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -1337,10 +1337,10 @@ var Editor = function(renderer, session) { return null; }; /** - * Editor.modifyNumber() - * - * If the character before the cursor is a number, you can increase/or decrease its value by one (by pressing Ctrl+Shift+Up/Down), or - * 10 (by pressing Ctrl+Alt+Shift+Up/Down). + * Editor.modifyNumber(amount) + * - amount (Number): The value to change the numeral by (can be negative to decrease value) + * + * If the character before the cursor is a number, this functions changes its value by `amount`. **/ this.modifyNumber = function(amount) { var row = this.selection.getCursor().row; From 961b6cecb6ce6e4d55bb402ef82fb2b1bd7ccab9 Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 23 Oct 2012 23:07:36 +0400 Subject: [PATCH 10/10] small cleanup --- lib/ace/editor.js | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 1551473c..1ab72848 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -1315,7 +1315,7 @@ var Editor = function(renderer, session) { /** * Editor.getNumberAt() -> Number * - * Works like [[Editor.getTokenAt]], excepts it returns a number. + * Works like [[Editor.getTokenAt]], except it returns a number. **/ this.getNumberAt = function( row, column ) { var _numberRx = /[\-]?[0-9]+(?:\.[0-9]+)?/g @@ -1346,20 +1346,16 @@ var Editor = function(renderer, session) { 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; + // get the char before the cursor + var charRange = new Range(row, column-1, row, 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 + // 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) { + // 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; @@ -1378,18 +1374,14 @@ var Editor = function(renderer, session) { 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; + var replaceRange = new Range(row, nr.start, row, nr.end); this.session.replace(replaceRange, nnr); //reposition the cursor this.moveCursorTo(row, Math.max(nr.start +1, column + nnr.length - nr.value.length)); } - } + } }; /** related to: EditSession.remove