Skip to content

Commit 8256abf

Browse files
committed
Color fields differently
1 parent 9f676ee commit 8256abf

10 files changed

Lines changed: 279 additions & 31 deletions

File tree

‎lib/extension.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// Import the module and reference it with the alias vscode in your code below
44
import * as Path from "path";
55
import * as FS from "fs";
6-
import { window, workspace, ExtensionContext, commands, tasks, Task, TaskExecution, ShellExecution, Uri, TaskDefinition, languages, IndentAction, Progress, ProgressLocation } from 'vscode';
7-
import {LanguageClient, LanguageClientOptions, ServerOptions, NotificationType} from "vscode-languageclient";
6+
import { window, workspace, ExtensionContext, commands, tasks, Task, TaskExecution, ShellExecution, Uri, TaskDefinition, languages, IndentAction, Progress, ProgressLocation, DecorationOptions, ThemeColor } from 'vscode';
7+
import {LanguageClient, LanguageClientOptions, ServerOptions, NotificationType, Range} from "vscode-languageclient";
8+
import * as VS from "vscode";
89

910
// If we want to profile using VisualVM, we have to run the language server using regular java, not jlink
1011
// This is intended to be used in the 'F5' debug-extension mode, where the extension is running against the actual source, not build.vsix
@@ -171,6 +172,11 @@ interface ProgressMessage {
171172
increment: number
172173
}
173174

175+
interface DecorationMessage {
176+
uri: string;
177+
fields: Range[]
178+
}
179+
174180
function createProgressListeners(client: LanguageClient) {
175181
// Create a "checking files" progress indicator
176182
let progressListener = new class {
@@ -211,7 +217,30 @@ function createProgressListeners(client: LanguageClient) {
211217
});
212218
client.onNotification(new NotificationType('java/endProgress'), () => {
213219
progressListener.endProgress();
220+
});
221+
222+
// Use custom notifications to do advanced syntax highlighting
223+
const fieldDecorationType = window.createTextEditorDecorationType({
224+
color: new ThemeColor('javaFieldColor')
214225
});
226+
client.onNotification(new NotificationType('java/setDecorations'), (event: DecorationMessage) => {
227+
if (!window.activeTextEditor) {
228+
console.log('No active text editor');
229+
return;
230+
}
231+
if (window.activeTextEditor.document.uri.toString() != event.uri) {
232+
console.log(`Decorations on ${event.uri} don't match open document ${window.activeTextEditor.document.uri}`);
233+
return;
234+
}
235+
const decorations: DecorationOptions[] = [];
236+
for (let field of event.fields) {
237+
const start = new VS.Position(field.start.line, field.start.character)
238+
const end = new VS.Position(field.end.line, field.end.character)
239+
const decoration = { range: new VS.Range(start, end) };
240+
decorations.push(decoration);
241+
}
242+
window.activeTextEditor.setDecorations(fieldDecorationType, decorations);
243+
});
215244
}
216245

