Skip to content

Commit aeb439c

Browse files
committed
refactor: from function to object
1 parent 4b5c024 commit aeb439c

45 files changed

Lines changed: 858 additions & 862 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { MovedFileNamespaceUpdater } from './update/MovedFileNamespaceUpdater';
2+
import { MultiFileReferenceUpdater } from './update/MultiFileReferenceUpdater';
3+
import { NamespaceCreator } from '@domain/namespace/NamespaceCreator';
4+
import { Uri } from 'vscode';
5+
6+
interface Props {
7+
newUri: Uri,
8+
oldUri: Uri,
9+
}
10+
11+
export class NamespaceBatchUpdater {
12+
public async execute({ newUri, oldUri }: Props) {
13+
const namespaceCreator = new NamespaceCreator();
14+
const {
15+
namespace: newNamespace,
16+
fullNamespace: useNewNamespace,
17+
} = await namespaceCreator.execute({
18+
uri: newUri,
19+
});
20+
21+
if (!newNamespace) {
22+
return;
23+
}
24+
25+
const { fullNamespace: useOldNamespace } = await namespaceCreator.execute({
26+
uri: oldUri,
27+
});
28+
29+
const updated = await new MovedFileNamespaceUpdater().execute({
30+
newNamespace,
31+
newUri,
32+
});
33+
34+
if (!updated) {
35+
return;
36+
}
37+
38+
await new MultiFileReferenceUpdater().execute({
39+
useOldNamespace,
40+
useNewNamespace,
41+
newUri,
42+
oldUri,
43+
});
44+
}
45+
}

src/app/namespace/openTextDocument.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/app/namespace/remove/removeImports.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/app/namespace/remove/removeUnusedImports.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Range, Uri, workspace, WorkspaceEdit } from 'vscode';
2+
import { TextDocumentOpener } from '@app/services/TextDocumentOpener';
3+
4+
interface Props {
5+
newNamespace: string,
6+
newUri: Uri,
7+
}
8+
9+
export class MovedFileNamespaceUpdater {
10+
public async execute({ newNamespace, newUri }: Props) {
11+
const { document, text } = await new TextDocumentOpener().execute({ uri: newUri });
12+
13+
const namespaceRegex = /^\s*namespace\s+[\w\\]+;/m;
14+
const match = text.match(namespaceRegex);
15+
16+
if (!match) {
17+
return false;
18+
}
19+
20+
const startIndex = match.index!;
21+
const startPosition = document.positionAt(startIndex);
22+
const endPosition = document.positionAt(startIndex + match[0].length);
23+
24+
const namespaceReplace = `\nnamespace ${newNamespace};`;
25+
26+
const edit = new WorkspaceEdit();
27+
edit.replace(
28+
newUri,
29+
new Range(startPosition, endPosition),
30+
namespaceReplace,
31+
);
32+
33+
workspace.applyEdit(edit);
34+
35+
return true;
36+
}
37+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { Uri, workspace, WorkspaceEdit } from 'vscode';
2+
import { ImportRemover } from '@app/services/remove/ImportRemover';
3+
import { TextDocumentOpener } from '@app/services/TextDocumentOpener';
4+
import { UseStatementCreator } from '@domain/namespace/UseStatementCreator';
5+
import { UseStatementInjector } from '@domain/namespace/UseStatementInjector';
6+
import { UseStatementLocator } from '@domain/namespace/UseStatementLocator';
7+
import { WorkspaceFileFinder } from '@app/services/workspace/WorkspaceFileFinder';
8+
import { WorkspacePathResolver } from '@domain/workspace/WorkspacePathResolver';
9+
10+
interface Props {
11+
useOldNamespace: string
12+
useNewNamespace: string
13+
newUri: Uri
14+
oldUri: Uri
15+
}
16+
17+
export class MultiFileReferenceUpdater {
18+
private workspacePathResolver: WorkspacePathResolver;
19+
20+
constructor() {
21+
this.workspacePathResolver = new WorkspacePathResolver();
22+
}
23+
24+
public async execute({
25+
useOldNamespace,
26+
useNewNamespace,
27+
newUri,
28+
oldUri,
29+
}: Props) {
30+
const directoryPath = this.workspacePathResolver.extractDirectoryFromPath(oldUri.fsPath);
31+
const className = this.workspacePathResolver.extractClassNameFromPath(oldUri.fsPath);
32+
33+
const useImport = new UseStatementCreator().single({ fullNamespace: useNewNamespace });
34+
35+
const ignoreFile = newUri.fsPath;
36+
37+
const files = await new WorkspaceFileFinder().execute();
38+
39+
const filesToProcess = files.filter(file => ignoreFile !== file.fsPath);
40+
41+
await Promise.all(filesToProcess.map(async (file) => {
42+
try {
43+
const fileStream = workspace.fs;
44+
45+
await fileStream.stat(file);
46+
47+
const fileContent = await fileStream.readFile(file);
48+
let text = Buffer.from(fileContent).toString();
49+
50+
if (!text.includes(useOldNamespace)) {
51+
await this.updateInFile(
52+
file,
53+
directoryPath,
54+
useImport,
55+
className,
56+
);
57+
58+
return;
59+
}
60+
61+
text = text.replace(useOldNamespace, useNewNamespace);
62+
await fileStream.writeFile(file, Buffer.from(text));
63+
64+
await this.updateInFile(
65+
file,
66+
directoryPath,
67+
useImport,
68+
className,
69+
);
70+
} catch (_) {
71+
return;
72+
}
73+
}));
74+
75+
await new ImportRemover().execute({ uri: newUri });
76+
}
77+
78+
private async updateInFile(
79+
file: Uri,
80+
oldDirectoryPath: string,
81+
useImport: string,
82+
className: string,
83+
): Promise<void> {
84+
const currentDir = this.workspacePathResolver.extractDirectoryFromPath(file.fsPath);
85+
if (oldDirectoryPath !== currentDir) {
86+
return;
87+
}
88+
89+
try {
90+
const { document, text } = await new TextDocumentOpener().execute({ uri: file });
91+
92+
if (!text.includes(className)) {
93+
return;
94+
}
95+
96+
const insertionIndex = new UseStatementLocator().execute({ document });
97+
if (insertionIndex === 0) {
98+
return;
99+
}
100+
101+
const edit = new WorkspaceEdit();
102+
103+
await new UseStatementInjector().save({
104+
document,
105+
workspaceEdit: edit,
106+
uri: file,
107+
lastUseEndIndex: insertionIndex,
108+
useNamespace: useImport,
109+
flush: true,
110+
});
111+
} catch (_) {
112+
return;
113+
}
114+
}
115+
}

src/app/namespace/update/import/findUnimportedClasses.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/app/namespace/update/import/getClassesNamesInDirectory.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)