From 472a5a13af3130f35731a98b3a79839c92b42984 Mon Sep 17 00:00:00 2001 From: jakob-ed Date: Sun, 9 Feb 2020 19:29:32 +0100 Subject: [PATCH 1/2] Fix assertions, move assertion emoji checking to BE, calculate unique emoji only once and save --- .../lsp/definitions/ExampleDefinition.java | 23 +++++++++++-------- .../tools/lsp/server/LanguageServerImpl.java | 7 +++--- .../server/request/SourceCodeEvaluator.java | 6 ++--- .../custom_lsp_actions/publishDecorations.ts | 11 +-------- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/definitions/ExampleDefinition.java b/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/definitions/ExampleDefinition.java index 8dbb8063ff22..aa8c08e99c1e 100644 --- a/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/definitions/ExampleDefinition.java +++ b/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/definitions/ExampleDefinition.java @@ -23,6 +23,7 @@ public enum ProbeMode { private int exampleDefinitionLine; private int exampleDefinitionEndColumn; private String uri; + private String uniqueEmoji; private static final String[] emojis = { "🍄", @@ -112,16 +113,20 @@ public String getExampleName() { } public String getUniqueEmoji() { - try { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - byte[] encodedhash = digest.digest(getExampleName().getBytes(StandardCharsets.UTF_8)); - String hexHash = bytesToHex(encodedhash); - long decimalHash = Long.parseLong(hexHash.substring(0, 8), 16); - int emojiIndex = Math.toIntExact(decimalHash % emojis.length); - return emojis[emojiIndex]; - } catch (NoSuchAlgorithmException e) { - return ""; + if (this.uniqueEmoji == null) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] encodedhash = digest.digest(getExampleName().getBytes(StandardCharsets.UTF_8)); + String hexHash = bytesToHex(encodedhash); + long decimalHash = Long.parseLong(hexHash.substring(0, 8), 16); + int emojiIndex = Math.toIntExact(decimalHash % emojis.length); + this.uniqueEmoji = emojis[emojiIndex]; + } catch (NoSuchAlgorithmException e) { + this.uniqueEmoji = ""; + } } + + return this.uniqueEmoji; } private String bytesToHex(byte[] hash) { diff --git a/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/LanguageServerImpl.java b/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/LanguageServerImpl.java index f380458f2a0d..948a101c495f 100644 --- a/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/LanguageServerImpl.java +++ b/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/LanguageServerImpl.java @@ -344,7 +344,7 @@ private Map> getMapOfDecorationsForEvaluatedExample(Exa Position.create(probe.getLine(), probe.getStartColumn()), Position.create(probe.getLine(), probe.getEndColumn()) ), - probe.getResult() != null ? probe.getResult().toString() : "null", + example.getUniqueEmoji() + (probe.getResult() != null ? probe.getResult().toString() : "null"), Decoration.PROBE_DECORATION_TYPE ); if (existingDecorations != null) { @@ -363,7 +363,8 @@ private Map> getMapOfDecorationsForEvaluatedExample(Exa Position.create(assertion.getLine(), assertion.getStartColumn()), Position.create(assertion.getLine(), assertion.getEndColumn()) ), - Boolean.toString(assertion.isAssertionTrue()), + // \u2705 is a ✅ and \u274c is a ❌ + example.getUniqueEmoji() + (assertion.isAssertionTrue() ? "\u2705" : "\u274c"), Decoration.ASSERTION_DECORATION_TYPE ); if (existingDecorations != null) { @@ -381,7 +382,7 @@ private Map> getMapOfDecorationsForEvaluatedExample(Exa Position.create(example.getExampleDefinitionLine(), 0), Position.create(example.getExampleDefinitionLine(), example.getExampleDefinitionEndColumn()) ), - example.getExampleResult() != null ? example.getExampleResult().toString() : "null", + example.getUniqueEmoji() + (example.getExampleResult() != null ? example.getExampleResult().toString() : "null"), Decoration.EXAMPLE_DECORATION_TYPE ); if (existingDecorations != null) { diff --git a/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/request/SourceCodeEvaluator.java b/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/request/SourceCodeEvaluator.java index a08ac0a43b08..0a04d35a21d9 100644 --- a/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/request/SourceCodeEvaluator.java +++ b/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/request/SourceCodeEvaluator.java @@ -266,7 +266,7 @@ public void onReturnValue(EventContext context, VirtualFrame frame, Object resul (example.getProbeMode() == ExampleDefinition.ProbeMode.DEFAULT && explicitProbeAnnotation.trim().equals("// "))) { ProbeDefinition probe = new ProbeDefinition(sourceSection.getStartLine()); example.getProbes().add(probe); - probe.setResult(example.getUniqueEmoji() + result); + probe.setResult(result); probe.setUri(uri); probe.setStartColumn(sourceSection.getStartColumn()); probe.setEndColumn(sourceSection.getEndColumn()); @@ -284,7 +284,7 @@ public void onReturnValue(EventContext context, VirtualFrame frame, Object resul Object expectedValueObject = SourceToBabylonParser.convertExpectedValueType(expectedValue); AssertionDefinition assertion = new AssertionDefinition(sourceSection.getStartLine(), expectedValueObject); example.getAssertions().add(assertion); - assertion.setResult(example.getUniqueEmoji() + result); + assertion.setResult(result); assertion.setUri(uri); assertion.setStartColumn(sourceSection.getStartColumn()); assertion.setEndColumn(sourceSection.getEndColumn()); @@ -304,7 +304,7 @@ public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwa } catch (Exception e) { exampleResult = e.getMessage(); } - example.setExampleResult(example.getUniqueEmoji() + exampleResult); + example.setExampleResult(exampleResult); eventBindingList.forEach(EventBinding::dispose); diff --git a/vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts b/vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts index 04903c3d8c8e..dffd9cce4c19 100644 --- a/vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts +++ b/vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts @@ -87,16 +87,7 @@ export function publishDecorations({ uri, decorations }): void { }, []); const probeDecorations = decorations.filter(decoration => decoration.decorationType === PROBE_DECORATION_TYPE); - const assertionDecorations = decorations - .filter(decoration => decoration.decorationType === ASSERTION_DECORATION_TYPE) - // replace "true"/"false" strings with emojis - .map(({range, decorationText}) => { - decorationText = decorationText - .split(", ") - .map(assertionResult => assertionResult === "true" ? "\u2705" : "\u274c") - .join(", "); - return {range, decorationText}; - }); + const assertionDecorations = decorations.filter(decoration => decoration.decorationType === ASSERTION_DECORATION_TYPE); const exampleDecorations = decorations.filter(decoration => decoration.decorationType === EXAMPLE_DECORATION_TYPE); openEditor.setDecorations( From 42772a12a818832929b1d675c098d928a106d5b2 Mon Sep 17 00:00:00 2001 From: jakob-ed Date: Sun, 9 Feb 2020 19:31:12 +0100 Subject: [PATCH 2/2] Remove "custom" LSP request method prefix in favor of more appropriate "textDocument" --- .../src/org/graalvm/tools/lsp/server/types/LanguageServer.java | 2 +- vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/types/LanguageServer.java b/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/types/LanguageServer.java index 417e5937026c..1856bb5a1703 100644 --- a/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/types/LanguageServer.java +++ b/tools/src/org.graalvm.tools.lsp/src/org/graalvm/tools/lsp/server/types/LanguageServer.java @@ -417,7 +417,7 @@ public void publishDiagnostics(PublishDiagnosticsParams params) { @Override public void publishDecorations(PublishDecorationsParams params) { - sendNotification("custom/publishDecorations", params.jsonData); + sendNotification("textDocument/publishDecorations", params.jsonData); } }); } diff --git a/vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts b/vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts index dffd9cce4c19..021f0b2baa2f 100644 --- a/vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts +++ b/vscode/graalvm/src/custom_lsp_actions/publishDecorations.ts @@ -3,7 +3,7 @@ import { Position, Range, window, TextEditorDecorationType} from 'vscode'; // import NamedDisposable from '../utils/namedDisposable'; -export const PUBLISH_DECORATIONS_REQUEST: string = "custom/publishDecorations"; +export const PUBLISH_DECORATIONS_REQUEST: string = "textDocument/publishDecorations"; const PROBE_DECORATION_TYPE = "PROBE_DECORATION"; const ASSERTION_DECORATION_TYPE = "ASSERTION_DECORATION"; const EXAMPLE_DECORATION_TYPE = "EXAMPLE_DECORATION";