Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to determine specific prefixes for quote insertion #5067

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/mode/behaviour/behaviour_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var XMLMode = require("../xml").Mode;
var HTMLMode = require("../html").Mode;
var CSSMode = require("../css").Mode;
var MarkdownMode = require("../markdown").Mode;
var PythonMode = require("../python").Mode;
var editor;
var exec = function(name, times, args) {
do {
Expand Down Expand Up @@ -286,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 Expand Up @@ -393,6 +404,17 @@ module.exports = {
exec("insertstring", 1, "`");
exec("insertstring", 1, "`");
assert.equal(editor.getValue(), "``-``");
},
"test: python": function() {
editor.session.setMode(new PythonMode());
editor.setValue("f", 1);
exec("insertstring", 1, '"');
assert.equal(editor.getValue(), 'f""');

// there is no such prefix for python
editor.setValue("p", 1);
exec("insertstring", 1, '"');
assert.equal(editor.getValue(), 'p"');
}
};

Expand Down
25 changes: 22 additions & 3 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 @@ -260,8 +268,19 @@ var CstyleBehaviour = function(options) {
wordRe.lastIndex = 0;
var isWordBefore = wordRe.test(leftChar);
wordRe.lastIndex = 0;
var isWordAfter = wordRe.test(leftChar);
if (isWordBefore || isWordAfter)
var isWordAfter = wordRe.test(rightChar);
var hasStringPrefixes = false;
if (options && options.quotesPrefixes && Array.isArray(options.quotesPrefixes)) {
for (var prefix of options.quotesPrefixes) {
mkslanc marked this conversation as resolved.
Show resolved Hide resolved
if (prefix.quotes && prefix.condition && prefix.quotes instanceof RegExp
mkslanc marked this conversation as resolved.
Show resolved Hide resolved
&& 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))
return null; // there is rightChar and it isn't closing
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: [
{
mkslanc marked this conversation as resolved.
Show resolved Hide resolved
quotes: new RegExp("`"),
condition: new RegExp("\\w")
}
]
});
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, TextMode);
Expand Down
10 changes: 9 additions & 1 deletion src/mode/python.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var oop = require("../lib/oop");
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var TextMode = require("./text").Mode;
var PythonHighlightRules = require("./python_highlight_rules").PythonHighlightRules;
var PythonFoldMode = require("./folding/pythonic").FoldMode;
Expand All @@ -9,7 +10,14 @@ var Range = require("../range").Range;
var Mode = function() {
this.HighlightRules = PythonHighlightRules;
this.foldingRules = new PythonFoldMode("\\:");
this.$behaviour = this.$defaultBehaviour;
this.$behaviour = new CstyleBehaviour({
quotesPrefixes: [
{
quotes: new RegExp("['\"]"),
mkslanc marked this conversation as resolved.
Show resolved Hide resolved
condition: new RegExp("[ruf]", "i")
}
]
});
};
oop.inherits(Mode, TextMode);

Expand Down