Skip to content

Commit

Permalink
MERGE: #21
Browse files Browse the repository at this point in the history
21: Upgrade frontend to work with Aya language server r=ice1000 a=imkiva

Changes:
- Add middleware to workaround microsoft/vscode-languageserver-node#495
- Provide light and dark colors for semantic highlight
- Upgrade some dependencies


Co-authored-by: imkiva <imkiva@islovely.icu>
  • Loading branch information
bors[bot] and imkiva authored Jun 12, 2022
2 parents 64efd4d + 0d7c85c commit 6c3fdfd
Show file tree
Hide file tree
Showing 11 changed files with 695 additions and 1,049 deletions.
508 changes: 6 additions & 502 deletions .editorconfig

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions language-configuration.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
{
"comments": {
// symbol used for single line comment. Remove this entry if your language does not support line comments
"lineComment": "--",
// symbols used for start and end a block comment. Remove this entry if your language does not support block comments
"blockComment": [ "{-", "-}" ]
},
// symbols used as brackets
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
],
// symbols that are auto closed when typing
"autoClosingPairs": [
["\"", "\""],
// ["'", "'"],
{ "open": "\"", "close": "\"", "notIn": ["string"] },
{ "open": "{-", "close": " -}", "notIn": ["string"] },
["{", "}"],
["[", "]"],
["(", ")"]
],
// symbols that can be used to surround a selection
"surroundingPairs": [
["\"", "\""],
// ["'", "'"],
["{", "}"],
["[", "]"],
["(", ")"]
Expand Down
106 changes: 102 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,104 @@
"meta.embedded.block.aya": "aya-embedded"
}
}
],
"colors": [
{
"id": "aya.color.FnCall",
"description": "Color for function calls",
"defaults": {
"light": "#005DAC",
"dark": "#79c0ff"
}
},
{
"id": "aya.color.FnDef",
"description": "Color for function definitions",
"defaults": {
"light": "#005DAC",
"dark": "#79c0ff"
}
},
{
"id": "aya.color.PrimCall",
"description": "Color for primitive calls",
"defaults": {
"light": "#005DAC",
"dark": "#79c0ff"
}
},
{
"id": "aya.color.PrimDef",
"description": "Color for primitive definitions",
"defaults": {
"light": "#005DAC",
"dark": "#79c0ff"
}
},
{
"id": "aya.color.DataCall",
"description": "Color for data calls",
"defaults": {
"light": "#218C21",
"dark": "#33d333"
}
},
{
"id": "aya.color.DataDef",
"description": "Color for data definitions",
"defaults": {
"light": "#218C21",
"dark": "#33d333"
}
},
{
"id": "aya.color.StructCall",
"description": "Color for struct calls",
"defaults": {
"light": "#218C21",
"dark": "#33d333"
}
},
{
"id": "aya.color.StructDef",
"description": "Color for struct definitions",
"defaults": {
"light": "#218C21",
"dark": "#33d333"
}
},
{
"id": "aya.color.ConCall",
"description": "Color for constructor calls",
"defaults": {
"light": "#A021EF",
"dark": "#d2a8ff"
}
},
{
"id": "aya.color.ConDef",
"description": "Color for constructor definitions",
"defaults": {
"light": "#A021EF",
"dark": "#d2a8ff"
}
},
{
"id": "aya.color.FieldCall",
"description": "Color for field calls",
"defaults": {
"light": "#A021EF",
"dark": "#d2a8ff"
}
},
{
"id": "aya.color.FieldDef",
"description": "Color for field definitions",
"defaults": {
"light": "#A021EF",
"dark": "#d2a8ff"
}
}
]
},
"scripts": {
Expand All @@ -151,17 +249,17 @@
"devDependencies": {
"@types/glob": "^7.1.4",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.1",
"@types/node": "^17.0.42",
"@types/vscode": "^1.61.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"glob": "^7.2.0",
"mocha": "^9.1.3",
"glob": "^8.0.3",
"mocha": "^10.0.0",
"typescript": "^4.4.4",
"vscode-test": "^1.6.1"
},
"dependencies": {
"vscode-languageclient": "^7.0.0"
"vscode-languageclient": "^8.0.1"
}
}
5 changes: 5 additions & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DocumentSelector } from "vscode-languageclient";

export const AYA_SELECTOR: DocumentSelector = [
{ language: 'aya', scheme: 'file' },
];
17 changes: 11 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import * as vscode from 'vscode';
import * as daemon from './server-daemon';
import * as find from './find';
import { AYA_SELECTOR } from "./constant";
import { UnicodeCompletionProvider } from "./unicode";

export async function activate(context: vscode.ExtensionContext) {
let lspLoadPath = await find.findAya();
if (lspLoadPath === null) return;

const initTasks: PromiseLike<void>[] = [];

initTasks.push(vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: "Loading Aya library",
}, async progress => {
await daemon.startDaemon(context, lspLoadPath!, progress);
await daemon.startDaemon(context, progress);
return new Promise(resolve => setTimeout(resolve, 5000));
}));

context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
AYA_SELECTOR,
new UnicodeCompletionProvider(),
"\\"
));

await Promise.all(initTasks);
}

// this method is called when your extension is deactivated
export function deactivate() { }
export function deactivate() {
}
3 changes: 1 addition & 2 deletions src/features.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode';
import { LanguageClient } from "vscode-languageclient/node";