217246
function platformSpecificLauncher(): string[] {
@@ -246,13 +275,16 @@ function visualVmConfig(context: ExtensionContext): ServerOptions {
246275
'-Xdebug',
247276
// '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005',
248277
'org.javacs.Main',
278+
// Exports, needed at compile and runtime for access
249279
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=javacs",
250280
"--add-exports", "jdk.compiler/com.sun.tools.javac.code=javacs",
251281
"--add-exports", "jdk.compiler/com.sun.tools.javac.comp=javacs",
252282
"--add-exports", "jdk.compiler/com.sun.tools.javac.main=javacs",
253283
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=javacs",
254284
"--add-exports", "jdk.compiler/com.sun.tools.javac.model=javacs",
255285
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=javacs",
286+
// Opens, needed at runtime for reflection
287+
"--add-opens", "jdk.compiler/com.sun.tools.javac.api=javacs",
256288
];
257289

258290
console.log(javaExecutablePath + ' ' + args.join(' '));

‎package-lock.json‎

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"license": "MIT",
1313
"engines": {
14-
"vscode": "^1.30.1"
14+
"vscode": "^1.33.0"
1515
},
1616
"categories": [
1717
"Programming Languages",
@@ -39,6 +39,17 @@
3939
"id": "java.hover"
4040
}
4141
],
42+
"colors": [
43+
{
44+
"id": "javaFieldColor",
45+
"description": "Color for java fields",
46+
"defaults": {
47+
"dark": "#9CDCFE",
48+
"light": "#001080",
49+
"highContrast": "#d4d4d4ff"
50+
}
51+
}
52+
],
4253
"grammars": [
4354
{
4455
"language": "java",

‎scripts/link_mac.sh‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ $JAVA_HOME/bin/jlink \
2525

2626
# Restore launcher
2727
echo '#!/bin/sh
28-
JLINK_VM_OPTIONS="--add-exports jdk.compiler/com.sun.tools.javac.api=javacs --add-exports jdk.compiler/com.sun.tools.javac.code=javacs --add-exports jdk.compiler/com.sun.tools.javac.comp=javacs --add-exports jdk.compiler/com.sun.tools.javac.main=javacs --add-exports jdk.compiler/com.sun.tools.javac.tree=javacs --add-exports jdk.compiler/com.sun.tools.javac.model=javacs --add-exports jdk.compiler/com.sun.tools.javac.util=javacs"
28+
JLINK_VM_OPTIONS="\
29+
--add-exports jdk.compiler/com.sun.tools.javac.api=javacs \
30+
--add-exports jdk.compiler/com.sun.tools.javac.code=javacs \
31+
--add-exports jdk.compiler/com.sun.tools.javac.comp=javacs \
32+
--add-exports jdk.compiler/com.sun.tools.javac.main=javacs \
33+
--add-exports jdk.compiler/com.sun.tools.javac.tree=javacs \
34+
--add-exports jdk.compiler/com.sun.tools.javac.model=javacs \
35+
--add-exports jdk.compiler/com.sun.tools.javac.util=javacs \
36+
--add-opens jdk.compiler/com.sun.tools.javac.api=javacs"
2937
DIR=`dirname $0`
3038
$DIR/java $JLINK_VM_OPTIONS -m javacs/org.javacs.Main $@' > dist/mac/bin/launcher

‎scripts/link_windows.sh‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,27 @@ $REAL_JAVA_HOME/bin/jlink \
3939

4040
# Restore launcher
4141
echo '#!/bin/sh
42-
JLINK_VM_OPTIONS="--add-exports jdk.compiler/com.sun.tools.javac.api=javacs --add-exports jdk.compiler/com.sun.tools.javac.code=javacs --add-exports jdk.compiler/com.sun.tools.javac.comp=javacs --add-exports jdk.compiler/com.sun.tools.javac.main=javacs --add-exports jdk.compiler/com.sun.tools.javac.tree=javacs --add-exports jdk.compiler/com.sun.tools.javac.model=javacs --add-exports jdk.compiler/com.sun.tools.javac.util=javacs"
42+
JLINK_VM_OPTIONS="\
43+
--add-exports jdk.compiler/com.sun.tools.javac.api=javacs \
44+
--add-exports jdk.compiler/com.sun.tools.javac.code=javacs \
45+
--add-exports jdk.compiler/com.sun.tools.javac.comp=javacs \
46+
--add-exports jdk.compiler/com.sun.tools.javac.main=javacs \
47+
--add-exports jdk.compiler/com.sun.tools.javac.tree=javacs \
48+
--add-exports jdk.compiler/com.sun.tools.javac.model=javacs \
49+
--add-exports jdk.compiler/com.sun.tools.javac.util=javacs \
50+
--add-opens jdk.compiler/com.sun.tools.javac.api=javacs"
4351
DIR=`dirname $0`
4452
$DIR/java $JLINK_VM_OPTIONS -m javacs/org.javacs.Main $@' > dist/windows/bin/launcher
4553

4654
echo '@echo off
47-
set JLINK_VM_OPTIONS="--add-exports jdk.compiler/com.sun.tools.javac.api=javacs --add-exports jdk.compiler/com.sun.tools.javac.code=javacs --add-exports jdk.compiler/com.sun.tools.javac.comp=javacs --add-exports jdk.compiler/com.sun.tools.javac.main=javacs --add-exports jdk.compiler/com.sun.tools.javac.tree=javacs --add-exports jdk.compiler/com.sun.tools.javac.model=javacs --add-exports jdk.compiler/com.sun.tools.javac.util=javacs"
55+
set JLINK_VM_OPTIONS="\
56+
--add-exports jdk.compiler/com.sun.tools.javac.api=javacs \
57+
--add-exports jdk.compiler/com.sun.tools.javac.code=javacs \
58+
--add-exports jdk.compiler/com.sun.tools.javac.comp=javacs \
59+
--add-exports jdk.compiler/com.sun.tools.javac.main=javacs \
60+
--add-exports jdk.compiler/com.sun.tools.javac.tree=javacs \
61+
--add-exports jdk.compiler/com.sun.tools.javac.model=javacs \
62+
--add-exports jdk.compiler/com.sun.tools.javac.util=javacs \
63+
--add-opens jdk.compiler/com.sun.tools.javac.api=javacs"
4864
set DIR=%~dp0
4965
"%DIR%\java" %JLINK_VM_OPTIONS% -m javacs/org.javacs.Main %*' > dist/windows/bin/launcher.bat
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.javacs;
2+
3+
import java.net.URI;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import org.javacs.lsp.Range;
7+
8+
public class DecorationParams {
9+
public URI uri;
10+
public List<Range> fields = new ArrayList<>();
11+
}

‎src/main/java/org/javacs/JavaLanguageServer.java‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,16 @@ private void updateCachedParse(URI file) {
855855
cacheParse = compiler.parseFile(file);
856856
cacheParseFile = file;
857857
cacheParseVersion = FileStore.version(file);
858+
createFieldDecorations();
859+
}
860+
861+
private void createFieldDecorations() {
862+
var decorations = new DecorationParams();
863+
decorations.uri = cacheParseFile;
864+
for (var f : cacheParse.fieldReferences()) {
865+
cacheParse.range(f).ifPresent(decorations.fields::add);
866+
}
867+
client.customNotification("java/setDecorations", gson.toJsonTree(decorations));
858868
}
859869

860870
@Override
@@ -1308,7 +1318,7 @@ private FoldingRange asFoldingRange(TreePath t, String kind) {
13081318
}
13091319

13101320
// Convert offset to 0-based line and character
1311-
var startLine = (int) lines.getLineNumber(start) - 1;
1321+
var startLine = (int) lines.getLineNumber(start) - 1; // TODO (int) is not coloring
13121322
var startChar = (int) lines.getColumnNumber(start) - 1;
13131323
var endLine = (int) lines.getLineNumber(end) - 1;
13141324
var endChar = (int) lines.getColumnNumber(end) - 1;
@@ -1336,9 +1346,12 @@ public WorkspaceEdit rename(RenameParams params) {
13361346
@Override
13371347
public void didOpenTextDocument(DidOpenTextDocumentParams params) {
13381348
FileStore.open(params);
1339-
recentlyOpened.add(params.textDocument.uri); // Lint this document later
1340-
updateCachedParse(
1341-
params.textDocument.uri); // So that subsequent documentSymbol and codeLens requests will be faster
1349+
if (FileStore.isJavaFile(params.textDocument.uri)) {
1350+
// Lint this document later
1351+
recentlyOpened.add(params.textDocument.uri);
1352+
// So that subsequent documentSymbol and codeLens requests will be faster
1353+
updateCachedParse(params.textDocument.uri);
1354+
}
13421355
}
13431356

13441357
@Override
@@ -1361,6 +1374,8 @@ public void didSaveTextDocument(DidSaveTextDocumentParams params) {
13611374
if (FileStore.isJavaFile(params.textDocument.uri)) {
13621375
// Re-lint all active documents
13631376
reportErrors(FileStore.activeDocuments());
1377+
// Re-label all fields in saved file
1378+
updateCachedParse(params.textDocument.uri);
13641379
}
13651380
}
13661381

0 commit comments

Comments
 (0)