Skip to content

Commit

Permalink
Merge pull request #43 from Anti-Li/simplify
Browse files Browse the repository at this point in the history
Closes #16. Add "CostumeCompileRun" and shortcut. Simplify the logic.
  • Loading branch information
danielpinto8zz6 authored Dec 1, 2018
2 parents 6fedcc5 + e0dfb26 commit 0863559
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 75 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ If you like this extension, you can donate via **[PayPal](https://www.paypal.me/

## Features

Compile & Run C/C++ opened file directly from the command pallet or by pressing 'f6'
Compile & Run C/C++ opened file directly from the command pallet or by pressing 'f6' or 'f7'

## Requirements

* If you are on linux you must install gcc
* If you are on window you must install mingw

## How to use
Make sure you have .c or .cpp file open and press "F6", this will compile the file.
If you want to register gcc/g++ path manually you can set it under settings.
Make sure you have .c or .cpp file open.
Press "F6", this will compile and run the file using default arguments in settings.
Or press "F7", this will use the arguments you specify for the program.
If you want to register gcc/g++ path manually, you can set it under settings.
You can also set to save file before compiling.

## Extension Settings
Expand Down
80 changes: 52 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,38 @@
},
"activationEvents": [
"onCommand:extension.CompileRun",
"onCommand:extension.CustomCompileRun",
"onCommand:extension.Compile",
"onCommand:extension.Run",
"onCommand:extension.CompileWithFlags",
"onCommand:extension.RunWithArguments"
"onCommand:extension.CustomCompile",
"onCommand:extension.CustomRun"
],
"main": "./out/extension",
"contributes": {
"commands": [
{
"command": "extension.CompileRun",
"title": "CompileRun : Compile & Run"
"title": "CompileRun : Compile with default flags & Run with default arguments"
},
{
"command": "extension.CustomCompileRun",
"title": "CompileRun : Compile with custom flags & Run with custom arguments"
},
{
"command": "extension.Compile",
"title": "CompileRun : Compile"
"title": "CompileRun : Compile with default flags"
},
{
"command": "extension.Run",
"title": "CompileRun : Run"
"title": "CompileRun : Run with default arguments"
},
{
"command": "extension.CompileWithFlags",
"title": "CompileRun : Compile with flags"
"command": "extension.CustomCompile",
"title": "CompileRun : Compile with custom flags"
},
{
"command": "extension.RunWithArguments",
"title": "CompileRun : Run with arguments"
"command": "extension.CustomRun",
"title": "CompileRun : Run with custom arguments"
}
],
"menus": {
Expand All @@ -70,55 +75,67 @@
"when": "editorLangId == cpp"
},
{
"command": "extension.Compile",
"when": "editorLangId == c"
"command": "extension.CompileRun",
"when": "editorLangId == cc"
},
{
"command": "extension.Compile",
"command": "extension.CustomCompileRun",
"when": "editorLangId == cpp"
},
{
"command": "extension.Run",
"when": "editorLangId == c"
"command": "extension.CustomCompileRun",
"when": "editorLangId == cpp"
},
{
"command": "extension.Run",
"when": "editorLangId == cpp"
"command": "extension.CustomCompileRun",
"when": "editorLangId == cc"
},
{
"command": "extension.CompileWithFlags",
"command": "extension.Compile",
"when": "editorLangId == c"
},
{
"command": "extension.CompileWithFlags",
"command": "extension.Compile",
"when": "editorLangId == cpp"
},
{
"command": "extension.RunWithArguments",
"command": "extension.Compile",
"when": "editorLangId == cc"
},
{
"command": "extension.Run",
"when": "editorLangId == c"
},
{
"command": "extension.RunWithArguments",
"command": "extension.Run",
"when": "editorLangId == cpp"
},
{
"command": "extension.CompileRun",
"command": "extension.Run",
"when": "editorLangId == cc"
},
{
"command": "extension.Compile",
"when": "editorLangId == cc"
"command": "extension.CustomCompile",
"when": "editorLangId == c"
},
{
"command": "extension.Run",
"when": "editorLangId == cc"
"command": "extension.CustomCompile",
"when": "editorLangId == cpp"
},
{
"command": "extension.CompileWithFlags",
"command": "extension.CustomCompile",
"when": "editorLangId == cc"
},
{
"command": "extension.RunWithArguments",
"command": "extension.CustomRun",
"when": "editorLangId == c"
},
{
"command": "extension.CustomRun",
"when": "editorLangId == cpp"
},
{
"command": "extension.CustomRun",
"when": "editorLangId == cc"
}
]
Expand All @@ -130,6 +147,13 @@
"linux": "f6",
"key": "f6",
"command": "extension.CompileRun"
},
{
"mac": "cmd+t",
"win": "f7",
"linux": "f7",
"key": "f7",
"command": "extension.CustomCompileRun"
}
],
"configuration": {
Expand Down Expand Up @@ -194,4 +218,4 @@
"vscode": "^1.1.21"
},
"dependencies": {}
}
}
36 changes: 19 additions & 17 deletions src/CompileRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class CompileRun {
this.terminal = VSCodeUI.compileRunTerminal;
}

