-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
211 lines (177 loc) · 6.75 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* global define, brackets, window, $ */
/** Reads a .gitattributes file and sets the line endings accordingly */
define(function(require, exports, module) {
'use strict';
var minimatch = require('./lib/minimatch');
var DocumentManager = brackets.getModule('document/DocumentManager'),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
FileUtils = brackets.getModule("file/FileUtils"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
ProjectManager = brackets.getModule("project/ProjectManager"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
prefs = PreferencesManager.getExtensionPrefs('line-endings'),
_preferences,
_command;
var CONFIG_FILE = '.gitattributes';
function parseGitAttributes(content)
{
content = removeComments(content);
var patRe = /^([^\s]*).*eol=(lf|crlf)/i;
var lines = content.split(/\n/);
var eolPatterns = { };
$.each(lines, function(i, line) {
var m = line.trim().match(patRe);
if (m) {
eolPatterns[m[1]] = m[2].toLowerCase();
}
});
return eolPatterns;
};
function removeComments(str) {
str = str || '';
str = str.replace(/#.*/g, '');
return str;
};
function _readConfig(dir, configFileName) {
var result = new $.Deferred(),
file;
configFileName = configFileName || CONFIG_FILE;
file = FileSystem.getFileForPath(dir + configFileName);
file.read(function (err, content) {
if (!err) {
var config;
try {
config = parseGitAttributes(content);
} catch (e) {
console.error("error parsing " + file.fullPath + ". Details: " + e);
result.reject(e);
return;
}
var baseConfigResult = $.Deferred();
baseConfigResult.resolve({});
baseConfigResult.done( function (baseConfig) {
result.resolve(config);
}).fail(function (e) {
result.reject(e);
});
} else {
result.reject(err);
}
});
return result.promise();
}
function _lookupAndLoad(root, dir, readConfig) {
var deferred = new $.Deferred(),
done = false,
cdir = dir,
file,
iter = {
next: function () {
if (done) {
return;
}
readConfig(root + cdir)
.then(function (cfg) {
this.stop(cfg);
}.bind(this))
.fail(function () {
if (!cdir) {
this.stop({});
}
if (!done) {
cdir = FileUtils.getDirectoryPath(cdir.substring(0, cdir.length - 1));
this.next();
}
}.bind(this));
},
stop: function (cfg) {
deferred.resolve(cfg);
done = true;
}
};
if (cdir === undefined || cdir === null) {
deferred.resolve({});
} else {
iter.next();
}
return deferred.promise();
}
// List of globs + line ending type
var _lineEndings = prefs.get('patterns');
var getBracketsLineEndings = function(eol) {
switch (eol) {
case "lf":
return FileUtils.LINE_ENDINGS_LF;
case "crlf":
return FileUtils.LINE_ENDINGS_CRLF;
default:
return FileUtils.getPlatformLineEndings();
}
};
var commandId = "jlamanna.gitattributes-line-endings.toggle";
var preferencesId = "jlamanna.gitattributes-line-endings";
var defaultPreferences = { checked: true };
// --- State Variables ---
function onCommandExecuted() {
if (!_command.getChecked()) {
_command.setChecked(true);
} else {
_command.setChecked(false);
}
}
function loadPreferences() {
_preferences = PreferencesManager.getExtensionPrefs(preferencesId);
_preferences.definePreference("checked", "boolean", defaultPreferences["checked"]);
}
function onCheckedStateChange() {
_preferences.set("checked", Boolean(_command.getChecked()));
}
function loadCommand() {
_command = CommandManager.get(commandId);
if (!_command) {
_command = CommandManager.register("Enable GitAttributes Line Endings", commandId, onCommandExecuted);
} else {
_command._commandFn = onCommandExecuted;
}
$(_command).on("checkedStateChange", onCheckedStateChange);
// Apply preferences
_command.setChecked(_preferences.get("checked"));
}
function unloadCommand() {
_command.setChecked(false);
$(_command).off("checkedStateChange", onCheckedStateChange);
_command._commandFn = null;
}
function loadMenuItem() {
Menus.getMenu("view-menu").addMenuItem(commandId, "");
}
function unloadMenuItem() {
Menus.getMenu("view-menu").removeMenuItem(commandId);
}
loadPreferences();
loadCommand();
loadMenuItem();
$(DocumentManager).on('documentSaved', function(evt, doc) {
var enabled = _preferences.get('checked');
if (!enabled) {
return;
}
var fullPath = doc.file.fullPath;
var projectRootEntry = ProjectManager.getProjectRoot();
var rootPath = projectRootEntry.fullPath;
var relPath = FileUtils.getRelativeFilename(rootPath, fullPath);
if (relPath) {
relPath = FileUtils.getDirectoryPath(relPath);
_lookupAndLoad(rootPath, relPath, _readConfig).done(function(cfg) {
$.each(cfg, function(glob, eol) {
if (minimatch(fullPath, glob, { matchBase: true })) {
console.log('Saving ', fullPath, 'with line endings: ', eol);
var newText = FileUtils.translateLineEndings(doc.getText(true), getBracketsLineEndings(eol));
FileUtils.writeText(doc.file, newText);
}
});
});
}
});
});