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+ }
0 commit comments