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

Add file system support for jars #923

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"onCommand:metals.new-scala-project",
"onDebugResolve:scala",
"onLanguage:scala",
"workspaceContains:build.sbt",
"workspaceContains:build.sc",
"workspaceContains:project/build.properties",
"workspaceContains:src/main/scala",
"workspaceContains:*/src/main/scala",
"workspaceContains:*/*/src/main/scala"
"!onFileSystem:jar && workspaceContains:build.sbt",
"!onFileSystem:jar && workspaceContains:build.sc",
"!onFileSystem:jar && workspaceContains:project/build.properties",
"!onFileSystem:jar && workspaceContains:src/main/scala",
"!onFileSystem:jar && workspaceContains:*/src/main/scala",
"!onFileSystem:jar && workspaceContains:*/*/src/main/scala"
],
"contributes": {
"languages": [
Expand Down
28 changes: 25 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ProviderResult,
Hover,
TextDocument,
FileSystemProvider,
} from "vscode";
import {
LanguageClient,
Expand Down Expand Up @@ -90,7 +91,7 @@ import { BuildTargetUpdate } from "./testExplorer/types";
import * as workbenchCommands from "./workbenchCommands";
import { getServerVersion } from "./getServerVersion";
import { getCoursierMirrorPath } from "./mirrors";

import MetalsFileSystemProvider from "./metalsFileSystemProvider";
const outputChannel = window.createOutputChannel("Metals");
const openSettingsAction = "Open settings";
const downloadJava = "Download Java";
Expand Down Expand Up @@ -228,7 +229,7 @@ function fetchAndLaunchMetals(
if (dottyIde.enabled) {
outputChannel.appendLine(
`Metals will not start since Dotty is enabled for this workspace. ` +
`To enable Metals, remove the file ${dottyIde.path} and run 'Reload window'`
`To enable Metals, remove the file ${dottyIde.path} and run 'Reload window'`
);
return;
}
Expand Down Expand Up @@ -441,6 +442,15 @@ function launchMetals(
);
}

function registerFileSystemProvider(
scheme: string,
provider: FileSystemProvider
) {
context.subscriptions.push(
workspace.registerFileSystemProvider(scheme, provider, { isCaseSensitive: true, isReadonly: true })
);
}

function registerTextDocumentContentProvider(
scheme: string,
provider: TextDocumentContentProvider
Expand All @@ -453,7 +463,6 @@ function launchMetals(
const metalsFileProvider = new MetalsFileProvider(client);

registerTextDocumentContentProvider("metalsDecode", metalsFileProvider);
registerTextDocumentContentProvider("jar", metalsFileProvider);

registerCommand("metals.show-cfr", async (uri: Uri) => {
await decodeAndShowFile(client, metalsFileProvider, uri, "cfr");
Expand Down Expand Up @@ -730,6 +739,19 @@ function launchMetals(
}
break;
}
case "metals-create-library-filesystem": {
const uri = params.arguments && params.arguments[0];
if (typeof uri === "string") {
const librariesURI = Uri.parse(uri)
// filesystem is persistent across VSCode sessions so may already exist
if (!workspace.getWorkspaceFolder(librariesURI))
workspace.updateWorkspaceFolders(1, 0, { uri: librariesURI, name: "Metals - Libraries" });

const metalsFileSystemProvider = new MetalsFileSystemProvider(client, outputChannel);
registerFileSystemProvider(librariesURI.scheme, metalsFileSystemProvider);
}
break;
}
case ClientCommands.FocusDiagnostics:
commands.executeCommand(ClientCommands.FocusDiagnostics);
break;
Expand Down
131 changes: 131 additions & 0 deletions src/metalsFileSystemProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//import { ServerCommands } from "metals-languageclient";
import {
EventEmitter,
//ProviderResult,
FileSystemProvider,
Uri,
Disposable,
Event,
FileChangeEvent,
FileStat,
FileType,
OutputChannel,
} from "vscode";
import { ExecuteCommandRequest } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";


export interface FSReadDirectoryResponse {
name: string,
isFile: boolean,
error?: string
}

export interface FSReadFileResponse {
value: string,
error?: string
}

export interface FSStatResponse {
isFile: boolean,
error?: string
}

export class GenericFileStat implements FileStat {
type: FileType;
ctime: number;
mtime: number;
size: number;

constructor(isFile: boolean) {
this.type = (isFile) ? FileType.File : FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
}
}

export default class MetalsFileSystemProvider implements FileSystemProvider {
readonly onDidChangeEmitter = new EventEmitter<Uri>();
readonly onDidChange = this.onDidChangeEmitter.event;
readonly client: LanguageClient;
readonly outputChannel: OutputChannel;

constructor(client: LanguageClient,
outputChannel: OutputChannel) {
this.outputChannel = outputChannel;
this.client = client;
}

private _emitter = new EventEmitter<FileChangeEvent[]>();
readonly onDidChangeFile: Event<FileChangeEvent[]> = this._emitter.event;

stat(uri: Uri): FileStat | Thenable<FileStat> {
return this.client.sendRequest(ExecuteCommandRequest.type, {
// TODO move to ServerCommands
command: "filesystem-stat",
arguments: [uri.toString(true)],
}).then((result) => {
// TODO how much info is required here?
const value = result as FSStatResponse;
return new GenericFileStat(value.isFile);
});
}

toReadDirResult(response: FSReadDirectoryResponse): [string, FileType] {
return [response.name, response.isFile ? FileType.File : FileType.Directory]
}

readDirectory(uri: Uri): [string, FileType][] | Thenable<[string, FileType][]> {
return this.client.sendRequest(ExecuteCommandRequest.type, {
// TODO move to ServerCommands
command: "filesystem-read-directory",
arguments: [uri.toString(true)],
}).then((result) => {
const value = result as FSReadDirectoryResponse[];
return value.map(this.toReadDirResult);
});
}

readFile(uri: Uri): Uint8Array | Thenable<Uint8Array> {
return this.client.sendRequest(ExecuteCommandRequest.type, {
// TODO move to ServerCommands
command: "filesystem-read-file",
arguments: [uri.toString(true)],
}).then((result) => {
const { value, error } = result as FSReadFileResponse;
let contents: string;
if (value != null) {
contents = value;
} else {
if (error)
contents = error;
else
contents = "Unknown error";
}
return Buffer.from(contents);
});
}


watch(uri: Uri, options: { recursive: boolean; excludes: string[]; }): Disposable {
this.outputChannel.appendLine(`ignoring watch ${uri} ${options}`);
throw new Error(`watch ${uri} ${options} not implemented.`);
}
createDirectory(uri: Uri): void | Thenable<void> {
this.outputChannel.appendLine(`ignoring createDirectory ${uri}`);
throw new Error(`createDirectory ${uri} not implemented.`);
}
delete(uri: Uri, options: { recursive: boolean; }): void | Thenable<void> {
this.outputChannel.appendLine(`ignoring delete ${uri} ${options}`);
throw new Error(`delete ${uri} ${options} not implemented.`);
}
rename(oldUri: Uri, newUri: Uri, options: { overwrite: boolean; }): void | Thenable<void> {
this.outputChannel.appendLine(`ignoring rename ${oldUri} ${newUri} ${options}`);
throw new Error(`rename ${oldUri} ${newUri} ${options} not implemented.`);
}
writeFile(uri: Uri, content: Uint8Array, options: { create: boolean; overwrite: boolean; }): void | Thenable<void> {
this.outputChannel.appendLine(`ignoring writeFile ${uri} ${content} ${options}`);
throw new Error(`writeFile ${uri} ${content} ${options} not implemented.`);
}
}