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
14 changes: 8 additions & 6 deletions src/app/services/rename/FileRenameResolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inject, injectable } from 'tsyringe';
import { NamespaceType, RenameTypeDetector } from './RenameTypeDetector';
import { Position, TextDocument, Uri, window } from 'vscode';
import { FileRenameHandler } from '@app/commands/FileRenameHandler';
import { RenameTypeDetector } from './RenameTypeDetector';
import { WorkspacePathResolver } from '@domain/workspace/WorkspacePathResolver';

interface Props {
Expand All @@ -23,14 +23,12 @@ export class FileRenameResolver {
throw new Error('New name cannot be empty');
}

const type = this.renameTypeDetector.execute({ document, position });

const currentUri = document.uri;
const oldPath = currentUri.fsPath;
const fileName = this.workspacePathResolver.extractClassNameFromPath(oldPath);

if (type === 'namespace') {
if (this.isNamespaceType(document, position)) {
try {
const fileName = this.workspacePathResolver.extractClassNameFromPath(oldPath);
const newDirectoryPath = await this.workspacePathResolver.getDirectoryFromNamespace(newName);

FileRenameHandler.create({
Expand All @@ -48,7 +46,11 @@ export class FileRenameResolver {

FileRenameHandler.create({
oldUri: currentUri,
newUri: Uri.file(`${directory}/${fileName}${extension}`),
newUri: Uri.file(`${directory}/${newName}${extension}`),
});
}

private isNamespaceType(document: TextDocument, position: Position): boolean {
return NamespaceType === this.renameTypeDetector.execute({ document, position });
}
}
9 changes: 6 additions & 3 deletions src/app/services/rename/RenameTypeDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ interface Props {
position: Position
}

type TypeRename = 'namespace' | 'class';
export const NamespaceType = 'namespace';
export const ClassType = 'class';

type TypeRename = typeof NamespaceType | typeof ClassType;

@injectable()
export class RenameTypeDetector {
Expand All @@ -16,11 +19,11 @@ export class RenameTypeDetector {
const currentLine = lines[position.line] ?? '';

if (currentLine.match(/^\s*namespace\s+/)) {
return 'namespace';
return NamespaceType;
}

if (currentLine.match(/^\s*(?:abstract\s+)?(?:final\s+)?(?:class|interface|trait)\s+/)) {
return 'class';
return ClassType;
}

throw new Error('Type rename not identified');
Expand Down