-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
255 lines (220 loc) · 10.3 KB
/
extension.js
File metadata and controls
255 lines (220 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const vscode = require('vscode');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Mustache Inline Highlighter est activé');
// Fonction pour récupérer les couleurs du thème actif depuis tokenColorCustomizations
function getThemeColors() {
console.log('[Mustache] === Getting theme colors ===');
// Récupérer les règles TextMate existantes (qui contiennent les couleurs du thème)
const editorConfig = vscode.workspace.getConfiguration('editor');
const tokenCustomizations = editorConfig.inspect('tokenColorCustomizations');
// Inspecter toutes les sources de configuration
console.log('[Mustache] Token customizations sources:', {
hasDefaultValue: !!tokenCustomizations?.defaultValue,
hasGlobalValue: !!tokenCustomizations?.globalValue,
hasWorkspaceValue: !!tokenCustomizations?.workspaceValue,
hasWorkspaceFolderValue: !!tokenCustomizations?.workspaceFolderValue
});
// Récupérer toutes les règles (en donnant priorité aux différentes sources)
const allRules = [
...(tokenCustomizations?.workspaceFolderValue?.textMateRules || []),
...(tokenCustomizations?.workspaceValue?.textMateRules || []),
...(tokenCustomizations?.globalValue?.textMateRules || []),
...(tokenCustomizations?.defaultValue?.textMateRules || [])
];
console.log('[Mustache] Total rules found:', allRules.length);
// Chercher les couleurs pour des scopes similaires (en excluant mustache)
const findColorForScope = (scopePatterns) => {
for (const pattern of scopePatterns) {
const rule = allRules.find(r =>
r.scope &&
!r.scope.includes('mustache') &&
(typeof r.scope === 'string' ? r.scope.includes(pattern) :
Array.isArray(r.scope) && r.scope.some(s => s.includes(pattern)))
);
if (rule?.settings?.foreground) {
console.log(`[Mustache] Found color for pattern '${pattern}':`, rule.settings.foreground, 'in scope:', rule.scope);
return rule.settings.foreground;
}
}
return null;
};
// Récupérer le type de thème pour les couleurs par défaut
const colorTheme = vscode.window.activeColorTheme;
const isDark = colorTheme.kind === vscode.ColorThemeKind.Dark ||
colorTheme.kind === vscode.ColorThemeKind.HighContrastDark;
console.log('[Mustache] Theme kind:', ['Light', 'Dark', 'HighContrast', 'HighContrastLight'][colorTheme.kind - 1]);
// Chercher les couleurs dans le thème actif
const keywordColor = findColorForScope(['keyword.control', 'keyword.operator', 'keyword']) ||
(isDark ? '#C586C0' : '#AF00DB');
const variableColor = findColorForScope(['variable.other', 'variable.parameter', 'variable']) ||
(isDark ? '#9CDCFE' : '#001080');
const punctuationColor = findColorForScope(['punctuation.definition', 'punctuation.bracket', 'punctuation']) ||
(isDark ? '#D4D4D4' : '#000000');
const embeddedColor = findColorForScope(['meta.embedded', 'string.quoted', 'entity.name.type']) ||
(isDark ? '#4EC9B0' : '#098658');
const colors = {
keywordColor,
variableColor,
punctuationColor,
embeddedColor
};
console.log('[Mustache] Final theme colors:', colors);
return colors;
} // Fonction pour appliquer les couleurs
function applyColors() {
console.log('[Mustache] === Applying colors ===');
const config = vscode.workspace.getConfiguration('mustacheInline');
const themeColors = getThemeColors();
// Récupérer les couleurs depuis la configuration ou utiliser celles du thème
const configBrackets = config.get('colors.brackets');
const configKeywords = config.get('colors.keywords');
const configVariables = config.get('colors.variables');
const configEmbedded = config.get('colors.embedded');
console.log('[Mustache] Config values:', {
brackets: configBrackets,
keywords: configKeywords,
variables: configVariables,
embedded: configEmbedded
});
const bracketsColor = configBrackets || themeColors.punctuationColor;
const keywordsColor = configKeywords || themeColors.keywordColor;
const variablesColor = configVariables || themeColors.variableColor;
const embeddedColor = configEmbedded || themeColors.embeddedColor;
const bracketsFontStyle = config.get('fontStyle.brackets', 'bold');
const keywordsFontStyle = config.get('fontStyle.keywords', 'bold');
console.log('[Mustache] Final colors to apply:', {
brackets: bracketsColor,
keywords: keywordsColor,
variables: variablesColor,
embedded: embeddedColor
});
// Obtenir la configuration actuelle de tokenColorCustomizations
const editorConfig = vscode.workspace.getConfiguration('editor');
const currentCustomizations = editorConfig.get('tokenColorCustomizations') || {};
const currentRules = currentCustomizations.textMateRules || [];
// Filtrer les règles existantes pour retirer celles de Mustache
const filteredRules = currentRules.filter(rule =>
!rule.scope || !rule.scope.includes('mustache')
);
// Ajouter les nouvelles règles Mustache
const mustacheRules = [
{
scope: 'punctuation.definition.tag.begin.mustache',
settings: {
foreground: bracketsColor,
fontStyle: bracketsFontStyle
}
},
{
scope: 'punctuation.definition.tag.end.mustache',
settings: {
foreground: bracketsColor,
fontStyle: bracketsFontStyle
}
},
{
scope: 'keyword.control.mustache',
settings: {
foreground: keywordsColor,
fontStyle: keywordsFontStyle
}
},
{
scope: 'variable.other.mustache',
settings: {
foreground: variablesColor
}
},
{
scope: 'meta.embedded.inline.mustache',
settings: {
foreground: embeddedColor
}
}
];
// Combiner les règles
const newRules = [...filteredRules, ...mustacheRules];
// Mettre à jour la configuration
const newCustomizations = {
...currentCustomizations,
textMateRules: newRules
};
// Nettoyer d'abord la configuration workspace pour éviter les conflits
editorConfig.update('tokenColorCustomizations', undefined, vscode.ConfigurationTarget.Workspace);
// Appliquer dans la configuration utilisateur (Global)
editorConfig.update('tokenColorCustomizations', newCustomizations, vscode.ConfigurationTarget.Global)
.then(() => {
console.log('[Mustache] Colors applied successfully!');
console.log('[Mustache] Total TextMate rules:', newRules.length);
console.log('[Mustache] Mustache rules:', mustacheRules.length);
}, (error) => {
console.error('[Mustache] Error applying colors:', error);
});
}
// Appliquer les couleurs au démarrage
applyColors();
// Écouter les changements de configuration
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('mustacheInline')) {
console.log('[Mustache] Config changed: mustacheInline');
applyColors();
}
if (e.affectsConfiguration('workbench.colorTheme')) {
console.log('[Mustache] Config changed: workbench.colorTheme');
applyColors();
}
})
);
// Écouter les changements de thème de couleur
context.subscriptions.push(
vscode.window.onDidChangeActiveColorTheme((theme) => {
console.log('[Mustache] Color theme changed:', theme.kind);
applyColors();
})
);
// Commande pour réappliquer les couleurs manuellement
let disposable = vscode.commands.registerCommand('mustacheInline.applyColors', applyColors);
context.subscriptions.push(disposable);
}
function deactivate() {
// Nettoyer les règles Mustache lors de la désinstallation
const editorConfig = vscode.workspace.getConfiguration('editor');
const currentCustomizations = editorConfig.get('tokenColorCustomizations') || {};
const currentRules = currentCustomizations.textMateRules || [];
// Filtrer pour retirer toutes les règles Mustache
const filteredRules = currentRules.filter(rule =>
!rule.scope || !rule.scope.includes('mustache')
);
// Mettre à jour la configuration sans les règles Mustache
const newCustomizations = {
...currentCustomizations,
textMateRules: filteredRules
};
// Nettoyer dans la configuration utilisateur (Global)
editorConfig.update('tokenColorCustomizations', newCustomizations, vscode.ConfigurationTarget.Global);
// Nettoyer aussi dans la configuration workspace si elle existe
editorConfig.update('tokenColorCustomizations', newCustomizations, vscode.ConfigurationTarget.Workspace);
// Supprimer les configurations personnalisées de mustacheInline
const mustacheConfig = vscode.workspace.getConfiguration('mustacheInline');
const configKeys = [
'colors.brackets',
'colors.keywords',
'colors.variables',
'colors.embedded',
'fontStyle.brackets',
'fontStyle.keywords'
];
// Supprimer chaque clé de configuration aux deux niveaux
configKeys.forEach(key => {
mustacheConfig.update(key, undefined, vscode.ConfigurationTarget.Global);
mustacheConfig.update(key, undefined, vscode.ConfigurationTarget.Workspace);
});
}
module.exports = {
activate,
deactivate
};