/
scm-contribution.ts
361 lines (340 loc) · 15.7 KB
/
scm-contribution.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// *****************************************************************************
// Copyright (C) 2019 Red Hat, Inc. 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 { Emitter } from '@theia/core/lib/common/event';
import {
AbstractViewContribution,
FrontendApplicationContribution, LabelProvider,
StatusBar,
StatusBarAlignment,
StatusBarEntry,
KeybindingRegistry,
ViewContainerTitleOptions,
codicon,
StylingParticipant,
ColorTheme,
CssStyleCollector
} from '@theia/core/lib/browser';
import { TabBarToolbarContribution, TabBarToolbarRegistry, TabBarToolbarItem } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { CommandRegistry, Command, Disposable, DisposableCollection, CommandService } from '@theia/core/lib/common';
import { ContextKeyService, ContextKey } from '@theia/core/lib/browser/context-key-service';
import { ScmService } from './scm-service';
import { ScmWidget } from '../browser/scm-widget';
import URI from '@theia/core/lib/common/uri';
import { ScmQuickOpenService } from './scm-quick-open-service';
import { ColorContribution } from '@theia/core/lib/browser/color-application-contribution';
import { ColorRegistry } from '@theia/core/lib/browser/color-registry';
import { Color } from '@theia/core/lib/common/color';
import { ScmCommand } from './scm-provider';
import { ScmDecorationsService } from '../browser/decorations/scm-decorations-service';
import { nls } from '@theia/core/lib/common/nls';
import { isHighContrast } from '@theia/core/lib/common/theme';
export const SCM_WIDGET_FACTORY_ID = ScmWidget.ID;
export const SCM_VIEW_CONTAINER_ID = 'scm-view-container';
export const SCM_VIEW_CONTAINER_TITLE_OPTIONS: ViewContainerTitleOptions = {
label: nls.localizeByDefault('Source Control'),
iconClass: codicon('source-control'),
closeable: true
};
export namespace SCM_COMMANDS {
export const CHANGE_REPOSITORY = {
id: 'scm.change.repository',
category: nls.localizeByDefault('Source Control'),
originalCategory: 'Source Control',
label: nls.localize('theia/scm/changeRepository', 'Change Repository...'),
originalLabel: 'Change Repository...'
};
export const ACCEPT_INPUT = {
id: 'scm.acceptInput'
};
export const TREE_VIEW_MODE = {
id: 'scm.viewmode.tree',
tooltip: nls.localizeByDefault('View as Tree'),
iconClass: codicon('list-tree'),
originalLabel: 'View as Tree',
label: nls.localizeByDefault('View as Tree')
};
export const LIST_VIEW_MODE = {
id: 'scm.viewmode.list',
tooltip: nls.localizeByDefault('View as List'),
iconClass: codicon('list-flat'),
originalLabel: 'View as List',
label: nls.localizeByDefault('View as List')
};
export const COLLAPSE_ALL = {
id: 'scm.collapseAll',
category: nls.localizeByDefault('Source Control'),
originalCategory: 'Source Control',
tooltip: nls.localizeByDefault('Collapse All'),
iconClass: codicon('collapse-all'),
label: nls.localizeByDefault('Collapse All'),
originalLabel: 'Collapse All'
};
}
export namespace ScmColors {
export const editorGutterModifiedBackground = 'editorGutter.modifiedBackground';
export const editorGutterAddedBackground = 'editorGutter.addedBackground';
export const editorGutterDeletedBackground = 'editorGutter.deletedBackground';
}
@injectable()
export class ScmContribution extends AbstractViewContribution<ScmWidget> implements
FrontendApplicationContribution,
TabBarToolbarContribution,
ColorContribution,
StylingParticipant {
@inject(StatusBar) protected readonly statusBar: StatusBar;
@inject(ScmService) protected readonly scmService: ScmService;
@inject(ScmQuickOpenService) protected readonly scmQuickOpenService: ScmQuickOpenService;
@inject(LabelProvider) protected readonly labelProvider: LabelProvider;
@inject(CommandService) protected readonly commands: CommandService;
@inject(CommandRegistry) protected readonly commandRegistry: CommandRegistry;
@inject(ContextKeyService) protected readonly contextKeys: ContextKeyService;
@inject(ScmDecorationsService) protected readonly scmDecorationsService: ScmDecorationsService;
protected scmFocus: ContextKey<boolean>;
constructor() {
super({
viewContainerId: SCM_VIEW_CONTAINER_ID,
widgetId: SCM_WIDGET_FACTORY_ID,
widgetName: SCM_VIEW_CONTAINER_TITLE_OPTIONS.label,
defaultWidgetOptions: {
area: 'left',
rank: 300
},
toggleCommandId: 'scmView:toggle',
toggleKeybinding: 'ctrlcmd+shift+g'
});
}
@postConstruct()
protected init(): void {
this.scmFocus = this.contextKeys.createKey('scmFocus', false);
}
async initializeLayout(): Promise<void> {
await this.openView();
}
onStart(): void {
this.updateStatusBar();
this.scmService.onDidAddRepository(() => this.updateStatusBar());
this.scmService.onDidRemoveRepository(() => this.updateStatusBar());
this.scmService.onDidChangeSelectedRepository(() => this.updateStatusBar());
this.scmService.onDidChangeStatusBarCommands(() => this.updateStatusBar());
this.labelProvider.onDidChange(() => this.updateStatusBar());
this.updateContextKeys();
this.shell.onDidChangeCurrentWidget(() => this.updateContextKeys());
}
protected updateContextKeys(): void {
this.scmFocus.set(this.shell.currentWidget instanceof ScmWidget);
}
override registerCommands(commandRegistry: CommandRegistry): void {
super.registerCommands(commandRegistry);
commandRegistry.registerCommand(SCM_COMMANDS.CHANGE_REPOSITORY, {
execute: () => this.scmQuickOpenService.changeRepository(),
isEnabled: () => this.scmService.repositories.length > 1
});
commandRegistry.registerCommand(SCM_COMMANDS.ACCEPT_INPUT, {
execute: () => this.acceptInput(),
isEnabled: () => !!this.scmFocus.get() && !!this.acceptInputCommand()
});
}
registerToolbarItems(registry: TabBarToolbarRegistry): void {
const viewModeEmitter = new Emitter<void>();
const registerToggleViewItem = (command: Command, mode: 'tree' | 'list') => {
const id = command.id;
const item: TabBarToolbarItem = {
id,
command: id,
tooltip: command.label,
onDidChange: viewModeEmitter.event
};
this.commandRegistry.registerCommand({ id, iconClass: command && command.iconClass }, {
execute: widget => {
if (widget instanceof ScmWidget) {
widget.viewMode = mode;
viewModeEmitter.fire();
}
},
isVisible: widget => {
if (widget instanceof ScmWidget) {
return !!this.scmService.selectedRepository
&& widget.viewMode !== mode;
}
return false;
},
});
registry.registerItem(item);
};
registerToggleViewItem(SCM_COMMANDS.TREE_VIEW_MODE, 'tree');
registerToggleViewItem(SCM_COMMANDS.LIST_VIEW_MODE, 'list');
this.commandRegistry.registerCommand(SCM_COMMANDS.COLLAPSE_ALL, {
execute: widget => {
if (widget instanceof ScmWidget && widget.viewMode === 'tree') {
widget.collapseScmTree();
}
},
isVisible: widget => {
if (widget instanceof ScmWidget) {
return !!this.scmService.selectedRepository && widget.viewMode === 'tree';
}
return false;
}
});
registry.registerItem({
...SCM_COMMANDS.COLLAPSE_ALL,
command: SCM_COMMANDS.COLLAPSE_ALL.id
});
}
override registerKeybindings(keybindings: KeybindingRegistry): void {
super.registerKeybindings(keybindings);
keybindings.registerKeybinding({
command: SCM_COMMANDS.ACCEPT_INPUT.id,
keybinding: 'ctrlcmd+enter',
when: 'scmFocus'
});
}
protected async acceptInput(): Promise<void> {
const command = this.acceptInputCommand();
if (command && command.command) {
await this.commands.executeCommand(command.command, ...command.arguments ? command.arguments : []);
}
}
protected acceptInputCommand(): ScmCommand | undefined {
const repository = this.scmService.selectedRepository;
if (!repository) {
return undefined;
}
return repository.provider.acceptInputCommand;
}
protected readonly statusBarDisposable = new DisposableCollection();
protected updateStatusBar(): void {
this.statusBarDisposable.dispose();
const repository = this.scmService.selectedRepository;
if (!repository) {
return;
}
const name = this.labelProvider.getName(new URI(repository.provider.rootUri));
if (this.scmService.repositories.length > 1) {
this.setStatusBarEntry(SCM_COMMANDS.CHANGE_REPOSITORY.id, {
text: `$(database) ${name}`,
tooltip: name.toString(),
command: SCM_COMMANDS.CHANGE_REPOSITORY.id,
alignment: StatusBarAlignment.LEFT,
priority: 100
});
}
const label = repository.provider.rootUri ? `${name} (${repository.provider.label})` : repository.provider.label;
this.scmService.statusBarCommands.forEach((value, index) => this.setStatusBarEntry(`scm.status.${index}`, {
text: value.title,
tooltip: label + (value.tooltip ? ` - ${value.tooltip}` : ''),
command: value.command,
arguments: value.arguments,
alignment: StatusBarAlignment.LEFT,
priority: 100
}));
}
protected setStatusBarEntry(id: string, entry: StatusBarEntry): void {
this.statusBar.setElement(id, entry);
this.statusBarDisposable.push(Disposable.create(() => this.statusBar.removeElement(id)));
}
/**
* It should be aligned with https://github.com/microsoft/vscode/blob/0dfa355b3ad185a6289ba28a99c141ab9e72d2be/src/vs/workbench/contrib/scm/browser/dirtydiffDecorator.ts#L808
*/
registerColors(colors: ColorRegistry): void {
colors.register(
{
id: ScmColors.editorGutterModifiedBackground, defaults: {
dark: '#1B81A8',
light: '#2090D3',
hcDark: '#1B81A8',
hcLight: '#2090D3'
}, description: 'Editor gutter background color for lines that are modified.'
},
{
id: ScmColors.editorGutterAddedBackground, defaults: {
dark: '#487E02',
light: '#48985D',
hcDark: '#487E02',
hcLight: '#48985D'
}, description: 'Editor gutter background color for lines that are added.'
},
{
id: ScmColors.editorGutterDeletedBackground, defaults: {
dark: 'editorError.foreground',
light: 'editorError.foreground',
hcDark: 'editorError.foreground',
hcLight: 'editorError.foreground'
}, description: 'Editor gutter background color for lines that are deleted.'
},
{
id: 'minimapGutter.modifiedBackground', defaults: {
dark: 'editorGutter.modifiedBackground',
light: 'editorGutter.modifiedBackground',
hcDark: 'editorGutter.modifiedBackground',
hcLight: 'editorGutter.modifiedBackground'
}, description: 'Minimap gutter background color for lines that are modified.'
},
{
id: 'minimapGutter.addedBackground', defaults: {
dark: 'editorGutter.addedBackground',
light: 'editorGutter.addedBackground',
hcDark: 'editorGutter.modifiedBackground',
hcLight: 'editorGutter.modifiedBackground'
}, description: 'Minimap gutter background color for lines that are added.'
},
{
id: 'minimapGutter.deletedBackground', defaults: {
dark: 'editorGutter.deletedBackground',
light: 'editorGutter.deletedBackground',
hcDark: 'editorGutter.deletedBackground',
hcLight: 'editorGutter.deletedBackground'
}, description: 'Minimap gutter background color for lines that are deleted.'
},
{
id: 'editorOverviewRuler.modifiedForeground', defaults: {
dark: Color.transparent(ScmColors.editorGutterModifiedBackground, 0.6),
light: Color.transparent(ScmColors.editorGutterModifiedBackground, 0.6),
hcDark: Color.transparent(ScmColors.editorGutterModifiedBackground, 0.6),
hcLight: Color.transparent(ScmColors.editorGutterModifiedBackground, 0.6)
}, description: 'Overview ruler marker color for modified content.'
},
{
id: 'editorOverviewRuler.addedForeground', defaults: {
dark: Color.transparent(ScmColors.editorGutterAddedBackground, 0.6),
light: Color.transparent(ScmColors.editorGutterAddedBackground, 0.6),
hcDark: Color.transparent(ScmColors.editorGutterAddedBackground, 0.6),
hcLight: Color.transparent(ScmColors.editorGutterAddedBackground, 0.6)
}, description: 'Overview ruler marker color for added content.'
},
{
id: 'editorOverviewRuler.deletedForeground', defaults: {
dark: Color.transparent(ScmColors.editorGutterDeletedBackground, 0.6),
light: Color.transparent(ScmColors.editorGutterDeletedBackground, 0.6),
hcDark: Color.transparent(ScmColors.editorGutterDeletedBackground, 0.6),
hcLight: Color.transparent(ScmColors.editorGutterDeletedBackground, 0.6)
}, description: 'Overview ruler marker color for deleted content.'
}
);
}
registerThemeStyle(theme: ColorTheme, collector: CssStyleCollector): void {
const contrastBorder = theme.getColor('contrastBorder');
if (contrastBorder && isHighContrast(theme.type)) {
collector.addRule(`
.theia-scm-input-message-container textarea {
outline: var(--theia-border-width) solid ${contrastBorder};
outline-offset: -1px;
}
`);
}
}
}