Skip to content

Commit 6dea4dd

Browse files
committed
Prompt to enable Semantic Hightlighting on startup
Signed-off-by: Fred Bricon <fbricon@gmail.com>
1 parent 6756d87 commit 6dea4dd

3 files changed

Lines changed: 59 additions & 50 deletions

File tree

README.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,18 @@ The following settings are supported:
154154
- `Hybrid`: Provides full features with better responsiveness. It starts a standard language server and a secondary syntax server. The syntax server provides syntax features until the standard server is ready. And the syntax server will be shutdown automatically after the standard server is fully ready.
155155

156156
Default launch mode is `Hybrid`. Legacy mode is `Standard`
157-
158-
New in 0.60.0:
159157
* `java.sources.organizeImports.starThreshold`: Specifies the number of imports added before a star-import declaration is used, default is 99.
160158
* `java.sources.organizeImports.staticStarThreshold`: Specifies the number of static imports added before a star-import declaration is used, default is 99.
161-
* `java.semanticHighlighting.enabled`: Enable/disable the [semantic highlighting](https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview). Defaults to `false`.
162-
`java.requirements.JDK11Warning`: Enable/disable a warning about the impending requirement of Java 11. Defaults to `true`.
159+
* `java.semanticHighlighting.enabled`: Enable/disable [Semantic Highlighting](https://github.com/redhat-developer/vscode-java/wiki/Semantic-Highlighting) for Java files. Defaults to `false`.
160+
* `java.requirements.JDK11Warning`: Enable/disable a warning about the impending requirement of Java 11. Defaults to `true`.
163161

164162
Semantic Highlighting
165163
===============
166-
[Semantic Highlighting](https://code.visualstudio.com/updates/v1_43#_typescript-semantic-highlighting) is a new feature enabled since VS Code 1.43. Color themes can now write [rules](https://code.visualstudio.com/updates/v1_44#_theme-support-for-semantic-tokens) to color semantic tokens reported by this extension. If current color theme does not provide any, you can define your own rules in user settings, e.g.
167-
```json
168-
"editor.tokenColorCustomizationsExperimental": {
169-
"variable":{
170-
"foreground": "#9CDCFE" // change color for tokens of type 'variable'
171-
},
172-
"*.static":{
173-
"fontStyle": "italic" // all tokens with modifier 'static' should be of italic style
174-
},
175-
"*.final":{
176-
"fontStyle": "bold" // all tokens with modifier 'final' should be of bold style
177-
}
178-
}
179-
```
180-
Disabled by default, as it's still experimental, it can be enabled with `"java.semanticHighlighting.enabled":true` in settings.json.
181-
More details in [Semantic Highlighting Wiki Page (Token Styling)](https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview#token-styling).
164+
[Semantic Highlighting](https://github.com/redhat-developer/vscode-java/wiki/Semantic-Highlighting) is controlled by the `java.semanticHighlighting.enabled` preference. When enabled, it fixes numerous syntax highlighting issues with the default Java Textmate grammar. However, you might experience different small issues, particularly a delay when it kicks in, as it needs to be computed by the Java Language server, when opening a new file or when typing.
165+
166+
You will be prompted to enable or disable it on startup:
167+
168+
![](https://user-images.githubusercontent.com/148698/80595049-65d2f000-8a24-11ea-8d9c-19b05b9cac15.png)
182169

183170
Troubleshooting
184171
===============

src/semanticTokenProvider.ts

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
import * as vscode from 'vscode';
22
import { Commands } from './commands';
3-
import { getJavaConfiguration } from './utils';
3+
import { getJavaConfiguration, isPreferenceOverridden } from './utils';
44

5-
export function registerSemanticTokensProvider(context: vscode.ExtensionContext) {
6-
if (!vscode.languages.registerDocumentSemanticTokensProvider) { // in case Theia doesn't support this API
7-
return;
8-
}
5+
const semanticHighlightingKey = 'java.semanticHighlighting.enabled';
96

10-
if (isSemanticHighlightingEnabled()) {
11-
getSemanticTokensLegend().then(legend => {
12-
const semanticTokensProviderDisposable = vscode.languages.registerDocumentSemanticTokensProvider({ scheme: 'file', language: 'java' }, semanticTokensProvider, legend);
13-
context.subscriptions.push(semanticTokensProviderDisposable);
14-
onceSemanticTokenEnabledChange(context, semanticTokensProviderDisposable);
15-
});
16-
} else {
17-
onceSemanticTokenEnabledChange(context, undefined);
18-
}
7+
export function registerSemanticTokensProvider(context: vscode.ExtensionContext) {
8+
if (!vscode.languages.registerDocumentSemanticTokensProvider) { // in case Theia doesn't support this API
9+
return;
10+
}
11+
if (!isPreferenceOverridden(semanticHighlightingKey)) {
12+
const enable = "Enable";
13+
const disable = "Disable";
14+
vscode.window.showInformationMessage("Enable [Semantic highlighting](https://github.com/redhat-developer/vscode-java/wiki/Semantic-Highlighting) for Java by default?", enable, disable).then(selection => {
15+
if (selection === enable) {
16+
vscode.workspace.getConfiguration().update(semanticHighlightingKey, true, vscode.ConfigurationTarget.Global);
17+
} else if (selection === disable) {
18+
vscode.workspace.getConfiguration().update(semanticHighlightingKey, false, vscode.ConfigurationTarget.Global);
19+
}
20+
});
21+
}
22+
if (isSemanticHighlightingEnabled()) {
23+
getSemanticTokensLegend().then(legend => {
24+
const semanticTokensProviderDisposable = vscode.languages.registerDocumentSemanticTokensProvider({ scheme: 'file', language: 'java' }, semanticTokensProvider, legend);
25+
context.subscriptions.push(semanticTokensProviderDisposable);
26+
onceSemanticTokenEnabledChange(context, semanticTokensProviderDisposable);
27+
});
28+
} else {
29+
onceSemanticTokenEnabledChange(context, undefined);
30+
}
1931
}
2032

2133
class SemanticTokensProvider implements vscode.DocumentSemanticTokensProvider {
@@ -39,23 +51,23 @@ async function getSemanticTokensLegend(): Promise<vscode.SemanticTokensLegend |
3951
}
4052

4153
function onceSemanticTokenEnabledChange(context: vscode.ExtensionContext, registeredDisposable?: vscode.Disposable) {
42-
const configChangeListener = vscode.workspace.onDidChangeConfiguration(e => {
43-
configChangeListener.dispose();
44-
if (e.affectsConfiguration('java.semanticHighlighting.enabled')) {
45-
if (isSemanticHighlightingEnabled()) {
46-
// turn on
47-
registerSemanticTokensProvider(context);
48-
} else if (registeredDisposable) {
49-
// turn off
50-
registeredDisposable.dispose();
51-
}
52-
onceSemanticTokenEnabledChange(context);
53-
}
54-
});
54+
const configChangeListener = vscode.workspace.onDidChangeConfiguration(e => {
55+
configChangeListener.dispose();
56+
if (e.affectsConfiguration(semanticHighlightingKey)) {
57+
if (isSemanticHighlightingEnabled()) {
58+
// turn on
59+
registerSemanticTokensProvider(context);
60+
} else if (registeredDisposable) {
61+
// turn off
62+
registeredDisposable.dispose();
63+
}
64+
onceSemanticTokenEnabledChange(context);
65+
}
66+
});
5567
}
5668

5769
function isSemanticHighlightingEnabled(): boolean {
58-
const config = getJavaConfiguration();
59-
const section = 'semanticHighlighting.enabled';
60-
return config.get(section);
70+
const config = getJavaConfiguration();
71+
const section = 'semanticHighlighting.enabled';
72+
return config.get(section);
6173
}

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ export function getJavaConfiguration(): WorkspaceConfiguration {
88
return workspace.getConfiguration('java');
99
}
1010

11+
export function isPreferenceOverridden(section: string): boolean {
12+
const config = workspace.getConfiguration();
13+
return config.inspect(section).workspaceFolderValue !== undefined ||
14+
config.inspect(section).workspaceFolderLanguageValue !== undefined ||
15+
config.inspect(section).workspaceValue !== undefined ||
16+
config.inspect(section).workspaceLanguageValue !== undefined ||
17+
config.inspect(section).globalValue !== undefined ||
18+
config.inspect(section).globalLanguageValue !== undefined;
19+
}
20+
1121
export function deleteDirectory(dir) {
1222
if (fs.existsSync(dir)) {
1323
fs.readdirSync(dir).forEach((child) => {

0 commit comments

Comments
 (0)