Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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/",
Comment thread
mguzmanm marked this conversation as resolved.
Outdated
"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
125 changes: 125 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,125 @@
/**
* 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 fs from 'node:fs';
import os from 'node:os';
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';

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

beforeEach(() => {
commandsProvider = commandsProviderFactory();
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'edit-command-'));
Comment thread
mguzmanm marked this conversation as resolved.
Outdated
cprojectPath = path.join(tmpDir, 'test.cproject.yml');

yamlContent = `project:
groups:
- group: App
files:
- file: src/main.c
groups:
- group: Drivers
files:
- file: drv.c
`;

fs.writeFileSync(cprojectPath, yamlContent, 'utf8');
Comment thread
mguzmanm marked this conversation as resolved.
Outdated

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();
fs.rmSync(tmpDir, { recursive: true, force: true });
});

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('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();
});
});
149 changes: 149 additions & 0 deletions src/views/solution-outline/commands/edit-command.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 * 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') {
return;
}
Comment thread
mguzmanm marked this conversation as resolved.

const filePath = node.originFilePath;
if (!filePath) {
return;
}

const groupPath = this.getGroupPathForNode(node);
if (groupPath.length === 0) {
return;
}

const parentType = this.getParentTypeFromNode(node);
const offset = tag === 'group'
? this.findGroupOffset(filePath, parentType, groupPath)
: this.findFileOffset(filePath, parentType, groupPath, node.getAttribute('fileUri'));

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 getParentTypeFromNode(node: COutlineItem): 'project' | 'layer' {
return node.getAttribute('layerUri') ? 'layer' : 'project';
}
}
16 changes: 16 additions & 0 deletions src/views/solution-outline/commands/open-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SolutionManager } from '../../../solutions/solution-manager';
import { IOpenFileExternal } from '../../../open-file-external-if';
import { contextDescriptorFromString } from '../../../solutions/descriptors/descriptors';
import { existsSync } from 'fs';
import { lineOf, readTextFile } from '../../../utils/fs-utils';


export class OpenCommand {
Expand Down Expand Up @@ -83,11 +84,26 @@ export class OpenCommand {
private async commandHandler(command: string, node: COutlineItem) {
const filePath = this.getFilePathForCommand(command, node);
if (filePath) {
if (command === OpenCommand.openProjectCommandId) {
this.openProjectFile(filePath);
Comment thread
mguzmanm marked this conversation as resolved.
Outdated
return;
}

const openExternal = command === OpenCommand.openDocCommandId;
this.openFile(filePath, openExternal);
}
}

private async openProjectFile(filePath: string): Promise<void> {
const projectLine = lineOf(readTextFile(filePath), 'project:');
const document = await vscode.workspace.openTextDocument(vscode.Uri.file(filePath));
const position = new vscode.Position(projectLine, 0);
await vscode.window.showTextDocument(document, {
selection: new vscode.Range(position, position),
preview: false,
});
}

private getFilePathForCommand(command: string, node: COutlineItem): string | undefined {
if (command === OpenCommand.openDocCommandId) {
return node.getAttribute('docPath');
Expand Down
Loading