Skip to content

Commit

Permalink
add type definition for Mode; move $pairQuotesAfter from options to…
Browse files Browse the repository at this point in the history
… mode property
  • Loading branch information
mkslanc committed Feb 24, 2023
1 parent e9c2c77 commit 7fb85f1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
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
12 changes: 6 additions & 6 deletions src/mode/behaviour/cstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ 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.
* @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) {
Expand Down Expand Up @@ -267,10 +266,11 @@ var CstyleBehaviour = function(options) {
var isWordBefore = wordRe.test(leftChar);
wordRe.lastIndex = 0;
var isWordAfter = wordRe.test(rightChar);
var pairQuotesAfter = options && options.pairQuotesAfter && options.pairQuotesAfter[quotes[text]]
&& options.pairQuotesAfter[quotes[text]].test(leftChar);

var pairQuotesAfter = session.$mode.$pairQuotesAfter;
var shouldPairQuotes = pairQuotesAfter && pairQuotesAfter[quote] && pairQuotesAfter[quote].test(leftChar);

if ((!pairQuotesAfter && isWordBefore) || isWordAfter)
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
9 changes: 4 additions & 5 deletions src/mode/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ var Mode = function() {
this.HighlightRules = JavaScriptHighlightRules;

this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour({
pairQuotesAfter: {
"`": /\w/
}
});
this.$behaviour = new CstyleBehaviour();
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(Mode, TextMode);
Expand All @@ -26,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
12 changes: 5 additions & 7 deletions src/mode/python.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"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 @@ -10,18 +9,17 @@ var Range = require("../range").Range;
var Mode = function() {
this.HighlightRules = PythonHighlightRules;
this.foldingRules = new PythonFoldMode("\\:");
this.$behaviour = new CstyleBehaviour({
pairQuotesAfter: {
"'": /[ruf]/i,
'"': /[ruf]/i
}
});
this.$behaviour = this.$defaultBehaviour;
};
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

0 comments on commit 7fb85f1

Please sign in to comment.