forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextMateUtils.java
More file actions
84 lines (76 loc) · 3.03 KB
/
Copy pathTextMateUtils.java
File metadata and controls
84 lines (76 loc) · 3.03 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.utils;
import java.util.Map;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.tm4e.core.grammar.IGrammar;
import org.eclipse.tm4e.registry.IGrammarRegistryManager;
import org.eclipse.tm4e.registry.TMEclipseRegistryPlugin;
import org.eclipse.tm4e.ui.TMUIPlugin;
import org.eclipse.tm4e.ui.text.TMPresentationReconciler;
/**
* Utility class for TextMate related operations.
*/
public class TextMateUtils {
private static final Map<String, String> LANGUAGE_ID_TO_EXTENSION = Map.ofEntries(
Map.entry("javascript", "js"),
Map.entry("typescript", "ts"),
Map.entry("python", "py"),
Map.entry("java", "java"),
Map.entry("cpp", "cpp"),
Map.entry("csharp", "cs"),
Map.entry("html", "html"),
Map.entry("css", "css"),
Map.entry("json", "json"),
Map.entry("markdown", "md"),
Map.entry("sql", "sql"),
Map.entry("go", "go"),
Map.entry("rust", "rs"),
Map.entry("php", "php"),
Map.entry("ruby", "rb"),
Map.entry("shellscript", "sh"),
Map.entry("yaml", "yaml"),
Map.entry("xml", "xml"));
/**
* Get or create a SourceViewerConfiguration for the given language.
*/
public static SourceViewerConfiguration getConfiguration(String lang) {
TMPresentationReconciler reconciler = new TMPresentationReconciler();
IGrammarRegistryManager mgr = TMEclipseRegistryPlugin.getGrammarRegistryManager();
IGrammar grammar = null;
try {
grammar = mgr.getGrammarForFileExtension(resolveFileExtension(lang));
} catch (Throwable e) {
// getGrammarForFileExtension not exist in org.eclipse.tm4e.registry versions 0.6.5 or earlier, skip the grammar
// setting for eclipse 2023-12 or earlier.
}
reconciler.setGrammar(grammar);
try {
// getDefaultTheme with isDark parameter not exist in org.eclipse.tm4e.ui versions 0.6.5 or earlier, skip the
// theme setting for eclipse 2023-12 or earlier.
reconciler.setTheme(TMUIPlugin.getThemeManager().getDefaultTheme(UiUtils.isDarkTheme()));
} catch (Throwable e) {
reconciler.setTheme(TMUIPlugin.getThemeManager().getDefaultTheme());
}
return new SourceViewerConfiguration() {
@Override
public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
return reconciler;
}
};
}
private static String resolveFileExtension(String languageOrExtension) {
if (languageOrExtension == null) {
return "";
}
String trimmed = languageOrExtension.trim();
if (trimmed.isEmpty()) {
return trimmed;
}
String normalized = trimmed.startsWith(".") && trimmed.length() > 1 ? trimmed.substring(1) : trimmed;
String mapped = LANGUAGE_ID_TO_EXTENSION.get(normalized.toLowerCase());
return mapped != null ? mapped : normalized;
}
}