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 a6d9c21
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 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, 'a');
exec("insertstring", 1, "`");
assert.equal(editor.getValue(), "a``");
},
"test: css": function() {
editor.session.setMode(new CSSMode());
Expand Down
14 changes: 11 additions & 3 deletions src/mode/behaviour/cstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,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("[ufr]", "i")
}
]
});
};
oop.inherits(Mode, TextMode);

Expand Down

0 comments on commit a6d9c21

Please sign in to comment.