-
Notifications
You must be signed in to change notification settings - Fork 3
Add Edit command to navigate from outline to specific entries in .cproject.yaml #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6af2a32
Add edit to group context menu
maribelguzmanm 47a92b4
Add edit to file context menu
maribelguzmanm 3b7dbc9
Remove openProjectFile
maribelguzmanm b87cf4e
Merge branch 'main' into ow_edit_feature
edriouk ec0063f
Add Edit command for components
maribelguzmanm 04af875
Update unit test
maribelguzmanm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/views/solution-outline/commands/edit-command.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
|
|
||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| 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'; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.