@@ -14,7 +14,7 @@ import { Range } from 'vs/editor/common/core/range';
1414import { DefaultEndOfLine , EndOfLinePreference , EndOfLineSequence , IIdentifiedSingleEditOperation , ITextBuffer , ITextBufferFactory , ITextModel , ITextModelCreationOptions } from 'vs/editor/common/model' ;
1515import { TextModel , createTextBuffer } from 'vs/editor/common/model/textModel' ;
1616import { IModelLanguageChangedEvent , IModelContentChangedEvent } from 'vs/editor/common/model/textModelEvents' ;
17- import { LanguageIdentifier , SemanticColoringProviderRegistry , SemanticColoringProvider , SemanticColoring , FontStyle , MetadataConsts } from 'vs/editor/common/modes' ;
17+ import { LanguageIdentifier , SemanticColoringProviderRegistry , SemanticColoringProvider , SemanticColoring , SemanticColoringLegend } from 'vs/editor/common/modes' ;
1818import { PLAINTEXT_LANGUAGE_IDENTIFIER } from 'vs/editor/common/modes/modesRegistry' ;
1919import { ILanguageSelection } from 'vs/editor/common/services/modeService' ;
2020import { IModelService } from 'vs/editor/common/services/modelService' ;
@@ -23,6 +23,7 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
2323import { RunOnceScheduler } from 'vs/base/common/async' ;
2424import { CancellationTokenSource } from 'vs/base/common/cancellation' ;
2525import { SparseEncodedTokens , MultilineTokens2 } from 'vs/editor/common/model/tokensStore' ;
26+ import { IThemeService } from 'vs/platform/theme/common/themeService' ;
2627
2728function MODEL_ID ( resource : URI ) : string {
2829 return resource . toString ( ) ;
@@ -118,7 +119,8 @@ export class ModelServiceImpl extends Disposable implements IModelService {
118119
119120 constructor (
120121 @IConfigurationService configurationService : IConfigurationService ,
121- @ITextResourcePropertiesService resourcePropertiesService : ITextResourcePropertiesService
122+ @ITextResourcePropertiesService resourcePropertiesService : ITextResourcePropertiesService ,
123+ @IThemeService themeService : IThemeService
122124 ) {
123125 super ( ) ;
124126 this . _configurationService = configurationService ;
@@ -129,7 +131,7 @@ export class ModelServiceImpl extends Disposable implements IModelService {
129131 this . _configurationServiceSubscription = this . _configurationService . onDidChangeConfiguration ( e => this . _updateModelOptions ( ) ) ;
130132 this . _updateModelOptions ( ) ;
131133
132- this . _register ( new SemanticColoringFeature ( this ) ) ;
134+ this . _register ( new SemanticColoringFeature ( this , themeService ) ) ;
133135 }
134136
135137 private static _readModelOptions ( config : IRawConfig , isForSimpleWidget : boolean ) : ITextModelCreationOptions {
@@ -440,11 +442,11 @@ export interface ILineSequence {
440442class SemanticColoringFeature extends Disposable {
441443 private _watchers : Record < string , ModelSemanticColoring > ;
442444
443- constructor ( modelService : IModelService ) {
445+ constructor ( modelService : IModelService , themeService : IThemeService ) {
444446 super ( ) ;
445447 this . _watchers = Object . create ( null ) ;
446448 this . _register ( modelService . onModelAdded ( ( model ) => {
447- this . _watchers [ model . uri . toString ( ) ] = new ModelSemanticColoring ( model ) ;
449+ this . _watchers [ model . uri . toString ( ) ] = new ModelSemanticColoring ( model , themeService ) ;
448450 } ) ) ;
449451 this . _register ( modelService . onModelRemoved ( ( model ) => {
450452 this . _watchers [ model . uri . toString ( ) ] . dispose ( ) ;
@@ -460,15 +462,17 @@ class ModelSemanticColoring extends Disposable {
460462 private readonly _fetchSemanticTokens : RunOnceScheduler ;
461463 private _currentResponse : SemanticColoring | null ;
462464 private _currentRequestCancellationTokenSource : CancellationTokenSource | null ;
465+ private _themeService : IThemeService ;
463466
464- constructor ( model : ITextModel ) {
467+ constructor ( model : ITextModel , themeService : IThemeService ) {
465468 super ( ) ;
466469
467470 this . _isDisposed = false ;
468471 this . _model = model ;
469472 this . _fetchSemanticTokens = this . _register ( new RunOnceScheduler ( ( ) => this . _fetchSemanticTokensNow ( ) , 500 ) ) ;
470473 this . _currentResponse = null ;
471474 this . _currentRequestCancellationTokenSource = null ;
475+ this . _themeService = themeService ;
472476
473477 this . _register ( this . _model . onDidChangeContent ( e => this . _fetchSemanticTokens . schedule ( ) ) ) ;
474478 this . _register ( SemanticColoringProviderRegistry . onDidChange ( e => this . _fetchSemanticTokens . schedule ( ) ) ) ;
@@ -509,16 +513,16 @@ class ModelSemanticColoring extends Disposable {
509513 request . then ( ( res ) => {
510514 this . _currentRequestCancellationTokenSource = null ;
511515 contentChangeListener . dispose ( ) ;
512- this . _setSemanticTokens ( res || null , pendingChanges ) ;
516+ this . _setSemanticTokens ( res || null , provider . getLegend ( ) , pendingChanges ) ;
513517 } , ( err ) => {
514518 errors . onUnexpectedError ( err ) ;
515519 this . _currentRequestCancellationTokenSource = null ;
516520 contentChangeListener . dispose ( ) ;
517- this . _setSemanticTokens ( null , pendingChanges ) ;
521+ this . _setSemanticTokens ( null , provider . getLegend ( ) , pendingChanges ) ;
518522 } ) ;
519523 }
520524
521- private _setSemanticTokens ( tokens : SemanticColoring | null , pendingChanges : IModelContentChangedEvent [ ] ) : void {
525+ private _setSemanticTokens ( tokens : SemanticColoring | null , legend : SemanticColoringLegend , pendingChanges : IModelContentChangedEvent [ ] ) : void {
522526 if ( this . _currentResponse ) {
523527 this . _currentResponse . dispose ( ) ;
524528 this . _currentResponse = null ;
@@ -540,30 +544,37 @@ class ModelSemanticColoring extends Disposable {
540544 for ( const area of this . _currentResponse . areas ) {
541545 const srcTokens = area . data ;
542546 const tokenCount = srcTokens . length / 5 ;
543- const destTokens = new Uint32Array ( 4 * tokenCount ) ;
547+ let destTokens = new Uint32Array ( 4 * tokenCount ) ;
548+ let destOffset = 0 ;
544549 for ( let i = 0 ; i < tokenCount ; i ++ ) {
545550 const srcOffset = 5 * i ;
546551 const deltaLine = srcTokens [ srcOffset ] ;
547552 const startCharacter = srcTokens [ srcOffset + 1 ] ;
548553 const endCharacter = srcTokens [ srcOffset + 2 ] ;
549- // const tokenType = srcTokens[srcOffset + 3];
550- // const tokenModifiers = srcTokens[srcOffset + 4];
551- // TODO@semantic : map here tokenType and tokenModifiers to metadata
552-
553- const fontStyle = FontStyle . Italic | FontStyle . Bold | FontStyle . Underline ;
554- const foregroundColorId = 3 ;
555- const metadata = (
556- ( fontStyle << MetadataConsts . FONT_STYLE_OFFSET )
557- | ( foregroundColorId << MetadataConsts . FOREGROUND_OFFSET )
558- ) >>> 0 ;
559-
560- const destOffset = 4 * i ;
561- destTokens [ destOffset ] = deltaLine ;
562- destTokens [ destOffset + 1 ] = startCharacter ;
563- destTokens [ destOffset + 2 ] = endCharacter ;
564- destTokens [ destOffset + 3 ] = metadata ;
554+ const tokenTypeIndex = srcTokens [ srcOffset + 3 ] ;
555+ const tokenType = legend . tokenTypes [ tokenTypeIndex ] ;
556+
557+ const tokenModifierSet = srcTokens [ srcOffset + 4 ] ;
558+ let tokenModifiers : string [ ] = [ ] ;
559+ for ( let modifierIndex = 0 ; tokenModifierSet !== 0 && modifierIndex < legend . tokenModifiers . length ; modifierIndex ++ ) {
560+ if ( tokenModifierSet & 1 ) {
561+ tokenModifiers . push ( legend . tokenTypes [ modifierIndex ] ) ;
562+ }
563+ }
564+
565+ const metadata = this . _themeService . getTheme ( ) . getTokenStyleMetadata ( tokenType , tokenModifiers ) ;
566+ if ( metadata !== undefined ) {
567+ destTokens [ destOffset ] = deltaLine ;
568+ destTokens [ destOffset + 1 ] = startCharacter ;
569+ destTokens [ destOffset + 2 ] = endCharacter ;
570+ destTokens [ destOffset + 3 ] = metadata ;
571+ destOffset += 4 ;
572+ }
565573 }
566574
575+ if ( destOffset !== destTokens . length ) {
576+ destTokens = destTokens . subarray ( 0 , destOffset ) ;
577+ }
567578 const tokens = new MultilineTokens2 ( area . line , new SparseEncodedTokens ( destTokens ) ) ;
568579 result . push ( tokens ) ;
569580 }
0 commit comments