1- import { EditorTheme } from "@code-hike/smooth-code/dist/themes"
1+ /**
2+ * A single theme setting.
3+ */
4+ interface IRawThemeSetting {
5+ name ?: string
6+ scope ?: string | string [ ]
7+ settings : {
8+ fontStyle ?: string
9+ foreground ?: string
10+ background ?: string
11+ }
12+ }
13+ /**
14+ * A TextMate theme.
15+ */
16+ export interface EditorTheme {
17+ name ?: string
18+ type ?: string
19+ colors ?: Record < string , string >
20+
21+ // not used:
22+ tokenColors ?: IRawThemeSetting [ ]
23+ settings ?: IRawThemeSetting [ ]
24+ semanticTokenColors ?: any
25+ semanticHighlighting ?: boolean
26+ }
227
328export enum ColorName {
29+ CodeForeground ,
30+ CodeBackground ,
31+ EditorForeground ,
432 EditorBackground ,
533 ActiveTabBackground ,
634 ActiveTabForeground ,
@@ -10,20 +38,32 @@ export enum ColorName {
1038 EditorGroupHeaderBackground ,
1139 TabBorder ,
1240 ActiveTabBottomBorder ,
41+ LineNumberForeground ,
1342}
1443
1544type Color = string | undefined
1645
1746const contrastBorder = "#6FC3DF"
1847
1948// defaults from: https://github.com/microsoft/vscode/blob/main/src/vs/workbench/common/theme.ts
49+ // and: https://github.com/microsoft/vscode/blob/main/src/vs/editor/common/view/editorColorRegistry.ts
2050// keys from : https://code.visualstudio.com/api/references/theme-color#editor-groups-tabs
2151export function getColor (
2252 theme : EditorTheme ,
2353 colorName : ColorName
2454) : Color {
25- const colors = ( theme as any ) . colors || { }
55+ const colors = theme . colors || { }
2656 switch ( colorName ) {
57+ case ColorName . CodeForeground :
58+ return (
59+ getGlobalSettings ( theme ) ?. foreground ||
60+ getColor ( theme , ColorName . EditorForeground )
61+ )
62+ case ColorName . CodeBackground :
63+ return (
64+ getGlobalSettings ( theme ) ?. background ||
65+ getColor ( theme , ColorName . EditorBackground )
66+ )
2767 case ColorName . EditorBackground :
2868 return (
2969 colors [ "editor.background" ] ||
@@ -33,6 +73,15 @@ export function getColor(
3373 hc : "#000000" ,
3474 } )
3575 )
76+ case ColorName . EditorForeground :
77+ return (
78+ colors [ "editor.foreground" ] ||
79+ getDefault ( theme , {
80+ light : "#333333" ,
81+ dark : "#BBBBBB" ,
82+ hc : "#fffffe" ,
83+ } )
84+ )
3685 case ColorName . ActiveTabBackground :
3786 return (
3887 colors [ "tab.activeBackground" ] ||
@@ -103,6 +152,15 @@ export function getColor(
103152 hc : undefined ,
104153 } )
105154 )
155+ case ColorName . LineNumberForeground :
156+ return (
157+ colors [ "editorLineNumber.foreground" ] ||
158+ getDefault ( theme , {
159+ dark : "#858585" ,
160+ light : "#237893" ,
161+ hc : "#fffffe" ,
162+ } )
163+ )
106164
107165 default :
108166 return "#f00"
@@ -122,10 +180,36 @@ function getDefault(
122180 theme : EditorTheme ,
123181 defaults : { dark : Color ; light : Color ; hc : Color }
124182) : Color {
125- const themeType = ( theme . type
183+ return defaults [ getThemeType ( theme ) ]
184+ }
185+
186+ function getThemeType (
187+ theme : EditorTheme
188+ ) : "dark" | "light" | "hc" {
189+ return ( theme . type
126190 ? theme . type
127191 : theme . name ?. toLowerCase ( ) . includes ( "light" )
128192 ? "light"
129193 : "dark" ) as "dark" | "light" | "hc"
130- return defaults [ themeType ]
194+ }
195+
196+ export function getCodeColors (
197+ theme : EditorTheme
198+ ) : { fg : string ; bg : string } {
199+ return {
200+ fg : getColor ( theme , ColorName . CodeForeground ) ! ,
201+ bg : getColor ( theme , ColorName . CodeBackground ) ! ,
202+ }
203+ }
204+
205+ function getGlobalSettings ( theme : EditorTheme ) {
206+ let settings = theme . settings
207+ ? theme . settings
208+ : theme . tokenColors
209+ const globalSetting = settings
210+ ? settings . find ( s => {
211+ return ! s . name && ! s . scope
212+ } )
213+ : undefined
214+ return globalSetting ?. settings
131215}
0 commit comments