Skip to content

Commit dafaade

Browse files
committed
fix(monaco-editor-textmate): to be used in standalone
1 parent 803fab3 commit dafaade

File tree

4 files changed

+135
-116
lines changed

4 files changed

+135
-116
lines changed
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "monaco-editor-textmate",
3-
"version": "2.2.2",
3+
"version": "0.0.0",
44
"description": "Wire monaco-textmate with monaco-editor",
55
"main": "dist/index.js",
66
"typings": "dist/typings/index.d.ts",
@@ -9,10 +9,6 @@
99
"watch": "tsc -w",
1010
"prepack": "npm run build"
1111
},
12-
"repository": {
13-
"type": "git",
14-
"url": "git+https://github.com/NeekSandhu/monaco-editor-textmate.git"
15-
},
1612
"keywords": [
1713
"monaco",
1814
"editor",
@@ -21,17 +17,10 @@
2117
"grammar",
2218
"vscode"
2319
],
24-
"author": "Neek Sandhu <neek.sandhu@outlook.com>",
2520
"license": "MIT",
26-
"bugs": {
27-
"url": "https://github.com/NeekSandhu/monaco-editor-textmate/issues"
28-
},
29-
"homepage": "https://github.com/NeekSandhu/monaco-editor-textmate#readme",
3021
"devDependencies": {
22+
"monaco-editor": "^0.23.0",
23+
"monaco-textmate": "^3.0.1",
3124
"typescript": "^2.8.3"
32-
},
33-
"peerDependencies": {
34-
"monaco-editor": "0.x.x",
35-
"monaco-textmate": "^3.0.0"
3625
}
3726
}
Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,69 @@
1-
import { Registry, StackElement, INITIAL } from 'monaco-textmate'
2-
import * as monacoNsps from 'monaco-editor'
3-
import { TMToMonacoToken } from './tm-to-monaco-token';
4-
5-
class TokenizerState implements monacoNsps.languages.IState {
6-
7-
constructor(
8-
private _ruleStack: StackElement
9-
) { }
10-
11-
public get ruleStack(): StackElement {
12-
return this._ruleStack
13-
}
14-
15-
public clone(): TokenizerState {
16-
return new TokenizerState(this._ruleStack);
17-
}
18-
19-
public equals(other: monacoNsps.languages.IState): boolean {
20-
if (!other ||
21-
!(other instanceof TokenizerState) ||
22-
other !== this ||
23-
other._ruleStack !== this._ruleStack
24-
) {
25-
return false;
26-
}
27-
return true;
28-
}
29-
}
30-
311
/**
32-
* Wires up monaco-editor with monaco-textmate
33-
*
34-
* @param monaco monaco namespace this operation should apply to (usually the `monaco` global unless you have some other setup)
35-
* @param registry TmGrammar `Registry` this wiring should rely on to provide the grammars
36-
* @param languages `Map` of language ids (string) to TM names (string)
2+
* monaco-editor-textmate@2.2.2
3+
* MIT License
4+
* https://github.com/NeekSandhu/monaco-editor-textmate/blob/master/LICENSE
375
*/
38-
export function wireTmGrammars(monaco: typeof monacoNsps, registry: Registry, languages: Map<string, string>, editor?: monacoNsps.editor.ICodeEditor) {
39-
return Promise.all(
40-
Array.from(languages.keys())
41-
.map(async (languageId) => {
42-
const grammar = await registry.loadGrammar(languages.get(languageId))
43-
monaco.languages.setTokensProvider(languageId, {
44-
getInitialState: () => new TokenizerState(INITIAL),
45-
tokenize: (line: string, state: TokenizerState) => {
46-
const res = grammar.tokenizeLine(line, state.ruleStack)
47-
return {
48-
endState: new TokenizerState(res.ruleStack),
49-
tokens: res.tokens.map(token => ({
50-
...token,
51-
// TODO: At the moment, monaco-editor doesn't seem to accept array of scopes
52-
scopes: editor ? TMToMonacoToken(editor, token.scopes) : token.scopes[token.scopes.length - 1]
53-
})),
54-
}
55-
}
56-
})
57-
})
58-
)
59-
}
6+
7+
import { Registry, StackElement, INITIAL } from 'monaco-textmate';
8+
import type * as monacoNsps from 'monaco-editor';
9+
import { TMToMonacoToken } from './tm-to-monaco-token';
10+
11+
class TokenizerState implements monacoNsps.languages.IState {
12+
constructor(private _ruleStack: StackElement) {}
13+
14+
public get ruleStack(): StackElement {
15+
return this._ruleStack;
16+
}
17+
18+
public clone(): TokenizerState {
19+
return new TokenizerState(this._ruleStack);
20+
}
21+
22+
public equals(other: monacoNsps.languages.IState): boolean {
23+
if (
24+
!other ||
25+
!(other instanceof TokenizerState) ||
26+
other !== this ||
27+
other._ruleStack !== this._ruleStack
28+
) {
29+
return false;
30+
}
31+
return true;
32+
}
33+
}
34+
35+
/**
36+
* Wires up monaco-editor with monaco-textmate
37+
*
38+
* @param monaco monaco namespace this operation should apply to (usually the `monaco` global unless you have some other setup)
39+
* @param registry TmGrammar `Registry` this wiring should rely on to provide the grammars
40+
* @param languages `Map` of language ids (string) to TM names (string)
41+
*/
42+
export function wireTmGrammars(
43+
monaco: typeof monacoNsps,
44+
registry: Registry,
45+
languages: Map<string, string>,
46+
editor?: monacoNsps.editor.ICodeEditor
47+
): Promise<void[]> {
48+
return Promise.all(
49+
Array.from(languages.keys()).map(async (languageId) => {
50+
const grammar = await registry.loadGrammar(languages.get(languageId));
51+
monaco.languages.setTokensProvider(languageId, {
52+
getInitialState: () => new TokenizerState(INITIAL),
53+
tokenize: (line: string, state: TokenizerState) => {
54+
const res = grammar.tokenizeLine(line, state.ruleStack);
55+
return {
56+
endState: new TokenizerState(res.ruleStack),
57+
tokens: res.tokens.map((token) => ({
58+
...token,
59+
// TODO: At the moment, monaco-editor doesn't seem to accept array of scopes
60+
scopes: editor
61+
? TMToMonacoToken(editor, token.scopes)
62+
: token.scopes[token.scopes.length - 1],
63+
})),
64+
};
65+
},
66+
});
67+
})
68+
);
69+
}
Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,56 @@
1-
import * as monacoNsps from 'monaco-editor'
1+
import type * as monacoNsps from 'monaco-editor';
22

