Skip to content

Commit

Permalink
Merge pull request #151 from olafurpg/dap
Browse files Browse the repository at this point in the history
Un-revert Debug Adapter Protocol changes
  • Loading branch information
Marek Żarnowski authored Oct 14, 2019
2 parents 77af15e + a51aa54 commit c8811b7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,16 @@
"when": "metals:enabled"
}
]
}
},
"debuggers": [
{
"type": "scala",
"label": "Scala Debugger",
"languages": [
"scala"
]
}
]
},
"main": "./out/extension",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions src/MetalsFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ import {
} from "vscode-languageclient";

export interface TreeViewProvider {}
export interface DebuggingProvider {}

export class MetalsFeatures implements StaticFeature {
treeViewProvider?: TreeViewProvider;
debuggingProvider?: DebuggingProvider;

fillInitializeParams(params: InitializeParams): void {
if (!params.capabilities.experimental) {
params.capabilities.experimental = {};
}
params.capabilities.experimental.treeViewProvider = true;
params.capabilities.experimental.debuggingProvider = true;
}
fillClientCapabilities(): void {}
initialize(capabilities: ServerCapabilities): void {
if (capabilities.experimental) {
this.treeViewProvider = capabilities.experimental.treeViewProvider;
this.debuggingProvider = capabilities.experimental.debuggingProvider;
}
}
}
3 changes: 2 additions & 1 deletion src/client-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
export const ClientCommands = {
toggleLogs: "metals-logs-toggle",
focusDiagnostics: "metals-diagnostics-focus",
runDoctor: "metals-doctor-run"
runDoctor: "metals-doctor-run",
startDebugSession: "metals-debug-session-start"
};
44 changes: 44 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
ExtensionContext,
window,
commands,
CodeLens,
CodeLensProvider,
EventEmitter,
StatusBarAlignment,
ProgressLocation,
IndentAction,
Expand All @@ -19,6 +22,7 @@ import {
Uri,
Range,
Selection,
TextDocument as VscodeTextDocument,
TextEditorRevealType,
TextEditor
} from "vscode";
Expand Down Expand Up @@ -53,6 +57,7 @@ import { getJavaOptions } from "./getJavaOptions";
import { startTreeView } from "./treeview";
import { MetalsFeatures } from "./MetalsFeatures";
import { MetalsTreeViewReveal, MetalsTreeViews } from "./tree-view-protocol";
import * as scalaDebugger from "./scalaDebugger";

const outputChannel = window.createOutputChannel("Metals");
const openSettingsAction = "Open settings";
Expand Down Expand Up @@ -329,12 +334,40 @@ function launchMetals(
client.outputChannel.show(true);
channelOpen = true;
}
},
startDebugSession: args => {
if (!features.debuggingProvider) return;

scalaDebugger.start(args).then(wasStarted => {
if (!wasStarted) {
window.showErrorMessage("Debug session not started");
}
});
}
};
Object.entries(clientCommands).forEach(([name, command]) =>
registerCommand(name, command)
);

// should be the compilation of a currently opened file
// but some race conditions may apply
let compilationDoneEmitter = new EventEmitter<void>();

let codeLensRefresher: CodeLensProvider = {
onDidChangeCodeLenses: compilationDoneEmitter.event,
provideCodeLenses: (
document: VscodeTextDocument,
token: CancellationToken
) => undefined,
resolveCodeLens: (codeLens: CodeLens, token: CancellationToken) =>
undefined
};

languages.registerCodeLensProvider(
{ scheme: "file", language: "scala" },
codeLensRefresher
);

// Handle the metals/executeClientCommand extension notification.
client.onNotification(ExecuteClientCommand.type, params => {
const isRun = params.command === "metals-doctor-run";
Expand Down Expand Up @@ -368,6 +401,9 @@ function launchMetals(
});
}
break;
case "metals-model-refresh":
compilationDoneEmitter.fire();
break;
default:
outputChannel.appendLine(`unknown command: ${params.command}`);
}
Expand Down Expand Up @@ -540,6 +576,14 @@ function launchMetals(
treeViews = startTreeView(client, outputChannel, context, viewIds);
context.subscriptions.concat(treeViews.disposables);
}
if (features.debuggingProvider) {
scalaDebugger
.initialize(outputChannel)
.forEach(disposable => context.subscriptions.push(disposable));
registerCommand(scalaDebugger.startSessionCommand, scalaDebugger.start);
} else {
outputChannel.appendLine("Debugging Scala sources is not supported");
}
});
}

Expand Down
65 changes: 65 additions & 0 deletions src/scalaDebugger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as vscode from "vscode";
import {
CancellationToken,
DebugConfiguration,
Disposable,
ProviderResult,
WorkspaceFolder
} from "vscode";

export const startAdapterCommand = "debug-adapter-start";
export const startSessionCommand = "metals-debug-session-start";
const configurationType = "scala";

export function initialize(outputChannel: vscode.OutputChannel): Disposable[] {
outputChannel.appendLine("Initializing Scala Debugger");
return [
vscode.debug.registerDebugConfigurationProvider(
configurationType,
new ScalaConfigProvider()
)
];
}

export async function start(parameters: any): Promise<Boolean> {
return vscode.commands
.executeCommand<DebugSession>(startAdapterCommand, parameters)
.then(response => {
if (response === undefined) return false;

const debugServer = vscode.Uri.parse(response.uri);
const segments = debugServer.authority.split(":");
const port = parseInt(segments[segments.length - 1]);

const configuration: vscode.DebugConfiguration = {
type: configurationType,
name: response.name,
request: "launch",
debugServer: port // note: MUST be a number. vscode magic - automatically connects to the server
};

return vscode.debug.startDebugging(undefined, configuration);
});
}

class ScalaConfigProvider implements vscode.DebugConfigurationProvider {
provideDebugConfigurations(
folder: WorkspaceFolder | undefined,
token?: CancellationToken
): ProviderResult<DebugConfiguration[]> {
return [];
}

resolveDebugConfiguration(
folder: WorkspaceFolder | undefined,
debugConfiguration: DebugConfiguration,
token?: CancellationToken
): ProviderResult<DebugConfiguration> {
return debugConfiguration;
}
}

export interface DebugSession {
name: string;
uri: string;
}

0 comments on commit c8811b7

Please sign in to comment.