forked from brackets-userland/brackets-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectTreeMarks.js
197 lines (168 loc) · 6.96 KB
/
ProjectTreeMarks.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
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
define(function (require) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
ProjectManager = brackets.getModule("project/ProjectManager");
var EventEmitter = require("src/EventEmitter"),
Events = require("src/Events"),
Git = require("src/git/Git"),
Preferences = require("src/Preferences"),
Promise = require("bluebird"),
Utils = require("src/Utils");
var ignoreEntries = [],
newPaths = [],
modifiedPaths = [];
function refreshIgnoreEntries() {
function regexEscape(str) {
// NOTE: We cannot use StringUtils.regexEscape() here because we don't wanna replace *
return str.replace(/([.?+\^$\[\]\\(){}|\-])/g, "\\$1");
}
return new Promise(function (resolve) {
if (!Preferences.get("markModifiedInTree")) {
return resolve();
}
var projectRoot = Utils.getProjectRoot();
FileSystem.getFileForPath(projectRoot + ".gitignore").read(function (err, content) {
if (err) {
ignoreEntries = [];
return resolve();
}
ignoreEntries = _.compact(_.map(content.split("\n"), function (line) {
// Rules: http://git-scm.com/docs/gitignore
var type = "deny",
leadingSlash,
trailingSlash,
regex;
line = line.trim();
if (!line || line.indexOf("#") === 0) {
return;
}
// handle explicitly allowed files/folders with a leading !
if (line.indexOf("!") === 0) {
line = line.slice(1);
type = "accept";
}
// handle lines beginning with a backslash, which is used for escaping ! or #
if (line.indexOf("\\") === 0) {
line = line.slice(1);
}
// handle lines beginning with a slash, which only matches files/folders in the root dir
if (line.indexOf("/") === 0) {
line = line.slice(1);
leadingSlash = true;
}
// handle lines ending with a slash, which only exludes dirs
if (line.lastIndexOf("/") === line.length) {
// a line ending with a slash ends with **
line += "**";
trailingSlash = true;
}
// NOTE: /(.{0,})/ is basically the same as /(.*)/, but we can't use it because the asterisk
// would be replaced later on
// create the intial regexp here. We need the absolute path 'cause it could be that there
// are external files with the same name as a project file
regex = regexEscape(projectRoot) + (leadingSlash ? "" : "((.+)/)?") + regexEscape(line) + (trailingSlash ? "" : "(/.{0,})?");
// replace all the possible asterisks
regex = regex.replace(/\*\*$/g, "(.{0,})").replace(/(\*\*|\*$)/g, "(.+)").replace(/\*/g, "([^/]+)");
regex = "^" + regex + "$";
return {
regexp: new RegExp(regex),
type: type
};
}));
return resolve();
});
});
}
function isIgnored(path) {
var ignored = false;
_.forEach(ignoreEntries, function (entry) {
if (entry.regexp.test(path)) {
ignored = (entry.type === "deny");
}
});
return ignored;
}
function isNew(fullPath) {
return newPaths.indexOf(fullPath) !== -1;
}
function isModified(fullPath) {
return modifiedPaths.indexOf(fullPath) !== -1;
}
ProjectManager.addClassesProvider(function (data) {
var fullPath = data.fullPath;
if (isIgnored(fullPath)) {
return "git-ignored";
} else if (isNew(fullPath)) {
return "git-new";
} else if (isModified(fullPath)) {
return "git-modified";
}
});
function _refreshProjectFiles(selector, dataEntry) {
$(selector).find("li").each(function () {
var $li = $(this),
data = $li.data(dataEntry);
if (data) {
var fullPath = data.fullPath;
$li.toggleClass("git-ignored", isIgnored(fullPath))
.toggleClass("git-new", isNew(fullPath))
.toggleClass("git-modified", isModified(fullPath));
}
});
}
var refreshOpenFiles = _.debounce(function () {
_refreshProjectFiles("#open-files-container", "file");
}, 100);
function refreshBoth() {
refreshOpenFiles();
}
function attachEvents() {
if (Preferences.get("markModifiedInTree")) {
$("#open-files-container").on("contentChanged", refreshOpenFiles).triggerHandler("contentChanged");
}
}
function detachEvents() {
$("#open-files-container").off("contentChanged", refreshOpenFiles);
}
// this will refresh ignore entries when .gitignore is modified
EventEmitter.on(Events.BRACKETS_FILE_CHANGED, function (evt, file) {
if (file.fullPath === Utils.getProjectRoot() + ".gitignore") {
refreshIgnoreEntries().finally(function () {
refreshBoth();
});
}
});
// this will refresh new/modified paths on every status results
EventEmitter.on(Events.GIT_STATUS_RESULTS, function (files) {
var projectRoot = Utils.getProjectRoot();
newPaths = [];
modifiedPaths = [];
files.forEach(function (entry) {
var isNew = entry.status.indexOf(Git.FILE_STATUS.UNTRACKED) !== -1 ||
entry.status.indexOf(Git.FILE_STATUS.ADDED) !== -1;
var fullPath = projectRoot + entry.file;
if (isNew) {
newPaths.push(fullPath);
} else {
modifiedPaths.push(fullPath);
}
});
if (Preferences.get("markModifiedInTree")) {
ProjectManager.rerenderTree();
}
refreshBoth();
});
// this will refresh ignore entries when git project is opened
EventEmitter.on(Events.GIT_ENABLED, function () {
refreshIgnoreEntries();
attachEvents();
});
// this will clear entries when non-git project is opened
EventEmitter.on(Events.GIT_DISABLED, function () {
ignoreEntries = [];
newPaths = [];
modifiedPaths = [];
detachEvents();
});
});