From bcbf05ab96e3e9007ae14c200318f7f40411a2d9 Mon Sep 17 00:00:00 2001 From: d2c7727d4668 Date: Mon, 8 Jul 2024 21:42:43 +0800 Subject: [PATCH] Add auto refactor when moving files in workspace mode --- src/extension.ts | 5 +++++ src/parser.ts | 9 ++------- src/refactor.ts | 25 +++++++++++++++++++++++++ src/utils.ts | 7 +++++++ 4 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/refactor.ts diff --git a/src/extension.ts b/src/extension.ts index 1a735f8..b6d8cba 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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; @@ -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({ diff --git a/src/parser.ts b/src/parser.ts index a9734f1..e1aae2c 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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 diff --git a/src/refactor.ts b/src/refactor.ts new file mode 100644 index 0000000..3011715 --- /dev/null +++ b/src/refactor.ts @@ -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, + ); + }); +}; diff --git a/src/utils.ts b/src/utils.ts index 012078a..335756c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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'); @@ -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;