This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathdirectory.coffee
280 lines (226 loc) · 8.16 KB
/
directory.coffee
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
path = require 'path'
_ = require 'underscore-plus'
{CompositeDisposable, Emitter} = require 'event-kit'
fs = require 'fs-plus'
PathWatcher = require 'pathwatcher'
File = require './file'
{repoForPath} = require './helpers'
realpathCache = {}
module.exports =
class Directory
constructor: ({@name, fullPath, @symlink, @expansionState, @isRoot, @ignoredPatterns}) ->
@destroyed = false
@emitter = new Emitter()
@subscriptions = new CompositeDisposable()
if atom.config.get('tree-view.squashDirectoryNames')
fullPath = @squashDirectoryNames(fullPath)
@path = fullPath
@realPath = @path
if fs.isCaseInsensitive()
@lowerCasePath = @path.toLowerCase()
@lowerCaseRealPath = @lowerCasePath
@isRoot ?= false
@expansionState ?= {}
@expansionState.isExpanded ?= false
@expansionState.entries ?= {}
@status = null
@entries = {}
@submodule = repoForPath(@path)?.isSubmodule(@path)
@subscribeToRepo()
@updateStatus()
@loadRealPath()
destroy: ->
@destroyed = true
@unwatch()
@subscriptions.dispose()
@emitter.emit('did-destroy')
onDidDestroy: (callback) ->
@emitter.on('did-destroy', callback)
onDidStatusChange: (callback) ->
@emitter.on('did-status-change', callback)
onDidAddEntries: (callback) ->
@emitter.on('did-add-entries', callback)
onDidRemoveEntries: (callback) ->
@emitter.on('did-remove-entries', callback)
loadRealPath: ->
fs.realpath @path, realpathCache, (error, realPath) =>
return if @destroyed
if realPath and realPath isnt @path
@realPath = realPath
@lowerCaseRealPath = @realPath.toLowerCase() if fs.isCaseInsensitive()
@updateStatus()
# Subscribe to project's repo for changes to the Git status of this directory.
subscribeToRepo: ->
repo = repoForPath(@path)
return unless repo?
@subscriptions.add repo.onDidChangeStatus (event) =>
@updateStatus(repo) if @contains(event.path)
@subscriptions.add repo.onDidChangeStatuses =>
@updateStatus(repo)
# Update the status property of this directory using the repo.
updateStatus: ->
repo = repoForPath(@path)
return unless repo?
newStatus = null
if repo.isPathIgnored(@path)
newStatus = 'ignored'
else
status = repo.getDirectoryStatus(@path)
if repo.isStatusModified(status)
newStatus = 'modified'
else if repo.isStatusNew(status)
newStatus = 'added'
if newStatus isnt @status
@status = newStatus
@emitter.emit('did-status-change', newStatus)
# Is the given path ignored?
isPathIgnored: (filePath) ->
if atom.config.get('tree-view.hideVcsIgnoredFiles')
repo = repoForPath(@path)
return true if repo? and repo.isProjectAtRoot() and repo.isPathIgnored(filePath)
if atom.config.get('tree-view.hideIgnoredNames')
for ignoredPattern in @ignoredPatterns
return true if ignoredPattern.match(filePath)
false
# Does given full path start with the given prefix?
isPathPrefixOf: (prefix, fullPath) ->
fullPath.indexOf(prefix) is 0 and fullPath[prefix.length] is path.sep
isPathEqual: (pathToCompare) ->
@path is pathToCompare or @realPath is pathToCompare
# Public: Does this directory contain the given path?
#
# See atom.Directory::contains for more details.
contains: (pathToCheck) ->
return false unless pathToCheck
# Normalize forward slashes to back slashes on windows
pathToCheck = pathToCheck.replace(/\//g, '\\') if process.platform is 'win32'
if fs.isCaseInsensitive()
directoryPath = @lowerCasePath
pathToCheck = pathToCheck.toLowerCase()
else
directoryPath = @path
return true if @isPathPrefixOf(directoryPath, pathToCheck)
# Check real path
if @realPath isnt @path
if fs.isCaseInsensitive()
directoryPath = @lowerCaseRealPath
else
directoryPath = @realPath
return @isPathPrefixOf(directoryPath, pathToCheck)
false
# Public: Stop watching this directory for changes.
unwatch: ->
if @watchSubscription?
@watchSubscription.close()
@watchSubscription = null
for key, entry of @entries
entry.destroy()
delete @entries[key]
# Public: Watch this directory for changes.
watch: ->
try
@watchSubscription ?= PathWatcher.watch @path, (eventType) =>
switch eventType
when 'change' then @reload()
when 'delete' then @destroy()
getEntries: ->
try
names = fs.readdirSync(@path)
catch error
names = []
names.sort(new Intl.Collator(undefined, {numeric: true, sensitivity: "base"}).compare)
files = []
directories = []
for name in names
fullPath = path.join(@path, name)
continue if @isPathIgnored(fullPath)
stat = fs.lstatSyncNoException(fullPath)
symlink = stat.isSymbolicLink?()
stat = fs.statSyncNoException(fullPath) if symlink
if stat.isDirectory?()
if @entries.hasOwnProperty(name)
# push a placeholder since this entry already exists but this helps
# track the insertion index for the created views
directories.push(name)
else
expansionState = @expansionState.entries[name]
directories.push(new Directory({name, fullPath, symlink, expansionState, @ignoredPatterns}))
else if stat.isFile?()
if @entries.hasOwnProperty(name)
# push a placeholder since this entry already exists but this helps
# track the insertion index for the created views
files.push(name)
else
files.push(new File({name, fullPath, symlink, realpathCache}))
@sortEntries(directories.concat(files))
normalizeEntryName: (value) ->
normalizedValue = value.name
unless normalizedValue?
normalizedValue = value
if normalizedValue?
normalizedValue = normalizedValue.toLowerCase()
normalizedValue
sortEntries: (combinedEntries) ->
if atom.config.get('tree-view.sortFoldersBeforeFiles')
combinedEntries
else
combinedEntries.sort (first, second) =>
firstName = @normalizeEntryName(first)
secondName = @normalizeEntryName(second)
firstName.localeCompare(secondName)
# Public: Perform a synchronous reload of the directory.
reload: ->
newEntries = []
removedEntries = _.clone(@entries)
index = 0
for entry in @getEntries()
if @entries.hasOwnProperty(entry)
delete removedEntries[entry]
index++
continue
entry.indexInParentDirectory = index
index++
newEntries.push(entry)
entriesRemoved = false
for name, entry of removedEntries
entriesRemoved = true
entry.destroy()
if @entries.hasOwnProperty(name)
delete @entries[name]
if @expansionState.entries.hasOwnProperty(name)
delete @expansionState.entries[name]
@emitter.emit('did-remove-entries', removedEntries) if entriesRemoved
if newEntries.length > 0
@entries[entry.name] = entry for entry in newEntries
@emitter.emit('did-add-entries', newEntries)
# Public: Collapse this directory and stop watching it.
collapse: ->
@expansionState.isExpanded = false
@expansionState = @serializeExpansionState()
@unwatch()
# Public: Expand this directory, load its children, and start watching it for
# changes.
expand: ->
@expansionState.isExpanded = true
@reload()
@watch()
serializeExpansionState: ->
expansionState = {}
expansionState.isExpanded = @expansionState.isExpanded
expansionState.entries = {}
for name, entry of @entries when entry.expansionState?
expansionState.entries[name] = entry.serializeExpansionState()
expansionState
squashDirectoryNames: (fullPath) ->
squashedDirs = [@name]
loop
contents = fs.listSync fullPath
break if contents.length isnt 1
break if not fs.isDirectorySync(contents[0])
relativeDir = path.relative(fullPath, contents[0])
squashedDirs.push relativeDir
fullPath = path.join(fullPath, relativeDir)
if squashedDirs.length > 1
@squashedName = squashedDirs[0..squashedDirs.length - 2].join(path.sep) + path.sep
@name = squashedDirs[squashedDirs.length - 1]
return fullPath