33namespace Microsoft.VisualStudio.FSharp.Editor
44
55open System
6+ open System.Composition
67open System.ComponentModel .Composition
78open System.Windows .Media
89
10+ open Microsoft.VisualStudio
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
@@ -40,7 +46,66 @@ module internal FSharpClassificationTypes =
4046| SemanticClassificationType.Property-> Property
4147| SemanticClassificationType.Interface-> Interface
4248
49+
50+
4351module internal ClassificationDefinitions =
52+
53+ [<Export; Shared>]
54+ type internal ThemeColors
55+ [<ImportingConstructor>]
56+ (
57+ classificationformatMapService: IClassificationFormatMapService,
58+ classificationTypeRegistry: IClassificationTypeRegistryService,
59+ [< Import( typeof< SVsServiceProvider>)>] serviceProvider: IServiceProvider
60+ ) =
61+
62+ let (| LighTheme | DarkTheme | UnknownTheme |) id =
63+ if id= Guid( " de3dbbcd-f642-433c-8353-8f1df4370aba" ) ||
64+ id= Guid( " a4d6a176-b948-4b29-8c66-53c97a1ed7d0" ) then LighTheme
65+ elif id= Guid( " 1ded0138-47ce-435e-84ef-9ec1f439b749" ) then DarkTheme
66+ else UnknownTheme
67+
68+ let getCurrentThemeId () =
69+ let themeService = serviceProvider.GetService( typeof< SVsColorThemeService>) :?> IVsColorThemeService
70+ themeService.CurrentTheme.ThemeId
71+
72+ let colorData = // name, (light, dark)
73+ [ FSharpClassificationTypes.Function, ( Colors.Black, Color.FromRgb( 220 uy, 220 uy, 220 uy))
74+ FSharpClassificationTypes.MutableVar, ( Color.FromRgb( 160 uy, 128 uy, 0 uy), Color.FromRgb( 255 uy, 210 uy, 28 uy))
75+ FSharpClassificationTypes.Printf, ( Color.FromRgb( 43 uy, 145 uy, 175 uy), Color.FromRgb( 78 uy, 220 uy, 176 uy))
76+ FSharpClassificationTypes.Property, ( Colors.Black, Color.FromRgb( 220 uy, 220 uy, 220 uy)) ]
77+
78+ let setColors _ =
79+ let fontAndColorStorage = serviceProvider.GetService( typeof< SVsFontAndColorStorage>) :?> IVsFontAndColorStorage
80+ let fontAndColorCacheManager = serviceProvider.GetService( typeof< SVsFontAndColorCacheManager>) :?> IVsFontAndColorCacheManager
81+ let textEditor = Guid( " A27B4E24-A735-4D1D-B8E7-9716E1E3D8E0" )
82+ fontAndColorCacheManager.CheckCache( ref textEditor) |> ignore
83+ fontAndColorStorage.OpenCategory( ref textEditor, uint32__ FCSTORAGEFLAGS.FCSF_ READONLY) |> ignore
84+
85+ let formatMap = classificationformatMapService.GetClassificationFormatMap( category= " text" )
86+ for ctype, ( light, dark) in colorDatado
87+ // we don't touch the changes made by the user
88+ if fontAndColorStorage.GetItem( ctype, Array.zeroCreate1 ) <> VSConstants.S_ OKthen
89+ let ict = classificationTypeRegistry.GetClassificationType( ctype)
90+ let oldProps = formatMap.GetTextProperties( ict)
91+ let newProps = match getCurrentThemeId() with
92+ | LighTheme-> oldProps.SetForeground light
93+ | DarkTheme-> oldProps.SetForeground dark
94+ | UnknownTheme-> oldProps
95+ formatMap.SetTextProperties( ict, newProps)
96+ fontAndColorStorage.CloseCategory() |> ignore
97+
98+ let handler = ThemeChangedEventHandler setColors
99+ do VSColorTheme.add_ ThemeChanged handler
100+ interface IDisposablewith member __.Dispose () = VSColorTheme.remove_ ThemeChanged handler
101+
102+ member __.getColor ( ctype ) =
103+ let light , dark = colorData|> Map.ofList|> Map.find ctype
104+ match getCurrentThemeId() with
105+ | LighTheme-> Nullable light
106+ | DarkTheme-> Nullable dark
107+ | UnknownTheme-> Nullable()
108+
44109[<Export; Name( FSharpClassificationTypes.Function); BaseDefinition( PredefinedClassificationTypeNames.FormalLanguage) >]
45110let FSharpFunctionClassificationType : ClassificationTypeDefinition = null
46111
@@ -58,39 +123,41 @@ module internal ClassificationDefinitions =
58123[<Name( FSharpClassificationTypes.Function) >]
59124[<UserVisible( true ) >]
60125[<Order( After= PredefinedClassificationTypeNames.Keyword) >]
61- type internal FSharpFunctionTypeFormat ( )as self=
126+ type internal FSharpFunctionTypeFormat [<ImportingConstructor>] ( theme : ThemeColors ) as self =
62127inherit ClassificationFormatDefinition()
63- // Not setting any colors here, so it will inherit from "Plain Text" by default
128+
64129do self.DisplayName<- SR.FSharpFunctionsOrMethodsClassificationType.Value
130+ self.ForegroundColor<- theme.getColor FSharpClassificationTypes.Function
65131
66132[<Export( typeof< EditorFormatDefinition>) >]
67133[<ClassificationType( ClassificationTypeNames= FSharpClassificationTypes.MutableVar) >]
68134[<Name( FSharpClassificationTypes.MutableVar) >]
69135[<UserVisible( true ) >]
70136[<Order( After= PredefinedClassificationTypeNames.Keyword) >]
71- type internal FSharpMutableVarTypeFormat ( )as self=
137+ type internal FSharpMutableVarTypeFormat [<ImportingConstructor>] ( theme : ThemeColors ) as self =
72138inherit ClassificationFormatDefinition()
73-
139+
74140do self.DisplayName<- SR.FSharpMutableVarsClassificationType.Value
75- self.ForegroundColor<- Nullable Colors.Red
141+ self.ForegroundColor<- theme.getColor FSharpClassificationTypes.MutableVar
76142
77143[<Export( typeof< EditorFormatDefinition>) >]
78144[<ClassificationType( ClassificationTypeNames= FSharpClassificationTypes.Printf) >]
79145[<Name( FSharpClassificationTypes.Printf) >]
80146[<UserVisible( true ) >]
81147[<Order( After= PredefinedClassificationTypeNames.String) >]
82- type internal FSharpPrintfTypeFormat ( )as self=
148+ type internal FSharpPrintfTypeFormat [<ImportingConstructor>] ( theme : ThemeColors ) as self =
83149inherit ClassificationFormatDefinition()
84-
150+
85151do self.DisplayName<- SR.FSharpPrintfFormatClassificationType.Value
86- self.ForegroundColor<- Nullable ( Color.FromRgb ( 43 uy , 145 uy , 175 uy ))
87-
152+ self.ForegroundColor<- theme.getColor FSharpClassificationTypes.Printf
153+
88154[<Export( typeof< EditorFormatDefinition>) >]
89155[<ClassificationType( ClassificationTypeNames= FSharpClassificationTypes.Property) >]
90156[<Name( FSharpClassificationTypes.Property) >]
91157[<UserVisible( true ) >]
92158[<Order( After= PredefinedClassificationTypeNames.Keyword) >]
93- type internal FSharpPropertyFormat ( )as self=
159+ type internal FSharpPropertyFormat [<ImportingConstructor>] ( theme : ThemeColors ) as self =
94160inherit ClassificationFormatDefinition()
95-
161+
96162do self.DisplayName<- SR.FSharpPropertiesClassificationType.Value
163+ self.ForegroundColor<- theme.getColor FSharpClassificationTypes.Property