Skip to content

Commit ecb8951

Browse files
committed
Enable injections
1 parent d26d82e commit ecb8951

File tree

7 files changed

+47
-53
lines changed

7 files changed

+47
-53
lines changed

.vscode/launch.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
1818
"args": ["--runInBand"],
1919
"console": "integratedTerminal"
20+
},
21+
{
22+
"type": "node",
23+
"request": "launch",
24+
"name": "Launch Example Site",
25+
"cwd": "${workspaceFolder}/examples/example-site",
26+
"program": "${workspaceFolder}/examples/example-site/node_modules/.bin/gatsby",
27+
"args": ["develop"],
28+
"console": "integratedTerminal"
2029
}
2130
]
2231
}

examples/example-site/content/blog/hello-world/index.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ date: "2015-05-01T22:12:03.284Z"
44
description: "Hello World"
55
---
66

7-
8-
9-
10-
11-
12-
13-
14-
157
```jsx
168
const Wrapper = styled.section`
179
border-radius: 4px;
@@ -27,4 +19,13 @@ const Wrapper = styled.section`
2719
export default function MyComponent({ children }) = (
2820
<Wrapper>{children}</Wrapper>
2921
)
22+
```
23+
24+
```js
25+
/**
26+
* @param {string} x
27+
*/
28+
function foo(x) {
29+
30+
}
3031
```

