Skip to content

Commit

Permalink
Restore custom F# color customization from before (dotnet#9915)
Browse files Browse the repository at this point in the history
* Restore custom F# color customization from before

* oopsie
  • Loading branch information
cartermp authored and nosami committed Feb 22, 2021
1 parent 3085c4d commit 4aa0e76
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 167 deletions.
132 changes: 101 additions & 31 deletions Classification/ClassificationDefinitions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ open FSharp.Compiler.SourceCodeServices

[<RequireQualifiedAccess>]
module internal FSharpClassificationTypes =
let [<Literal>] Function = "FSharp.Function"
let [<Literal>] MutableVar = "FSharp.MutableVar"
let [<Literal>] Disposable = "FSharp.Disposable"
let [<Literal>] DisposableType = "FSharp.DisposableType"
let [<Literal>] DisposableValue = "FSharp.DisposableValue"
let [<Literal>] Printf = "FSharp.Printf"

let getClassificationTypeName = function
| SemanticClassificationType.MutableRecordField
| SemanticClassificationType.MutableVar -> MutableVar
| SemanticClassificationType.DisposableValue
| SemanticClassificationType.DisposableType -> Disposable
| SemanticClassificationType.DisposableValue -> DisposableValue
| SemanticClassificationType.DisposableType -> DisposableType
| SemanticClassificationType.NameSpace -> ClassificationTypeNames.NamespaceName
| SemanticClassificationType.Printf -> Printf
| SemanticClassificationType.Exception
| SemanticClassificationType.Module
| SemanticClassificationType.Type
| SemanticClassificationType.TypeDef
| SemanticClassificationType.ConstructorForReferenceType
| SemanticClassificationType.Printf
| SemanticClassificationType.ReferenceType -> ClassificationTypeNames.ClassName
| SemanticClassificationType.ConstructorForValueType
| SemanticClassificationType.ValueType -> ClassificationTypeNames.StructName
Expand All @@ -48,7 +51,7 @@ module internal FSharpClassificationTypes =
| SemanticClassificationType.Interface -> ClassificationTypeNames.InterfaceName
| SemanticClassificationType.TypeArgument -> ClassificationTypeNames.TypeParameterName
| SemanticClassificationType.Operator -> ClassificationTypeNames.Operator
| SemanticClassificationType.Function
| SemanticClassificationType.Function -> Function
| SemanticClassificationType.Method -> ClassificationTypeNames.MethodName
| SemanticClassificationType.ExtensionMethod -> ClassificationTypeNames.ExtensionMethodName
| SemanticClassificationType.Literal -> ClassificationTypeNames.ConstantName
Expand Down Expand Up @@ -82,54 +85,95 @@ module internal ClassificationDefinitions =
let themeService = serviceProvider.GetService(typeof<SVsColorThemeService>) :?> IVsColorThemeService
themeService.CurrentTheme.ThemeId

let customColorData = // name, (light, dark)
let customColorData =
[
FSharpClassificationTypes.MutableVar, (Color.FromRgb(160uy, 128uy, 0uy), Color.FromRgb(255uy, 210uy, 28uy))
FSharpClassificationTypes.Disposable, (Colors.Green, Color.FromRgb(2uy, 183uy, 43uy))
{| ClassificationName = FSharpClassificationTypes.MutableVar
LightThemeColor = Color.FromRgb(160uy, 128uy, 0uy)
DarkThemeColor = Color.FromRgb(255uy, 210uy, 28uy) |}
{| ClassificationName = FSharpClassificationTypes.DisposableType
LightThemeColor = Colors.Green
DarkThemeColor = Color.FromRgb(2uy, 183uy, 43uy) |}
{| ClassificationName = FSharpClassificationTypes.DisposableValue
LightThemeColor = Colors.Green
DarkThemeColor = Color.FromRgb(2uy, 183uy, 43uy) |}
{| ClassificationName = FSharpClassificationTypes.Function
LightThemeColor = Color.FromRgb(116uy, 83uy, 31uy)
DarkThemeColor = Color.FromRgb(220uy, 220uy, 170uy) |}
{| ClassificationName = FSharpClassificationTypes.Printf
LightThemeColor = Color.FromRgb(43uy, 145uy, 175uy)
DarkThemeColor = Color.FromRgb(78uy, 220uy, 176uy) |}
]

let setColors _ =
let fontAndColorStorage = serviceProvider.GetService(typeof<SVsFontAndColorStorage>) :?> IVsFontAndColorStorage
let fontAndColorCacheManager = serviceProvider.GetService(typeof<SVsFontAndColorCacheManager>) :?> IVsFontAndColorCacheManager
fontAndColorCacheManager.CheckCache( ref DefGuidList.guidTextEditorFontCategory) |> ignore
fontAndColorCacheManager.CheckCache(ref DefGuidList.guidTextEditorFontCategory) |> ignore
fontAndColorStorage.OpenCategory(ref DefGuidList.guidTextEditorFontCategory, uint32 __FCSTORAGEFLAGS.FCSF_READONLY) |> ignore

