1+ import * as vscode from 'vscode' ;
2+ import GunController from './GunController' ;
3+
4+ export default class SyntaxHighlightingGunController implements GunController {
5+ private readonly configuration : vscode . WorkspaceConfiguration ;
6+ private readonly tokenColorCustomizations : any ;
7+
8+ isGunTaken : boolean ;
9+
10+ private initialSettings = ( new class {
11+ private tokenColorCustomizations : any ;
12+
13+ store ( configuration : vscode . WorkspaceConfiguration ) {
14+ this . tokenColorCustomizations = configuration . get ( "editor.tokenColorCustomizations" ) ;
15+ }
16+
17+ restore ( configuration : vscode . WorkspaceConfiguration ) {
18+ configuration . update ( "editor.tokenColorCustomizations" , this . tokenColorCustomizations ) ;
19+ }
20+ } )
21+
22+ constructor ( configuration : vscode . WorkspaceConfiguration ) {
23+ this . configuration = configuration ;
24+ this . isGunTaken = false ;
25+ this . tokenColorCustomizations = SyntaxHighlightingGunController . createTokenColorCustomizations ( ) ;
26+ }
27+
28+ takeTheGun ( ) {
29+ if ( this . isGunTaken ) return ;
30+
31+ this . initialSettings . store ( this . configuration ) ;
32+
33+ this . configuration . update ( "editor.tokenColorCustomizations" , this . tokenColorCustomizations ) ;
34+
35+ this . isGunTaken = true ;
36+ }
37+
38+ giveTheGunBack ( ) {
39+ if ( ! this . isGunTaken ) return ;
40+
41+ this . initialSettings . restore ( this . configuration ) ;
42+
43+ this . isGunTaken = false ;
44+ }
45+
46+ private static createTokenColorCustomizations ( ) {
47+ let editorForegroundColor = this . getEditorForegroundColor ( ) ;
48+
49+ let textMateRules =
50+ SyntaxHighlightingGunController . getCSharpTokenColorCustomizations ( )
51+ . concat ( SyntaxHighlightingGunController . getTypescriptTokenColorCustomizations ( ) )
52+ . map ( scopeName => ( {
53+ scope : scopeName ,
54+ settings : {
55+ foreground : "#FFFFFF"
56+ }
57+ } ) ) ;
58+
59+ return {
60+ comments : editorForegroundColor ,
61+ functions : editorForegroundColor ,
62+ keywords : editorForegroundColor ,
63+ numbers : editorForegroundColor ,
64+ strings : editorForegroundColor ,
65+ types : editorForegroundColor ,
66+ variables : editorForegroundColor ,
67+ textMateRules : textMateRules
68+ } ;
69+ }
70+
71+ // TODO-IG: Make these token customizations configurable.
72+ // Or even better, is it possible to get them dynamically in the extension itself?
73+ private static getCSharpTokenColorCustomizations ( ) {
74+ return [
75+ "storage.modifier" , // E.g. public.
76+ "constant.language" // E.g. null.
77+ ]
78+ }
79+
80+ private static getTypescriptTokenColorCustomizations ( ) {
81+ return [
82+ "keyword.control" , // E.g. import.
83+ "storage.type" , // E.g. class.
84+ "support.type.primitive" , // E.g. boolean.
85+ "meta.object-literal.key" , // E.g. { literalKey : value }.
86+ "entity.other.inherited-class" // E.g. Class implements InheritedClass.
87+ ]
88+ }
89+
90+ private static getEditorForegroundColor ( ) {
91+ return "#FFFFFF" ; // TODO-IG: Find a way to get it from the current theme.
92+ }
93+ }
0 commit comments