/
git-diff-widget.tsx
151 lines (128 loc) · 5.57 KB
/
git-diff-widget.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// *****************************************************************************
// Copyright (C) 2018 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import {
BaseWidget, Widget, StatefulWidget, Panel, PanelLayout, Message, MessageLoop, codicon
} from '@theia/core/lib/browser';
import { EditorManager, DiffNavigatorProvider } from '@theia/editor/lib/browser';
import { GitDiffTreeModel } from './git-diff-tree-model';
import { GitWatcher } from '../../common';
import { GitDiffHeaderWidget } from './git-diff-header-widget';
import { ScmService } from '@theia/scm/lib/browser/scm-service';
import { GitRepositoryProvider } from '../git-repository-provider';
import { ScmTreeWidget } from '@theia/scm/lib/browser/scm-tree-widget';
import { ScmPreferences } from '@theia/scm/lib/browser/scm-preferences';
import { nls } from '@theia/core';
export const GIT_DIFF = 'git-diff';
@injectable()
export class GitDiffWidget extends BaseWidget implements StatefulWidget {
protected readonly GIT_DIFF_TITLE = nls.localize('theia/git/diff', 'Diff');
@inject(GitRepositoryProvider) protected readonly repositoryProvider: GitRepositoryProvider;
@inject(DiffNavigatorProvider) protected readonly diffNavigatorProvider: DiffNavigatorProvider;
@inject(EditorManager) protected readonly editorManager: EditorManager;
@inject(GitWatcher) protected readonly gitWatcher: GitWatcher;
@inject(GitDiffHeaderWidget) protected readonly diffHeaderWidget: GitDiffHeaderWidget;
@inject(ScmTreeWidget) protected readonly resourceWidget: ScmTreeWidget;
@inject(GitDiffTreeModel) protected readonly model: GitDiffTreeModel;
@inject(ScmService) protected readonly scmService: ScmService;
@inject(ScmPreferences) protected readonly scmPreferences: ScmPreferences;
protected panel: Panel;
constructor() {
super();
this.id = GIT_DIFF;
this.title.label = this.GIT_DIFF_TITLE;
this.title.caption = this.GIT_DIFF_TITLE;
this.title.closable = true;
this.title.iconClass = codicon('git-compare');
this.addClass('theia-scm');
this.addClass('theia-git');
this.addClass('git-diff-container');
}
@postConstruct()
protected init(): void {
const layout = new PanelLayout();
this.layout = layout;
this.panel = new Panel({
layout: new PanelLayout({
})
});
this.panel.node.tabIndex = -1;
this.panel.node.setAttribute('class', 'theia-scm-panel');
layout.addWidget(this.panel);
this.containerLayout.addWidget(this.diffHeaderWidget);
this.containerLayout.addWidget(this.resourceWidget);
this.updateViewMode(this.scmPreferences.get('scm.defaultViewMode'));
this.toDispose.push(this.scmPreferences.onPreferenceChanged(e => {
if (e.preferenceName === 'scm.defaultViewMode') {
this.updateViewMode(e.newValue);
}
}));
}
set viewMode(mode: 'tree' | 'list') {
this.resourceWidget.viewMode = mode;
}
get viewMode(): 'tree' | 'list' {
return this.resourceWidget.viewMode;
}
async setContent(options: GitDiffTreeModel.Options): Promise<void> {
this.model.setContent(options);
this.diffHeaderWidget.setContent(options.diffOptions);
this.update();
}
get containerLayout(): PanelLayout {
return this.panel.layout as PanelLayout;
}
/**
* Updates the view mode based on the preference value.
* @param preference the view mode preference.
*/
protected updateViewMode(preference: 'tree' | 'list'): void {
this.viewMode = preference;
}
protected updateImmediately(): void {
this.onUpdateRequest(Widget.Msg.UpdateRequest);
}
protected override onUpdateRequest(msg: Message): void {
MessageLoop.sendMessage(this.diffHeaderWidget, msg);
MessageLoop.sendMessage(this.resourceWidget, msg);
super.onUpdateRequest(msg);
}
protected override onAfterAttach(msg: Message): void {
this.node.appendChild(this.diffHeaderWidget.node);
this.node.appendChild(this.resourceWidget.node);
super.onAfterAttach(msg);
this.update();
}
goToPreviousChange(): void {
this.resourceWidget.goToPreviousChange();
}
goToNextChange(): void {
this.resourceWidget.goToNextChange();
}
storeState(): object {
const state = {
commitState: this.diffHeaderWidget.storeState(),
changesTreeState: this.resourceWidget.storeState(),
};
return state;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
restoreState(oldState: any): void {
const { commitState, changesTreeState } = oldState;
this.diffHeaderWidget.restoreState(commitState);
this.resourceWidget.restoreState(changesTreeState);
}
}