let formatMap = classificationformatMapService.GetClassificationFormatMap(category = "text")
try
formatMap.BeginBatchUpdate()
for ctype, (light, dark) in customColorData do
for item in customColorData do
// we don't touch the changes made by the user
if fontAndColorStorage.GetItem(ctype, Array.zeroCreate 1) <> VSConstants.S_OK then
let ict = classificationTypeRegistry.GetClassificationType(ctype)
if fontAndColorStorage.GetItem(item.ClassificationName, Array.zeroCreate 1) <> VSConstants.S_OK then
let ict = classificationTypeRegistry.GetClassificationType(item.ClassificationName)
let oldProps = formatMap.GetTextProperties(ict)
let newProps =
match getCurrentThemeId() with
| LightTheme -> oldProps.SetForeground light
| DarkTheme -> oldProps.SetForeground dark
| LightTheme -> oldProps.SetForeground item.LightThemeColor
| DarkTheme -> oldProps.SetForeground item.DarkThemeColor
| UnknownTheme -> oldProps
formatMap.SetTextProperties(ict, newProps)
fontAndColorStorage.CloseCategory() |> ignore
finally formatMap.EndBatchUpdate()

let handler = ThemeChangedEventHandler setColors
do VSColorTheme.add_ThemeChanged handler
interface IDisposable with member __.Dispose() = VSColorTheme.remove_ThemeChanged handler

member __.GetColor(ctype) =
let light, dark = customColorData |> Map.ofList |> Map.find ctype
match getCurrentThemeId() with
| LightTheme -> Nullable light
| DarkTheme -> Nullable dark
| UnknownTheme -> Nullable()
do VSColorTheme.add_ThemeChanged handler

interface ISetThemeColors with member this.SetColors() = setColors()
member _.GetColor(ctype) =
match customColorData |> List.tryFind (fun item -> item.ClassificationName = ctype) with
| Some item ->
let light, dark = item.LightThemeColor, item.DarkThemeColor
match getCurrentThemeId() with
| LightTheme -> Nullable light
| DarkTheme -> Nullable dark
| UnknownTheme -> Nullable()
| None ->
Nullable()

interface ISetThemeColors with
member _.SetColors() = setColors()

interface IDisposable with
member _.Dispose() = VSColorTheme.remove_ThemeChanged handler

[<Export; Name(FSharpClassificationTypes.MutableVar); BaseDefinition(PredefinedClassificationTypeNames.FormalLanguage)>]
let FSharpMutableVarClassificationType : ClassificationTypeDefinition = null


[<Export; Name(FSharpClassificationTypes.Disposable); BaseDefinition(PredefinedClassificationTypeNames.FormalLanguage)>]
[<Export; Name(FSharpClassificationTypes.DisposableType); BaseDefinition(PredefinedClassificationTypeNames.FormalLanguage)>]
let FSharpDisposableClassificationType : ClassificationTypeDefinition = null

[<Export; Name(FSharpClassificationTypes.DisposableValue); BaseDefinition(PredefinedClassificationTypeNames.FormalLanguage)>]
let FSharpDisposableValueClassificationType : ClassificationTypeDefinition = null

[<Export; Name(FSharpClassificationTypes.Function); BaseDefinition(PredefinedClassificationTypeNames.FormalLanguage)>]
let FSharpFunctionClassificationType : ClassificationTypeDefinition = null

[<Export; Name(FSharpClassificationTypes.Printf); BaseDefinition(PredefinedClassificationTypeNames.FormalLanguage)>]
let FSharpPrintfClassificationType : ClassificationTypeDefinition = null

[<Export(typeof<EditorFormatDefinition>)>]
[<ClassificationType(ClassificationTypeNames = FSharpClassificationTypes.Function)>]
[<Name(FSharpClassificationTypes.Function)>]
[<UserVisible(true)>]
[<Order(After = PredefinedClassificationTypeNames.Keyword)>]
type internal FSharpFunctionTypeFormat [<ImportingConstructor>](theme: ThemeColors) as self =
inherit ClassificationFormatDefinition()

do
self.DisplayName <- SR.FSharpFunctionsClassificationType()
self.ForegroundColor <- theme.GetColor FSharpClassificationTypes.Function

[<Export(typeof<EditorFormatDefinition>)>]
[<ClassificationType(ClassificationTypeNames = FSharpClassificationTypes.MutableVar)>]
Expand All @@ -139,16 +183,42 @@ module internal ClassificationDefinitions =
type internal FSharpMutableVarTypeFormat [<ImportingConstructor>](theme: ThemeColors) as self =
inherit ClassificationFormatDefinition()

