-
Notifications
You must be signed in to change notification settings - Fork 29.5k
/
explorerViewer.ts
1075 lines (865 loc) · 38.4 KB
/
explorerViewer.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { TPromise } from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import lifecycle = require('vs/base/common/lifecycle');
import objects = require('vs/base/common/objects');
import DOM = require('vs/base/browser/dom');
import URI from 'vs/base/common/uri';
import { once } from 'vs/base/common/functional';
import paths = require('vs/base/common/paths');
import resources = require('vs/base/common/resources');
import errors = require('vs/base/common/errors');
import { IAction, ActionRunner as BaseActionRunner, IActionRunner } from 'vs/base/common/actions';
import comparers = require('vs/base/common/comparers');
import { InputBox, MessageType } from 'vs/base/browser/ui/inputbox/inputBox';
import { isMacintosh, isLinux } from 'vs/base/common/platform';
import glob = require('vs/base/common/glob');
import { FileLabel, IFileLabelOptions } from 'vs/workbench/browser/labels';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { IFilesConfiguration, SortOrder } from 'vs/workbench/parts/files/common/files';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { FileOperationError, FileOperationResult, IFileService, FileKind } from 'vs/platform/files/common/files';
import { ResourceMap } from 'vs/base/common/map';
import { DuplicateFileAction, ImportFileAction, IEditableData, IFileViewletState, FileCopiedContext } from 'vs/workbench/parts/files/electron-browser/fileActions';
import { IDataSource, ITree, IAccessibilityProvider, IRenderer, ContextMenuEvent, ISorter, IFilter, IDragAndDropData, IDragOverReaction, DRAG_OVER_ACCEPT_BUBBLE_DOWN, DRAG_OVER_ACCEPT_BUBBLE_DOWN_COPY, DRAG_OVER_ACCEPT_BUBBLE_UP, DRAG_OVER_ACCEPT_BUBBLE_UP_COPY, DRAG_OVER_REJECT } from 'vs/base/parts/tree/browser/tree';
import { DesktopDragAndDropData, ExternalElementsDragAndDropData } from 'vs/base/parts/tree/browser/treeDnd';
import { ClickBehavior } from 'vs/base/parts/tree/browser/treeDefaults';
import { FileStat, NewStatPlaceholder, Model } from 'vs/workbench/parts/files/common/explorerModel';
import { DragMouseEvent, IMouseEvent } from 'vs/base/browser/mouseEvent';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IPartService } from 'vs/workbench/services/part/common/partService';
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService, ConfigurationTarget } from 'vs/platform/configuration/common/configuration';
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IContextViewService, IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IProgressService } from 'vs/platform/progress/common/progress';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { KeyCode } from 'vs/base/common/keyCodes';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { IMenuService, IMenu, MenuId } from 'vs/platform/actions/common/actions';
import { fillInActions } from 'vs/platform/actions/browser/menuItemActionItem';
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
import { attachInputBoxStyler } from 'vs/platform/theme/common/styler';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { IWindowService } from 'vs/platform/windows/common/windows';
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import { extractResources, SimpleFileResourceDragAndDrop, CodeDataTransfers, fillResourceDataTransfers } from 'vs/workbench/browser/dnd';
import { relative } from 'path';
import { distinctParents } from 'vs/base/common/resources';
import { WorkbenchTree, WorkbenchTreeController } from 'vs/platform/list/browser/listService';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { DataTransfers } from 'vs/base/browser/dnd';
import { Schemas } from 'vs/base/common/network';
import { IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import { rtrim } from 'vs/base/common/strings';
import { IDialogService, IConfirmationResult, IConfirmation, getConfirmMessage } from 'vs/platform/dialogs/common/dialogs';
import { INotificationService } from 'vs/platform/notification/common/notification';
export class FileDataSource implements IDataSource {
constructor(
@IProgressService private progressService: IProgressService,
@INotificationService private notificationService: INotificationService,
@IFileService private fileService: IFileService,
@IPartService private partService: IPartService
) { }
public getId(tree: ITree, stat: FileStat | Model): string {
if (stat instanceof Model) {
return 'model';
}
return `${stat.root.resource.toString()}:${stat.getId()}`;
}
public hasChildren(tree: ITree, stat: FileStat | Model): boolean {
return stat instanceof Model || (stat instanceof FileStat && stat.isDirectory);
}
public getChildren(tree: ITree, stat: FileStat | Model): TPromise<FileStat[]> {
if (stat instanceof Model) {
return TPromise.as(stat.roots);
}
// Return early if stat is already resolved
if (stat.isDirectoryResolved) {
return TPromise.as(stat.children);
}
// Resolve children and add to fileStat for future lookup
else {
// Resolve
const promise = this.fileService.resolveFile(stat.resource, { resolveSingleChildDescendants: true }).then(dirStat => {
// Convert to view model
const modelDirStat = FileStat.create(dirStat, stat.root);
// Add children to folder
for (let i = 0; i < modelDirStat.children.length; i++) {
stat.addChild(modelDirStat.children[i]);
}
stat.isDirectoryResolved = true;
return stat.children;
}, (e: any) => {
this.notificationService.error(e);
return []; // we could not resolve any children because of an error
});
this.progressService.showWhile(promise, this.partService.isCreated() ? 800 : 3200 /* less ugly initial startup */);
return promise;
}
}
public getParent(tree: ITree, stat: FileStat | Model): TPromise<FileStat> {
if (!stat) {
return TPromise.as(null); // can be null if nothing selected in the tree
}
// Return if root reached
if (tree.getInput() === stat) {
return TPromise.as(null);
}
// Return if parent already resolved
if (stat instanceof FileStat && stat.parent) {
return TPromise.as(stat.parent);
}
// We never actually resolve the parent from the disk for performance reasons. It wouldnt make
// any sense to resolve parent by parent with requests to walk up the chain. Instead, the explorer
// makes sure to properly resolve a deep path to a specific file and merges the result with the model.
return TPromise.as(null);
}
}
export class FileViewletState implements IFileViewletState {
private editableStats: ResourceMap<IEditableData>;
constructor() {
this.editableStats = new ResourceMap<IEditableData>();
}
public getEditableData(stat: FileStat): IEditableData {
return this.editableStats.get(stat.resource);
}
public setEditable(stat: FileStat, editableData: IEditableData): void {
if (editableData) {
this.editableStats.set(stat.resource, editableData);
}
}
public clearEditable(stat: FileStat): void {
this.editableStats.delete(stat.resource);
}
}
export class ActionRunner extends BaseActionRunner implements IActionRunner {
private viewletState: FileViewletState;
constructor(state: FileViewletState) {
super();
this.viewletState = state;
}
public run(action: IAction, context?: any): TPromise<any> {
return super.run(action, { viewletState: this.viewletState });
}
}
export interface IFileTemplateData {
label: FileLabel;
container: HTMLElement;
}
// Explorer Renderer
export class FileRenderer implements IRenderer {
private static readonly ITEM_HEIGHT = 22;
private static readonly FILE_TEMPLATE_ID = 'file';
private state: FileViewletState;
private config: IFilesConfiguration;
private configListener: IDisposable;
constructor(
state: FileViewletState,
@IContextViewService private contextViewService: IContextViewService,
@IInstantiationService private instantiationService: IInstantiationService,
@IThemeService private themeService: IThemeService,
@IConfigurationService private configurationService: IConfigurationService,
@IWorkspaceContextService private contextService: IWorkspaceContextService
) {
this.state = state;
this.config = this.configurationService.getValue<IFilesConfiguration>();
this.configListener = this.configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('explorer')) {
this.config = this.configurationService.getValue();
}
});
}
dispose(): void {
this.configListener.dispose();
}
public getHeight(tree: ITree, element: any): number {
return FileRenderer.ITEM_HEIGHT;
}
public getTemplateId(tree: ITree, element: any): string {
return FileRenderer.FILE_TEMPLATE_ID;
}
public disposeTemplate(tree: ITree, templateId: string, templateData: IFileTemplateData): void {
templateData.label.dispose();
}
public renderTemplate(tree: ITree, templateId: string, container: HTMLElement): IFileTemplateData {
const label = this.instantiationService.createInstance(FileLabel, container, void 0);
return { label, container };
}
public renderElement(tree: ITree, stat: FileStat, templateId: string, templateData: IFileTemplateData): void {
const editableData: IEditableData = this.state.getEditableData(stat);
// File Label
if (!editableData) {
templateData.label.element.style.display = 'flex';
const extraClasses = ['explorer-item'];
templateData.label.setFile(stat.resource, {
hidePath: true,
fileKind: stat.isRoot ? FileKind.ROOT_FOLDER : stat.isDirectory ? FileKind.FOLDER : FileKind.FILE,
extraClasses,
fileDecorations: this.config.explorer.decorations
});
}
// Input Box
else {
templateData.label.element.style.display = 'none';
this.renderInputBox(templateData.container, tree, stat, editableData);
}
}
private renderInputBox(container: HTMLElement, tree: ITree, stat: FileStat, editableData: IEditableData): void {
// Use a file label only for the icon next to the input box
const label = this.instantiationService.createInstance(FileLabel, container, void 0);
const extraClasses = ['explorer-item', 'explorer-item-edited'];
const fileKind = stat.isRoot ? FileKind.ROOT_FOLDER : (stat.isDirectory || (stat instanceof NewStatPlaceholder && stat.isDirectoryPlaceholder())) ? FileKind.FOLDER : FileKind.FILE;
const labelOptions: IFileLabelOptions = { hidePath: true, hideLabel: true, fileKind, extraClasses };
label.setFile(stat.resource, labelOptions);
// Input field for name
const inputBox = new InputBox(label.element, this.contextViewService, {
validationOptions: {
validation: editableData.validator
},
ariaLabel: nls.localize('fileInputAriaLabel', "Type file name. Press Enter to confirm or Escape to cancel.")
});
const styler = attachInputBoxStyler(inputBox, this.themeService);
const parent = resources.dirname(stat.resource);
inputBox.onDidChange(value => {
label.setFile(parent.with({ path: paths.join(parent.path, value) }), labelOptions); // update label icon while typing!
});
const value = stat.name || '';
const lastDot = value.lastIndexOf('.');
inputBox.value = value;
inputBox.select({ start: 0, end: lastDot > 0 && !stat.isDirectory ? lastDot : value.length });
inputBox.focus();
const done = once((commit: boolean, blur: boolean) => {
tree.clearHighlight();
if (commit && inputBox.value) {
editableData.action.run({ value: inputBox.value });
}
setTimeout(() => {
if (!blur) { // https://github.com/Microsoft/vscode/issues/20269
tree.domFocus();
}
lifecycle.dispose(toDispose);
container.removeChild(label.element);
}, 0);
});
const toDispose = [
inputBox,
DOM.addStandardDisposableListener(inputBox.inputElement, DOM.EventType.KEY_DOWN, (e: IKeyboardEvent) => {
if (e.equals(KeyCode.Enter)) {
if (inputBox.validate()) {
done(true, false);
}
} else if (e.equals(KeyCode.Escape)) {
done(false, false);
}
}),
DOM.addStandardDisposableListener(inputBox.inputElement, DOM.EventType.KEY_UP, (e: IKeyboardEvent) => {
const initialRelPath: string = relative(stat.root.resource.fsPath, stat.parent.resource.fsPath);
let projectFolderName: string = '';
if (this.contextService.getWorkbenchState() === WorkbenchState.WORKSPACE) {
projectFolderName = paths.basename(stat.root.resource.fsPath); // show root folder name in multi-folder project
}
this.displayCurrentPath(inputBox, initialRelPath, fileKind, projectFolderName);
}),
DOM.addDisposableListener(inputBox.inputElement, DOM.EventType.BLUR, () => {
done(inputBox.isInputValid(), true);
}),
label,
styler
];
}
private displayCurrentPath(inputBox: InputBox, initialRelPath: string, fileKind: FileKind, projectFolderName: string = '') {
if (inputBox.validate()) {
const value = inputBox.value;
if (value && value.search(/[\\/]/) !== -1) { // only show if there's a slash
let newPath = paths.normalize(paths.join(initialRelPath, value), true);
newPath = rtrim(newPath, paths.nativeSep);
const fileType: string = FileKind[fileKind].toLowerCase();
inputBox.showMessage({
type: MessageType.INFO,
content: nls.localize('constructedPath', "Create {0} in **{1}**", fileType, newPath),
formatContent: true
});
}
}
}
}
// Explorer Accessibility Provider
export class FileAccessibilityProvider implements IAccessibilityProvider {
public getAriaLabel(tree: ITree, stat: FileStat): string {
return nls.localize('filesExplorerViewerAriaLabel', "{0}, Files Explorer", stat.name);
}
}
// Explorer Controller
export class FileController extends WorkbenchTreeController implements IDisposable {
private fileCopiedContextKey: IContextKey<boolean>;
private contributedContextMenu: IMenu;
private toDispose: IDisposable[];
private previousSelectionRangeStop: FileStat;
constructor(
@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
@IContextMenuService private contextMenuService: IContextMenuService,
@ITelemetryService private telemetryService: ITelemetryService,
@IMenuService private menuService: IMenuService,
@IContextKeyService contextKeyService: IContextKeyService,
@IClipboardService private clipboardService: IClipboardService,
@IConfigurationService configurationService: IConfigurationService
) {
super({ clickBehavior: ClickBehavior.ON_MOUSE_UP /* do not change to not break DND */ }, configurationService);
this.fileCopiedContextKey = FileCopiedContext.bindTo(contextKeyService);
this.toDispose = [];
}
public onLeftClick(tree: WorkbenchTree, stat: FileStat | Model, event: IMouseEvent, origin: string = 'mouse'): boolean {
const payload = { origin: origin };
const isDoubleClick = (origin === 'mouse' && event.detail === 2);
// Handle Highlight Mode
if (tree.getHighlight()) {
// Cancel Event
event.preventDefault();
event.stopPropagation();
tree.clearHighlight(payload);
return false;
}
// Handle root
if (stat instanceof Model) {
tree.clearFocus(payload);
tree.clearSelection(payload);
return false;
}
// Cancel Event
const isMouseDown = event && event.browserEvent && event.browserEvent.type === 'mousedown';
if (!isMouseDown) {
event.preventDefault(); // we cannot preventDefault onMouseDown because this would break DND otherwise
}
event.stopPropagation();
// Set DOM focus
tree.domFocus();
if (stat instanceof NewStatPlaceholder) {
return true;
}
// Allow to multiselect
if ((tree.useAltAsMultipleSelectionModifier && event.altKey) || !tree.useAltAsMultipleSelectionModifier && (event.ctrlKey || event.metaKey)) {
const selection = tree.getSelection();
this.previousSelectionRangeStop = undefined;
if (selection.indexOf(stat) >= 0) {
tree.setSelection(selection.filter(s => s !== stat));
} else {
tree.setSelection(selection.concat(stat));
tree.setFocus(stat, payload);
}
}
// Allow to unselect
else if (event.shiftKey) {
const focus = tree.getFocus();
if (focus) {
if (this.previousSelectionRangeStop) {
tree.deselectRange(stat, this.previousSelectionRangeStop);
}
tree.selectRange(focus, stat, payload);
this.previousSelectionRangeStop = stat;
}
}
// Select, Focus and open files
else {
// Expand / Collapse
if (isDoubleClick || this.openOnSingleClick || this.isClickOnTwistie(event)) {
tree.toggleExpansion(stat, event.altKey);
this.previousSelectionRangeStop = undefined;
}
const preserveFocus = !isDoubleClick;
tree.setFocus(stat, payload);
if (isDoubleClick) {
event.preventDefault(); // focus moves to editor, we need to prevent default
}
tree.setSelection([stat], payload);
if (!stat.isDirectory && (isDoubleClick || this.openOnSingleClick)) {
let sideBySide = false;
if (event) {
sideBySide = tree.useAltAsMultipleSelectionModifier ? (event.ctrlKey || event.metaKey) : event.altKey;
}
this.openEditor(stat, { preserveFocus, sideBySide, pinned: isDoubleClick });
}
}
return true;
}
public onContextMenu(tree: WorkbenchTree, stat: FileStat | Model, event: ContextMenuEvent): boolean {
if (event.target && event.target.tagName && event.target.tagName.toLowerCase() === 'input') {
return false;
}
event.preventDefault();
event.stopPropagation();
tree.setFocus(stat);
// update dynamic contexts
this.fileCopiedContextKey.set(this.clipboardService.hasFiles());
if (!this.contributedContextMenu) {
this.contributedContextMenu = this.menuService.createMenu(MenuId.ExplorerContext, tree.contextKeyService);
this.toDispose.push(this.contributedContextMenu);
}
const anchor = { x: event.posx, y: event.posy };
const selection = tree.getSelection();
this.contextMenuService.showContextMenu({
getAnchor: () => anchor,
getActions: () => {
const actions: IAction[] = [];
fillInActions(this.contributedContextMenu, { arg: stat instanceof FileStat ? stat.resource : {}, shouldForwardArgs: true }, actions, this.contextMenuService);
return TPromise.as(actions);
},
onHide: (wasCancelled?: boolean) => {
if (wasCancelled) {
tree.domFocus();
}
},
getActionsContext: () => selection && selection.indexOf(stat) >= 0
? selection.map((fs: FileStat) => fs.resource)
: stat instanceof FileStat ? [stat.resource] : []
});
return true;
}
public openEditor(stat: FileStat, options: { preserveFocus: boolean; sideBySide: boolean; pinned: boolean; }): void {
if (stat && !stat.isDirectory) {
/* __GDPR__
"workbenchActionExecuted" : {
"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
this.telemetryService.publicLog('workbenchActionExecuted', { id: 'workbench.files.openFile', from: 'explorer' });
this.editorService.openEditor({ resource: stat.resource, options }, options.sideBySide).done(null, errors.onUnexpectedError);
}
}
public dispose(): void {
this.toDispose = dispose(this.toDispose);
}
}
// Explorer Sorter
export class FileSorter implements ISorter {
private toDispose: IDisposable[];
private sortOrder: SortOrder;
constructor(
@IConfigurationService private configurationService: IConfigurationService,
@IWorkspaceContextService private contextService: IWorkspaceContextService
) {
this.toDispose = [];
this.updateSortOrder();
this.registerListeners();
}
private registerListeners(): void {
this.toDispose.push(this.configurationService.onDidChangeConfiguration(e => this.updateSortOrder()));
}
private updateSortOrder(): void {
this.sortOrder = this.configurationService.getValue('explorer.sortOrder') || 'default';
}
public compare(tree: ITree, statA: FileStat, statB: FileStat): number {
// Do not sort roots
if (statA.isRoot) {
if (statB.isRoot) {
return this.contextService.getWorkspaceFolder(statA.resource).index - this.contextService.getWorkspaceFolder(statB.resource).index;
}
return -1;
}
if (statB.isRoot) {
return 1;
}
// Sort Directories
switch (this.sortOrder) {
case 'type':
if (statA.isDirectory && !statB.isDirectory) {
return -1;
}
if (statB.isDirectory && !statA.isDirectory) {
return 1;
}
if (statA.isDirectory && statB.isDirectory) {
return comparers.compareFileNames(statA.name, statB.name);
}
break;
case 'filesFirst':
if (statA.isDirectory && !statB.isDirectory) {
return 1;
}
if (statB.isDirectory && !statA.isDirectory) {
return -1;
}
break;
case 'mixed':
break; // not sorting when "mixed" is on
default: /* 'default', 'modified' */
if (statA.isDirectory && !statB.isDirectory) {
return -1;
}
if (statB.isDirectory && !statA.isDirectory) {
return 1;
}
break;
}
// Sort "New File/Folder" placeholders
if (statA instanceof NewStatPlaceholder) {
return -1;
}
if (statB instanceof NewStatPlaceholder) {
return 1;
}
// Sort Files
switch (this.sortOrder) {
case 'type':
return comparers.compareFileExtensions(statA.name, statB.name);
case 'modified':
if (statA.mtime !== statB.mtime) {
return statA.mtime < statB.mtime ? 1 : -1;
}
return comparers.compareFileNames(statA.name, statB.name);
default: /* 'default', 'mixed', 'filesFirst' */
return comparers.compareFileNames(statA.name, statB.name);
}
}
public dispose(): void {
this.toDispose = dispose(this.toDispose);
}
}
// Explorer Filter
export class FileFilter implements IFilter {
private static readonly MAX_SIBLINGS_FILTER_THRESHOLD = 2000;
private hiddenExpressionPerRoot: Map<string, glob.IExpression>;
private workspaceFolderChangeListener: IDisposable;
constructor(
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@IConfigurationService private configurationService: IConfigurationService
) {
this.hiddenExpressionPerRoot = new Map<string, glob.IExpression>();
this.registerListeners();
}
public registerListeners(): void {
this.workspaceFolderChangeListener = this.contextService.onDidChangeWorkspaceFolders(() => this.updateConfiguration());
}
public updateConfiguration(): boolean {
let needsRefresh = false;
this.contextService.getWorkspace().folders.forEach(folder => {
const configuration = this.configurationService.getValue<IFilesConfiguration>({ resource: folder.uri });
const excludesConfig = (configuration && configuration.files && configuration.files.exclude) || Object.create(null);
needsRefresh = needsRefresh || !objects.equals(this.hiddenExpressionPerRoot.get(folder.uri.toString()), excludesConfig);
this.hiddenExpressionPerRoot.set(folder.uri.toString(), objects.deepClone(excludesConfig)); // do not keep the config, as it gets mutated under our hoods
});
return needsRefresh;
}
public isVisible(tree: ITree, stat: FileStat): boolean {
return this.doIsVisible(stat);
}
private doIsVisible(stat: FileStat): boolean {
if (stat instanceof NewStatPlaceholder || stat.isRoot) {
return true; // always visible
}
// Workaround for O(N^2) complexity (https://github.com/Microsoft/vscode/issues/9962)
let siblings = stat.parent && stat.parent.children && stat.parent.children;
if (siblings && siblings.length > FileFilter.MAX_SIBLINGS_FILTER_THRESHOLD) {
siblings = void 0;
}
// Hide those that match Hidden Patterns
const siblingsFn = () => siblings && siblings.map(c => c.name);
const expression = this.hiddenExpressionPerRoot.get(stat.root.resource.toString()) || Object.create(null);
if (glob.match(expression, paths.normalize(relative(stat.root.resource.fsPath, stat.resource.fsPath), true), siblingsFn)) {
return false; // hidden through pattern
}
return true;
}
public dispose(): void {
this.workspaceFolderChangeListener = dispose(this.workspaceFolderChangeListener);
}
}
// Explorer Drag And Drop Controller
export class FileDragAndDrop extends SimpleFileResourceDragAndDrop {
private static readonly CONFIRM_DND_SETTING_KEY = 'explorer.confirmDragAndDrop';
private toDispose: IDisposable[];
private dropEnabled: boolean;
constructor(
@INotificationService private notificationService: INotificationService,
@IDialogService private dialogService: IDialogService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@IFileService private fileService: IFileService,
@IConfigurationService private configurationService: IConfigurationService,
@IInstantiationService instantiationService: IInstantiationService,
@ITextFileService private textFileService: ITextFileService,
@IBackupFileService private backupFileService: IBackupFileService,
@IWindowService private windowService: IWindowService,
@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService
) {
super(stat => this.statToResource(stat), instantiationService);
this.toDispose = [];
this.updateDropEnablement();
this.registerListeners();
}
private statToResource(stat: FileStat): URI {
if (stat.isDirectory) {
return URI.from({ scheme: 'folder', path: stat.resource.path }); // indicates that we are dragging a folder
}
return stat.resource;
}
private registerListeners(): void {
this.toDispose.push(this.configurationService.onDidChangeConfiguration(e => this.updateDropEnablement()));
}
private updateDropEnablement(): void {
this.dropEnabled = this.configurationService.getValue('explorer.enableDragAndDrop');
}
public onDragStart(tree: ITree, data: IDragAndDropData, originalEvent: DragMouseEvent): void {
const sources: FileStat[] = data.getData();
if (sources && sources.length) {
// When dragging folders, make sure to collapse them to free up some space
sources.forEach(s => {
if (s.isDirectory && tree.isExpanded(s)) {
tree.collapse(s, false);
}
});
// Apply some datatransfer types to allow for dragging the element outside of the application
this.instantiationService.invokeFunction(fillResourceDataTransfers, sources, originalEvent);
// The only custom data transfer we set from the explorer is a file transfer
// to be able to DND between multiple code file explorers across windows
const fileResources = sources.filter(s => !s.isDirectory && s.resource.scheme === Schemas.file).map(r => r.resource.fsPath);
if (fileResources.length) {
originalEvent.dataTransfer.setData(CodeDataTransfers.FILES, JSON.stringify(fileResources));
}
}
}
public onDragOver(tree: ITree, data: IDragAndDropData, target: FileStat | Model, originalEvent: DragMouseEvent): IDragOverReaction {
if (!this.dropEnabled) {
return DRAG_OVER_REJECT;
}
const isCopy = originalEvent && ((originalEvent.ctrlKey && !isMacintosh) || (originalEvent.altKey && isMacintosh));
const fromDesktop = data instanceof DesktopDragAndDropData;
// Desktop DND
if (fromDesktop) {
const types: string[] = originalEvent.dataTransfer.types;
const typesArray: string[] = [];
for (let i = 0; i < types.length; i++) {
typesArray.push(types[i].toLowerCase()); // somehow the types are lowercase
}
if (typesArray.indexOf(DataTransfers.FILES.toLowerCase()) === -1 && typesArray.indexOf(CodeDataTransfers.FILES.toLowerCase()) === -1) {
return DRAG_OVER_REJECT;
}
}
// Other-Tree DND
else if (data instanceof ExternalElementsDragAndDropData) {
return DRAG_OVER_REJECT;
}
// In-Explorer DND
else {
const sources: FileStat[] = data.getData();
if (target instanceof Model) {
if (sources.length === 1 && sources[0].isRoot) {
return DRAG_OVER_ACCEPT_BUBBLE_DOWN(false);
}
return DRAG_OVER_REJECT;
}
if (!Array.isArray(sources)) {
return DRAG_OVER_REJECT;
}
if (sources.some((source) => {
if (source instanceof NewStatPlaceholder) {
return true; // NewStatPlaceholders can not be moved
}
if (source.isRoot && (sources.length > 1 || target instanceof FileStat && !target.isRoot)) {
return true; // Root folder can not be moved to a non root file stat. Do not allow root folder move when multi selection drag.
}
if (source.resource.toString() === target.resource.toString()) {
return true; // Can not move anything onto itself
}
if (!isCopy && resources.dirname(source.resource).toString() === target.resource.toString()) {
return true; // Can not move a file to the same parent unless we copy
}
if (resources.isEqualOrParent(target.resource, source.resource, !isLinux /* ignorecase */)) {
return true; // Can not move a parent folder into one of its children
}
return false;
})) {
return DRAG_OVER_REJECT;
}
}
// All (target = model)
if (target instanceof Model) {
return this.contextService.getWorkbenchState() === WorkbenchState.WORKSPACE ? DRAG_OVER_ACCEPT_BUBBLE_DOWN_COPY(false) : DRAG_OVER_REJECT; // can only drop folders to workspace
}
// All (target = file/folder)
else {
if (target.isDirectory) {
return fromDesktop || isCopy ? DRAG_OVER_ACCEPT_BUBBLE_DOWN_COPY(true) : DRAG_OVER_ACCEPT_BUBBLE_DOWN(true);
}
if (this.contextService.getWorkspace().folders.every(folder => folder.uri.toString() !== target.resource.toString())) {
return fromDesktop || isCopy ? DRAG_OVER_ACCEPT_BUBBLE_UP_COPY : DRAG_OVER_ACCEPT_BUBBLE_UP;
}
}
return DRAG_OVER_REJECT;
}
public drop(tree: ITree, data: IDragAndDropData, target: FileStat | Model, originalEvent: DragMouseEvent): void {
let promise: TPromise<void> = TPromise.as(null);
// Desktop DND (Import file)
if (data instanceof DesktopDragAndDropData) {
promise = this.handleExternalDrop(tree, data, target, originalEvent);
}
// In-Explorer DND (Move/Copy file)
else {
promise = this.handleExplorerDrop(tree, data, target, originalEvent);
}
promise.done(null, errors.onUnexpectedError);
}
private handleExternalDrop(tree: ITree, data: DesktopDragAndDropData, target: FileStat | Model, originalEvent: DragMouseEvent): TPromise<void> {
const droppedResources = extractResources(originalEvent.browserEvent as DragEvent, true);
// Check for dropped external files to be folders
return this.fileService.resolveFiles(droppedResources).then(result => {
// Pass focus to window
this.windowService.focusWindow();
// Handle folders by adding to workspace if we are in workspace context
const folders = result.filter(r => r.success && r.stat.isDirectory).map(result => ({ uri: result.stat.resource }));
if (folders.length > 0) {
// If we are in no-workspace context, ask for confirmation to create a workspace
let confirmedPromise: TPromise<IConfirmationResult> = TPromise.wrap({ confirmed: true });
if (this.contextService.getWorkbenchState() !== WorkbenchState.WORKSPACE) {
confirmedPromise = this.dialogService.confirm({
message: folders.length > 1 ? nls.localize('dropFolders', "Do you want to add the folders to the workspace?") : nls.localize('dropFolder', "Do you want to add the folder to the workspace?"),
type: 'question',
primaryButton: folders.length > 1 ? nls.localize('addFolders', "&&Add Folders") : nls.localize('addFolder', "&&Add Folder")
});
}
return confirmedPromise.then(res => {
if (res.confirmed) {
return this.workspaceEditingService.addFolders(folders);
}
return void 0;
});
}
// Handle dropped files (only support FileStat as target)
else if (target instanceof FileStat) {
const importAction = this.instantiationService.createInstance(ImportFileAction, tree, target, null);
return importAction.run(droppedResources.map(res => res.resource));
}
return void 0;
});
}
private handleExplorerDrop(tree: ITree, data: IDragAndDropData, target: FileStat | Model, originalEvent: DragMouseEvent): TPromise<void> {
const sources: FileStat[] = distinctParents(data.getData(), s => s.resource);
const isCopy = (originalEvent.ctrlKey && !isMacintosh) || (originalEvent.altKey && isMacintosh);
let confirmPromise: TPromise<IConfirmationResult>;
// Handle confirm setting
const confirmDragAndDrop = !isCopy && this.configurationService.getValue<boolean>(FileDragAndDrop.CONFIRM_DND_SETTING_KEY);
if (confirmDragAndDrop) {
confirmPromise = this.dialogService.confirm({
message: sources.length > 1 ? getConfirmMessage(nls.localize('confirmMultiMove', "Are you sure you want to move the following {0} files?", sources.length), sources.map(s => s.resource))
: nls.localize('confirmMove', "Are you sure you want to move '{0}'?", sources[0].name),
checkbox: {
label: nls.localize('doNotAskAgain', "Do not ask me again")
},
type: 'question',
primaryButton: nls.localize({ key: 'moveButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Move")
});
} else {
confirmPromise = TPromise.as({ confirmed: true } as IConfirmationResult);
}
return confirmPromise.then(res => {
// Check for confirmation checkbox
let updateConfirmSettingsPromise: TPromise<void> = TPromise.as(void 0);
if (res.confirmed && res.checkboxChecked === true) {
updateConfirmSettingsPromise = this.configurationService.updateValue(FileDragAndDrop.CONFIRM_DND_SETTING_KEY, false, ConfigurationTarget.USER);
}
return updateConfirmSettingsPromise.then(() => {
if (res.confirmed) {
return TPromise.join(sources.map(source => this.doHandleExplorerDrop(tree, source, target, isCopy))).then(() => void 0);
}
return TPromise.as(void 0);
});
});
}
private doHandleExplorerDrop(tree: ITree, source: FileStat, target: FileStat | Model, isCopy: boolean): TPromise<void> {
return tree.expand(target).then(() => {
if (source.isRoot) {
const folders = this.contextService.getWorkspace().folders;
let sourceIndex: number;
let targetIndex: number;
const workspaceCreationData: IWorkspaceFolderCreationData[] = [];
const targetUri = target instanceof FileStat ? target.resource : folders[folders.length - 1].uri;
for (let index = 0; index < folders.length; index++) {
if (folders[index].uri.toString() === source.resource.toString()) {
sourceIndex = index;
}
if (folders[index].uri.toString() === targetUri.toString()) {
targetIndex = index;
}
workspaceCreationData.push({
name: folders[index].name,
uri: folders[index].uri
});
}
const swap = workspaceCreationData[sourceIndex];
workspaceCreationData[sourceIndex] = workspaceCreationData[targetIndex];
workspaceCreationData[targetIndex] = swap;
return this.workspaceEditingService.updateFolders(0, workspaceCreationData.length, workspaceCreationData);
}
// Reuse duplicate action if user copies
if (isCopy) {
return this.instantiationService.createInstance(DuplicateFileAction, tree, source, target).run();
}
const dirtyMoved: URI[] = [];
// Success: load all files that are dirty again to restore their dirty contents
// Error: discard any backups created during the process
const onSuccess = () => TPromise.join(dirtyMoved.map(t => this.textFileService.models.loadOrCreate(t)));
const onError = (error?: Error, showError?: boolean) => {
if (showError) {