@@ -6,6 +6,12 @@ open System
66open System.ComponentModel .Composition
77open System.Windows .Media
88
9+ open Microsoft.VisualStudio
10+ open Microsoft.VisualStudio .Editor
11+ open Microsoft.VisualStudio .PlatformUI
12+ open Microsoft.VisualStudio .Shell
13+ open Microsoft.VisualStudio .Shell .Interop
14+ open Microsoft.Internal .VisualStudio .Shell .Interop
915open Microsoft.VisualStudio .Language .StandardClassification
1016open Microsoft.VisualStudio .Text .Classification
1117open Microsoft.VisualStudio .Utilities
@@ -41,6 +47,64 @@ module internal FSharpClassificationTypes =
4147| SemanticClassificationType.Interface-> Interface
4248
4349module internal ClassificationDefinitions =
50+
51+ [<Export>]
52+ type internal ThemeColors
53+ [<ImportingConstructor>]
54+ (
55+ classificationformatMapService: IClassificationFormatMapService,
56+ classificationTypeRegistry: IClassificationTypeRegistryService,
57+ [< Import( typeof< SVsServiceProvider>)>] serviceProvider: IServiceProvider
58+ ) =
59+
60+ let (| LightTheme | DarkTheme | UnknownTheme |) id =
61+ if id= KnownColorThemes.Light|| id= KnownColorThemes.Bluethen LightTheme
62+ elif id= KnownColorThemes.Darkthen DarkTheme
63+ else UnknownTheme
64+
65+ let getCurrentThemeId () =
66+ let themeService = serviceProvider.GetService( typeof< SVsColorThemeService>) :?> IVsColorThemeService
67+ themeService.CurrentTheme.ThemeId
68+
69+ let colorData = // name, (light, dark)
70+ [ FSharpClassificationTypes.Function, ( Colors.Black, Color.FromRgb( 220 uy, 220 uy, 220 uy))
71+ FSharpClassificationTypes.MutableVar, ( Color.FromRgb( 160 uy, 128 uy, 0 uy), Color.FromRgb( 255 uy, 210 uy, 28 uy))
72+ FSharpClassificationTypes.Printf, ( Color.FromRgb( 43 uy, 145 uy, 175 uy), Color.FromRgb( 78 uy, 220 uy, 176 uy))
73+ FSharpClassificationTypes.Property, ( Colors.Black, Color.FromRgb( 220 uy, 220 uy, 220 uy)) ]
74+
75+ let setColors _ =
76+ let fontAndColorStorage = serviceProvider.GetService( typeof< SVsFontAndColorStorage>) :?> IVsFontAndColorStorage
77+ let fontAndColorCacheManager = serviceProvider.GetService( typeof< SVsFontAndColorCacheManager>) :?> IVsFontAndColorCacheManager
78+ fontAndColorCacheManager.CheckCache( ref DefGuidList.guidTextEditorFontCategory) |> ignore
79+ fontAndColorStorage.OpenCategory( ref DefGuidList.guidTextEditorFontCategory, uint32__ FCSTORAGEFLAGS.FCSF_ READONLY) |> ignore
80+
81+ let formatMap = classificationformatMapService.GetClassificationFormatMap( category= " text" )
82+ try
83+ formatMap.BeginBatchUpdate()
84+ for ctype, ( light, dark) in colorDatado
85+ // we don't touch the changes made by the user
86+ if fontAndColorStorage.GetItem( ctype, Array.zeroCreate1 ) <> VSConstants.S_ OKthen
87+ let ict = classificationTypeRegistry.GetClassificationType( ctype)
88+ let oldProps = formatMap.GetTextProperties( ict)
89+ let newProps = match getCurrentThemeId() with
90+ | LightTheme-> oldProps.SetForeground light
91+ | DarkTheme-> oldProps.SetForeground dark
92+ | UnknownTheme-> oldProps
93+ formatMap.SetTextProperties( ict, newProps)
94+ fontAndColorStorage.CloseCategory() |> ignore
95+ finally formatMap.EndBatchUpdate()
96+
97+ let handler = ThemeChangedEventHandler setColors
98+ do VSColorTheme.add_ ThemeChanged handler
99+ interface IDisposablewith member __.Dispose () = VSColorTheme.remove_ ThemeChanged handler
100+
101+ member __.GetColor ( ctype ) =
102+ let light , dark = colorData|> Map.ofList|> Map.find ctype
103+ match getCurrentThemeId() with
104+ | LightTheme-> Nullable light
105+ | DarkTheme-> Nullable dark
106+ | UnknownTheme-> Nullable()
107+
44108[<Export; Name( FSharpClassificationTypes.Function); BaseDefinition( PredefinedClassificationTypeNames.FormalLanguage) >]
45109let FSharpFunctionClassificationType : ClassificationTypeDefinition = null
46110
@@ -58,39 +122,41 @@ module internal ClassificationDefinitions =
58122[<Name( FSharpClassificationTypes.Function) >]
59123[<UserVisible( true ) >]
60124[<Order( After= PredefinedClassificationTypeNames.Keyword) >]
61- type internal FSharpFunctionTypeFormat ( )as self=
125+ type internal FSharpFunctionTypeFormat [<ImportingConstructor>] ( theme : ThemeColors ) as self =
62126inherit ClassificationFormatDefinition()
63- // Not setting any colors here, so it will inherit from "Plain Text" by default
127+
64128do self.DisplayName<- SR.FSharpFunctionsOrMethodsClassificationType.Value
129+ self.ForegroundColor<- theme.GetColor FSharpClassificationTypes.Function
65130
66131[<Export( typeof< EditorFormatDefinition>) >]
67132[<ClassificationType( ClassificationTypeNames= FSharpClassificationTypes.MutableVar) >]
68133[<Name( FSharpClassificationTypes.MutableVar) >]
69134[<UserVisible( true ) >]
70135[<Order( After= PredefinedClassificationTypeNames.Keyword) >]
71- type internal FSharpMutableVarTypeFormat ( )as self=
136+ type internal FSharpMutableVarTypeFormat [<ImportingConstructor>] ( theme : ThemeColors ) as self =
72137inherit ClassificationFormatDefinition()
73-
138+
74139do self.DisplayName<- SR.FSharpMutableVarsClassificationType.Value
75- self.ForegroundColor<- Nullable Colors.Red
140+ self.ForegroundColor<- theme.GetColor FSharpClassificationTypes.MutableVar
76141
77142[<Export( typeof< EditorFormatDefinition>) >]
78143[<ClassificationType( ClassificationTypeNames= FSharpClassificationTypes.Printf) >]
79144[<Name( FSharpClassificationTypes.Printf) >]
80145[<UserVisible( true ) >]
81146[<Order( After= PredefinedClassificationTypeNames.String) >]
82- type internal FSharpPrintfTypeFormat ( )as self=
147+ type internal FSharpPrintfTypeFormat [<ImportingConstructor>] ( theme : ThemeColors ) as self =
83148inherit ClassificationFormatDefinition()
84-
149+
85150do self.DisplayName<- SR.FSharpPrintfFormatClassificationType.Value
86- self.ForegroundColor<- Nullable ( Color.FromRgb ( 43 uy , 145 uy , 175 uy ))
87-
151+ self.ForegroundColor<- theme.GetColor FSharpClassificationTypes.Printf
152+
88153[<Export( typeof< EditorFormatDefinition>) >]
89154[<ClassificationType( ClassificationTypeNames= FSharpClassificationTypes.Property) >]
90155[<Name( FSharpClassificationTypes.Property) >]
91156[<UserVisible( true ) >]
92157[<Order( After= PredefinedClassificationTypeNames.Keyword) >]
93- type internal FSharpPropertyFormat ( )as self=
158+ type internal FSharpPropertyFormat [<ImportingConstructor>] ( theme : ThemeColors ) as self =
94159inherit ClassificationFormatDefinition()
95-
160+
96161do self.DisplayName<- SR.FSharpPropertiesClassificationType.Value
162+ self.ForegroundColor<- theme.GetColor FSharpClassificationTypes.Property