Skip to content

Commit

Permalink
Merge pull request #72 from SAP/Done.isInTheia
Browse files Browse the repository at this point in the history
isInTheia - mapping vscode commands to theia commands [Revert this ch…
  • Loading branch information
rimasirich authored Dec 31, 2019
2 parents c48dcb0 + 310a0d9 commit 98d18b2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
6 changes: 3 additions & 3 deletions backend/.nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"report-dir": "./reports/coverage",
"check-coverage": true,
"branches": 28,
"lines": 44,
"functions": 32,
"statements": 44
"lines": 42,
"functions": 30,
"statements": 42
}
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "Apache 2.0",
"description": "Provide rich user experience for Yeoman generators using VSCode extension or the browser",
"repository": "https://github.com/SAP/yeoman-ui",
"version": "0.0.15",
"version": "0.0.16",
"engines": {
"vscode": "^1.38.0"
},
Expand Down
32 changes: 22 additions & 10 deletions backend/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { YouiLog } from "./youi-log";
import { OutputChannelLog } from './output-channel-log';
import { GeneratorFilter } from './filter';
import backendMessages from "./messages";
import { Theia } from './theia';


export function activate(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -81,13 +82,15 @@ export class YeomanUIPanel {
private readonly extensionPath: string;
private disposables: vscode.Disposable[] = [];
private questionsResolutions: Map<number, any>;
private theia: Theia;

private constructor(panel: vscode.WebviewPanel, extensionPath: string) {
this.questionsResolutions = new Map();
this.panel = panel;
this.extensionPath = extensionPath;
this.rpc = new RpcExtension(this.panel.webview);
const logger: YouiLog = new OutputChannelLog();
this.theia = new Theia();

this.yeomanui = new YeomanUI(this.rpc, logger, YeomanUIPanel.genFilter);

Expand Down Expand Up @@ -121,17 +124,26 @@ export class YeomanUIPanel {
resolve(message.data);
return;
case 'vscodecommand':
const commandName = _.get(message, "commandName");
let commandParam = _.get(message, "commandParams[0]");
if (commandName === "vscode.open" || commandName === "vscode.openFolder") {
commandParam = vscode.Uri.file(commandParam);
}
vscode.commands.executeCommand(commandName, commandParam).then(success => {
console.debug(`Execution of command ${commandName} returned ${success}`);
}, failure => {
console.debug(`Execution of command ${commandName} returned ${failure}`);
this.theia.isInTheia().then((value) => {
let commandName = _.get(message, "commandName");
let commandParam = _.get(message, "commandParams[0]");
if (commandName === "vscode.open" || commandName === "vscode.openFolder") {
commandParam = vscode.Uri.file(commandParam);
}
if (value) {
const commandMappings: Map<string, string> = this.theia.getCommandMappings()
const theiaCommand = commandMappings.get(commandName);
if (theiaCommand !== undefined) {
commandName = theiaCommand;
}
}
vscode.commands.executeCommand(commandName, commandParam).then(success => {
console.debug(`Execution of command ${commandName} returned ${success}`);
}, failure => {
console.debug(`Execution of command ${commandName} returned ${failure}`);
});
return;
});
return;
}
},
null,
Expand Down
36 changes: 36 additions & 0 deletions backend/src/theia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// TODO: remove this class when those vscode commands are implemented in theia.
import * as vscode from "vscode";

export class Theia {
private isInTheiaCached: boolean;
private commandMappings: Map<string, string>;

constructor() {
this.isInTheiaCached = undefined;
this.commandMappings = new Map();
this.initCommandMappings();
}

private initCommandMappings(): void {
this.commandMappings.set("vscode.openFolder", "workspace:openWorkspace");
this.commandMappings.set("workbench.action.closeActiveEditor", "core.close.tab");
}

public getCommandMappings(): Map<string, string> {
return this.commandMappings;
}

/**
* isInTheia - just one way of finding out whether running in Theia (can/should be changed)
*/
public async isInTheia(): Promise<boolean> {
if (this.isInTheiaCached !== undefined) {
return Promise.resolve(this.isInTheiaCached);
}

return vscode.commands.getCommands(true).then((commands) => {
return this.isInTheiaCached = (commands.indexOf("change_theme") >= 0);
});
}

}

0 comments on commit 98d18b2

Please sign in to comment.