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
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,10 @@
"command": "cmsis-csolution.list.find",
"title": "Search"
},
{
"command": "cmsis-csolution.edit",
"title": "Edit"
},
{
"command": "cmsis-csolution.showSolutionOutline",
"title": "Show Solution Outline"
Expand Down Expand Up @@ -929,6 +933,10 @@
"command": "cmsis-csolution.addToGroup",
"when": "false"
},
{
"command": "cmsis-csolution.edit",
"when": "false"
},
{
"command": "cmsis-csolution.showConfigWizardPreview",
"when": "false"
Expand Down Expand Up @@ -1145,6 +1153,11 @@
"when": "view == cmsis-csolution.outline && viewItem =~ /group|file/",
"group": "contextMenu"
},
{
"command": "cmsis-csolution.edit",
"when": "view == cmsis-csolution.outline && viewItem =~ /(^|;)(group|file|component)(;|$)/",
"group": "contextMenu"
},
{
"command": "cmsis-csolution.runGenerator",
"when": "view == cmsis-csolution.outline && viewItem =~ /component-gen/",
Expand Down
3 changes: 3 additions & 0 deletions src/desktop/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { CreateSolutionWebviewMain } from '../views/create-solutions/create-solu
import { ManageLayersWebviewMain } from '../views/manage-layers/manage-layers-webview-main';
import { AddToGroupCommand } from '../views/solution-outline/commands/add-to-group-command';
import { DeleteCommand } from '../views/solution-outline/commands/delete-command';
import { EditCommand } from '../views/solution-outline/commands/edit-command';
import { OpenCommand } from '../views/solution-outline/commands/open-command';
import { FindCommand } from '../views/solution-outline/commands/find-command';
import { MergeCommand } from '../views/solution-outline/commands/merge-command';
Expand Down Expand Up @@ -210,6 +211,7 @@ export const activate = async (context: ExtensionContext): Promise<CsolutionExte

const addToGroupCommand = new AddToGroupCommand(workspaceFsProvider, commandsProvider, solutionManager);
const deleteCommand = new DeleteCommand(commandsProvider, workspaceFsProvider);
const editCommand = new EditCommand(commandsProvider);
const copyHeaderCommand = new CopyHeaderCommand(commandsProvider);
const openCommand = new OpenCommand(solutionManager, commandsProvider, externalFileOpener);
const findCommand = new FindCommand(commandsProvider);
Expand Down Expand Up @@ -273,6 +275,7 @@ export const activate = async (context: ExtensionContext): Promise<CsolutionExte
solutionLanguageFeatures.activate(context),
addToGroupCommand.activate(context),
deleteCommand.activate(context),
editCommand.activate(context),
copyHeaderCommand.activate(context),
openCommand.activate(context),
findCommand.activate(context),
Expand Down
149 changes: 149 additions & 0 deletions src/views/solution-outline/commands/edit-command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* 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 path from 'node:path';
import * as vscode from 'vscode';
import { EditCommand } from './edit-command';
import { extensionContextFactory } from '../../../vscode-api/extension-context.factories';
import { commandsProviderFactory, MockCommandsProvider } from '../../../vscode-api/commands-provider.factories';
import { COutlineItem } from '../tree-structure/solution-outline-item';
import { TestDataHandler } from '../../../__test__/test-data';
import * as fsUtils from '../../../utils/fs-utils';

describe('EditCommand', () => {
let commandsProvider: MockCommandsProvider;
let cprojectPath: string;
let yamlContent: string;
let positionAt: jest.Mock;
const testDataHandler = new TestDataHandler();

beforeEach(() => {
commandsProvider = commandsProviderFactory();
cprojectPath = path.join(testDataHandler.tmpDir, 'test.cproject.yml');

yamlContent = `project:
groups:
- group: App
files:
- file: src/main.c
groups:
- group: Drivers
files:
- file: drv.c
components:
- component: ARM::CMSIS:RTOS2
`;

fsUtils.writeTextFile(cprojectPath, yamlContent);

positionAt = jest.fn().mockReturnValue(new vscode.Position(0, 0));
jest.spyOn(vscode.workspace, 'openTextDocument').mockResolvedValue({ positionAt } as unknown as vscode.TextDocument);
jest.spyOn(vscode.window, 'showTextDocument').mockResolvedValue({} as vscode.TextEditor);
});

afterEach(() => {
jest.restoreAllMocks();
testDataHandler.rmFile(cprojectPath);
});

afterAll(() => {
testDataHandler.dispose();
});

it('registers the edit command on activation', async () => {
const editCommand = new EditCommand(commandsProvider);

await editCommand.activate(extensionContextFactory());

expect(commandsProvider.registerCommand).toHaveBeenCalledWith(
EditCommand.editCommandId,
expect.any(Function),
editCommand,
);
});

it('opens cproject.yml at the selected group entry', async () => {
const editCommand = new EditCommand(commandsProvider);
await editCommand.activate(extensionContextFactory());

const groupNode = new COutlineItem('group');
groupNode.setAttribute('groupPath', 'App;Drivers');
groupNode.setAttribute('projectUri', cprojectPath);

await commandsProvider.mockRunRegistered(EditCommand.editCommandId, groupNode);

expect(vscode.workspace.openTextDocument).toHaveBeenCalledWith(vscode.Uri.file(cprojectPath));
expect(positionAt).toHaveBeenCalledTimes(1);
const actualOffset = positionAt.mock.calls[0][0] as number;
expect(actualOffset).toBeGreaterThanOrEqual(0);
expect(yamlContent.slice(actualOffset)).toContain('group: Drivers');
expect(vscode.window.showTextDocument).toHaveBeenCalled();
});

it('opens cproject.yml at the selected file entry', async () => {
const editCommand = new EditCommand(commandsProvider);
await editCommand.activate(extensionContextFactory());

const groupNode = new COutlineItem('group');
groupNode.setAttribute('groupPath', 'App;Drivers');
groupNode.setAttribute('projectUri', cprojectPath);

const fileNode = groupNode.createChild('file');
fileNode.setAttribute('projectUri', cprojectPath);
fileNode.setAttribute('fileUri', 'drv.c');

await commandsProvider.mockRunRegistered(EditCommand.editCommandId, fileNode);

expect(vscode.workspace.openTextDocument).toHaveBeenCalledWith(vscode.Uri.file(cprojectPath));
expect(positionAt).toHaveBeenCalledTimes(1);
const actualOffset = positionAt.mock.calls[0][0] as number;
expect(actualOffset).toBeGreaterThanOrEqual(0);
expect(yamlContent.slice(actualOffset)).toContain('file: drv.c');
expect(vscode.window.showTextDocument).toHaveBeenCalled();
});

it('opens cproject.yml at the selected component entry', async () => {
const editCommand = new EditCommand(commandsProvider);
await editCommand.activate(extensionContextFactory());

const componentNode = new COutlineItem('component');
componentNode.setAttribute('label', 'ARM::CMSIS:RTOS2');
componentNode.setAttribute('projectUri', cprojectPath);

await commandsProvider.mockRunRegistered(EditCommand.editCommandId, componentNode);

expect(vscode.workspace.openTextDocument).toHaveBeenCalledWith(vscode.Uri.file(cprojectPath));
expect(positionAt).toHaveBeenCalledTimes(1);
const actualOffset = positionAt.mock.calls[0][0] as number;
expect(actualOffset).toBeGreaterThanOrEqual(0);
expect(yamlContent.slice(actualOffset)).toContain('component: ARM::CMSIS:RTOS2');
expect(vscode.window.showTextDocument).toHaveBeenCalled();
});

it('ignores file nodes outside editable groups', async () => {
const editCommand = new EditCommand(commandsProvider);
await editCommand.activate(extensionContextFactory());

const nonEditableNode = new COutlineItem('file');
nonEditableNode.setAttribute('fileUri', 'out/test.map');

await commandsProvider.mockRunRegistered(EditCommand.editCommandId, nonEditableNode);

expect(vscode.workspace.openTextDocument).not.toHaveBeenCalled();
expect(vscode.window.showTextDocument).not.toHaveBeenCalled();
});

});
179 changes: 179 additions & 0 deletions src/views/solution-outline/commands/edit-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
Comment thread
mguzmanm marked this conversation as resolved.

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';
}
}
Loading
Loading