33
// as described in issue: https://github.com/NeekSandhu/monaco-textmate/issues/5
4-
export const TMToMonacoToken = (editor: monacoNsps.editor.ICodeEditor, scopes: string[]) => {
5-
let scopeName = "";
6-
// get the scope name. Example: cpp , java, haskell
7-
for (let i = scopes[0].length - 1; i >= 0; i -= 1) {
8-
const char = scopes[0][i];
9-
if (char === ".") {
10-
break;
11-
}
12-
scopeName = char + scopeName;
4+
export const TMToMonacoToken = (
5+
editor: monacoNsps.editor.ICodeEditor,
6+
scopes: string[]
7+
): string => {
8+
let scopeName = '';
9+
// get the scope name. Example: cpp , java, haskell
10+
for (let i = scopes[0].length - 1; i >= 0; i -= 1) {
11+
const char = scopes[0][i];
12+
if (char === '.') {
13+
break;
1314
}
15+
scopeName = char + scopeName;
16+
}
1417

15-
// iterate through all scopes from last to first
16-
for (let i = scopes.length - 1; i >= 0; i -= 1) {
17-
const scope = scopes[i];
18+
// iterate through all scopes from last to first
19+
for (let i = scopes.length - 1; i >= 0; i -= 1) {
20+
const scope = scopes[i];
1821

19-
/**
20-
* Try all possible tokens from high specific token to low specific token
21-
*
22-
* Example:
23-
* 0 meta.function.definition.parameters.cpp
24-
* 1 meta.function.definition.parameters
25-
*
26-
* 2 meta.function.definition.cpp
27-
* 3 meta.function.definition
28-
*
29-
* 4 meta.function.cpp
30-
* 5 meta.function
31-
*
32-
* 6 meta.cpp
33-
* 7 meta
34-
*/
35-
for (let i = scope.length - 1; i >= 0; i -= 1) {
36-
const char = scope[i];
37-
if (char === ".") {
38-
const token = scope.slice(0, i);
39-
if (
40-
editor['_themeService'].getTheme()._tokenTheme._match(token + "." + scopeName)._foreground >
41-
1
42-
) {
43-
return token + "." + scopeName;
44-
}
45-
if (editor['_themeService'].getTheme()._tokenTheme._match(token)._foreground > 1) {
46-
return token;
47-
}
48-
}
22+
/**
23+
* Try all possible tokens from high specific token to low specific token
24+
*
25+
* Example:
26+
* 0 meta.function.definition.parameters.cpp
27+
* 1 meta.function.definition.parameters
28+
*
29+
* 2 meta.function.definition.cpp
30+
* 3 meta.function.definition
31+
*
32+
* 4 meta.function.cpp
33+
* 5 meta.function
34+
*
35+
* 6 meta.cpp
36+
* 7 meta
37+
*/
38+
for (let i = scope.length - 1; i >= 0; i -= 1) {
39+
const char = scope[i];
40+
if (char === '.') {
41+
const token = scope.slice(0, i);
42+
const theme =
43+
editor['_standaloneThemeService']._theme ||
44+
editor['_themeService'].getTheme();
45+
if (theme._tokenTheme._match(token + '.' + scopeName)._foreground > 1) {
46+
return token + '.' + scopeName;
47+
}
48+
if (theme._tokenTheme._match(token)._foreground > 1) {
49+
return token;
4950
}
51+
}
5052
}
53+
}
5154

52-
return "";
53-
};
55+
return '';
56+
};

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,11 @@ fast-json-stable-stringify@^2.0.0:
24572457
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
24582458
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
24592459

2460+
fast-plist@^0.1.2:
2461+
version "0.1.2"
2462+
resolved "https://registry.yarnpkg.com/fast-plist/-/fast-plist-0.1.2.tgz#a45aff345196006d406ca6cdcd05f69051ef35b8"
2463+
integrity sha1-pFr/NFGWAG1AbKbNzQX2kFHvNbg=
2464+
24602465
fast-url-parser@1.1.3:
24612466
version "1.1.3"
24622467
resolved "https://registry.yarnpkg.com/fast-url-parser/-/fast-url-parser-1.1.3.tgz#f4af3ea9f34d8a271cf58ad2b3759f431f0b318d"
@@ -4066,6 +4071,18 @@ modify-values@^1.0.0:
40664071
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
40674072
integrity sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==
40684073

4074+
monaco-editor@^0.23.0:
4075+
version "0.23.0"
4076+
resolved "https://registry.yarnpkg.com/monaco-editor/-/monaco-editor-0.23.0.tgz#24844ba5640c7adb3a2a3ff3b520cf2d7170a6f0"
4077+
integrity sha512-q+CP5zMR/aFiMTE9QlIavGyGicKnG2v/H8qVvybLzeFsARM8f6G9fL0sMST2tyVYCwDKkGamZUI6647A0jR/Lg==
4078+
4079+
monaco-textmate@^3.0.1:
4080+
version "3.0.1"
4081+
resolved "https://registry.yarnpkg.com/monaco-textmate/-/monaco-textmate-3.0.1.tgz#b6d26d266aa12edaff7069dae0d6e3747cba5cd7"
4082+
integrity sha512-ZxxY3OsqUczYP1sGqo97tu+CJmMBwuSW+dL0WEBdDhOZ5G1zntw72hvBc68ZQAirosWvbDKgN1dL5k173QtFww==
4083+
dependencies:
4084+
fast-plist "^0.1.2"
4085+
40694086
ms@2.0.0:
40704087
version "2.0.0"
40714088
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

0 commit comments

Comments
 (0)