Single quotes are not auto-paired like double quotes

fixed #663
This commit is contained in:
Fabian Jakobs 2012-04-06 12:20:10 +02:00
commit d09c0bcdbf

View file

@ -52,12 +52,12 @@ var CstyleBehaviour = function () {
return { return {
text: '{' + selected + '}', text: '{' + selected + '}',
selection: false selection: false
} };
} else { } else {
return { return {
text: '{}', text: '{}',
selection: [1, 1] selection: [1, 1]
} };
} }
} else if (text == '}') { } else if (text == '}') {
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
@ -69,7 +69,7 @@ var CstyleBehaviour = function () {
return { return {
text: '', text: '',
selection: [1, 1] selection: [1, 1]
} };
} }
} }
} else if (text == "\n") { } else if (text == "\n") {
@ -87,7 +87,7 @@ var CstyleBehaviour = function () {
return { return {
text: '\n' + indent + '\n' + next_indent, text: '\n' + indent + '\n' + next_indent,
selection: [1, indent.length, 1, indent.length] selection: [1, indent.length, 1, indent.length]
} };
} }
} }
}); });
@ -112,12 +112,12 @@ var CstyleBehaviour = function () {
return { return {
text: '(' + selected + ')', text: '(' + selected + ')',
selection: false selection: false
} };
} else { } else {
return { return {
text: '()', text: '()',
selection: [1, 1] selection: [1, 1]
} };
} }
} else if (text == ')') { } else if (text == ')') {
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
@ -129,7 +129,7 @@ var CstyleBehaviour = function () {
return { return {
text: '', text: '',
selection: [1, 1] selection: [1, 1]
} };
} }
} }
} }
@ -148,14 +148,15 @@ var CstyleBehaviour = function () {
}); });
this.add("string_dquotes", "insertion", function (state, action, editor, session, text) { this.add("string_dquotes", "insertion", function (state, action, editor, session, text) {
if (text == '"') { if (text == '"' || text == "'") {
var quote = 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 !== "") {
return { return {
text: '"' + selected + '"', text: quote + selected + quote,
selection: false selection: false
} };
} else { } else {
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row); var line = session.doc.getLine(cursor.row);
@ -176,7 +177,7 @@ var CstyleBehaviour = function () {
if (token.type == "string") { if (token.type == "string") {
quotepos = -1; quotepos = -1;
} else if (quotepos < 0) { } else if (quotepos < 0) {
quotepos = token.value.indexOf('"'); quotepos = token.value.indexOf(quote);
} }
if ((token.value.length + col) > selection.start.column) { if ((token.value.length + col) > selection.start.column) {
break; break;
@ -185,19 +186,19 @@ var CstyleBehaviour = function () {
} }
// Try and be smart about when we auto insert. // Try and be smart about when we auto insert.
if (!token || (quotepos < 0 && token.type !== "comment" && (token.type !== "string" || ((selection.start.column !== token.value.length+col-1) && token.value.lastIndexOf('"') === token.value.length-1)))) { if (!token || (quotepos < 0 && token.type !== "comment" && (token.type !== "string" || ((selection.start.column !== token.value.length+col-1) && token.value.lastIndexOf(quote) === token.value.length-1)))) {
return { return {
text: '""', text: quote + quote,
selection: [1,1] selection: [1,1]
} };
} else if (token && token.type === "string") { } else if (token && token.type === "string") {
// Ignore input and move right one if we're typing over the closing quote. // Ignore input and move right one if we're typing over the closing quote.
var rightChar = line.substring(cursor.column, cursor.column + 1); var rightChar = line.substring(cursor.column, cursor.column + 1);
if (rightChar == '"') { if (rightChar == quote) {
return { return {
text: '', text: '',
selection: [1, 1] selection: [1, 1]
} };
} }
} }
} }
@ -206,7 +207,7 @@ var CstyleBehaviour = function () {
this.add("string_dquotes", "deletion", function (state, action, editor, session, range) { this.add("string_dquotes", "deletion", function (state, action, editor, session, range) {
var selected = session.doc.getTextRange(range); var selected = session.doc.getTextRange(range);
if (!range.isMultiLine() && selected == '"') { if (!range.isMultiLine() && (selected == '"' || selected == "'")) {
var line = session.doc.getLine(range.start.row); var line = session.doc.getLine(range.start.row);
var rightChar = line.substring(range.start.column + 1, range.start.column + 2); var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
if (rightChar == '"') { if (rightChar == '"') {
@ -216,7 +217,8 @@ var CstyleBehaviour = function () {
} }
}); });
} };
oop.inherits(CstyleBehaviour, Behaviour); oop.inherits(CstyleBehaviour, Behaviour);
exports.CstyleBehaviour = CstyleBehaviour; exports.CstyleBehaviour = CstyleBehaviour;