-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathedit-command.ts
More file actions
179 lines (150 loc) · 6.25 KB
/
edit-command.ts
File metadata and controls
179 lines (150 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Copyright 2026 Arm Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as vscode from 'vscode';
import * as yaml from 'yaml';
import { CommandsProvider } from '../../../vscode-api/commands-provider';
import * as manifest from '../../../manifest';
import { COutlineItem } from '../tree-structure/solution-outline-item';
import { getGroupPathArray } from '../utils';
import { buildPathFromContentToGroup } from '../../../solutions/edit/manage-group-items';
import { getYamlNodeAtPath, listItem, mapKey } from '../../../solutions/edit/edit-yaml';
import { readTextFile } from '../../../utils/fs-utils';
export class EditCommand {
public static readonly editCommandId = `${manifest.PACKAGE_NAME}.edit`;
constructor(
private readonly commandsProvider: CommandsProvider,
) { }
public async activate(context: Pick<vscode.ExtensionContext, 'subscriptions'>) {
context.subscriptions.push(
this.commandsProvider.registerCommand(EditCommand.editCommandId, async (node: COutlineItem) => {
await this.editNode(node);
}, this),
);
}
private async editNode(node: COutlineItem): Promise<void> {
const tag = node.getTag();
if (tag !== 'group' && tag !== 'file' && tag !== 'component') {
return;
}
const filePath = node.originFilePath;
if (!filePath) {
return;
}
const groupPath = this.getGroupPathForNode(node);
if ((tag === 'group' || tag === 'file') && groupPath.length === 0) {
return;
}
const parentType = this.getParentTypeFromNode(node);
const offset = tag === 'group'
? this.findGroupOffset(filePath, parentType, groupPath)
: tag === 'file'
? this.findFileOffset(filePath, parentType, groupPath, node.getAttribute('fileUri'))
: this.findComponentOffset(filePath, parentType, node.getAttribute('label'));
if (offset === undefined) {
return;
}
const document = await vscode.workspace.openTextDocument(vscode.Uri.file(filePath));
const position = document.positionAt(offset);
await vscode.window.showTextDocument(document, {
selection: new vscode.Range(position, position),
preview: false,
});
}
private getGroupPathForNode(node: COutlineItem): string[] {
if (node.getTag() === 'group') {
return getGroupPathArray(node);
}
const parent = node.getParent();
if (!parent || !(parent instanceof COutlineItem) || parent.getTag() !== 'group') {
return [];
}
return getGroupPathArray(parent);
}
private findGroupOffset(filePath: string, parentType: 'project' | 'layer', groupPath: string[]): number | undefined {
const input = readTextFile(filePath);
if (!input) {
return undefined;
}
const yamlDocument = yaml.parseDocument(input);
const contents = yamlDocument.contents;
if (!contents) {
return undefined;
}
const targetGroupName = groupPath[groupPath.length - 1];
if (!targetGroupName) {
return undefined;
}
const pathToParentGroup = buildPathFromContentToGroup(groupPath.slice(0, -1), [mapKey(parentType)]);
const pathToTargetGroup = [
...pathToParentGroup,
mapKey('groups'),
listItem(item => yaml.isMap(item) && item.get('group') === targetGroupName),
];
const targetGroupNode = getYamlNodeAtPath(contents, pathToTargetGroup);
return (targetGroupNode && yaml.isNode(targetGroupNode) && targetGroupNode.range)
? targetGroupNode.range[0]
: undefined;
}
private findFileOffset(filePath: string, parentType: 'project' | 'layer', groupPath: string[], fileUri?: string): number | undefined {
if (!fileUri) {
return undefined;
}
const input = readTextFile(filePath);
if (!input) {
return undefined;
}
const yamlDocument = yaml.parseDocument(input);
const contents = yamlDocument.contents;
if (!contents) {
return undefined;
}
const pathToTargetFile = [
...buildPathFromContentToGroup(groupPath, [mapKey(parentType)]),
mapKey('files'),
listItem(item => yaml.isMap(item) && item.get('file') === fileUri),
];
const targetFileNode = getYamlNodeAtPath(contents, pathToTargetFile);
return (targetFileNode && yaml.isNode(targetFileNode) && targetFileNode.range)
? targetFileNode.range[0]
: undefined;
}
private findComponentOffset(filePath: string, parentType: 'project' | 'layer', componentId?: string): number | undefined {
if (!componentId) {
return undefined;
}
const input = readTextFile(filePath);
if (!input) {
return undefined;
}
const yamlDocument = yaml.parseDocument(input);
const contents = yamlDocument.contents;
if (!contents) {
return undefined;
}
const pathToTargetComponent = [
mapKey(parentType),
mapKey('components'),
listItem(item => yaml.isMap(item) && item.get('component') === componentId),
];
const targetComponentNode = getYamlNodeAtPath(contents, pathToTargetComponent);
return (targetComponentNode && yaml.isNode(targetComponentNode) && targetComponentNode.range)
? targetComponentNode.range[0]
: undefined;
}
private getParentTypeFromNode(node: COutlineItem): 'project' | 'layer' {
return node.getAttribute('layerUri') ? 'layer' : 'project';
}
}