@@ -33,6 +33,8 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
3333import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService' ;
3434import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService' ;
3535import { ITextModelResolverService } from 'vs/editor/common/services/resolverService' ;
36+ import { IState , ITokenizationSupport , TokenizationRegistry } from 'vs/editor/common/modes' ;
37+ import { NULL_STATE , nullTokenize } from 'vs/editor/common/modes/nullMode' ;
3638
3739/**
3840 * @internal
@@ -255,6 +257,53 @@ export function colorizeModelLine(model: IModel, lineNumber: number, tabSize: nu
255257 return Colorizer . colorizeModelLine ( model , lineNumber , tabSize ) ;
256258}
257259
260+ export class Token {
261+ public readonly offset : number ;
262+ public readonly type : string ;
263+
264+ constructor ( offset : number , type : string ) {
265+ this . offset = offset ;
266+ this . type = type ;
267+ }
268+ }
269+
270+ /**
271+ * @internal
272+ */
273+ function getSafeTokenizationSupport ( languageId : string ) : ITokenizationSupport {
274+ let tokenizationSupport = TokenizationRegistry . get ( languageId ) ;
275+ if ( tokenizationSupport ) {
276+ return tokenizationSupport ;
277+ }
278+ return {
279+ getInitialState : ( ) => NULL_STATE ,
280+ tokenize : ( line : string , state : IState , deltaOffset : number ) => nullTokenize ( languageId , line , state , deltaOffset ) ,
281+ tokenize3 : undefined ,
282+ } ;
283+ }
284+
285+ /**
286+ * Tokenize `text` using language `languageId`
287+ */
288+ export function tokenize ( text : string , languageId : string ) : Token [ ] [ ] {
289+ let modeService = StaticServices . modeService . get ( ) ;
290+ // Needed in order to get the mode registered for subsequent look-ups
291+ modeService . getOrCreateMode ( languageId ) ;
292+
293+ let tokenizationSupport = getSafeTokenizationSupport ( languageId ) ;
294+ let lines = text . split ( / \r \n | \r | \n / ) ;
295+ let result : Token [ ] [ ] = [ ] ;
296+ let state = tokenizationSupport . getInitialState ( ) ;
297+ for ( let i = 0 , len = lines . length ; i < len ; i ++ ) {
298+ let line = lines [ i ] ;
299+ let tokenizationResult = tokenizationSupport . tokenize ( line , state , 0 ) ;
300+
301+ result [ i ] = tokenizationResult . tokens . map ( ( t ) => new Token ( t . startIndex , t . type ) ) ;
302+ state = tokenizationResult . endState ;
303+ }
304+ return result ;
305+ }
306+
258307/**
259308 * @internal
260309 */
@@ -279,6 +328,7 @@ export function createMonacoEditorAPI(): typeof monaco.editor {
279328 colorizeElement : colorizeElement ,
280329 colorize : colorize ,
281330 colorizeModelLine : colorizeModelLine ,
331+ tokenize : tokenize ,
282332
283333 // enums
284334 ScrollbarVisibility : ScrollbarVisibility ,
@@ -306,6 +356,7 @@ export function createMonacoEditorAPI(): typeof monaco.editor {
306356 BareFontInfo : < any > editorCommon . BareFontInfo ,
307357 FontInfo : < any > editorCommon . FontInfo ,
308358 TextModelResolvedOptions : < any > editorCommon . TextModelResolvedOptions ,
359+ Token : Token ,
309360
310361 // vars
311362 EditorType : editorCommon . EditorType ,
0 commit comments