Merge pull request #1055 from ajaxorg/selective-bracket-insertion
Selective bracket insertion/skipping
This commit is contained in:
commit
f38a4e0e5b
1 changed files with 63 additions and 7 deletions
|
|
@ -33,19 +33,70 @@ define(function(require, exports, module) {
|
||||||
|
|
||||||
var oop = require("../../lib/oop");
|
var oop = require("../../lib/oop");
|
||||||
var Behaviour = require("../behaviour").Behaviour;
|
var Behaviour = require("../behaviour").Behaviour;
|
||||||
|
var TokenIterator = require("../../token_iterator").TokenIterator;
|
||||||
|
|
||||||
|
var autoInsertedBrackets = 0;
|
||||||
|
var autoInsertedRow = -1;
|
||||||
|
var autoInsertedLineEnd = "";
|
||||||
|
|
||||||
var CstyleBehaviour = function () {
|
var CstyleBehaviour = function () {
|
||||||
|
|
||||||
|
CstyleBehaviour.isSaneInsertion = function(editor, session) {
|
||||||
|
var cursor = editor.getCursorPosition();
|
||||||
|
var iterator = new TokenIterator(session, cursor.row, cursor.column);
|
||||||
|
|
||||||
|
// Don't insert in the middle of a keyword/identifier/lexical
|
||||||
|
if (!this.$matchTokenType(iterator.getCurrentToken() || "text", ["text", "paren.rparen"])) {
|
||||||
|
// Look ahead in case we're at the end of a token
|
||||||
|
iterator = new TokenIterator(session, cursor.row, cursor.column + 1);
|
||||||
|
if (!this.$matchTokenType(iterator.getCurrentToken() || "text", ["text", "paren.rparen"]))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only insert in front of whitespace/comments
|
||||||
|
iterator.stepForward();
|
||||||
|
return iterator.getCurrentTokenRow() !== cursor.row ||
|
||||||
|
this.$matchTokenType(iterator.getCurrentToken() || "text", ["text", "comment", "paren.rparen"]);
|
||||||
|
};
|
||||||
|
|
||||||
|
CstyleBehaviour.$matchTokenType = function(token, types) {
|
||||||
|
return types.indexOf(token.type || token) > -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
CstyleBehaviour.recordAutoInsert = function(editor, session, bracket) {
|
||||||
|
var cursor = editor.getCursorPosition();
|
||||||
|
var line = session.doc.getLine(cursor.row);
|
||||||
|
// Reset previous state if text or context changed too much
|
||||||
|
if (!this.isAutoInsertedClosing(cursor, line, autoInsertedLineEnd[0]))
|
||||||
|
autoInsertedBrackets = 0;
|
||||||
|
autoInsertedRow = cursor.row;
|
||||||
|
autoInsertedLineEnd = bracket + line.substr(cursor.column);
|
||||||
|
autoInsertedBrackets++;
|
||||||
|
};
|
||||||
|
|
||||||
|
CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
|
||||||
|
return autoInsertedBrackets > 0 &&
|
||||||
|
cursor.row === autoInsertedRow &&
|
||||||
|
bracket === autoInsertedLineEnd[0] &&
|
||||||
|
line.substr(cursor.column) === autoInsertedLineEnd;
|
||||||
|
};
|
||||||
|
|
||||||
|
CstyleBehaviour.popAutoInsertedClosing = function() {
|
||||||
|
autoInsertedLineEnd = autoInsertedLineEnd.substr(1);
|
||||||
|
autoInsertedBrackets--;
|
||||||
|
};
|
||||||
|
|
||||||
this.add("braces", "insertion", function (state, action, editor, session, text) {
|
this.add("braces", "insertion", function (state, action, editor, session, text) {
|
||||||
if (text == '{') {
|
if (text == '{') {
|
||||||
var selection = editor.getSelectionRange();
|
var selection = editor.getSelectionRange();
|
||||||
var selected = session.doc.getTextRange(selection);
|
var selected = session.doc.getTextRange(selection);
|
||||||
if (selected !== "") {
|
if (selected !== "" && selected !== "{") {
|
||||||
return {
|
return {
|
||||||
text: '{' + selected + '}',
|
text: '{' + selected + '}',
|
||||||
selection: false
|
selection: false
|
||||||
};
|
};
|
||||||
} else {
|
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
||||||
|
CstyleBehaviour.recordAutoInsert(editor, session, "}");
|
||||||
return {
|
return {
|
||||||
text: '{}',
|
text: '{}',
|
||||||
selection: [1, 1]
|
selection: [1, 1]
|
||||||
|
|
@ -57,7 +108,8 @@ var CstyleBehaviour = function () {
|
||||||
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
||||||
if (rightChar == '}') {
|
if (rightChar == '}') {
|
||||||
var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
|
var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
|
||||||
if (matching !== null) {
|
if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
|
||||||
|
CstyleBehaviour.popAutoInsertedClosing();
|
||||||
return {
|
return {
|
||||||
text: '',
|
text: '',
|
||||||
selection: [1, 1]
|
selection: [1, 1]
|
||||||
|
|
@ -105,7 +157,8 @@ var CstyleBehaviour = function () {
|
||||||
text: '(' + selected + ')',
|
text: '(' + selected + ')',
|
||||||
selection: false
|
selection: false
|
||||||
};
|
};
|
||||||
} else {
|
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
||||||
|
CstyleBehaviour.recordAutoInsert(editor, session, ")");
|
||||||
return {
|
return {
|
||||||
text: '()',
|
text: '()',
|
||||||
selection: [1, 1]
|
selection: [1, 1]
|
||||||
|
|
@ -117,7 +170,8 @@ var CstyleBehaviour = function () {
|
||||||
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
||||||
if (rightChar == ')') {
|
if (rightChar == ')') {
|
||||||
var matching = session.$findOpeningBracket(')', {column: cursor.column + 1, row: cursor.row});
|
var matching = session.$findOpeningBracket(')', {column: cursor.column + 1, row: cursor.row});
|
||||||
if (matching !== null) {
|
if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
|
||||||
|
CstyleBehaviour.popAutoInsertedClosing();
|
||||||
return {
|
return {
|
||||||
text: '',
|
text: '',
|
||||||
selection: [1, 1]
|
selection: [1, 1]
|
||||||
|
|
@ -148,7 +202,8 @@ var CstyleBehaviour = function () {
|
||||||
text: '[' + selected + ']',
|
text: '[' + selected + ']',
|
||||||
selection: false
|
selection: false
|
||||||
};
|
};
|
||||||
} else {
|
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
||||||
|
CstyleBehaviour.recordAutoInsert(editor, session, "]");
|
||||||
return {
|
return {
|
||||||
text: '[]',
|
text: '[]',
|
||||||
selection: [1, 1]
|
selection: [1, 1]
|
||||||
|
|
@ -160,7 +215,8 @@ var CstyleBehaviour = function () {
|
||||||
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
||||||
if (rightChar == ']') {
|
if (rightChar == ']') {
|
||||||
var matching = session.$findOpeningBracket(']', {column: cursor.column + 1, row: cursor.row});
|
var matching = session.$findOpeningBracket(']', {column: cursor.column + 1, row: cursor.row});
|
||||||
if (matching !== null) {
|
if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
|
||||||
|
CstyleBehaviour.popAutoInsertedClosing();
|
||||||
return {
|
return {
|
||||||
text: '',
|
text: '',
|
||||||
selection: [1, 1]
|
selection: [1, 1]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue