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 3 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
15 changes: 12 additions & 3 deletions src/mode/behaviour/cstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ 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 {{[quote: string]: RegExp}} options.pairQuotesAfter - An object containing conditions to determine whether to apply matching quote or not.
*/
var CstyleBehaviour = function(options) {
this.add("braces", "insertion", function(state, action, editor, session, text) {
var cursor = editor.getCursorPosition();
Expand Down Expand Up @@ -260,8 +266,11 @@ 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 pairQuotesAfter = options && options.pairQuotesAfter && options.pairQuotesAfter[quotes[text]]
&& options.pairQuotesAfter[quotes[text]].test(leftChar);
mkslanc marked this conversation as resolved.
Show resolved Hide resolved

if ((!pairQuotesAfter && 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
8 changes: 6 additions & 2 deletions src/mode/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ var CStyleFoldMode = require("./folding/cstyle").FoldMode;

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

this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.$behaviour = new CstyleBehaviour({
pairQuotesAfter: {
"`": /\w/
}
});
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, TextMode);
Expand Down
8 changes: 7 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,12 @@ var Range = require("../range").Range;
var Mode = function() {
this.HighlightRules = PythonHighlightRules;
this.foldingRules = new PythonFoldMode("\\:");
this.$behaviour = this.$defaultBehaviour;
this.$behaviour = new CstyleBehaviour({
pairQuotesAfter: {
"'": /[ruf]/i,
'"': /[ruf]/i
}
});
};
oop.inherits(Mode, TextMode);

Expand Down