|
1 | 1 | import { Command } from 'command/Command'; |
2 | | -import IndexManager from 'indexer/IndexManager'; |
3 | | -import ModuleIndexer from 'indexer/module/ModuleIndexer'; |
| 2 | +import GetMagentoPath from 'common/GetMagentoPath'; |
4 | 3 | import { Uri, window, env } from 'vscode'; |
5 | 4 |
|
6 | 5 | export default class CopyMagentoPathCommand extends Command { |
7 | | - public static readonly TEMPLATE_EXTENSIONS = ['.phtml']; |
8 | | - public static readonly WEB_EXTENSIONS = ['.css', '.js']; |
9 | | - public static readonly IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.svg']; |
10 | | - |
11 | | - private static readonly TEMPLATE_PATHS = [ |
12 | | - 'view/frontend/templates/', |
13 | | - 'view/adminhtml/templates/', |
14 | | - 'view/base/templates/', |
15 | | - 'templates/', |
16 | | - ]; |
17 | | - |
18 | | - private static readonly WEB_PATHS = [ |
19 | | - 'view/frontend/web/', |
20 | | - 'view/adminhtml/web/', |
21 | | - 'view/base/web/', |
22 | | - 'web/', |
23 | | - ]; |
24 | | - |
25 | 6 | constructor() { |
26 | 7 | super('magento-toolbox.copyMagentoPath'); |
27 | 8 | } |
28 | 9 |
|
29 | 10 | public async execute(file: Uri): Promise<void> { |
30 | | - if (!file) { |
31 | | - return; |
32 | | - } |
33 | | - |
34 | | - const moduleIndexData = IndexManager.getIndexData(ModuleIndexer.KEY); |
35 | | - |
36 | | - if (!moduleIndexData) { |
37 | | - return; |
38 | | - } |
39 | | - |
40 | | - const module = moduleIndexData.getModuleByUri(file); |
41 | | - |
42 | | - if (!module) { |
43 | | - return; |
44 | | - } |
| 11 | + const magentoPath = GetMagentoPath.getMagentoPath(file); |
45 | 12 |
|
46 | | - const paths = this.getPaths(file); |
47 | | - |
48 | | - if (!paths) { |
| 13 | + if (!magentoPath) { |
49 | 14 | return; |
50 | 15 | } |
51 | 16 |
|
52 | | - const pathIndex = paths.findIndex(p => file.fsPath.lastIndexOf(p) !== -1); |
53 | | - |
54 | | - if (pathIndex === -1) { |
55 | | - return; |
56 | | - } |
57 | | - |
58 | | - const endIndex = file.fsPath.lastIndexOf(paths[pathIndex]); |
59 | | - const offset = paths[pathIndex].length; |
60 | | - const relativePath = file.fsPath.slice(offset + endIndex); |
61 | | - |
62 | | - const magentoPath = `${module.name}::${relativePath}`; |
63 | | - |
64 | 17 | await env.clipboard.writeText(magentoPath); |
65 | 18 | window.showInformationMessage(`Copied: ${magentoPath}`); |
66 | 19 | } |
67 | | - |
68 | | - private getPaths(file: Uri): string[] | undefined { |
69 | | - if (CopyMagentoPathCommand.TEMPLATE_EXTENSIONS.some(ext => file.fsPath.endsWith(ext))) { |
70 | | - return CopyMagentoPathCommand.TEMPLATE_PATHS; |
71 | | - } |
72 | | - |
73 | | - if ( |
74 | | - CopyMagentoPathCommand.WEB_EXTENSIONS.some(ext => file.fsPath.endsWith(ext)) || |
75 | | - CopyMagentoPathCommand.IMAGE_EXTENSIONS.some(ext => file.fsPath.endsWith(ext)) |
76 | | - ) { |
77 | | - return CopyMagentoPathCommand.WEB_PATHS; |
78 | | - } |
79 | | - |
80 | | - return undefined; |
81 | | - } |
82 | 20 | } |
0 commit comments