import * as highlight from './highlight';
import * as compute from './compute-term';

Expand Down Expand Up @@ -59,7 +58,7 @@ export function setupAyaSpecialFeatures(context: vscode.ExtensionContext, client
if (!editor) return;
// VSC seems to trigger this event when the log is printed. Stop it!
if (editor.document.uri.toString() !== e.document.uri.toString()) return;
if (e.contentChanges.length == 0) return;
if (e.contentChanges.length === 0) return;
console.log("Document content change");
// Now it's safe to remove the highlight
highlight.removeHighlight(editor);
Expand Down
48 changes: 17 additions & 31 deletions src/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import * as vscode from "vscode";
import {Uri} from "vscode";
import { Uri } from "vscode";

/** @see lsp/src/main/java/org/aya/lsp/models/HighlightResult.java */
export enum Kind {
// definitions
ModuleDef,
Expand Down Expand Up @@ -33,37 +34,22 @@ export interface HighlightResult {

export type HighlightResponse = HighlightResult[];

/**
* All possible token types from Aya language server
*/
const TOKEN_TYPES = [
"namespace", // ModuleDef
"function", // FnDef, PrimDef
"enum", // DataDef
"struct", // StructDef
"property", // FieldDef, ConDef
];
const TOKEN_MODIFIERS = [
"definition", // All defs
"defaultLibrary", // PrimDef
];

const EMACS_COLORS = new Map<number, string>([
[Kind.FnCall, "#005DAC"],
[Kind.FnDef, "#005DAC"],
[Kind.PrimCall, "#005DAC"],
[Kind.PrimDef, "#005DAC"],
[Kind.DataCall, "#218C21"],
[Kind.DataDef, "#218C21"],
[Kind.StructCall, "#218C21"],
[Kind.StructDef, "#218C21"],
[Kind.ConCall, "#A021EF"],
[Kind.ConDef, "#A021EF"],
[Kind.FieldCall, "#A021EF"],
[Kind.FieldDef, "#A021EF"],
const EMACS_COLORS = new Map<number, vscode.ThemeColor>([
[Kind.FnCall, new vscode.ThemeColor("aya.color.FnCall")],
[Kind.FnDef, new vscode.ThemeColor("aya.color.FnDef")],
[Kind.PrimCall, new vscode.ThemeColor("aya.color.PrimCall")],
[Kind.PrimDef, new vscode.ThemeColor("aya.color.PrimDef")],
[Kind.DataCall, new vscode.ThemeColor("aya.color.DataCall")],
[Kind.DataDef, new vscode.ThemeColor("aya.color.DataDef")],
[Kind.StructCall, new vscode.ThemeColor("aya.color.StructCall")],
[Kind.StructDef, new vscode.ThemeColor("aya.color.StructDef")],
[Kind.ConCall, new vscode.ThemeColor("aya.color.ConCall")],
[Kind.ConDef, new vscode.ThemeColor("aya.color.ConDef")],
[Kind.FieldCall, new vscode.ThemeColor("aya.color.FieldCall")],
[Kind.FieldDef, new vscode.ThemeColor("aya.color.FieldDef")],
]);

let highlights : HighlightResponse | null = null;
let highlights: HighlightResponse | null = null;
let decorations: Array<vscode.TextEditorDecorationType> = [];

function highlightSetter(editor: vscode.TextEditor, symbol: Symbol): () => void {
Expand Down Expand Up @@ -98,6 +84,6 @@ export function highlight(editor: vscode.TextEditor) {
findHighlight(uri)?.symbols.forEach(symbol => highlightSetter(editor, symbol)());
}

function findHighlight(uri: Uri) : HighlightResult | undefined {
function findHighlight(uri: Uri): HighlightResult | undefined {
return highlights?.find((a) => Uri.parse(a.uri).toString() === uri.toString());
}
38 changes: 38 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Middleware, ResolveCodeLensSignature } from "vscode-languageclient";
import * as vscode from "vscode";

export class AyaMiddleware implements Middleware {
public resolveCodeLens(
codeLens: vscode.CodeLens,
token: vscode.CancellationToken,
next: ResolveCodeLensSignature
): vscode.ProviderResult<vscode.CodeLens> {
// see: https://github.com/microsoft/vscode-languageserver-node/issues/495
const javaResolved = next(codeLens, token);
if ((javaResolved as Thenable<vscode.CodeLens>).then)
return (javaResolved as Thenable<vscode.CodeLens>).then(fixCodeLens);
else if (javaResolved as vscode.CodeLens)
return fixCodeLens(javaResolved as vscode.CodeLens);
else return javaResolved;
}
}

function fixCodeLens(codeLens: vscode.CodeLens): vscode.CodeLens {
if (codeLens.command?.command === "editor.action.showReferences") {
const [javaUri, javaRange, usages] = codeLens.command.arguments!;
codeLens.command.arguments = [
vscode.Uri.parse(javaUri),
new vscode.Position(javaRange.line, javaRange.character),
usages.map((loc: { uri: string, range: vscode.Range }) => new vscode.Location(
vscode.Uri.parse(loc.uri.toString()),
new vscode.Range(
loc.range.start.line,
loc.range.start.character,
loc.range.end.line,
loc.range.end.character)
)),
];
}

return codeLens;
}
Loading

0 comments on commit 6c3fdfd

Please sign in to comment.