From 05e676936f49522302f0d2ee363f2256c1de8485 Mon Sep 17 00:00:00 2001 From: Rejman Date: Sun, 19 Oct 2025 14:16:34 -0300 Subject: [PATCH] feat: rename file and also refactor namespace --- src/app/services/NamespaceBatchUpdater.ts | 35 ++++++++------- src/app/services/update/ClassNameUpdater.ts | 49 +++++++++++++++++++++ 2 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 src/app/services/update/ClassNameUpdater.ts diff --git a/src/app/services/NamespaceBatchUpdater.ts b/src/app/services/NamespaceBatchUpdater.ts index ca6b8f6..a83b8d7 100644 --- a/src/app/services/NamespaceBatchUpdater.ts +++ b/src/app/services/NamespaceBatchUpdater.ts @@ -1,7 +1,8 @@ import { inject, injectable } from "tsyringe"; +import { Namespace, NamespaceCreator } from '@domain/namespace/NamespaceCreator'; +import { ClassNameUpdater } from './update/ClassNameUpdater'; import { MovedFileNamespaceUpdater } from './update/MovedFileNamespaceUpdater'; import { MultiFileReferenceUpdater } from './update/MultiFileReferenceUpdater'; -import { NamespaceCreator } from '@domain/namespace/NamespaceCreator'; import { Uri } from 'vscode'; interface Props { @@ -15,38 +16,40 @@ export class NamespaceBatchUpdater { @inject(MovedFileNamespaceUpdater) private movedFileNamespaceUpdater: MovedFileNamespaceUpdater, @inject(MultiFileReferenceUpdater) private multiFileReferenceUpdater: MultiFileReferenceUpdater, @inject(NamespaceCreator) private namespaceCreator: NamespaceCreator, + @inject(ClassNameUpdater) private classNameUpdater: ClassNameUpdater, ) {} public async execute({ newUri, oldUri }: Props) { - const { - namespace: newNamespace, - fullNamespace: useNewNamespace, - } = await this.namespaceCreator.execute({ - uri: newUri, - }); + const { namespace, fullNamespace } = await this.getNamespace(newUri); - if (!newNamespace) { + if (!namespace) { return; } - const { fullNamespace: useOldNamespace } = await this.namespaceCreator.execute({ - uri: oldUri, - }); + const { namespace: old, fullNamespace: oldFullNamespace } = await this.getNamespace(oldUri); + + if (namespace === old && fullNamespace !== oldFullNamespace) { + this.classNameUpdater.execute({ newUri }); + } - const updated = await this.movedFileNamespaceUpdater.execute({ - newNamespace, + const isUpdated = await this.movedFileNamespaceUpdater.execute({ + newNamespace: namespace, newUri, }); - if (!updated) { + if (!isUpdated) { return; } await this.multiFileReferenceUpdater.execute({ - useOldNamespace, - useNewNamespace, + useOldNamespace: oldFullNamespace, + useNewNamespace: fullNamespace, newUri, oldUri, }); } + + private async getNamespace(uri: Uri): Promise { + return await this.namespaceCreator.execute({ uri }); + } } diff --git a/src/app/services/update/ClassNameUpdater.ts b/src/app/services/update/ClassNameUpdater.ts new file mode 100644 index 0000000..6ca9b2c --- /dev/null +++ b/src/app/services/update/ClassNameUpdater.ts @@ -0,0 +1,49 @@ +import { inject, injectable } from "tsyringe"; +import { Range, Uri, workspace, WorkspaceEdit } from "vscode"; +import { TextDocumentOpener } from "../TextDocumentOpener"; +import { WorkspacePathResolver } from '@domain/workspace/WorkspacePathResolver'; + +const CLASS_REGEX = /^\s*(?:abstract\s+)?(?:final\s+)?(?:class|interface|trait)\s+(\w+)/m; + +interface Props { + newUri: Uri, +} + +@injectable() +export class ClassNameUpdater { + constructor( + @inject(TextDocumentOpener) private textDocumentOpener: TextDocumentOpener, + @inject(WorkspacePathResolver) private workspacePathResolver: WorkspacePathResolver, + ) {} + + public async execute({ newUri }: Props): Promise { + const { document, text } = await this.textDocumentOpener.execute({ uri: newUri }); + + const match = CLASS_REGEX.exec(text); + if (!match) { + return; + } + const currentName = match[1]; + + const expectedName = this.workspacePathResolver.extractClassNameFromPath(newUri.fsPath); + + if (currentName === expectedName) { + return; + } + + const startIndex = match.index! + match[0].indexOf(currentName); + const endIndex = startIndex + currentName.length; + + const edit = new WorkspaceEdit(); + edit.replace( + newUri, + new Range( + document.positionAt(startIndex), + document.positionAt(endIndex) + ), + expectedName + ); + + workspace.applyEdit(edit); + } +}