Skip to content

Commit

Permalink
Implement a new viewer for decompiled code based on virtual documents
Browse files Browse the repository at this point in the history
- Replace the temporary files opened in normal text editors with virtual
  documents
- Introduce a setting for the default output language of decompilation,
  which is overridable for single documents through editor toolbar.
  There is no need to open both C# and IL.
  • Loading branch information
Rpinski committed Sep 13, 2021
1 parent 0e37c3b commit 3c1a220
Show file tree
Hide file tree
Showing 17 changed files with 378 additions and 182 deletions.
34 changes: 33 additions & 1 deletion vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
],
"main": "./out/extension.js",
"contributes": {
"configuration": [
{
"title": "ILSpy",
"properties": {
"ilspy.defaultOutputLanguage": {
"description": "Defines to which language ILSpy will decompile assemblies by default. Note that you can choose a different language for a specific view later.",
"type": "string",
"default": "C#",
"enum": [
"C#",
"IL"
],
"enumDescriptions": [
"Decompile to C# by default",
"Decompile to readable IL instructions by default"
]
}
}
}
],
"views": {
"explorer": [
{
Expand Down Expand Up @@ -56,6 +76,11 @@
"command": "ilspy.unloadAssembly",
"title": "Unload Assembly",
"category": "ILSpy"
},
{
"command": "ilspy.selectOutputLanguage",
"title": "Output language",
"category": "ILSpy"
}
],
"menus": {
Expand All @@ -66,6 +91,13 @@
"group": "1_GeneralCommands@1"
}
],
"editor/title": [
{
"command": "ilspy.selectOutputLanguage",
"group": "navigation",
"when": "resourceScheme == ilspy"
}
],
"commandPalette": [
{
"command": "ilspy.decompileAssemblyInWorkspace",
Expand Down Expand Up @@ -112,4 +144,4 @@
"extensionDependencies": [
"ms-dotnettools.vscode-dotnet-runtime"
]
}
}
6 changes: 2 additions & 4 deletions vscode-extension/src/commands/decompileAssemblyInWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

import * as vscode from "vscode";
import * as path from "path";
import {
DecompiledTreeProvider,
MemberNode,
} from "../decompiler/DecompiledTreeProvider";
import { DecompiledTreeProvider } from "../decompiler/DecompiledTreeProvider";
import { addAssemblyToTree } from "./utils";
import { MemberNode } from "../decompiler/MemberNode";

export function registerDecompileAssemblyInWorkspace(
decompiledTreeProvider: DecompiledTreeProvider,
Expand Down
6 changes: 2 additions & 4 deletions vscode-extension/src/commands/decompileAssemblyViaDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from "vscode";
import {
DecompiledTreeProvider,
MemberNode,
} from "../decompiler/DecompiledTreeProvider";
import { DecompiledTreeProvider } from "../decompiler/DecompiledTreeProvider";
import { MemberNode } from "../decompiler/MemberNode";
import { addAssemblyFromFilePath } from "./utils";

export function registerDecompileAssemblyViaDialog(
Expand Down
56 changes: 56 additions & 0 deletions vscode-extension/src/commands/selectOutputLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*------------------------------------------------------------------------------------------------
* Copyright (c) 2021 ICSharpCode
* Licensed under the MIT License. See LICENSE.TXT in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import * as vscode from "vscode";
import { DecompilerTextDocumentContentProvider } from "../decompiler/DecompilerTextDocumentContentProvider";
import { languageInfos } from "../decompiler/languageInfos";
import { ILSPY_URI_SCHEME } from "../decompiler/memberNodeUri";
import { getDefaultOutputLanguage } from "../decompiler/settings";
import { LanguageName } from "../protocol/DecompileResponse";

type OutputLanguageQuickPickItem = vscode.QuickPickItem & {
languageName: string;
};

export function registerSelectOutputLanguage(
contentProvider: DecompilerTextDocumentContentProvider
) {
return vscode.commands.registerCommand(
"ilspy.selectOutputLanguage",
async () => {
let document = vscode.window.activeTextEditor?.document;
if (document?.uri.scheme !== ILSPY_URI_SCHEME) {
return;
}

const language = await vscode.window.showQuickPick(
Object.entries(languageInfos).map((languageInfoEntry) => {
const [languageName, languageInfo] = languageInfoEntry;
return {
label: languageInfo.displayName,
description:
getDefaultOutputLanguage() === languageName
? "default"
: undefined,
languageName,
} as OutputLanguageQuickPickItem;
}),
{
title: "Please select language of decompiled code output",
}
);
if (language) {
contentProvider.setDocumentOutputLanguage(
document.uri,
language.languageName as LanguageName
);
vscode.languages.setTextDocumentLanguage(
document,
languageInfos[language.languageName].vsLanguageMode
);
}
}
);
}
35 changes: 35 additions & 0 deletions vscode-extension/src/commands/showCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*------------------------------------------------------------------------------------------------
* Copyright (c) 2021 ICSharpCode
* Licensed under the MIT License. See LICENSE.TXT in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import * as vscode from "vscode";
import { DecompiledTreeProvider } from "../decompiler/DecompiledTreeProvider";
import { memberNodeToUri } from "../decompiler/memberNodeUri";
import { MemberNode } from "../decompiler/MemberNode";
import { languageInfos } from "../decompiler/languageInfos";
import { getDefaultOutputLanguage } from "../decompiler/settings";

let lastSelectedNode: MemberNode | undefined = undefined;

export function registerShowCode(
decompiledTreeProvider: DecompiledTreeProvider
) {
return vscode.commands.registerCommand(
"showCode",
async (node: MemberNode) => {
if (lastSelectedNode === node) {
return;
}

lastSelectedNode = node;

let doc = await vscode.workspace.openTextDocument(memberNodeToUri(node));
vscode.languages.setTextDocumentLanguage(
doc,
languageInfos[getDefaultOutputLanguage()].vsLanguageMode
);
await vscode.window.showTextDocument(doc, { preview: true });
}
);
}
80 changes: 0 additions & 80 deletions vscode-extension/src/commands/showDecompiledCode.ts

This file was deleted.

6 changes: 2 additions & 4 deletions vscode-extension/src/commands/unloadAssembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from "vscode";
import {
DecompiledTreeProvider,
MemberNode,
} from "../decompiler/DecompiledTreeProvider";
import { DecompiledTreeProvider } from "../decompiler/DecompiledTreeProvider";
import { MemberNode } from "../decompiler/MemberNode";

export function registerUnloadAssembly(
decompiledTreeProvider: DecompiledTreeProvider
Expand Down
6 changes: 2 additions & 4 deletions vscode-extension/src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

import * as vscode from "vscode";
import * as fs from "fs";
import {
DecompiledTreeProvider,
MemberNode,
} from "../decompiler/DecompiledTreeProvider";
import { DecompiledTreeProvider } from "../decompiler/DecompiledTreeProvider";
import { MemberNode } from "../decompiler/MemberNode";

export function addAssemblyFromFilePath(
filePath: string,
Expand Down
Loading

0 comments on commit 3c1a220

Please sign in to comment.