do self.DisplayName <- SR.FSharpMutableVarsClassificationType()
self.ForegroundColor <- theme.GetColor FSharpClassificationTypes.MutableVar
do
self.DisplayName <- SR.FSharpMutableVarsClassificationType()
self.ForegroundColor <- theme.GetColor FSharpClassificationTypes.MutableVar

[<Export(typeof<EditorFormatDefinition>)>]
[<ClassificationType(ClassificationTypeNames = FSharpClassificationTypes.Disposable)>]
[<Name(FSharpClassificationTypes.Disposable)>]
[<ClassificationType(ClassificationTypeNames = FSharpClassificationTypes.Printf)>]
[<Name(FSharpClassificationTypes.Printf)>]
[<UserVisible(true)>]
[<Order(After = PredefinedClassificationTypeNames.String)>]
type internal FSharpPrintfTypeFormat [<ImportingConstructor>](theme: ThemeColors) as self =
inherit ClassificationFormatDefinition()

do
self.DisplayName <- SR.FSharpPrintfFormatClassificationType()
self.ForegroundColor <- theme.GetColor FSharpClassificationTypes.Printf

[<Export(typeof<EditorFormatDefinition>)>]
[<ClassificationType(ClassificationTypeNames = FSharpClassificationTypes.DisposableType)>]
[<Name(FSharpClassificationTypes.DisposableType)>]
[<UserVisible(true)>]
[<Order(After = PredefinedClassificationTypeNames.Keyword)>]
type internal FSharpDisposableFormat [<ImportingConstructor>](theme: ThemeColors) as self =
inherit ClassificationFormatDefinition()

do self.DisplayName <- SR.FSharpDisposablesClassificationType()
self.ForegroundColor <- theme.GetColor FSharpClassificationTypes.Disposable
do
self.DisplayName <- SR.FSharpDisposableTypesClassificationType()
self.ForegroundColor <- theme.GetColor FSharpClassificationTypes.DisposableType

[<Export(typeof<EditorFormatDefinition>)>]
[<ClassificationType(ClassificationTypeNames = FSharpClassificationTypes.DisposableValue)>]
[<Name(FSharpClassificationTypes.DisposableValue)>]
[<UserVisible(true)>]
[<Order(After = PredefinedClassificationTypeNames.Keyword)>]
type internal FSharpDisposableValueFormat [<ImportingConstructor>](theme: ThemeColors) as self =
inherit ClassificationFormatDefinition()

