Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/app/services/NamespaceBatchUpdater.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<Namespace> {
return await this.namespaceCreator.execute({ uri });
}
}
49 changes: 49 additions & 0 deletions src/app/services/update/ClassNameUpdater.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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);
}
}