Skip to content

Commit

Permalink
Fix problem path error
Browse files Browse the repository at this point in the history
  • Loading branch information
d2c7727d4668 committed Jul 8, 2024
1 parent bcbf05a commit 4320739
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
21 changes: 21 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ export const getProblem = (srcPath: string): Problem | null => {
}
};

/** Get the problem from the problem path instead of the source path */
export const getProblemFromProbPath = (probPath: string): Problem | null => {
let problem: string;
try {
problem = fs.readFileSync(probPath).toString();
} catch (e) {
return null;
}
const parsedProblem = JSON.parse(problem);
const workspaceRoot = getWorkspaceRoot();

if (parsedProblem == null) return null;
if (workspaceRoot) {
parsedProblem.srcPath = path.resolve(
workspaceRoot,
parsedProblem.srcPath,
);
}
return parsedProblem;
};

/** Save the problem (metadata) */
export const saveProblem = (srcPath: string, problem: Problem) => {
const srcFolder = path.dirname(srcPath);
Expand Down
12 changes: 8 additions & 4 deletions src/refactor.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import * as vscode from 'vscode';
import { getWorkspaceRoot } from './utils';
import { getProblem, getProbSaveLocation, saveProblem } from './parser';
import {
getProblemFromProbPath,
getProbSaveLocation,
saveProblem,
} from './parser';
import fs from 'fs';

export const editorRename = (e: vscode.FileRenameEvent) => {
const workspaceRoot = getWorkspaceRoot();
if (!workspaceRoot) return;
e.files.forEach((file) => {
const problem = getProblem(file.oldUri.fsPath);
const oldProblemPath = getProbSaveLocation(file.oldUri.fsPath);
const problem = getProblemFromProbPath(oldProblemPath);
if (!problem) return;
problem.srcPath = file.newUri.fsPath;

saveProblem(file.newUri.fsPath, problem);
const oldProblem = getProbSaveLocation(file.oldUri.fsPath);
fs.unlinkSync(oldProblem);
fs.unlinkSync(oldProblemPath);

console.log(
'Renamed problem:',
Expand Down
10 changes: 3 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { spawn } from 'child_process';
import { existsSync, readFileSync } from 'fs';
import { platform } from 'os';
import path from 'path';
import * as vscode from 'vscode';

import config from './config';
import { getProbSaveLocation } from './parser';
import { getProblemFromProbPath, getProbSaveLocation } from './parser';
import {
getCArgsPref,
getCppArgsPref,
Expand Down Expand Up @@ -194,11 +193,8 @@ export const getProblemForDocument = (

const srcPath = document.fileName;
const probPath = getProbSaveLocation(srcPath);
if (!existsSync(probPath)) {
return undefined;
}
const problem: Problem = JSON.parse(readFileSync(probPath).toString());
return problem;
const problem = getProblemFromProbPath(probPath);
return problem || undefined;
};

export const getWorkspaceRoot = () =>
Expand Down
12 changes: 3 additions & 9 deletions src/webview/editorChange.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as vscode from 'vscode';
import { getProbSaveLocation } from '../parser';
import { existsSync, readFileSync } from 'fs';
import { Problem } from '../types';
import { getProblemFromProbPath, getProbSaveLocation } from '../parser';
import { getJudgeViewProvider } from '../extension';
import { getProblemForDocument } from '../utils';
import { getAutoShowJudgePref } from '../preferences';
Expand Down Expand Up @@ -61,12 +59,8 @@ export const editorClosed = (e: vscode.TextDocument) => {
console.log('Closed editor:', e.uri.fsPath);
const srcPath = e.uri.fsPath;
const probPath = getProbSaveLocation(srcPath);

if (!existsSync(probPath)) {
return;
}

const problem: Problem = JSON.parse(readFileSync(probPath).toString());
const problem = getProblemFromProbPath(probPath);
if (!problem) return;

if (getJudgeViewProvider().problemPath === problem.srcPath) {
getJudgeViewProvider().extensionToJudgeViewMessage({
Expand Down

0 comments on commit 4320739

Please sign in to comment.