-
Notifications
You must be signed in to change notification settings - Fork 26
/
linterSettings.js
164 lines (126 loc) · 4.25 KB
/
linterSettings.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
/**
* Interactive Linter Copyright (c) 2015 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require/*, exports, module*/) {
"use strict";
var Dialogs = brackets.getModule("widgets/Dialogs"),
ProjectManager = brackets.getModule("project/ProjectManager"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
FileUtils = brackets.getModule("file/FileUtils"),
Promise = require("libs/js/spromise"),
currentProject = {},
currentLinter = {};
function findFile(fileName, filePath, traverse) {
var deferred = Promise.defer();
function find(filePath) {
if (!filePath) {
return deferred.reject(false);
}
try {
var file = FileSystem.getFileForPath(filePath + "/" + fileName);
file.exists(function(err, exists) {
if (exists) {
deferred.resolve(file);
}
else if (err || !traverse || filePath.indexOf(currentProject.fullPath) === -1) {
deferred.reject(false);
}
else {
find(FileUtils.getParentPath(filePath));
}
});
}
catch(ex) {
deferred.reject(false);
}
}
find(FileUtils.getDirectoryPath(filePath));
return deferred.promise;
}
function readFile(file) {
var deferred = Promise.defer();
file.read(function(err, content /*, stat*/) {
if (err) {
deferred.reject(err);
return;
}
deferred.resolve(content);
});
return deferred.promise;
}
function setSettings(settings) {
var deferred = Promise.defer();
settings = stripComments(settings);
try {
settings = JSON.parse(settings);
deferred.resolve(settings);
}
catch(ex) {
if (!settings) {
deferred.resolve();
return;
}
Dialogs.showModalDialog(
"interactiveLinterErr",
"Interactive Linter Error",
"Error processing linter settings<br>" +
ex.toString()
);
deferred.reject("Error processing linter settings");
}
return deferred.promise;
}
FileSystem.on("change", function(evt, file) {
if (currentLinter.file && currentLinter.fileObject && file && file.fullPath === currentLinter.fileObject.fullPath) {
loadFile().done(currentLinter.linter.lint);
}
});
function loadFile() {
var traverse = currentLinter.path.indexOf(currentProject.fullPath) !== -1;
return findFile(currentLinter.file, currentLinter.path, traverse)
.always(function(file) {
currentLinter.fileObject = file;
})
.then(readFile, $.noop)
.then(setSettings, $.noop)
.always(function(settings) {
currentLinter.settings = settings;
});
}
function loadSettings(file, path, linter) {
if (!file) {
return Promise.resolve();
}
// Cache so that we are not loading up the same file when navigating in the same directory...
if (path === currentLinter.path && file === currentLinter.file) {
return Promise.resolve(currentLinter.settings);
}
currentLinter = {
path: normalizePath(path),
file: file,
linter: linter
};
return loadFile();
}
/**
* Make sure we only have forward slashes
*/
function normalizePath(path) {
return path.replace(/[\/\\]/g, "/");
}
/**
* Strips all commments from a json string.
*/
function stripComments(text) {
// Regex from requirejs. Thanks James!
return (text || "").replace(/(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg, "");
}
ProjectManager.on("projectOpen", function(e, project) {
currentProject = project;
});
return {
loadSettings: loadSettings
};
});