Skip to content

Commit 5043686

Browse files
dbaeumerCopilot
andcommitted
Make structural diffs reusable outside the Source Control view
The model that loads and refreshes the structural changes of files now works with any kind of file, which an adapter describes, and reports in telemetry which view requested them. The Source Control view uses it through a subclass. Finding where a diff editor shows a structural item moved into a helper that other views can use too. The state of Show Structural Diff moved from the Source Control view into the SCM view service, so that other views can follow it, and the styles of structural rows that do not depend on the Source Control view moved into a stylesheet that the renderer imports. The Source Control view does not change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8cc591e commit 5043686

10 files changed

Lines changed: 292 additions & 191 deletions

File tree

‎src/vs/workbench/contrib/scm/browser/media/scm.css‎

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -290,45 +290,6 @@
290290
overflow: hidden;
291291
}
292292

293-
.scm-view .scm-structural-change > .scm-structural-change-icon {
294-
display: flex;
295-
align-items: center;
296-
flex-shrink: 0;
297-
width: var(--vscode-spacing-size160);
298-
margin-right: var(--vscode-spacing-size60);
299-
}
300-
301-
.scm-view .scm-structural-change > .name {
302-
min-width: 0;
303-
}
304-
305-
.scm-view .scm-structural-change > .scm-structural-change-metadata {
306-
display: flex;
307-
align-items: center;
308-
flex-shrink: 0;
309-
gap: var(--vscode-spacing-size60);
310-
margin-left: var(--vscode-spacing-size60);
311-
}
312-
313-
.scm-view .scm-structural-change-annotation {
314-
font-size: var(--vscode-fontSize-label1);
315-
white-space: nowrap;
316-
opacity: 0.7;
317-
}
318-
319-
.scm-view .scm-structural-change-status.added {
320-
color: var(--vscode-gitDecoration-addedResourceForeground);
321-
}
322-
323-
.scm-view .scm-structural-change-status.deleted {
324-
color: var(--vscode-gitDecoration-deletedResourceForeground);
325-
}
326-
327-
.scm-view .scm-structural-change-status.modified,
328-
.scm-view .scm-structural-change-status.renamed {
329-
color: var(--vscode-gitDecoration-modifiedResourceForeground);
330-
}
331-
332293
/* Like the file badges in iconlabel.css, status letters are dimmed and use the selection foreground in a focused list. */
333294
.scm-view .scm-structural-change-status {
334295
opacity: 0.75;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
/* The rows of structural changes, which views that list files align with their own rows. */
7+
8+
.scm-structural-change {
9+
display: flex;
10+
height: 100%;
11+
}
12+
13+
.scm-structural-change > .scm-structural-change-icon {
14+
display: flex;
15+
align-items: center;
16+
flex-shrink: 0;
17+
width: var(--vscode-spacing-size160);
18+
margin-right: var(--vscode-spacing-size60);
19+
}
20+
21+
.scm-structural-change > .name {
22+
flex: 1;
23+
min-width: 0;
24+
overflow: hidden;
25+
}
26+
27+
.scm-structural-change > .scm-structural-change-metadata {
28+
display: flex;
29+
align-items: center;
30+
flex-shrink: 0;
31+
gap: var(--vscode-spacing-size60);
32+
margin-left: var(--vscode-spacing-size60);
33+
}
34+
35+
.scm-structural-change-annotation {
36+
font-size: var(--vscode-fontSize-label1);
37+
white-space: nowrap;
38+
opacity: 0.7;
39+
}
40+
41+
.scm-structural-change-status.added {
42+
color: var(--vscode-gitDecoration-addedResourceForeground);
43+
}
44+
45+
.scm-structural-change-status.deleted {
46+
color: var(--vscode-gitDecoration-deletedResourceForeground);
47+
}
48+
49+
.scm-structural-change-status.modified,
50+
.scm-structural-change-status.renamed {
51+
color: var(--vscode-gitDecoration-modifiedResourceForeground);
52+
}

‎src/vs/workbench/contrib/scm/browser/scmStructuralDiffRenderer.ts‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import { DisposableStore, IDisposable } from '../../../../base/common/lifecycle.
1414
import { ThemeIcon } from '../../../../base/common/themables.js';
1515
import { generateUuid } from '../../../../base/common/uuid.js';
1616
import { SymbolKinds } from '../../../../editor/common/languages.js';
17-
import { SCMStructuralDiffElement, SCMStructuralDiffNode, getSCMStructuralDiffAriaLabel, getSCMStructuralDiffChange, getSCMStructuralDiffChangeAnnotation, getSCMStructuralDiffLabel } from '../common/scmStructuralDiff.js';
18-
import { ICodeEditor, isDiffEditor } from '../../../../editor/browser/editorBrowser.js';
19-
import { IRange, Range } from '../../../../editor/common/core/range.js';
17+
import { SCMStructuralDiffElement, SCMStructuralDiffNode, getSCMStructuralDiffAriaLabel, getSCMStructuralDiffChange, getSCMStructuralDiffChangeAnnotation, getSCMStructuralDiffLabel, getSCMStructuralDiffTarget } from '../common/scmStructuralDiff.js';
18+
import { isDiffEditor } from '../../../../editor/browser/editorBrowser.js';
2019
import { ScrollType } from '../../../../editor/common/editorCommon.js';
2120
import { IEditorOptions, TextEditorSelectionSource } from '../../../../platform/editor/common/editor.js';
2221
import { IHoverService } from '../../../../platform/hover/browser/hover.js';
2322
import { localize } from '../../../../nls.js';
2423
import { DEFAULT_EDITOR_ASSOCIATION } from '../../../common/editor.js';
2524
import { IEditorService, SIDE_GROUP } from '../../../services/editor/common/editorService.js';
2625
import '../../../../editor/contrib/symbolIcons/browser/symbolIcons.js';
26+
import './media/scmStructuralDiff.css';
2727

2828
interface StructuralDiffTemplate extends IDisposable {
2929
readonly icon: HTMLElement;
@@ -34,7 +34,7 @@ interface StructuralDiffTemplate extends IDisposable {
3434
readonly elementDisposables: DisposableStore;
3535
}
3636

37-
export class SCMStructuralDiffRenderer implements ICompressibleTreeRenderer<SCMStructuralDiffElement, FuzzyScore, StructuralDiffTemplate> {
37+
export class SCMStructuralDiffRenderer implements ICompressibleTreeRenderer<SCMStructuralDiffElement<object>, FuzzyScore | void, StructuralDiffTemplate> {
3838
static readonly TEMPLATE_ID = 'scmStructuralDiff';
3939
readonly templateId = SCMStructuralDiffRenderer.TEMPLATE_ID;
4040
private readonly hoverGroupId = generateUuid();
@@ -56,7 +56,7 @@ export class SCMStructuralDiffRenderer implements ICompressibleTreeRenderer<SCMS
5656
return { icon, label, metadata, annotation, status, elementDisposables, dispose: () => disposables.dispose() };
5757
}
5858

59-
renderElement(node: ITreeNode<SCMStructuralDiffElement, FuzzyScore>, _index: number, template: StructuralDiffTemplate): void {
59+
renderElement(node: ITreeNode<SCMStructuralDiffElement<object>, FuzzyScore | void>, _index: number, template: StructuralDiffTemplate): void {
6060
const element = node.element;
6161
const item = element.type === 'structuralDiff' ? element.node.modifiedItem ?? element.node.originalItem : undefined;
6262
const change = getSCMStructuralDiffChange(element);
@@ -67,7 +67,7 @@ export class SCMStructuralDiffRenderer implements ICompressibleTreeRenderer<SCMS
6767
template.icon.classList.toggle('codicon-colored', !!item);
6868
template.label.setLabel(getSCMStructuralDiffLabel(element), item?.detail, {
6969
title: getSCMStructuralDiffAriaLabel(element),
70-
matches: createMatches(node.filterData),
70+
matches: createMatches(node.filterData as FuzzyScore | undefined),
7171
labelEscapeNewLines: true,
7272
});
7373
template.annotation.textContent = annotation?.label ?? '';
@@ -91,7 +91,7 @@ export class SCMStructuralDiffRenderer implements ICompressibleTreeRenderer<SCMS
9191
}
9292
}
9393

94-
disposeElement(_node: ITreeNode<SCMStructuralDiffElement, FuzzyScore>, _index: number, template: StructuralDiffTemplate): void {
94+
disposeElement(_node: ITreeNode<SCMStructuralDiffElement<object>, FuzzyScore | void>, _index: number, template: StructuralDiffTemplate): void {
9595
template.elementDisposables.clear();
9696
}
9797

@@ -117,20 +117,8 @@ export async function openSCMStructuralDiff(element: SCMStructuralDiffNode, opti
117117
if (!isDiffEditor(control) || !isCurrent()) {
118118
return;
119119
}
120-
const { originalItem, modifiedItem } = element.node;
121-
let target: ICodeEditor;
122-
let range: IRange;
123-
if (modifiedItem) {
124-
target = control.getModifiedEditor();
125-
range = modifiedItem.selectionRange ?? modifiedItem.range;
126-
} else if (control.renderSideBySide || !element.deletedAt) {
127-
target = control.getOriginalEditor();
128-
range = originalItem!.selectionRange ?? originalItem!.range;
129-
} else {
130-
// An inline diff hides the original editor and shows deleted text where it was deleted from the modified document.
131-
target = control.getModifiedEditor();
132-
range = Range.fromPositions(element.deletedAt);
133-
}
120+
const { side, range } = getSCMStructuralDiffTarget(element, control.renderSideBySide);
121+
const target = side === 'original' ? control.getOriginalEditor() : control.getModifiedEditor();
134122
target.setSelection(range, TextEditorSelectionSource.NAVIGATION);
135123
target.revealRangeInCenterIfOutsideViewport(range, ScrollType.Smooth);
136124
if (!options.preserveFocus) {

‎src/vs/workbench/contrib/scm/browser/scmViewPane.ts‎

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,10 +1386,11 @@ class ToggleShowStructuralDiffAction extends ViewAction<SCMViewPane> {
13861386
});
13871387
}
13881388

1389-
runInView(accessor: ServicesAccessor, view: SCMViewPane): void {
1390-
const show = !view.showStructuralDiff;
1391-
view.showStructuralDiff = show;
1392-
// Reported here rather than in the setter, which also runs when another window changes the choice.
1389+
runInView(accessor: ServicesAccessor): void {
1390+
const scmViewService = accessor.get(ISCMViewService);
1391+
const show = !scmViewService.showStructuralDiff.get();
1392+
scmViewService.setShowStructuralDiff(show);
1393+
// Reported here rather than in the service, which also updates when another window changes the choice.
13931394
accessor.get(ITelemetryService).publicLog2<StructuralDiffToggledEvent, StructuralDiffToggledClassification>('scmStructuralDiffToggled', { enabled: show ? 1 : 0 });
13941395
}
13951396
}
@@ -1526,19 +1527,7 @@ export class SCMViewPane extends ViewPane {
15261527
private readonly _onDidChangeViewSortKey = this._register(new Emitter<ViewSortKey>());
15271528
readonly onDidChangeViewSortKey = this._onDidChangeViewSortKey.event;
15281529

1529-
/** The user's choice from the View & Sort menu. Structural diffs are only shown while `scm.structuralDiff.enabled` is also set. */
1530-
get showStructuralDiff(): boolean { return this._showStructuralDiff; }
1531-
set showStructuralDiff(show: boolean) {
1532-
if (this._showStructuralDiff === show) {
1533-
return;
1534-
}
1535-
this._showStructuralDiff = show;
1536-
this.storageService.store('scm.showStructuralDiff', show, StorageScope.WORKSPACE, StorageTarget.USER);
1537-
this.updateStructuralDiffEnablement();
1538-
}
1539-
1540-
private updateStructuralDiffEnablement(): void {
1541-
const enabled = this._showStructuralDiff && this.configurationService.getValue<boolean>('scm.structuralDiff.enabled') === true;
1530+
private updateStructuralDiffEnablement(enabled: boolean): void {
15421531
if (this.structuralDiff.enabled === enabled) {
15431532
return;
15441533
}
@@ -1563,7 +1552,6 @@ export class SCMViewPane extends ViewPane {
15631552

15641553
private viewModeContextKey: IContextKey<ViewMode>;
15651554
private viewSortKeyContextKey: IContextKey<ViewSortKey>;
1566-
private _showStructuralDiff: boolean;
15671555
private showStructuralDiffContextKey: IContextKey<boolean>;
15681556
private areAllRepositoriesCollapsedContextKey: IContextKey<boolean>;
15691557
private isAnyRepositoryCollapsibleContextKey: IContextKey<boolean>;
@@ -1597,7 +1585,6 @@ export class SCMViewPane extends ViewPane {
15971585
super({ ...options, titleMenuId: MenuId.SCMTitle }, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService, hoverService);
15981586

15991587
this.structuralDiff = this.disposables.add(instantiationService.createInstance(SCMStructuralDiffModel));
1600-
this._showStructuralDiff = this.getShowStructuralDiff();
16011588

16021589
// View mode and sort key
16031590
this._viewMode = this.getViewMode();
@@ -1609,8 +1596,7 @@ export class SCMViewPane extends ViewPane {
16091596
this.viewSortKeyContextKey = ContextKeys.SCMViewSortKey.bindTo(contextKeyService);
16101597
this.viewSortKeyContextKey.set(this.viewSortKey);
16111598
this.showStructuralDiffContextKey = ContextKeys.SCMViewShowStructuralDiff.bindTo(contextKeyService);
1612-
this.updateStructuralDiffEnablement();
1613-
Event.filter(this.configurationService.onDidChangeConfiguration, e => e.affectsConfiguration('scm.structuralDiff.enabled'), this.disposables)(() => this.updateStructuralDiffEnablement(), this, this.disposables);
1599+
this.disposables.add(autorun(reader => this.updateStructuralDiffEnablement(this.scmViewService.showStructuralDiff.read(reader))));
16141600
this.disposables.add(bindContextKey(ContextKeys.SCMViewHasStructuralDiffProvider, contextKeyService, reader => this.structuralDiff.hasProvider.read(reader)));
16151601
this.areAllRepositoriesCollapsedContextKey = ContextKeys.SCMViewAreAllRepositoriesCollapsed.bindTo(contextKeyService);
16161602
this.isAnyRepositoryCollapsibleContextKey = ContextKeys.SCMViewIsAnyRepositoryCollapsible.bindTo(contextKeyService);
@@ -1629,9 +1615,6 @@ export class SCMViewPane extends ViewPane {
16291615
case 'scm.viewSortKey':
16301616
this.viewSortKey = this.getViewSortKey();
16311617
break;
1632-
case 'scm.showStructuralDiff':
1633-
this.showStructuralDiff = this.getShowStructuralDiff();
1634-
break;
16351618
}
16361619
}, this, this.disposables);
16371620

@@ -2139,11 +2122,6 @@ export class SCMViewPane extends ViewPane {
21392122
return viewSortKey;
21402123
}
21412124

2142-
/** Structural diffs are shown by default once `scm.structuralDiff.enabled` is set, unless the user hid them in this workspace. */
2143-
private getShowStructuralDiff(): boolean {
2144-
return this.storageService.getBoolean('scm.showStructuralDiff', StorageScope.WORKSPACE, true);
2145-
}
2146-
21472125
private loadTreeViewState(): IAsyncDataTreeViewState | undefined {
21482126
const storageViewState = this.storageService.get('scm.viewState2', StorageScope.WORKSPACE);
21492127
if (!storageViewState) {

‎src/vs/workbench/contrib/scm/browser/scmViewService.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export class SCMViewService implements ISCMViewService {
134134
readonly selectionModeConfig: IObservable<ISCMRepositorySelectionMode>;
135135
readonly graphShowIncomingChangesConfig: IObservable<boolean>;
136136
readonly graphShowOutgoingChangesConfig: IObservable<boolean>;
137+
readonly showStructuralDiff: IObservable<boolean>;
138+
/** The user's choice from the View & Sort menu, which is only shown while `scm.structuralDiff.enabled` is set. */
139+
private readonly showStructuralDiffChoice: ISettableObservable<boolean>;
137140

138141
private didSelectRepository: boolean = false;
139142
private previousState: ISCMViewServiceState | undefined;
@@ -273,6 +276,14 @@ export class SCMViewService implements ISCMViewService {
273276
return explorerEnabledConfig.read(reader) === true && this.selectionModeConfig.read(reader) === ISCMRepositorySelectionMode.Single;
274277
});
275278

279+
this.showStructuralDiffChoice = observableValue(this, this.getShowStructuralDiffChoice());
280+
const structuralDiffEnabled = observableConfigValue<boolean>('scm.structuralDiff.enabled', false, this.configurationService);
281+
this.showStructuralDiff = derived(this, reader => structuralDiffEnabled.read(reader) === true && this.showStructuralDiffChoice.read(reader));
282+
// Another window of the workspace can change the choice.
283+
this.storageService.onDidChangeValue(StorageScope.WORKSPACE, 'scm.showStructuralDiff', this.disposables)(() => {
284+
this.showStructuralDiffChoice.set(this.getShowStructuralDiffChoice(), undefined);
285+
}, this, this.disposables);
286+
276287
try {
277288
this.previousState = JSON.parse(storageService.get('scm:view:visibleRepositories', StorageScope.WORKSPACE, ''));
278289

@@ -576,6 +587,16 @@ export class SCMViewService implements ISCMViewService {
576587
this._activeRepositoryPinnedObs.set(repository, undefined);
577588
}
578589

590+
setShowStructuralDiff(show: boolean): void {
591+
this.storageService.store('scm.showStructuralDiff', show, StorageScope.WORKSPACE, StorageTarget.USER);
592+
this.showStructuralDiffChoice.set(show, undefined);
593+
}
594+
595+
/** Structural changes are shown by default once `scm.structuralDiff.enabled` is set, unless the user hid them in this workspace. */
596+
private getShowStructuralDiffChoice(): boolean {
597+
return this.storageService.getBoolean('scm.showStructuralDiff', StorageScope.WORKSPACE, true);
598+
}
599+
579600
private compareRepositories(op1: ISCMRepositoryView, op2: ISCMRepositoryView): number {
580601
// Sort by discovery time
581602
if (this._repositoriesSortKey === ISCMRepositorySortKey.DiscoveryTime) {

‎src/vs/workbench/contrib/scm/common/scm.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,13 @@ export interface ISCMViewService {
268268
*/
269269
readonly activeRepository: IObservable<{ repository: ISCMRepository; pinned: boolean } | undefined>;
270270
pinActiveRepository(repository: ISCMRepository | undefined): void;
271+
272+
/**
273+
* Whether structural changes are shown, below the files of the Source Control view and of the file list of the
274+
* multi-file diff editor. They require the `scm.structuralDiff.enabled` setting, and the user can hide them in a
275+
* workspace with "Show Structural Diff" in the View & Sort menu of the Source Control view.
276+
*/
277+
readonly showStructuralDiff: IObservable<boolean>;
278+
/** Shows or hides structural changes in this workspace, see {@link showStructuralDiff}. */
279+
setShowStructuralDiff(show: boolean): void;
271280
}

0 commit comments

Comments
 (0)