extension.ts 11 KB

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