extension.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. 'use strict';
  2. import * as vscode from 'vscode';
  3. import { existsSync, writeFileSync, readdirSync, copyFileSync } from 'fs';
  4. import { join, extname } from 'path';
  5. import { SITES, getSite } from './conn';
  6. import { newContestFromId, testSolution, veredictName, stressSolution, upgradeArena, newProblemFromId, removeExtension, solFile, initAcmX, currentProblem, compileCode, ATTIC, SRC } from './core';
  7. import { Veredict, SiteDescription } from './types';
  8. import { startCompetitiveCompanionService } from './companion';
  9. import { hideTerminals } from './terminal';
  10. const TESTCASES = 'testcases';
  11. function quickPickSites() {
  12. let sites: any[] = [];
  13. SITES.forEach(value => {
  14. sites.push({
  15. "label" : value.name,
  16. "target" : value.name,
  17. "description" : value.description,
  18. });
  19. });
  20. return sites;
  21. }
  22. // Create a new problem
  23. async function addProblem() {
  24. let site_info = await vscode.window.showQuickPick(quickPickSites(), { placeHolder: 'Select contest site' });
  25. if (site_info === undefined){
  26. vscode.window.showErrorMessage("Site not provided.");
  27. return;
  28. }
  29. let site: SiteDescription = getSite(site_info.target);
  30. let id = await vscode.window.showInputBox({placeHolder: site.problemIdPlaceholder});
  31. if (id === undefined){
  32. vscode.window.showErrorMessage("Problem ID not provided.");
  33. return;
  34. }
  35. let path: string | undefined = vscode.workspace.getConfiguration('acmx.configuration', null).get('solutionPath');
  36. path = join(path!, site.name, 'single');
  37. let problemPath = await newProblemFromId(path, site, id);
  38. await vscode.commands.executeCommand("vscode.openFolder", vscode.Uri.file(problemPath));
  39. // TODO: 007
  40. // Just want to run two commands below
  41. // await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(solFile()));
  42. // vscode.window.showInformationMessage(`Add problem ${site}/${id} at ${path}`);
  43. }
  44. async function addContest() {
  45. let path: string | undefined = vscode.workspace.getConfiguration('acmx.configuration', null).get('solutionPath');
  46. let site_info = await vscode.window.showQuickPick(quickPickSites(), { placeHolder: 'Select contest site' });
  47. if (site_info === undefined){
  48. vscode.window.showErrorMessage("Site not provided.");
  49. return;
  50. }
  51. let site = getSite(site_info.target);
  52. let id = undefined;
  53. if (site.name === "empty"){
  54. let name= await vscode.window.showInputBox({placeHolder: site.contestIdPlaceholder});
  55. if (name === undefined){
  56. vscode.window.showErrorMessage("Name not provided.");
  57. return;
  58. }
  59. let probCountStr = await vscode.window.showInputBox({placeHolder: "Number of problems"});
  60. if (name === undefined){
  61. vscode.window.showErrorMessage("Number of problems not provided.");
  62. return;
  63. }
  64. id = name + '-' + probCountStr!;
  65. }
  66. else{
  67. id = await vscode.window.showInputBox({placeHolder: site.contestIdPlaceholder});
  68. if (id === undefined){
  69. vscode.window.showErrorMessage("Contest ID not provided.");
  70. return;
  71. }
  72. }
  73. let contestPath = await newContestFromId(path!, site, id);
  74. vscode.commands.executeCommand("vscode.openFolder", vscode.Uri.file(contestPath));
  75. }
  76. async function debugTestcase(path: string, tcId: string){
  77. // Change editor layout to show failing test
  78. await vscode.commands.executeCommand("vscode.setEditorLayout", { orientation: 0, groups: [{ groups: [{}], size: 0.5 }, { groups: [{}, {}, {}], size: 0.5 }] });
  79. let sol = join(path, solFile());
  80. let inp = join(path, TESTCASES, `${tcId}.in`);
  81. let out = join(path, TESTCASES, `${tcId}.out`);
  82. let cur = join(path, TESTCASES, `${tcId}.real`);
  83. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(sol), vscode.ViewColumn.One);
  84. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(inp), vscode.ViewColumn.Two);
  85. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(out), vscode.ViewColumn.Three);
  86. // This file might not exist!
  87. if (existsSync(cur)){
  88. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(cur), vscode.ViewColumn.Four);
  89. }
  90. }
  91. async function runSolution(){
  92. let path = currentProblem();
  93. if (path === undefined){
  94. vscode.window.showErrorMessage("No active problem");
  95. return;
  96. }
  97. let result = testSolution(path);
  98. if (result.status === Veredict.OK){
  99. vscode.window.showInformationMessage(`OK. Time ${result.maxTime!}ms`);
  100. }
  101. else if (result.status === Veredict.NO_TESTCASES){
  102. vscode.window.showErrorMessage(`No testcases.`);
  103. }
  104. else{
  105. vscode.window.showErrorMessage(`${veredictName(result.status)} on test ${result.failTcId}`);
  106. debugTestcase(path, result.failTcId!);
  107. }
  108. }
  109. async function compile(){
  110. let path = currentProblem();
  111. if (path === undefined){
  112. vscode.window.showErrorMessage("No active problem");
  113. return;
  114. }
  115. let sol = join(path, solFile());
  116. let out = join(path, ATTIC, 'sol');
  117. if (!existsSync(sol)){
  118. throw new Error("Open a coding environment first.");
  119. }
  120. // Compile solution
  121. let xresult = compileCode(sol, out);
  122. if (xresult.status !== 0){
  123. throw new Error(`Compilation Error. ${sol}`);
  124. }
  125. else{
  126. vscode.window.showInformationMessage("Compilation successfully.");
  127. }
  128. }
  129. async function openTestcase() {
  130. let path = currentProblem();
  131. if (path === undefined){
  132. vscode.window.showErrorMessage("No active problem");
  133. return;
  134. }
  135. let tcs: any[] = [];
  136. // Read testcases
  137. readdirSync(join(path, TESTCASES)).
  138. filter( function (tcpath) {
  139. return extname(tcpath) === '.in';}).
  140. map( function(tcpath) {
  141. let name = removeExtension(tcpath);
  142. tcs.push({
  143. 'label' : name,
  144. 'target' : name,
  145. });
  146. });
  147. let tc = await vscode.window.showQuickPick(tcs, { placeHolder: 'Select testcase' });
  148. if (tc !== undefined){
  149. let inp = join(path, TESTCASES, `${tc.target}.in`);
  150. let out = join(path, TESTCASES, `${tc.target}.out`);
  151. await vscode.commands.executeCommand("vscode.setEditorLayout", { orientation: 0, groups: [{}, {}]});
  152. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(inp), vscode.ViewColumn.One);
  153. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(out), vscode.ViewColumn.Two);
  154. }
  155. }
  156. async function addTestcase() {
  157. let path = currentProblem();
  158. if (path === undefined){
  159. vscode.window.showErrorMessage("No active problem");
  160. return;
  161. }
  162. let index = 0;
  163. while (existsSync(join(path, TESTCASES, `${index}.hand.in`))){
  164. index += 1;
  165. }
  166. let inp = join(path, TESTCASES, `${index}.hand.in`);
  167. let out = join(path, TESTCASES, `${index}.hand.out`);
  168. writeFileSync(inp, "");
  169. writeFileSync(out, "");
  170. await vscode.commands.executeCommand("vscode.setEditorLayout", { orientation: 0, groups: [{}, {}]});
  171. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(inp), vscode.ViewColumn.One);
  172. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(out), vscode.ViewColumn.Two);
  173. }
  174. async function coding() {
  175. hideTerminals();
  176. let path = currentProblem();
  177. if (path === undefined){
  178. vscode.window.showErrorMessage("No active problem");
  179. return;
  180. }
  181. await vscode.commands.executeCommand("vscode.setEditorLayout", { groups: [{}]});
  182. let sol = join(path, solFile());
  183. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(sol), vscode.ViewColumn.One);
  184. }
  185. async function stress(){
  186. let path = currentProblem();
  187. if (path === undefined){
  188. vscode.window.showErrorMessage("No active problem");
  189. return;
  190. }
  191. let stressTimes: number | undefined = vscode.workspace.getConfiguration('acmx.stress', null).get('times');
  192. // Use default
  193. if (stressTimes === undefined){
  194. stressTimes = 10;
  195. }
  196. let result = stressSolution(path, stressTimes);
  197. if (result.status === Veredict.OK){
  198. vscode.window.showInformationMessage(`OK. Time ${result.maxTime!}ms`);
  199. }
  200. else{
  201. vscode.window.showErrorMessage(`${veredictName(result.status)} on test ${result.failTcId}`);
  202. debugTestcase(path, result.failTcId!);
  203. }
  204. }
  205. async function upgrade(){
  206. let path = currentProblem();
  207. if (path === undefined){
  208. vscode.window.showErrorMessage("No active problem");
  209. return;
  210. }
  211. upgradeArena(path);
  212. }
  213. function fileList(dir: string): string[]{
  214. return readdirSync(dir).reduce((list: string[], file: string) => {
  215. return list.concat([file]);
  216. }, []);
  217. }
  218. async function setChecker(){
  219. let path = currentProblem();
  220. if (path === undefined){
  221. vscode.window.showErrorMessage("No active problem");
  222. return;
  223. }
  224. let all_checkers_plain = fileList(join(SRC, 'static', 'checkers'))
  225. .filter((name: string) => name !== 'testlib.h')
  226. .map((name: string) => name.slice(0, name.length - 4));
  227. let all_checkers = all_checkers_plain.map((value: string) => {
  228. return {
  229. 'label' : value,
  230. 'target' : value + '.cpp'
  231. };
  232. });
  233. let checker_info = await vscode.window.showQuickPick(all_checkers, { placeHolder: 'Select custom checker.' });
  234. if (checker_info === undefined){
  235. vscode.window.showErrorMessage("Checker not provided.");
  236. return;
  237. }
  238. let checker = checker_info.target;
  239. let checker_path = join(SRC, 'static', 'checkers', checker);
  240. let checker_dest = join(path, ATTIC, 'checker.cpp');
  241. copyFileSync(checker_path, checker_dest);
  242. }
  243. async function debugTest(){
  244. console.log("no bugs :O");
  245. }
  246. // TODO: Make all the code async.
  247. // this method is called when your extension is activated
  248. // your extension is activated the very first time the command is executed
  249. export function activate(context: vscode.ExtensionContext) {
  250. initAcmX();
  251. startCompetitiveCompanionService();
  252. let addProblemCommand = vscode.commands.registerCommand('acmx.addProblem', addProblem);
  253. let addContestCommand = vscode.commands.registerCommand('acmx.addContest', addContest);
  254. let runSolutionCommand = vscode.commands.registerCommand('acmx.runSolution', runSolution);
  255. let openTestcaseCommand = vscode.commands.registerCommand('acmx.openTestcase', openTestcase);
  256. let addTestcaseCommand = vscode.commands.registerCommand('acmx.addTestcase', addTestcase);
  257. let codingCommand = vscode.commands.registerCommand('acmx.coding', coding);
  258. let stressCommand = vscode.commands.registerCommand('acmx.stress', stress);
  259. let upgradeCommand = vscode.commands.registerCommand('acmx.upgrade', upgrade);
  260. let compileCommand = vscode.commands.registerCommand('acmx.compile', compile);
  261. let setCheckerCommand = vscode.commands.registerCommand('acmx.setChecker', setChecker);
  262. let debugTestCommand = vscode.commands.registerCommand('acmx.debugTest', debugTest);
  263. context.subscriptions.push(addProblemCommand);
  264. context.subscriptions.push(addContestCommand);
  265. context.subscriptions.push(runSolutionCommand);
  266. context.subscriptions.push(openTestcaseCommand);
  267. context.subscriptions.push(addTestcaseCommand);
  268. context.subscriptions.push(codingCommand);
  269. context.subscriptions.push(stressCommand);
  270. context.subscriptions.push(upgradeCommand);
  271. context.subscriptions.push(compileCommand);
  272. context.subscriptions.push(setCheckerCommand);
  273. context.subscriptions.push(debugTestCommand);
  274. }
  275. // this method is called when your extension is deactivated
  276. export function deactivate() {
  277. }