Skip to content

Commit

Permalink
make quotes check more generic; add auto quote insertion for javascri…
Browse files Browse the repository at this point in the history
…pt (backtick)
  • Loading branch information
mkslanc committed Feb 22, 2023
1 parent f8b1ca7 commit 6286eb5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/mode/behaviour/behaviour_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ module.exports = {
exec("insertstring", 1, '`');
exec("insertstring", 1, 'b');
assert.equal(editor.getValue(), "`b`");

editor.setValue("");
exec("insertstring", 1, 'a');
exec("insertstring", 1, "'");
assert.equal(editor.getValue(), "a'");

editor.setValue("");
exec("insertstring", 1, 'b');
exec("insertstring", 1, "`");
assert.equal(editor.getValue(), "b``");
},
"test: css": function() {
editor.session.setMode(new CSSMode());
Expand Down
24 changes: 20 additions & 4 deletions src/mode/behaviour/cstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ var getWrapped = function(selection, selected, opening, closing) {
]
};
};

/**
* Creates a new Cstyle behaviour object with the specified options.
* @constructor
* @param {Object} options - The options for the Cstyle behaviour object.
* @param {boolean} options.braces - Whether to force braces auto-pairing.
* @param {Object[]} options.quotesPrefixes - An array of objects containing information about quotes prefixes.
* @param {RegExp} options.quotesPrefixes[].quotes - The regular expression used to determine which quote type the prefix applies to.
* @param {RegExp} options.quotesPrefixes[].condition - The regular expression used to determine if the character on the left of the quote is suitable.
*/
var CstyleBehaviour = function(options) {
this.add("braces", "insertion", function(state, action, editor, session, text) {
var cursor = editor.getCursorPosition();
Expand Down Expand Up @@ -261,9 +269,17 @@ var CstyleBehaviour = function(options) {
var isWordBefore = wordRe.test(leftChar);
wordRe.lastIndex = 0;
var isWordAfter = wordRe.test(rightChar);
let hasStringPrefixes = (options && options.quotePrefixes &&
Array.isArray(options.quotePrefixes)) ? options.quotePrefixes.includes(leftChar) : null;

var hasStringPrefixes = false;
if (options && options.quotesPrefixes && Array.isArray(options.quotesPrefixes)) {
for (var prefix of options.quotesPrefixes) {
if (prefix.quotes && prefix.condition && prefix.quotes instanceof RegExp
&& prefix.quotes.test(quotes[text]) && prefix.condition instanceof RegExp
&& prefix.condition.test(leftChar)) {
hasStringPrefixes = true;
break;
}
}
}
if ((!hasStringPrefixes && isWordBefore) || isWordAfter)
return null; // before or after alphanumeric
if (rightChar && !/[\s;,.})\]\\]/.test(rightChar))
Expand Down
11 changes: 9 additions & 2 deletions src/mode/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ var CStyleFoldMode = require("./folding/cstyle").FoldMode;

var Mode = function() {
this.HighlightRules = JavaScriptHighlightRules;

this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = new CstyleBehaviour({
quotesPrefixes: [
{
quotes: new RegExp("`"),
condition: new RegExp("\\w")
}
]
});
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, TextMode);
Expand Down
9 changes: 8 additions & 1 deletion src/mode/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ var Range = require("../range").Range;
var Mode = function() {
this.HighlightRules = PythonHighlightRules;
this.foldingRules = new PythonFoldMode("\\:");
this.$behaviour = new CstyleBehaviour({quotePrefixes: ["f", "F", "u", "U", "r", "R"]});
this.$behaviour = new CstyleBehaviour({
quotesPrefixes: [
{
quotes: new RegExp("['\"]"),
condition: new RegExp("[ruf]", "i")
}
]
});
};
oop.inherits(Mode, TextMode);

Expand Down

0 comments on commit 6286eb5

Please sign in to comment.