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 all 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
11 changes: 8 additions & 3 deletions src/edit_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,19 @@ EditSession.$uid = 0;

this.$modes = config.$modes;

/**
*
* @type {TextMode|null}
*/
this.$mode = null;
this.$modeId = null;

/**
* Sets a new text mode for the `EditSession`. This method also emits the `'changeMode'` event. If a [[BackgroundTokenizer `BackgroundTokenizer`]] is set, the `'tokenizerUpdate'` event is also emitted.
* @param {TextMode} mode Set a new text mode
* @param {cb} optional callback
* @param {Function} cb optional callback
*
**/
this.$mode = null;
this.$modeId = null;
this.setMode = function(mode, cb) {
if (mode && typeof mode === "object") {
if (mode.getTokenizer)
Expand Down
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,12 @@ 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.
*/
var CstyleBehaviour = function(options) {
this.add("braces", "insertion", function(state, action, editor, session, text) {
var cursor = editor.getCursorPosition();
Expand Down Expand Up @@ -260,8 +265,12 @@ 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 = session.$mode.$pairQuotesAfter;
var shouldPairQuotes = pairQuotesAfter && pairQuotesAfter[quote] && pairQuotesAfter[quote].test(leftChar);

if ((!shouldPairQuotes && 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
5 changes: 4 additions & 1 deletion src/mode/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var CStyleFoldMode = require("./folding/cstyle").FoldMode;

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

this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.foldingRules = new CStyleFoldMode();
Expand All @@ -22,6 +22,9 @@ oop.inherits(Mode, TextMode);
this.lineCommentStart = "//";
this.blockComment = {start: "/*", end: "*/"};
this.$quotes = {'"': '"', "'": "'", "`": "`"};
this.$pairQuotesAfter = {
"`": /\w/
};

this.getNextLineIndent = function(state, line, tab) {
var indent = this.$getIndent(line);
Expand Down
4 changes: 4 additions & 0 deletions src/mode/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ oop.inherits(Mode, TextMode);
(function() {

this.lineCommentStart = "#";
this.$pairQuotesAfter = {
"'": /[ruf]/i,
'"': /[ruf]/i
};

this.getNextLineIndent = function(state, line, tab) {
var indent = this.$getIndent(line);
Expand Down
14 changes: 14 additions & 0 deletions src/mode/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ var lang = require("../lib/lang");
var TokenIterator = require("../token_iterator").TokenIterator;
var Range = require("../range").Range;

/**
*
* @constructor
* @alias TextMode
* @property {{[quote: string]: string}} [$quotes] - quotes used by language mode
* @property {string} lineCommentStart - characters that indicate the start of a line comment
* @property {{start: string, end: string}} [blockComment] - characters that indicate the start and end of a block comment
* @property {TextHighlightRules} HighlightRules - language specific highlighters
* @property {FoldMode} foldingRules - language specific folding rules
* @property {MatchingBraceOutdent} $outdent
* @property {RegExp} tokenRe
* @property {RegExp} nonTokenRe
* @property {{[quote: string]: RegExp}} [$pairQuotesAfter] - An object containing conditions to determine whether to apply matching quote or not.
*/
var Mode = function() {
this.HighlightRules = TextHighlightRules;
};
Expand Down