Skip to content

Commit

Permalink
Fix several issues (#95)
Browse files Browse the repository at this point in the history
* Fix #94 Save automatically on stress.

Typo on README

* Fix #93 Open main solution on new problem

* By default don't show any button.

Buttons can be added manually on settings.
Reenable by default when #87 lands.

* Fix #92 Edit language .json from command
  • Loading branch information
mfornet authored May 31, 2020
1 parent f2659a0 commit 27e4ccb
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://img.shields.io/github/workflow/status/mfornet/acmx/vscode-ext-test)](https://img.shields.io/github/workflow/status/mfornet/acmx/vscode-ext-test) [![Visual Studio Marketplace Installs](https://img.shields.io/visual-studio-marketplace/i/marx24.acmx)](https://marketplace.visualstudio.com/items?itemName=marx24.acmx) [![Telegram chat](https://img.shields.io/badge/telegram-chat-blue?logo=telegram)](https://t.me/acm_x) [![Version](https://img.shields.io/github/package-json/v/mfornet/acmx?color=green&logo=visual-studio-code&logoColor=blue)](https://github.com/mfornet/acmx/releases)

**acmX** is tool that empower contestants to solve competitive programming problems easily.
**acmX** is a tool that empower contestants to solve competitive programming problems easily.

### Features

Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@
},
"acmx.execution.showRunIconInEditorTitleMenu": {
"type": "boolean",
"default": true,
"default": false,
"description": "Whether to show 'Run File' icon in editor title menu.",
"scope": "resource"
},
"acmx.execution.showCompileIconInEditorTitleMenu": {
"type": "boolean",
"default": true,
"default": false,
"description": "Whether to show 'Compile File' icon in editor title menu.",
"scope": "resource"
},
"acmx.execution.showUpgradeIconInEditorTitleMenu": {
"type": "boolean",
"default": true,
"default": false,
"description": "Whether to show 'Upgrade' icon in editor title menu.",
"scope": "resource"
},
"acmx.execution.showStressIconInEditorTitleMenu": {
"type": "boolean",
"default": true,
"default": false,
"description": "Whether to show 'Upgrade' icon in editor title menu.",
"scope": "resource"
},
Expand Down Expand Up @@ -164,6 +164,10 @@
"command": "acmx.coding",
"title": "ACMX: View: Code"
},
{
"command": "acmx.editLanguage",
"title": "ACMX: Edit Language"
},
{
"command": "acmx.stress",
"title": "ACMX: Stress",
Expand Down
18 changes: 14 additions & 4 deletions src/companion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ export function startCompetitiveCompanionService() {
const data = req.body;

res.sendStatus(200);
let contestPath = newProblemFromCompanion(data);
await vscode.commands.executeCommand(
"vscode.openFolder",
vscode.Uri.file(contestPath)
let problmeInContest = newProblemFromCompanion(data);

let contestPath = problmeInContest.contestPath;
let mainSolution = problmeInContest.problemConfig.mainSolution.unwrapOr(
""
);

await vscode.commands
.executeCommand("vscode.openFolder", vscode.Uri.file(contestPath))
.then(async () => {
await vscode.commands.executeCommand(
"vscode.open",
vscode.Uri.file(mainSolution)
);
});
});

app.listen(port, (err: any) => {
Expand Down
32 changes: 26 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ConfigFile,
CHECKER_BINARY,
GENERATED_TEST_CASE,
ProblemInContest,
} from "./primitives";
import {
substituteArgWith,
Expand Down Expand Up @@ -166,6 +167,10 @@ export function globalHomePath(testPath?: string): string {
return path;
}

export function globalLanguagePath() {
return join(globalHomePath(), LANGUAGES);
}

/**
* Initialize acmx environment.
*/
Expand All @@ -175,7 +180,7 @@ export function initAcmX(testPath?: string) {
createFolder(globalHome);

// Copy default languages config
let languagesFolder = join(globalHome, LANGUAGES);
let languagesFolder = globalLanguagePath();
let languageStaticFolder = join(pathToStatic(), LANGUAGES);
// TODO: Check for each languages, and copy if don't exist.
// Rationale: If we add a new language by default, users that already have this
Expand Down Expand Up @@ -269,7 +274,7 @@ function populateMainSolution(path: string, override: boolean): string {
return copyFromTemplate(path, templatePath, override);
}

export function newArena(path: string) {
export function newArena(path: string): ConfigFile {
debug("newArena", `path: ${path}`);
createFolder(path);

Expand All @@ -286,6 +291,8 @@ export function newArena(path: string) {
}

config.dump(path);

return config;
}

export function testCasesName(path: string) {
Expand Down Expand Up @@ -403,8 +410,12 @@ function copyDefaultFilesToWorkspace(path: string) {
}
}

function newProblem(path: string, problem: Problem, isWorkspace: boolean) {
newArena(path);
function newProblem(
path: string,
problem: Problem,
isWorkspace: boolean
): ConfigFile {
let config = newArena(path);

if (isWorkspace) {
copyDefaultFilesToWorkspace(path);
Expand All @@ -421,6 +432,8 @@ function newProblem(path: string, problem: Problem, isWorkspace: boolean) {
writeSync(fd, value);
closeSync(fd);
});

return config;
}

export function newProblemFromId(
Expand Down Expand Up @@ -455,6 +468,13 @@ export function getSolutionPath() {
return path;
}

/**
* Create new problem with configuration from competitive companion.
*
* @param config Json file with all data received from competitive companion.
*
* TODO: Change type of config(any) to a class with explicit arguments.
*/
export function newProblemFromCompanion(config: any) {
let path = getSolutionPath();

Expand All @@ -472,13 +492,13 @@ export function newProblemFromCompanion(config: any) {

copyDefaultFilesToWorkspace(contestPath);

newProblem(
let problemConfig = newProblem(
problemPath,
new Problem(config.name, config.name, inputs, outputs),
false
);

return contestPath;
return new ProblemInContest(problemConfig, contestPath);
}

/**
Expand Down
87 changes: 58 additions & 29 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
testSolution,
upgradeArena,
mainSolution,
globalLanguagePath,
} from "./core";
import {
SiteDescription,
Expand Down Expand Up @@ -61,15 +62,6 @@ async function addProblem() {
"vscode.openFolder",
vscode.Uri.file(problemPath)
);

// TODO(#42): Run two commands below
// await vscode.commands.executeCommand(
// "vscode.open",
// vscode.Uri.file(mainSolution(problemPath))
// );
// vscode.window.showInformationMessage(
// `Add problem ${site}/${id} at ${path}`
// );
}

function parseNumberOfProblems(numberOfProblems: string | undefined) {
Expand Down Expand Up @@ -367,34 +359,38 @@ async function stress() {

let path = path_.unwrap();

let stressTimes: number | undefined = vscode.workspace
let _stressTimes: number | undefined = vscode.workspace
.getConfiguration("acmx.stress", null)
.get("times");

// Use default
if (stressTimes === undefined) {
stressTimes = 10;
// Default stress times is 10
let stressTimes = 10;

if (_stressTimes !== undefined) {
stressTimes = _stressTimes;
}

let result_ = stressSolution(path, stressTimes);
await vscode.window.activeTextEditor?.document.save().then(() => {
let result_ = stressSolution(path, stressTimes);

if (result_.isNone()) {
return;
}
if (result_.isNone()) {
return;
}

let result = result_.unwrap();
let result = result_.unwrap();

if (result.isOk()) {
vscode.window.showInformationMessage(
`OK. Time ${result.getMaxTime()}ms`
);
} else {
let failTestCaseId = result.getFailTestCaseId();
vscode.window.showErrorMessage(
`${verdictName(result.status)} on test ${failTestCaseId}`
);
debugTestCase(path, failTestCaseId);
}
if (result.isOk()) {
vscode.window.showInformationMessage(
`OK. Time ${result.getMaxTime()}ms`
);
} else {
let failTestCaseId = result.getFailTestCaseId();
vscode.window.showErrorMessage(
`${verdictName(result.status)} on test ${failTestCaseId}`
);
debugTestCase(path, failTestCaseId);
}
});
}

async function upgrade() {
Expand Down Expand Up @@ -521,6 +517,34 @@ async function copySubmissionToClipboard() {
vscode.window.showInformationMessage("Submission copied to clipboard!");
}

async function editLanguage() {
let languages: any[] = [];

readdirSync(globalLanguagePath())
.filter(function (testCasePath) {
return extname(testCasePath) === ".json";
})
.map(function (testCasePath) {
let name = removeExtension(testCasePath);

languages.push({
label: name,
target: testCasePath,
});
});

let selectedLanguage = await vscode.window.showQuickPick(languages, {
placeHolder: "Select language",
});

if (selectedLanguage !== undefined) {
await vscode.commands.executeCommand(
"vscode.open",
vscode.Uri.file(join(globalLanguagePath(), selectedLanguage.target))
);
}
}

async function debugTest() {
vscode.window.showInformationMessage(String.fromCharCode(65));
}
Expand Down Expand Up @@ -573,6 +597,10 @@ export function activate(context: vscode.ExtensionContext) {
"acmx.copyToClipboard",
copySubmissionToClipboard
);
let editLanguageCommand = vscode.commands.registerCommand(
"acmx.editLanguage",
editLanguage
);

let debugTestCommand = vscode.commands.registerCommand(
"acmx.debugTest",
Expand All @@ -591,6 +619,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(setCheckerCommand);
context.subscriptions.push(selectDebugTestCaseCommand);
context.subscriptions.push(copySubmissionToClipboardCommand);
context.subscriptions.push(editLanguageCommand);

context.subscriptions.push(debugTestCommand);
}
Expand Down
10 changes: 10 additions & 0 deletions src/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ export class ConfigFile {
}
}

export class ProblemInContest {
problemConfig: ConfigFile;
contestPath: string;

constructor(problemConfig: ConfigFile, contestPath: string) {
this.problemConfig = problemConfig;
this.contestPath = contestPath;
}
}

export function verdictName(verdict: Verdict) {
switch (verdict) {
case Verdict.OK:
Expand Down
12 changes: 3 additions & 9 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { spawnSync } from "child_process";
import {
Execution,
LanguageCommand,
Option,
LANGUAGES,
ATTIC,
} from "./primitives";
import { globalHomePath } from "./core";
import { Execution, LanguageCommand, Option, ATTIC } from "./primitives";
import { globalLanguagePath } from "./core";
import { join, basename } from "path";
import { readdirSync, readFileSync, existsSync } from "fs";
import { extension, substituteArgsWith, debug, writeToFileSync } from "./utils";
import { onCompilationError } from "./errors";
import md5File = require("md5-file");

function loadConfig(extension: string): LanguageCommand {
let languagesPath = join(globalHomePath(), LANGUAGES);
let languagesPath = globalLanguagePath();
let candidates: string[] = [];

let filtered = readdirSync(languagesPath).filter((file) => {
Expand Down

0 comments on commit 27e4ccb

Please sign in to comment.