|
| 1 | +const { CompositeDisposable } = require("atom"); |
| 2 | + |
| 3 | + |
| 4 | +let package, |
| 5 | + disposableStyleSheets = {}; |
| 6 | + |
| 7 | +const thisPackage = { |
| 8 | + |
| 9 | + name: "javascript-regex", |
| 10 | + |
| 11 | + // Scope names of languages for which relevant injection points will be overridden. |
| 12 | + languageScopeNames: [ |
| 13 | + "source.js", |
| 14 | + "source.jsx", |
| 15 | + "source.ts", |
| 16 | + "source.flow", |
| 17 | + ], |
| 18 | + |
| 19 | + // Rule names in the language grammar for which injection points will be overridden. |
| 20 | + languageGrammarRuleNames: [ |
| 21 | + "regex", |
| 22 | + "regex_pattern", |
| 23 | + ], |
| 24 | + |
| 25 | + |
| 26 | + injectionPoints: [ |
| 27 | + { |
| 28 | + // Injection type: the name of the rule in the language grammar that this injection point is to be added to. |
| 29 | + type: "regex", |
| 30 | + |
| 31 | + // Get the name of the grammar that should be injected into a matching node. This should match the grammar's "injectionRegex" property. |
| 32 | + language(regexNode){ |
| 33 | + const {lastChild} = regexNode; |
| 34 | + if(lastChild.type === "regex_flags"){ // Flags are specified. |
| 35 | + const flags = lastChild.text; |
| 36 | + let validFlags = "gimsy".split(""), |
| 37 | + f = -1; |
| 38 | + while(validFlags.length && ++f < flags.length){ |
| 39 | + const v = validFlags.indexOf(flags[f]); |
| 40 | + if(flags[f] === "u"){ |
| 41 | + return "regex_u"; // Unicode flag is set. |
| 42 | + } |
| 43 | + else if(v < 0){ |
| 44 | + break; // Invalid or duplicate flag. |
| 45 | + } |
| 46 | + validFlags.splice(v, 1); |
| 47 | + } |
| 48 | + } |
| 49 | + return "regex"; // Unicode flag is not set. |
| 50 | + }, |
| 51 | + |
| 52 | + // Get the node with the text content that should be parsed using the injected grammar. |
| 53 | + content(regexNode){ |
| 54 | + // Return the node containing the regex pattern between the slashes. |
| 55 | + return regexNode.children[1]; |
| 56 | + } |
| 57 | + }, |
| 58 | + ], |
| 59 | +}; |
| 60 | + |
| 61 | +function toggleConfig(configName){ |
| 62 | + atom.config.set(configName, !atom.config.get(`${thisPackage.name}.${configName}`)); |
| 63 | +}; |
| 64 | + |
| 65 | +function updateStyleSheet(configName, filename){ |
| 66 | + console.log(disposableStyleSheets); |
| 67 | + disposableStyleSheets[configName]?.dispose(); |
| 68 | + if(atom.config.get(`${thisPackage.name}.${configName}`)){ |
| 69 | + let path = `${package.path}/styles/${filename}.less`, |
| 70 | + css = package.themeManager.loadStylesheet(path); |
| 71 | + disposableStyleSheets[configName] = package.styleManager.addStyleSheet(css, { sourcePath: path, priority: 1 }); |
| 72 | + } |
| 73 | + else{ |
| 74 | + disposableStyleSheets[configName] = void 0; |
| 75 | + atom.config.set(`${thisPackage.name}.${configName}`, false); |
| 76 | + } |
| 77 | + console.log(">>",disposableStyleSheets); |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = { |
| 81 | + |
| 82 | + activate(state){ |
| 83 | + |
| 84 | + package = atom.packages.loadedPackages[thisPackage.name]; |
| 85 | + |
| 86 | + // Add or replace grammars that are associated with this package. |
| 87 | + // (i.e., grammars with scope names "source.js.regexp" and "source.js.regexp.unicode") |
| 88 | + const packageGrammars = package.grammars; |
| 89 | + for(const newGrammar of packageGrammars){ |
| 90 | + |
| 91 | + const existingGrammar = atom.grammars.treeSitterGrammarsById[newGrammar.scopeName]; |
| 92 | + if(existingGrammar){ |
| 93 | + // A grammar is currently assigned to the new grammar's scope name. |
| 94 | + |
| 95 | + if(existingGrammar.packageName !== thisPackage.name){ |
| 96 | + // The existing grammar is not the one associated with this package. |
| 97 | + |
| 98 | + // Replace the existing grammar with the new one. |
| 99 | + atom.grammars.removeGrammar(existingGrammar); |
| 100 | + atom.grammars.addGrammar(newGrammar); |
| 101 | + } |
| 102 | + } |
| 103 | + else{ |
| 104 | + // Add the new grammar. |
| 105 | + atom.grammars.addGrammar(newGrammar); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // For each associated language scope... |
| 110 | + // (i.e., scope names "source.js", "source.jsx", "source.ts", and "source.flow") |
| 111 | + for(const languageScopeName of thisPackage.languageScopeNames){ |
| 112 | + |
| 113 | + const languageGrammar = atom.grammars.treeSitterGrammarsById[languageScopeName]; |
| 114 | + if(!languageGrammar) continue; // A grammar does not exist for this language scope. |
| 115 | + |
| 116 | + // Add injection points for associated rules in this language's grammar. |
| 117 | + for(const grammarRuleName of thisPackage.languageGrammarRuleNames){ |
| 118 | + |
| 119 | + /* |
| 120 | + // Remove any existing injection points of this type. |
| 121 | + // (i.e., types matching grammar rule names "regex" and "regex_pattern") |
| 122 | + const existingInjectionPoints = languageGrammar.injectionPointsByType[grammarRuleName]; |
| 123 | + for(const injectionPoint of existingInjectionPoints){ |
| 124 | + languageGrammar.removeInjectionPoint(injectionPoint); |
| 125 | + } |
| 126 | + */ |
| 127 | + |
| 128 | + // Add new injection points of this type. |
| 129 | + // (i.e., types matching grammar rule names "regex" and "regex_pattern") |
| 130 | + for(newInjectionPoint of thisPackage.injectionPoints){ |
| 131 | + if(newInjectionPoint.type === grammarRuleName){ |
| 132 | + languageGrammar.addInjectionPoint(newInjectionPoint); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + // Create an instance of the CompositeDisposable class so it can register all the commands that can be called from the plugin, |
| 140 | + // allowing other plugins to subscribe to those events. |
| 141 | + this.subscriptions = new CompositeDisposable(); |
| 142 | + |
| 143 | + // Register the commands. |
| 144 | + this.subscriptions.add(atom.commands.add('atom-workspace', { |
| 145 | + "javascript-regex:toggle-character-set-backgrounds": ()=>toggleConfig("showCharacterSetBackgrounds"), |
| 146 | + "javascript-regex:toggle-group-outlines": ()=>toggleConfig("showGroupOutlines"), |
| 147 | + })); |
| 148 | + |
| 149 | + // Load the main syntax highlighting stylesheet. |
| 150 | + let path = `${package.path}/styles/javascript-regex.less`, |
| 151 | + css = package.themeManager.loadStylesheet(path); |
| 152 | + disposableStyleSheets["main"] = package.styleManager.addStyleSheet(css, { sourcePath: path, priority: 1 }); |
| 153 | + |
| 154 | + // Load stylesheets per the package settings. |
| 155 | + updateStyleSheet("showCharacterSetBackgrounds", "charset-backgrounds"); |
| 156 | + updateStyleSheet("showGroupOutlines", "group-outlines"); |
| 157 | + |
| 158 | + // Observe changes to the package settings. |
| 159 | + atom.config.onDidChange(`${thisPackage.name}.showCharacterSetBackgrounds`, {}, ()=>updateStyleSheet("showCharacterSetBackgrounds", "charset-backgrounds")); |
| 160 | + atom.config.onDidChange(`${thisPackage.name}.showGroupOutlines`, {}, ()=>updateStyleSheet("showGroupOutlines", "group-outlines")); |
| 161 | + }, |
| 162 | + |
| 163 | + serialize(){}, |
| 164 | + |
| 165 | + deactivate(){ |
| 166 | + disposableStyleSheets["showCharacterSetBackgrounds"]?.dispose(); |
| 167 | + disposableStyleSheets["showGroupOutlines"]?.dispose(); |
| 168 | + disposableStyleSheets["main"].dispose(); |
| 169 | + this.subscriptions.dispose(); |
| 170 | + }, |
| 171 | + |
| 172 | + config: { |
| 173 | + showCharacterSetBackgrounds: { |
| 174 | + title: "Display character set backgrounds", |
| 175 | + description: "Styles character sets (the contents of a pair of square brackets) with a background to make them easier to interpret as a single character.", |
| 176 | + type: "boolean", |
| 177 | + default: true, |
| 178 | + }, |
| 179 | + showGroupOutlines: { |
| 180 | + title: "Display group outlines", |
| 181 | + description: "Styles groups (the contents of a pair of parentheses) with outlines to make them easier to distinguish.", |
| 182 | + type: "boolean", |
| 183 | + default: false, |
| 184 | + }, |
| 185 | + }, |
| 186 | + |
| 187 | +}; |
0 commit comments