private async compile(file: File, doRun: boolean = false, withFlags: boolean = false) {
private async compile(file: File, withFlags: boolean, callback: (file: File) => void = null) {
if (Settings.saveBeforeCompile) {
await window.activeTextEditor.document.save();
}
Expand Down Expand Up @@ -66,7 +66,7 @@ export class CompileRun {
if (choiceForDetails === CHANGE_PATH) {
let path = await this.promptForPath();
await workspace.getConfiguration("c-cpp-compile-run", null).update(compilerSettingKey.path, path, ConfigurationTarget.Global);
this.compile(file, doRun, withFlags);
this.compile(file, withFlags, callback);
return;
}
return;
Expand Down Expand Up @@ -97,8 +97,8 @@ export class CompileRun {
if (data === 0) {
// Compiled successfully let's tell the user & execute
window.showInformationMessage("Compiled successfuly!");
if (doRun) {
this.run(file);
if (callback !== null) {
callback(file);
}
} else {
// Error compiling
Expand All @@ -107,7 +107,7 @@ export class CompileRun {
});
}

private async run(file: File, inputArgs: boolean = false) {
private async run(file: File, inputArgs: boolean) {
if (!existsSync(file.$path)) {
window.showErrorMessage(`"${file.$path}" doesn't exists!`);
return;
Expand All @@ -123,14 +123,13 @@ export class CompileRun {
}

if (Settings.runInExternalTerminal()) {
if (!this.runExternal(file, args)) {
commands.executeCommand("workbench.action.terminal.clear");
this.terminal.runExecutable(file.$executable, args, { cwd: file.$directory });
if (this.runExternal(file, args)) {
return;
}
} else {
commands.executeCommand("workbench.action.terminal.clear");
this.terminal.runExecutable(file.$executable, args, { cwd: file.$directory });
}
// Otherwise
commands.executeCommand("workbench.action.terminal.clear");
this.terminal.runExecutable(file.$executable, args, { cwd: file.$directory });
}

public async compileRun(action: Constants.Action) {
Expand All @@ -142,20 +141,23 @@ export class CompileRun {

switch (action) {
case Constants.Action.Compile:
this.compile(file);
this.compile(file, false);
break;
case Constants.Action.Run:
this.run(file);
this.run(file, false);
break;
case Constants.Action.CompileRun:
this.compile(file, true);
this.compile(file, false, (file) => this.run(file, false));
break;
case Constants.Action.CompileWithFlags:
this.compile(file, false, true);
case Constants.Action.CustomCompile:
this.compile(file, true);
break;
case Constants.Action.RunWithArguments:
case Constants.Action.CustomRun:
this.run(file, true);
break;
case Constants.Action.CustomCompileRun:
this.compile(file, true, (file) => this.run(file, true));
break;
default: return;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export namespace Constants {
Compile,
Run,
CompileRun,
CompileWithFlags,
RunWithArguments
CustomCompile,
CustomRun,
CustomCompileRun
}
}
37 changes: 12 additions & 25 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,18 @@ import { ExtensionContext, commands, window, Terminal } from "vscode";
export function activate(context: ExtensionContext) {
const compileRun = new CompileRun();

let CompileRunCommand = commands.registerCommand('extension.CompileRun', () => {
compileRun.compileRun(Constants.Action.CompileRun);
});

let CompileCommand = commands.registerCommand('extension.Compile', () => {
compileRun.compileRun(Constants.Action.Compile);
});

let RunCommand = commands.registerCommand('extension.Run', () => {
compileRun.compileRun(Constants.Action.Run);
});

let CompileWithFlagsCommand = commands.registerCommand('extension.CompileWithFlags', () => {
compileRun.compileRun(Constants.Action.CompileWithFlags);
});

let RunWithArgumentsCommand = commands.registerCommand('extension.RunWithArguments', () => {
compileRun.compileRun(Constants.Action.RunWithArguments);
});

context.subscriptions.push(CompileRunCommand);
context.subscriptions.push(CompileCommand);
context.subscriptions.push(RunCommand);
context.subscriptions.push(CompileWithFlagsCommand);
context.subscriptions.push(RunWithArgumentsCommand);
let register = (num: Constants.Action) => {
context.subscriptions.push(commands.registerCommand('extension.' + Constants.Action[num], () => {
compileRun.compileRun(num);
}));
};

register(Constants.Action.CompileRun);
register(Constants.Action.CustomCompileRun);
register(Constants.Action.Compile);
register(Constants.Action.Run);
register(Constants.Action.CustomCompile);
register(Constants.Action.CustomRun);

context.subscriptions.push(window.onDidCloseTerminal((closedTerminal: Terminal) => {
VSCodeUI.compileRunTerminal.onDidCloseTerminal(closedTerminal);
Expand Down

0 comments on commit 0863559

Please sign in to comment.