do
self.DisplayName <- SR.FSharpDisposableValuesClassificationType()
self.ForegroundColor <- theme.GetColor FSharpClassificationTypes.DisposableValue
15 changes: 9 additions & 6 deletions FSharp.Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,16 @@
<data name="NameCanBeSimplified" xml:space="preserve">
<value>Name can be simplified.</value>
</data>
<data name="FSharpFunctionsOrMethodsClassificationType" xml:space="preserve">
<value>F# Functions / Methods</value>
<data name="FSharpFunctionsClassificationType" xml:space="preserve">
<value>F# Functions</value>
</data>
<data name="FSharpMutableVarsClassificationType" xml:space="preserve">
<value>F# Mutable Variables / Reference Cells</value>
</data>
<data name="FSharpPrintfFormatClassificationType" xml:space="preserve">
<value>F# Printf Format</value>
</data>
<data name="FSharpPropertiesClassificationType" xml:space="preserve">
<value>F# Properties</value>
</data>
<data name="FSharpDisposablesClassificationType" xml:space="preserve">
<data name="FSharpDisposableTypesClassificationType" xml:space="preserve">
<value>F# Disposable Types</value>
</data>
<data name="RemoveUnusedOpens" xml:space="preserve">
Expand Down Expand Up @@ -213,4 +210,10 @@
<data name="RenameValueToDoubleUnderscore" xml:space="preserve">
<value>Rename '{0}' to '__'</value>
</data>
<data name="FSharpDisposableValuesClassificationType" xml:space="preserve">
<value>F# Disposable Values</value>
</data>
<data name="FSharpPropertiesClassificationType" xml:space="preserve">
<value>F# Properties</value>
</data>
</root>
25 changes: 15 additions & 10 deletions xlf/FSharp.Editor.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
<target state="translated">Přidejte klíčové slovo new.</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposableTypesClassificationType">
<source>F# Disposable Types</source>
<target state="new">F# Disposable Types</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposableValuesClassificationType">
<source>F# Disposable Values</source>
<target state="new">F# Disposable Values</target>
<note />
</trans-unit>
<trans-unit id="FSharpFunctionsClassificationType">
<source>F# Functions</source>
<target state="new">F# Functions</target>
<note />
</trans-unit>
<trans-unit id="ImplementInterface">
<source>Implement interface</source>
<target state="translated">Implementujte rozhraní.</target>
Expand Down Expand Up @@ -37,11 +52,6 @@
<target state="translated">Název se dá zjednodušit.</target>
<note />
</trans-unit>
<trans-unit id="FSharpFunctionsOrMethodsClassificationType">
<source>F# Functions / Methods</source>
<target state="translated">Funkce / metody jazyka F#</target>
<note />
</trans-unit>
<trans-unit id="FSharpMutableVarsClassificationType">
<source>F# Mutable Variables / Reference Cells</source>
<target state="translated">Měnitelné proměnné / referenční buňky jazyka F#</target>
Expand All @@ -57,11 +67,6 @@
<target state="translated">Vlastnosti jazyka F#</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposablesClassificationType">
<source>F# Disposable Types</source>
<target state="translated">Jednoúčelové typy jazyka F#</target>
<note />
</trans-unit>
<trans-unit id="RemoveUnusedOpens">
<source>Remove unused open declarations</source>
<target state="translated">Odeberte nepoužité otevřené deklarace.</target>
Expand Down
25 changes: 15 additions & 10 deletions xlf/FSharp.Editor.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
<target state="translated">Schlüsselwort "new" hinzufügen</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposableTypesClassificationType">
<source>F# Disposable Types</source>
<target state="new">F# Disposable Types</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposableValuesClassificationType">
<source>F# Disposable Values</source>
<target state="new">F# Disposable Values</target>
<note />
</trans-unit>
<trans-unit id="FSharpFunctionsClassificationType">
<source>F# Functions</source>
<target state="new">F# Functions</target>
<note />
</trans-unit>
<trans-unit id="ImplementInterface">
<source>Implement interface</source>
<target state="translated">Schnittstelle implementieren</target>
Expand Down Expand Up @@ -37,11 +52,6 @@
<target state="translated">Der Name kann vereinfacht werden.</target>
<note />
</trans-unit>
<trans-unit id="FSharpFunctionsOrMethodsClassificationType">
<source>F# Functions / Methods</source>
<target state="translated">F#-Funktionen/-Methoden</target>
<note />
</trans-unit>
<trans-unit id="FSharpMutableVarsClassificationType">
<source>F# Mutable Variables / Reference Cells</source>
<target state="translated">Änderbare F#-Variablen/Verweiszellen</target>
Expand All @@ -57,11 +67,6 @@
<target state="translated">F#-Eigenschaften</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposablesClassificationType">
<source>F# Disposable Types</source>
<target state="translated">Verwerfbare F#-Typen</target>
<note />
</trans-unit>
<trans-unit id="RemoveUnusedOpens">
<source>Remove unused open declarations</source>
<target state="translated">Nicht verwendete open-Deklarationen entfernen</target>
Expand Down
25 changes: 15 additions & 10 deletions xlf/FSharp.Editor.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
<target state="translated">Agregar "nueva" palabra clave</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposableTypesClassificationType">
<source>F# Disposable Types</source>
<target state="new">F# Disposable Types</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposableValuesClassificationType">
<source>F# Disposable Values</source>
<target state="new">F# Disposable Values</target>
<note />
</trans-unit>
<trans-unit id="FSharpFunctionsClassificationType">
<source>F# Functions</source>
<target state="new">F# Functions</target>
<note />
</trans-unit>
<trans-unit id="ImplementInterface">
<source>Implement interface</source>
<target state="translated">Implementar interfaz</target>
Expand Down Expand Up @@ -37,11 +52,6 @@
<target state="translated">El nombre se puede simplificar.</target>
<note />
</trans-unit>
<trans-unit id="FSharpFunctionsOrMethodsClassificationType">
<source>F# Functions / Methods</source>
<target state="translated">Funciones y métodos de F#</target>
<note />
</trans-unit>
<trans-unit id="FSharpMutableVarsClassificationType">
<source>F# Mutable Variables / Reference Cells</source>
<target state="translated">Variables mutables y celdas de referencia de F#</target>
Expand All @@ -57,11 +67,6 @@
<target state="translated">Propiedades de F#</target>
<note />
</trans-unit>
<trans-unit id="FSharpDisposablesClassificationType">
<source>F# Disposable Types</source>
<target state="translated">Tipos descartables de F#</target>
<note />
</trans-unit>
<trans-unit id="RemoveUnusedOpens">
<source>Remove unused open declarations</source>
<target state="translated">Quitar declaraciones abiertas no usadas</target>
Expand Down

0 comments on commit 4aa0e76

Please sign in to comment.