src/createGetRegistry.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
const fs = require('fs');
3-
const { getGrammarLocation, getGrammar } = require('./storeUtils');
3+
const { getGrammarLocation, getGrammar, getAllGrammars } = require('./storeUtils');
44
const { promisify } = require('util');
55
const { Registry, parseRawGrammar } = require('vscode-textmate');
66
const readFile = promisify(fs.readFile);
@@ -49,15 +49,25 @@ function createGetRegistry() {
4949
*/
5050
async function getRegistry(cache, onMissingLanguageFile) {
5151
if (!registry) {
52+
const grammars = getAllGrammars(await cache.get('grammars'));
5253
registry = new Registry({
5354
loadGrammar: async scopeName => {
54-
const grammarInfo = getGrammar(scopeName, await cache.get('grammars'));
55+
const grammarInfo = getGrammar(scopeName, grammars);
5556
const fileName = grammarInfo && getGrammarLocation(grammarInfo);
5657
if (fileName) {
5758
const contents = await readFile(fileName, 'utf8');
5859
return parseRawGrammar(contents, fileName);
5960
}
6061
onMissingLanguageFile(scopeName);
62+
},
63+
getInjections: scopeName => {
64+
return Object.keys(grammars).reduce((acc, s) => {
65+
const grammar = grammars[s];
66+
if (grammar.injectTo && grammar.injectTo.includes(scopeName)) {
67+
acc.push(s);
68+
}
69+
return acc;
70+
}, []);
6171
}
6272
});
6373
}

src/downloadExtension.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@ const util = require('util');
66
const request = require('request');
77
const decompress = require('decompress');
88
const processExtension = require('./processExtension');
9-
const {
10-
getScope,
11-
getGrammar,
12-
getGrammarLocation,
13-
getThemeLocation,
14-
highestBuiltinLanguageId
15-
} = require('./storeUtils');
9+
const { highestBuiltinLanguageId } = require('./storeUtils');
1610
const {
1711
parseExtensionIdentifier,
1812
getExtensionPath,
1913
getExtensionBasePath,
2014
getExtensionPackageJson
2115
} = require('./utils');
22-
const exists = util.promisify(fs.exists);
2316
const gunzip = util.promisify(zlib.gunzip);
2417
let languageId = highestBuiltinLanguageId + 1;
2518

@@ -83,21 +76,17 @@ async function downloadExtension(extensionDemand, cache, extensionDir) {
8376

8477
/**
8578
* @typedef {object} DownloadExtensionOptions
86-
* @property {'grammar' | 'theme'} type
87-
* @property {string} name
8879
* @property {import('.').ExtensionDemand[]} extensions
8980
* @property {*} cache
90-
* @property {Record<string, string>} languageAliases
9181
* @property {string} extensionDir
9282
*/
9383

9484
/**
9585
* @param {DownloadExtensionOptions} options
9686
*/
97-
async function downloadExtensionIfNeeded({ type, name, extensions, cache, languageAliases, extensionDir }) {
87+
async function downloadExtensionsIfNeeded({ extensions, cache, extensionDir }) {
9888
extensions = extensions.slice();
99-
const extensionExists = type === 'grammar' ? grammarExists : themeExists;
100-
while (extensions.length && !(await extensionExists(name))) {
89+
while (extensions.length) {
10190
const extensionDemand = extensions.shift();
10291
const { identifier, version } = extensionDemand;
10392
const extensionPath = getExtensionBasePath(identifier, extensionDir);
@@ -113,19 +102,6 @@ async function downloadExtensionIfNeeded({ type, name, extensions, cache, langua
113102

114103
await syncExtensionData(extensionDemand, cache, extensionDir);
115104
}
116-
117-
/** @param {string} languageName */
118-
async function grammarExists(languageName) {
119-
const grammarCache = await cache.get('grammars');
120-
const grammar = getGrammar(getScope(languageName, grammarCache, languageAliases), grammarCache);
121-
return grammar && getGrammarLocation(grammar) && exists(getGrammarLocation(grammar));
122-
}
123-
124-
/** @param {string} themeName */
125-
async function themeExists(themeName) {
126-
const location = getThemeLocation(themeName, await cache.get('themes'));
127-
return location && exists(location);
128-
}
129105
}
130106

131-
module.exports = { downloadExtension, downloadExtensionIfNeeded, syncExtensionData };
107+
module.exports = { downloadExtension, downloadExtensionIfNeeded: downloadExtensionsIfNeeded, syncExtensionData };

src/index.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const parseCodeFenceHeader = require('./parseCodeFenceHeader');
99
const { sanitizeForClassName } = require('./utils');
1010
const { loadColorTheme } = require('../lib/vscode/colorThemeData');
1111
const { getClassNameFromMetadata } = require('../lib/vscode/modes');
12-
const { downloadExtensionIfNeeded } = require('./downloadExtension');
12+
const { downloadExtensionIfNeeded: downloadExtensionsIfNeeded } = require('./downloadExtension');
1313
const { getGrammar, getScope, getThemeLocation } = require('./storeUtils');
1414
const { generateTokensCSSForColorMap } = require('../lib/vscode/tokenization');
1515
const { renderRule, prefersDark, prefersLight, prefixRules, joinClassNames } = require('./cssUtils');
@@ -155,12 +155,9 @@ function createPlugin() {
155155
const text = node.value || (node.children && node.children[0] && node.children[0].value);
156156
if (!text) continue;
157157
const { languageName, options } = parseCodeFenceHeader(node.lang ? node.lang.toLowerCase() : '');
158-
await downloadExtensionIfNeeded({
159-
type: 'grammar',
160-
name: languageName,
158+
await downloadExtensionsIfNeeded({
161159
extensions,
162160
cache,
163-
languageAliases,
164161
extensionDir: extensionDataDirectory
165162
});
166163

@@ -190,14 +187,6 @@ function createPlugin() {
190187
for (const setting in colorThemeSettings) {
191188
const colorThemeIdentifier = colorThemeSettings[setting];
192189
if (!colorThemeIdentifier) continue;
193-
await downloadExtensionIfNeeded({
194-
type: 'theme',
195-
name: colorThemeIdentifier,
196-
extensions,
197-
cache,
198-
languageAliases,
199-
extensionDir: extensionDataDirectory
200-
});
201190

202191
const themeClassName = themeClassNames[setting];
203192
const themeCache = await cache.get('themes');

src/processExtension.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async function processExtension(packageJsonPath) {
1919
path: sourcePath,
2020
tokenTypes: grammar.tokenTypes,
2121
embeddedLanguages: grammar.embeddedLanguages,
22+
injectTo: grammar.injectTo,
2223
languageNames: languageRegistration ? getLanguageNames(languageRegistration) : []
2324
};
2425
})
@@ -31,6 +32,7 @@ async function processExtension(packageJsonPath) {
3132
path: grammar.path,
3233
tokenTypes: grammar.tokenTypes,
3334
embeddedLanguages: grammar.embeddedLanguages,
35+
injectTo: grammar.injectTo,
3436
languageNames: grammar.languageNames
3537
}
3638
}),

src/storeUtils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ const highestBuiltinLanguageId = Object.keys(grammarManifest).reduce(
6161
* @param {*} grammarCache
6262
*/
6363
function getGrammar(scopeName, grammarCache) {
64-
return { ...grammarManifest, ...grammarCache }[scopeName];
64+
return getAllGrammars(grammarCache)[scopeName];
6565
}
6666

67-
module.exports = { getScope, getGrammar, getGrammarLocation, getThemeLocation, highestBuiltinLanguageId };
67+
/**
68+
* @param {*} grammarCache
69+
*/
70+
function getAllGrammars(grammarCache) {
71+
return { ...grammarManifest, ...grammarCache };
72+
}
73+
74+
module.exports = { getScope, getGrammar, getGrammarLocation, getThemeLocation, highestBuiltinLanguageId, getAllGrammars };

0 commit comments

Comments
 (0)