extension.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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{
  102. vscode.window.showErrorMessage(`${veredictName(result.status)} on test ${result.failTcId}`);
  103. debugTestcase(path, result.failTcId!);
  104. }
  105. }
  106. async function compile(){
  107. let path = currentProblem();
  108. if (path === undefined){
  109. vscode.window.showErrorMessage("No active problem");
  110. return;
  111. }
  112. let sol = join(path, solFile());
  113. let out = join(path, ATTIC, 'sol');
  114. if (!existsSync(sol)){
  115. throw new Error("Open a coding environment first.");
  116. }
  117. // Compile solution
  118. let xresult = compileCode(sol, out);
  119. if (xresult.status !== 0){
  120. throw new Error(`Compilation Error. ${sol}`);
  121. }
  122. else{
  123. vscode.window.showInformationMessage("Compilation successfully.");
  124. }
  125. }
  126. async function openTestcase() {
  127. let path = currentProblem();
  128. if (path === undefined){
  129. vscode.window.showErrorMessage("No active problem");
  130. return;
  131. }
  132. let tcs: any[] = [];
  133. // Read testcases
  134. readdirSync(join(path, TESTCASES)).
  135. filter( function (tcpath) {
  136. return extname(tcpath) === '.in';}).
  137. map( function(tcpath) {
  138. let name = removeExtension(tcpath);
  139. tcs.push({
  140. 'label' : name,
  141. 'target' : name,
  142. });
  143. });
  144. let tc = await vscode.window.showQuickPick(tcs, { placeHolder: 'Select testcase' });
  145. if (tc !== undefined){
  146. let inp = join(path, TESTCASES, `${tc.target}.in`);
  147. let out = join(path, TESTCASES, `${tc.target}.out`);
  148. await vscode.commands.executeCommand("vscode.setEditorLayout", { orientation: 0, groups: [{}, {}]});
  149. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(inp), vscode.ViewColumn.One);
  150. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(out), vscode.ViewColumn.Two);
  151. }
  152. }
  153. async function addTestcase() {
  154. let path = currentProblem();
  155. if (path === undefined){
  156. vscode.window.showErrorMessage("No active problem");
  157. return;
  158. }
  159. let index = 0;
  160. while (existsSync(join(path, TESTCASES, `${index}.hand.in`))){
  161. index += 1;
  162. }
  163. let inp = join(path, TESTCASES, `${index}.hand.in`);
  164. let out = join(path, TESTCASES, `${index}.hand.out`);
  165. writeFileSync(inp, "");
  166. writeFileSync(out, "");
  167. await vscode.commands.executeCommand("vscode.setEditorLayout", { orientation: 0, groups: [{}, {}]});
  168. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(inp), vscode.ViewColumn.One);
  169. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(out), vscode.ViewColumn.Two);
  170. }
  171. async function coding() {
  172. hideTerminals();
  173. let path = currentProblem();
  174. if (path === undefined){
  175. vscode.window.showErrorMessage("No active problem");
  176. return;
  177. }
  178. await vscode.commands.executeCommand("vscode.setEditorLayout", { groups: [{}]});
  179. let sol = join(path, solFile());
  180. await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(sol), vscode.ViewColumn.One);
  181. }
  182. async function stress(){
  183. let path = currentProblem();
  184. if (path === undefined){
  185. vscode.window.showErrorMessage("No active problem");
  186. return;
  187. }
  188. let stressTimes: number | undefined = vscode.workspace.getConfiguration('acmx.stress', null).get('times');
  189. // Use default
  190. if (stressTimes === undefined){
  191. stressTimes = 10;
  192. }
  193. let result = stressSolution(path, stressTimes);
  194. if (result.status === Veredict.OK){
  195. vscode.window.showInformationMessage(`OK. Time ${result.maxTime!}ms`);
  196. }
  197. else{
  198. vscode.window.showErrorMessage(`${veredictName(result.status)} on test ${result.failTcId}`);
  199. debugTestcase(path, result.failTcId!);
  200. }
  201. }
  202. async function upgrade(){
  203. let path = currentProblem();
  204. if (path === undefined){
  205. vscode.window.showErrorMessage("No active problem");
  206. return;
  207. }
  208. upgradeArena(path);
  209. }
  210. function fileList(dir: string): string[]{
  211. return readdirSync(dir).reduce((list: string[], file: string) => {
  212. return list.concat([file]);
  213. }, []);
  214. }
  215. async function setChecker(){
  216. let path = currentProblem();
  217. if (path === undefined){
  218. vscode.window.showErrorMessage("No active problem");
  219. return;
  220. }
  221. let all_checkers_plain = fileList(join(SRC, 'static', 'checkers'))
  222. .filter((name: string) => name !== 'testlib.h')
  223. .map((name: string) => name.slice(0, name.length - 4));
  224. let all_checkers = all_checkers_plain.map((value: string) => {
  225. return {
  226. 'label' : value,
  227. 'target' : value + '.cpp'
  228. };
  229. });
  230. let checker_info = await vscode.window.showQuickPick(all_checkers, { placeHolder: 'Select custom checker.' });
  231. if (checker_info === undefined){
  232. vscode.window.showErrorMessage("Checker not provided.");
  233. return;
  234. }
  235. let checker = checker_info.target;
  236. let checker_path = join(SRC, 'static', 'checkers', checker);
  237. let checker_dest = join(path, ATTIC, 'checker.cpp');
  238. copyFileSync(checker_path, checker_dest);
  239. }
  240. async function debugTest(){
  241. console.log("no bugs :O");
  242. }
  243. // TODO: Make all the code async.
  244. // this method is called when your extension is activated
  245. // your extension is activated the very first time the command is executed
  246. export function activate(context: vscode.ExtensionContext) {
  247. initAcmX();
  248. startCompetitiveCompanionService();
  249. let addProblemCommand = vscode.commands.registerCommand('acmx.addProblem', addProblem);
  250. let addContestCommand = vscode.commands.registerCommand('acmx.addContest', addContest);
  251. let runSolutionCommand = vscode.commands.registerCommand('acmx.runSolution', runSolution);
  252. let openTestcaseCommand = vscode.commands.registerCommand('acmx.openTestcase', openTestcase);
  253. let addTestcaseCommand = vscode.commands.registerCommand('acmx.addTestcase', addTestcase);
  254. let codingCommand = vscode.commands.registerCommand('acmx.coding', coding);
  255. let stressCommand = vscode.commands.registerCommand('acmx.stress', stress);
  256. let upgradeCommand = vscode.commands.registerCommand('acmx.upgrade', upgrade);
  257. let compileCommand = vscode.commands.registerCommand('acmx.compile', compile);
  258. let setCheckerCommand = vscode.commands.registerCommand('acmx.setChecker', setChecker);
  259. let debugTestCommand = vscode.commands.registerCommand('acmx.debugTest', debugTest);
  260. context.subscriptions.push(addProblemCommand);
  261. context.subscriptions.push(addContestCommand);
  262. context.subscriptions.push(runSolutionCommand);
  263. context.subscriptions.push(openTestcaseCommand);
  264. context.subscriptions.push(addTestcaseCommand);
  265. context.subscriptions.push(codingCommand);
  266. context.subscriptions.push(stressCommand);
  267. context.subscriptions.push(upgradeCommand);
  268. context.subscriptions.push(compileCommand);
  269. context.subscriptions.push(setCheckerCommand);
  270. context.subscriptions.push(debugTestCommand);
  271. }
  272. // this method is called when your extension is deactivated
  273. export function deactivate() {
  274. }