This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathtree-view-package.js
115 lines (101 loc) · 3.92 KB
/
tree-view-package.js
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
const {Disposable, CompositeDisposable} = require('atom')
const path = require('path')
const getIconServices = require('./get-icon-services')
const TreeView = require('./tree-view')
module.exports =
class TreeViewPackage {
activate () {
this.disposables = new CompositeDisposable()
this.disposables.add(atom.commands.add('atom-workspace', {
'tree-view:show': () => this.getTreeViewInstance().show(),
'tree-view:toggle': () => this.getTreeViewInstance().toggle(),
'tree-view:toggle-focus': () => this.getTreeViewInstance().toggleFocus(),
'tree-view:reveal-active-file': () => this.getTreeViewInstance().revealActiveFile({show: true}),
'tree-view:add-file': () => this.getTreeViewInstance().add(true),
'tree-view:add-folder': () => this.getTreeViewInstance().add(false),
'tree-view:duplicate': () => this.getTreeViewInstance().copySelectedEntry(),
'tree-view:remove': () => this.getTreeViewInstance().removeSelectedEntries(),
'tree-view:rename': () => this.getTreeViewInstance().moveSelectedEntry(),
'tree-view:show-current-file-in-file-manager': () => this.getTreeViewInstance().showCurrentFileInFileManager()
}))
this.disposables.add(atom.project.onDidChangePaths(this.createOrDestroyTreeViewIfNeeded.bind(this)))
if (this.shouldAttachTreeView()) {
const treeView = this.getTreeViewInstance()
const showOnAttach = !atom.workspace.getActivePaneItem()
this.treeViewOpenPromise = atom.workspace.open(treeView, {
activatePane: showOnAttach,
activateItem: showOnAttach,
searchAllPanes: true
})
} else {
this.treeViewOpenPromise = Promise.resolve()
}
}
async deactivate () {
this.disposables.dispose()
await this.treeViewOpenPromise // Wait for Tree View to finish opening before destroying it
if (this.treeView) this.treeView.destroy()
this.treeView = null
}
consumeElementIcons (service) {
getIconServices().setElementIcons(service)
return new Disposable(() => {
getIconServices().resetElementIcons()
})
}
consumeFileIcons (service) {
getIconServices().setFileIcons(service)
if (this.treeView) this.treeView.updateRoots()
return new Disposable(() => {
getIconServices().resetFileIcons()
if (this.treeView) this.treeView.updateRoots()
})
}
provideTreeView () {
return {
selectedPaths: () => this.getTreeViewInstance().selectedPaths(),
entryForPath: (entryPath) => this.getTreeViewInstance().entryForPath(entryPath)
}
}
getTreeViewInstance (state = {}) {
if (this.treeView == null) {
this.treeView = new TreeView(state)
this.treeView.onDidDestroy(() => { this.treeView = null })
}
return this.treeView
}
createOrDestroyTreeViewIfNeeded () {
if (this.shouldAttachTreeView()) {
const treeView = this.getTreeViewInstance()
const paneContainer = atom.workspace.paneContainerForURI(treeView.getURI())
if (paneContainer) {
paneContainer.show()
} else {
atom.workspace.open(treeView, {
activatePane: false,
activateItem: false
}).then(() => {
const paneContainer = atom.workspace.paneContainerForURI(treeView.getURI())
if (paneContainer) paneContainer.show()
})
}
} else {
if (this.treeView) {
const pane = atom.workspace.paneForItem(this.treeView)
if (pane) pane.removeItem(this.treeView)
}
}
}
shouldAttachTreeView () {
if (atom.project.getPaths().length === 0) return false
// Avoid opening the tree view if Atom was opened as the Git editor...
// Only show it if the .git folder was explicitly opened.
if (path.basename(atom.project.getPaths()[0]) === '.git') {
return atom.project.getPaths()[0] === atom.getLoadSettings().pathToOpen
}
return true
}
shouldShowTreeViewAfterAttaching () {
if (atom.workspace.getActivePaneItem()) return false
}
}