From aabfcf4a3bfb667694bebb6ddebf58fd71274f59 Mon Sep 17 00:00:00 2001 From: vince-fugnitto Date: Thu, 11 Aug 2022 09:48:41 -0400 Subject: [PATCH 1/2] scm: select node based on active editor Signed-off-by: vince-fugnitto --- packages/scm/src/browser/scm-tree-widget.tsx | 16 +++++++++++++++- packages/scm/src/browser/scm-widget.tsx | 9 ++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/scm/src/browser/scm-tree-widget.tsx b/packages/scm/src/browser/scm-tree-widget.tsx index e1df209eabb58..b54d5a649d00d 100644 --- a/packages/scm/src/browser/scm-tree-widget.tsx +++ b/packages/scm/src/browser/scm-tree-widget.tsx @@ -21,7 +21,10 @@ import { injectable, inject } from '@theia/core/shared/inversify'; import URI from '@theia/core/lib/common/uri'; import { isOSX } from '@theia/core/lib/common/os'; import { DisposableCollection, Disposable } from '@theia/core/lib/common/disposable'; -import { TreeWidget, TreeNode, SelectableTreeNode, TreeModel, TreeProps, NodeProps, TREE_NODE_SEGMENT_CLASS, TREE_NODE_SEGMENT_GROW_CLASS } from '@theia/core/lib/browser/tree'; +import { + TreeWidget, TreeNode, SelectableTreeNode, TreeModel, TreeProps, + NodeProps, TREE_NODE_SEGMENT_CLASS, TREE_NODE_SEGMENT_GROW_CLASS, DepthFirstTreeIterator +} from '@theia/core/lib/browser/tree'; import { ScmTreeModel, ScmFileChangeRootNode, ScmFileChangeGroupNode, ScmFileChangeFolderNode, ScmFileChangeNode } from './scm-tree-model'; import { MenuModelRegistry, ActionMenuNode, CompositeMenuNode, MenuPath } from '@theia/core/lib/common/menu'; import { ScmResource } from './scm-provider'; @@ -355,6 +358,17 @@ export class ScmTreeWidget extends TreeWidget { } } + selectNodeByUri(uri: URI): void { + const root = this.model.root; + if (!root) { return; } + for (const node of new DepthFirstTreeIterator(root)) { + if (ScmFileChangeNode.is(node) && node.sourceUri.includes(uri.path.toString())) { + this.model.selectNode(node); + return; + } + } + } + protected getFirstSelectableNode(): SelectableTreeNode | undefined { if (this.model.root) { const root = this.model.root as ScmFileChangeRootNode; diff --git a/packages/scm/src/browser/scm-widget.tsx b/packages/scm/src/browser/scm-widget.tsx index 8db003052ebe3..5b4e20e34dd7d 100644 --- a/packages/scm/src/browser/scm-widget.tsx +++ b/packages/scm/src/browser/scm-widget.tsx @@ -20,7 +20,7 @@ import { Message } from '@theia/core/shared/@phosphor/messaging'; import { injectable, inject, postConstruct } from '@theia/core/shared/inversify'; import { DisposableCollection } from '@theia/core/lib/common/disposable'; import { - BaseWidget, Widget, StatefulWidget, Panel, PanelLayout, MessageLoop, CompositeTreeNode, SelectableTreeNode, + BaseWidget, Widget, StatefulWidget, Panel, PanelLayout, MessageLoop, CompositeTreeNode, SelectableTreeNode, ApplicationShell, NavigatableWidget, } from '@theia/core/lib/browser'; import { ScmCommitWidget } from './scm-commit-widget'; import { ScmAmendWidget } from './scm-amend-widget'; @@ -37,6 +37,7 @@ export class ScmWidget extends BaseWidget implements StatefulWidget { static ID = 'scm-view'; + @inject(ApplicationShell) protected readonly shell: ApplicationShell; @inject(ScmService) protected readonly scmService: ScmService; @inject(ScmCommitWidget) protected readonly commitWidget: ScmCommitWidget; @inject(ScmTreeWidget) readonly resourceWidget: ScmTreeWidget; @@ -84,6 +85,12 @@ export class ScmWidget extends BaseWidget implements StatefulWidget { this.updateViewMode(e.newValue); } })); + this.toDispose.push(this.shell.onDidChangeCurrentWidget(({ newValue }) => { + const uri = NavigatableWidget.getUri(newValue || undefined); + if (uri) { + this.resourceWidget.selectNodeByUri(uri); + } + })); } From e234317f627a9a05f5ecb224852fa2575caa34ed Mon Sep 17 00:00:00 2001 From: vince-fugnitto Date: Thu, 11 Aug 2022 15:25:11 -0400 Subject: [PATCH 2/2] scm: rework `selectNodeByUri` Signed-off-by: vince-fugnitto --- packages/scm/src/browser/scm-tree-widget.tsx | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/scm/src/browser/scm-tree-widget.tsx b/packages/scm/src/browser/scm-tree-widget.tsx index b54d5a649d00d..2061ca9518d25 100644 --- a/packages/scm/src/browser/scm-tree-widget.tsx +++ b/packages/scm/src/browser/scm-tree-widget.tsx @@ -21,10 +21,7 @@ import { injectable, inject } from '@theia/core/shared/inversify'; import URI from '@theia/core/lib/common/uri'; import { isOSX } from '@theia/core/lib/common/os'; import { DisposableCollection, Disposable } from '@theia/core/lib/common/disposable'; -import { - TreeWidget, TreeNode, SelectableTreeNode, TreeModel, TreeProps, - NodeProps, TREE_NODE_SEGMENT_CLASS, TREE_NODE_SEGMENT_GROW_CLASS, DepthFirstTreeIterator -} from '@theia/core/lib/browser/tree'; +import { TreeWidget, TreeNode, SelectableTreeNode, TreeModel, TreeProps, NodeProps, TREE_NODE_SEGMENT_CLASS, TREE_NODE_SEGMENT_GROW_CLASS } from '@theia/core/lib/browser/tree'; import { ScmTreeModel, ScmFileChangeRootNode, ScmFileChangeGroupNode, ScmFileChangeFolderNode, ScmFileChangeNode } from './scm-tree-model'; import { MenuModelRegistry, ActionMenuNode, CompositeMenuNode, MenuPath } from '@theia/core/lib/common/menu'; import { ScmResource } from './scm-provider'; @@ -359,10 +356,11 @@ export class ScmTreeWidget extends TreeWidget { } selectNodeByUri(uri: URI): void { - const root = this.model.root; - if (!root) { return; } - for (const node of new DepthFirstTreeIterator(root)) { - if (ScmFileChangeNode.is(node) && node.sourceUri.includes(uri.path.toString())) { + for (const group of this.model.groups) { + const sourceUri = new URI(uri.path.toString()); + const id = `${group.id}:${sourceUri.toString()}`; + const node = this.model.getNode(id); + if (SelectableTreeNode.is(node)) { this.model.selectNode(node); return; }