Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support synthetic decorations in worksheets #862

Merged
merged 2 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/decoration-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface MetalsDecorationOptions {

export interface PublishDecorationsParams {
uri: string;
isInline: boolean | undefined;
options: MetalsDecorationOptions[];
}

Expand Down
215 changes: 111 additions & 104 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ import {
import * as metalsLanguageClient from "metals-languageclient";
import { startTreeView } from "./treeview";
import * as scalaDebugger from "./scalaDebugger";
import {
DecorationTypeDidChange,
DecorationsRangesDidChange,
} from "./decoration-protocol";
import { DecorationsRangesDidChange } from "./decoration-protocol";
import { clearTimeout } from "timers";
import { increaseIndentPattern } from "./indentPattern";
import { gotoLocation, WindowLocation } from "./goToLocation";
Expand All @@ -95,14 +92,15 @@ const installJava11Action = "Install Java (JDK 11)";
let treeViews: MetalsTreeViews | undefined;
let currentClient: LanguageClient | undefined;

const worksheetDecorationType: TextEditorDecorationType =
// Inline needs to be first to be shown always first
const inlineDecorationType: TextEditorDecorationType =
window.createTextEditorDecorationType({
isWholeLine: true,
rangeBehavior: DecorationRangeBehavior.OpenClosed,
rangeBehavior: DecorationRangeBehavior.OpenOpen,
});

let decorationType: TextEditorDecorationType =
const decorationType: TextEditorDecorationType =
window.createTextEditorDecorationType({
isWholeLine: true,
rangeBehavior: DecorationRangeBehavior.OpenClosed,
});

Expand Down Expand Up @@ -650,89 +648,97 @@ function launchMetals(
context.subscriptions.push(testManager.testController);

// Handle the metals/executeClientCommand extension notification.
client.onNotification(ExecuteClientCommand.type, (params) => {
switch (params.command) {
case ClientCommands.GotoLocation: {
const location = params.arguments?.[0] as WindowLocation;
commands.executeCommand(
`metals.${ClientCommands.GotoLocation}`,
location
);
break;
}
case ClientCommands.RefreshModel:
compilationDoneEmitter.fire();
break;
case ClientCommands.OpenFolder: {
const openWindowParams = params
.arguments?.[0] as MetalsOpenWindowParams;
if (openWindowParams) {
const executeClientCommandDisposable = client.onNotification(
ExecuteClientCommand.type,
(params) => {
switch (params.command) {
case ClientCommands.GotoLocation: {
const location = params.arguments?.[0] as WindowLocation;
commands.executeCommand(
"vscode.openFolder",
Uri.parse(openWindowParams.uri),
openWindowParams.openNewWindow
`metals.${ClientCommands.GotoLocation}`,
location
);
break;
}
break;
}
case "metals-show-stacktrace": {
const html = params.arguments && params.arguments[0];
if (typeof html === "string") {
const panel = getStacktracePanel();
panel.webview.html = html;
case ClientCommands.RefreshModel:
compilationDoneEmitter.fire();
break;
case ClientCommands.OpenFolder: {
const openWindowParams = params
.arguments?.[0] as MetalsOpenWindowParams;
if (openWindowParams) {
commands.executeCommand(
"vscode.openFolder",
Uri.parse(openWindowParams.uri),
openWindowParams.openNewWindow
);
}
break;
}
break;
}
case ClientCommands.RunDoctor:
case ClientCommands.ReloadDoctor: {
const isRun = params.command === ClientCommands.RunDoctor;
const isReload = params.command === ClientCommands.ReloadDoctor;
if (isRun || (doctor && isReload)) {
case "metals-show-stacktrace": {
const html = params.arguments && params.arguments[0];
if (typeof html === "string") {
const panel = getDoctorPanel(isReload);
const panel = getStacktracePanel();
panel.webview.html = html;
}
break;
}
break;
case ClientCommands.RunDoctor:
case ClientCommands.ReloadDoctor: {
const isRun = params.command === ClientCommands.RunDoctor;
const isReload = params.command === ClientCommands.ReloadDoctor;
if (isRun || (doctor && isReload)) {
const html = params.arguments && params.arguments[0];
if (typeof html === "string") {
const panel = getDoctorPanel(isReload);
panel.webview.html = html;
}
}
break;
}
case ClientCommands.FocusDiagnostics:
commands.executeCommand(ClientCommands.FocusDiagnostics);
break;
default:
outputChannel.appendLine(`unknown command: ${params.command}`);
}
case ClientCommands.FocusDiagnostics:
commands.executeCommand(ClientCommands.FocusDiagnostics);
break;
default:
outputChannel.appendLine(`unknown command: ${params.command}`);
}

// Ignore other commands since they are less important.
});
// Ignore other commands since they are less important.
}
);

context.subscriptions.push(executeClientCommandDisposable);
// The server updates the client with a brief text message about what
// it is currently doing, for example "Compiling..".
const item = window.createStatusBarItem(StatusBarAlignment.Right, 100);
item.command = ClientCommands.ToggleLogs;
item.hide();
client.onNotification(MetalsStatus.type, (params) => {
item.text = params.text;
if (params.show) {
item.show();
} else if (params.hide) {
item.hide();
}
if (params.tooltip) {
item.tooltip = params.tooltip;
}
if (params.command) {
const command = params.command;
item.command = params.command;
commands.getCommands().then((values) => {
if (values.includes(command)) {
commands.executeCommand(command);
}
});
} else {
item.command = undefined;
const metalsStatusDisposable = client.onNotification(
MetalsStatus.type,
(params) => {
item.text = params.text;
if (params.show) {
item.show();
} else if (params.hide) {
item.hide();
}
if (params.tooltip) {
item.tooltip = params.tooltip;
}
if (params.command) {
const command = params.command;
item.command = params.command;
commands.getCommands().then((values) => {
if (values.includes(command)) {
commands.executeCommand(command);
}
});
} else {
item.command = undefined;
}
}
});
);
context.subscriptions.push(metalsStatusDisposable);

registerTextEditorCommand(`metals.run-current-file`, (editor) => {
const args: DebugDiscoveryParams = {
Expand Down Expand Up @@ -1068,39 +1074,40 @@ function launchMetals(
scalaDebugger.initialize(outputChannel).forEach((disposable) => {
context.subscriptions.push(disposable);
});
client.onNotification(DecorationTypeDidChange.type, (options) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never used and not sure if it actually should.

decorationType = window.createTextEditorDecorationType(options);
});
client.onNotification(DecorationsRangesDidChange.type, (params) => {
const editors = window.visibleTextEditors;
const path = Uri.parse(params.uri).toString();
const workheetEditors = editors.filter(
(editor) => editor.document.uri.toString() == path
);
if (workheetEditors.length > 0) {
const options = params.options.map<DecorationOptions>((o) => {
return {
range: new Range(
new Position(o.range.start.line, o.range.start.character),
new Position(o.range.end.line, o.range.end.character)
),
hoverMessage: o.hoverMessage?.value,
renderOptions: o.renderOptions,
};
});
workheetEditors.forEach((editor) => {
if (params.uri.endsWith(".worksheet.sc")) {
editor.setDecorations(worksheetDecorationType, options);
} else {
editor.setDecorations(decorationType, options);
}
});
} else {
outputChannel.appendLine(
`Ignoring decorations for non-active document '${params.uri}'.`
const decorationsRangesDidChangeDispoasable = client.onNotification(
DecorationsRangesDidChange.type,
(params) => {
const editors = window.visibleTextEditors;
const path = Uri.parse(params.uri).toString();
const workheetEditors = editors.filter(
(editor) => editor.document.uri.toString() == path
);
if (workheetEditors.length > 0) {
const options = params.options.map<DecorationOptions>((o) => {
return {
range: new Range(
new Position(o.range.start.line, o.range.start.character),
new Position(o.range.end.line, o.range.end.character)
),
hoverMessage: o.hoverMessage?.value,
renderOptions: o.renderOptions,
};
});
workheetEditors.forEach((editor) => {
if (params.isInline) {
editor.setDecorations(inlineDecorationType, options);
} else {
editor.setDecorations(decorationType, options);
}
});
} else {
outputChannel.appendLine(
`Ignoring decorations for non-active document '${params.uri}'.`
);
}
}
});
);
context.subscriptions.push(decorationsRangesDidChangeDispoasable);
},
(reason) => {
if (reason instanceof Error) {
Expand Down
36 changes: 20 additions & 16 deletions src/treeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,26 @@ export function startTreeView(
});

// Update tree nodes on server notificiations
client.onNotification(MetalsTreeViewDidChange.type, (params) => {
params.nodes.forEach((node) => {
const provider = allProviders.get(node.viewId);
if (!provider) {
return;
}
if (node.nodeUri) {
provider.items.set(node.nodeUri, node);
}
if (node.nodeUri) {
provider.didChange.fire(node.nodeUri);
} else {
provider.didChange.fire(undefined);
}
});
});
const metalsTreeViewDidChangeDispoasble = client.onNotification(
MetalsTreeViewDidChange.type,
(params) => {
params.nodes.forEach((node) => {
const provider = allProviders.get(node.viewId);
if (!provider) {
return;
}
if (node.nodeUri) {
provider.items.set(node.nodeUri, node);
}
if (node.nodeUri) {
provider.didChange.fire(node.nodeUri);
} else {
provider.didChange.fire(undefined);
}
});
}
);
context.subscriptions.push(metalsTreeViewDidChangeDispoasble);

return {
disposables: ([] as Disposable[]).concat(...disposables),
Expand Down