Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class FileItem {
this.createFileNode(cgroupItem, f, isApi, addContextMenu);
}
}
cgroupItem.sortChildrenByLabel();
cgroupItem.sortChildrenByGroupThenLabel();
}

private createFileNode(cgroupItem: COutlineItem, f: ITreeItem<CTreeItem>, isApi?: boolean, addContextMenu?: boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,42 @@ describe('COutlineItem', () => {
it('returns undefined if features attribute is not set', () => {
expect(childItem.getFeatures()).toEqual('');
});

it('sorts groups before files and keeps labels alphabetical', () => {
const folderZ = rootItem.createChild('folderZ');
folderZ.setTag('group');
folderZ.setAttribute('label', 'Z_subfolder3');
Comment thread
edriouk marked this conversation as resolved.
Outdated

const fileA = rootItem.createChild('fileA');
fileA.setTag('file');
fileA.setAttribute('label', 'A_file1');

const folderX = rootItem.createChild('folderX');
folderX.setTag('group');
folderX.setAttribute('label', 'X_subfolder1');

const fileC = rootItem.createChild('fileC');
fileC.setTag('file');
fileC.setAttribute('label', 'C_file3');

const folderY = rootItem.createChild('folderY');
folderY.setTag('group');
folderY.setAttribute('label', 'Y_subfolder2');

const fileB = rootItem.createChild('fileB');
fileB.setTag('file');
fileB.setAttribute('label', 'B_file2');

rootItem.sortChildrenByGroupThenLabel();

const labels = rootItem.getChildren().map((item) => item.getAttribute('label'));
expect(labels).toEqual([
'X_subfolder1',
'Y_subfolder2',
'Z_subfolder3',
'A_file1',
'B_file2',
'C_file3',
]);
});
});
20 changes: 20 additions & 0 deletions src/views/solution-outline/tree-structure/solution-outline-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,28 @@ export class COutlineItem extends CTreeItem {
COutlineItem.sortTreeNodesByLabel(this.getChildren() as COutlineItem[]);
}

sortChildrenByGroupThenLabel(): void {
COutlineItem.sortTreeNodesByGroupThenLabel(this.getChildren() as COutlineItem[]);
}

static sortTreeNodesByLabel(input?: COutlineItem[]): void {
input?.sort((a, b) =>
(a.getAttribute('label') || '').localeCompare(b.getAttribute('label') || ''));
}

static sortTreeNodesByGroupThenLabel(input?: COutlineItem[]): void {
input?.sort((a, b) => {
const tagA = a.getTag();
const tagB = b.getTag();

if (tagA === 'group' && tagB !== 'group') {
return -1;
}
if (tagB === 'group' && tagA !== 'group') {
return 1;
}

return (a.getAttribute('label') || '').localeCompare(b.getAttribute('label') || '');
});
}
}
14 changes: 7 additions & 7 deletions test-data/solutions/USBD/CmsisViewTreeRef.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ solution
groupPath=Documentation
projectUri=TEST_DIR/solutions/USBD/HID/HID.cproject.yml
expandable=1
file
label=README.md
expandable=0
resourcePath=TEST_DIR/solutions/USBD/HID/README.md
features=file
fileUri=README.md
projectUri=TEST_DIR/solutions/USBD/HID/HID.cproject.yml
group
label=subDoc
iconPath=csolution-files
Expand All @@ -54,6 +47,13 @@ solution
features=file
fileUri=subDocFile
projectUri=TEST_DIR/solutions/USBD/HID/HID.cproject.yml
file
label=README.md
expandable=0
resourcePath=TEST_DIR/solutions/USBD/HID/README.md
features=file
fileUri=README.md
projectUri=TEST_DIR/solutions/USBD/HID/HID.cproject.yml
group
label=USB
iconPath=csolution-files
Expand Down