Skip to content

Commit

Permalink
Add auto refactor when moving files in workspace mode
Browse files Browse the repository at this point in the history
  • Loading branch information
d2c7727d4668 committed Jul 8, 2024
1 parent a399289 commit bcbf05a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import JudgeViewProvider from './webview/JudgeView';
import { getRetainWebviewContextPref } from './preferences';
import TelemetryReporter from '@vscode/extension-telemetry';
import config from './config';
import { editorRename } from './refactor';

let judgeViewProvider: JudgeViewProvider;

Expand Down Expand Up @@ -95,6 +96,10 @@ export function activate(context: vscode.ExtensionContext) {
editorChanged(e);
});

vscode.workspace.onDidRenameFiles((e) => {
editorRename(e);
});

vscode.window.onDidChangeVisibleTextEditors((editors) => {
if (editors.length === 0) {
getJudgeViewProvider().extensionToJudgeViewMessage({
Expand Down
9 changes: 2 additions & 7 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import path from 'path';
import fs from 'fs';
import { Problem } from './types';
import { getSaveLocationPref, getWorkspaceModePref } from './preferences';
import { getSaveLocationPref } from './preferences';
import crypto from 'crypto';
import { workspace } from 'vscode';

const getWorkspaceRoot = () =>
getWorkspaceModePref()
? workspace.workspaceFolders?.[0].uri.fsPath
: undefined;
import { getWorkspaceRoot } from './utils';

/**
* Get the location (file path) to save the generated problem file in. If save
Expand Down
25 changes: 25 additions & 0 deletions src/refactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as vscode from 'vscode';
import { getWorkspaceRoot } from './utils';
import { getProblem, 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);
if (!problem) return;
problem.srcPath = file.newUri.fsPath;

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

console.log(
'Renamed problem:',
file.oldUri.fsPath,
'->',
file.newUri.fsPath,
);
});
};
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import {
getJsCommand,
getGoCommand,
getHaskellCommand,
getWorkspaceModePref,
} from './preferences';
import { Language, Problem } from './types';
import telmetry from './telmetry';
import { workspace } from 'vscode';

const oc = vscode.window.createOutputChannel('cph');

Expand Down Expand Up @@ -198,3 +200,8 @@ export const getProblemForDocument = (
const problem: Problem = JSON.parse(readFileSync(probPath).toString());
return problem;
};

export const getWorkspaceRoot = () =>
getWorkspaceModePref()
? workspace.workspaceFolders?.[0].uri.fsPath
: undefined;

0 comments on commit bcbf05a

Please sign in to comment.