This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.js
279 lines (266 loc) · 9.52 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
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
'use babel'
var time_last_lint = new Date().getTime();
var lint_waiting = false;
CompositeDisposable = require('atom').CompositeDisposable;
module.exports = {
config: {
execPath: {
title: "GCC Executable Path",
description: "Note for Windows/Mac OS X users: please ensure that GCC is in your ```$PATH``` otherwise the linter might not work. If your path contains spaces, it needs to be enclosed in double quotes.",
type: "string",
default: "g++",
order : 1
},
gccDefaultCFlags: {
title: "C Flags",
description: "Supports the use of escaped characters",
type: "string",
default: "-c -Wall",
order : 2
},
gccDefaultCppFlags: {
title: "C++ Flags",
description: "Supports the use of escaped characters",
type: "string",
default: "-c -Wall -std=c++11",
order : 3
},
gccIncludePaths: {
title: "GCC Include Paths",
description: "Enter your include paths as a comma-separated list. Paths starting with ```.``` or ```..``` are expanded relative to the project root path and paths starting with a ```-``` are expanded relative to the path of the active file. If any of your paths contain spaces, they need to be enclosed in double quotes. To expand a directory recursively, add ```/*``` to the end of the path",
type: "string",
default: " ",
order : 4
},
gccISystemPaths: {
title: "GCC -isystem Include Paths",
description: "Enter your include paths the same as above. These will be included using the -isystem option, treating the included files like system files and ignoring their warnings.",
type: "string",
default: " ",
order : 5
},
gccErrorLimit: {
title: "GCC Error Limit",
description: "To completely remove `-fmax-errors`, simply enter `-1` here.",
type: "integer",
default: 0,
order : 6
},
gccSuppressWarnings: {
title: "Suppress GCC Warnings",
type: "boolean",
default: false,
order : 7
},
gccErrorString: {
title: "String GCC prepends to errors",
type: "string",
default: "error",
order: 8
},
gccWarningString: {
title: "String GCC prepends to warnings",
type: "string",
default: "warning",
order: 9
},
gccNoteString: {
title: "String GCC prepends to notes",
type: "string",
default: "note",
order: 10
},
gccLintOnTheFly: {
title: "Lint on-the-fly",
description: "Please ensure any auto-saving packages are disabled before using this feature",
type: "boolean",
default: false,
order : 11
},
gccLintOnTheFlyInterval: {
title: "Lint on-the-fly Interval",
description: "Time interval (in ms) between linting",
type: "integer",
default: 300,
order : 12
},
gccRelintMessageSources: {
title: "Re-lint message source files",
description: "Reruns linter on files that contributed messages to the current working file",
type: "boolean",
default: false,
order : 13
},
gccDebug: {
title: "Show Debugging Messages",
description: "Please read the linter-gcc wiki [here](https://github.com/hebaishi/linter-gcc/wiki) before reporting any issues.",
type: "boolean",
default: false,
order : 14
},
gcc7orGreater: {
title: "Gcc version 7 or newer",
description: "Check if you have a Gcc version that recognizes \"-fdiagnostics-parseable-fixits\" (7 or newer)",
type: "boolean",
default: true,
order : 15
},
compileCommandsFile: {
title: "Compile commands file",
description: "Path to cmake compile_commands.json",
type: "string",
default: "./build/compile_commands.json",
order : 16
}
},
messages: {},
last_linted_files: {},
linter_gcc: undefined,
temp_file : {
"C++": require("tempfile")(".cpp"),
"C": require("tempfile")(".c")
},
lint: function(editor, linted_file, real_file){
//create list of extra files to lint
otherFiles = new Set([])
if (atom.config.get("linter-gcc.gccRelintMessageSources") == true){
for (var file in module.exports.messages){
if (!module.exports.messages.hasOwnProperty(file)) continue;
var fileMessages = module.exports.messages[file];
for (var msg in fileMessages){
if (!fileMessages.hasOwnProperty(msg)) continue;
var message = fileMessages[msg];
if (message.location.file === real_file && file !== real_file && !module.exports.last_linted_files.has(file)){
otherFiles.add(file);
}
}
}
for (let file of otherFiles){
module.exports.lint(editor, file, file);
}
}
module.exports.last_linted_files.add(real_file)
command = require("./utility").buildCommand(editor, linted_file, real_file);
return require("sb-exec").exec(command.binary, command.args, {stream: "stderr"}).then(output => {
msgs = require("./utility.js").parse(output,editor)
msgs.forEach(function(entry){
if (entry.location.file === module.exports.temp_file["C"] || entry.location.file === module.exports.temp_file["C++"]){
entry.location.file = real_file;
}
if (entry.severity ===`${atom.config.get("linter-gcc.gccWarningString")}`)
entry.severity = "warning";
else if (entry.severity ===`${atom.config.get("linter-gcc.gccErrorString")}`)
entry.severity = "error";
else if (entry.severity ===`${atom.config.get("linter-gcc.gccNoteString")}`)
entry.severity = "info";
})
//If parsing failed, return a general message if there was an error
if (msgs.length == 0 && output.indexOf("error") != -1){
msgs = [{
severity: 'error',
excerpt: output,
location: {
file: real_file,
position: [[0, 0], [0, 0]]
}
}];
}
module.exports.messages[real_file] = msgs;
// console.log(msgs)
if (typeof module.exports.linter_gcc != "undefined"){
module.exports.linter_gcc.setAllMessages(require("./utility").flattenHash(module.exports.messages))
}
return msgs;
})
},
activate: function() {
this.subscriptions = new CompositeDisposable()
if(!atom.packages.getLoadedPackages("linter")) {
atom.notifications.addError(
"Linter package not found.",
{
detail: "Please install the `linter` package in your Settings view."
}
);
}
require("atom-package-deps").install("linter-gcc");
time_last_lint = new Date().getTime()
lint_waiting = false
},
deactivate: function() {
this.subscriptions.dispose()
},
consumeLinter: function(registerIndie) {
module.exports.linter_gcc = registerIndie({
name: 'GCC'
})
subs = this.subscriptions;
utility = require("./utility")
lintOnTheFly = function() {
module.exports.last_linted_files = new Set([])
editor = utility.getValidEditor(atom.workspace.getActiveTextEditor());
if (!editor) return;
if (atom.config.get("linter-gcc.gccLintOnTheFly") == false) return;
if (lint_waiting) return;
lint_waiting = true
interval = atom.config.get("linter-gcc.gccLintOnTheFlyInterval")
time_now = new Date().getTime()
timeout = interval - (time_now - time_last_lint);
setTimeout(
function() {
time_last_lint = new Date().getTime()
lint_waiting = false
grammar_type = utility.grammarType(editor.getGrammar().name)
filename = String(module.exports.temp_file[grammar_type])
require('fs-extra').outputFileSync(filename, editor.getText());
module.exports.lint(editor, filename, editor.getPath());
},
timeout
);
};
lintOnSave = function(){
module.exports.last_linted_files = new Set([])
editor = utility.getValidEditor(atom.workspace.getActiveTextEditor());
if (!editor) return;
if (atom.config.get("linter-gcc.gccLintOnTheFly") == true) return;
real_file = editor.getPath();
//cleanup messages in this file created by linting other files
if (atom.config.get("linter-gcc.gccRelintMessageSources") == false){
for (var file in module.exports.messages){
if (!module.exports.messages.hasOwnProperty(file)) continue;
var fileMessages = module.exports.messages[file];
for (var msg in fileMessages){
if (!fileMessages.hasOwnProperty(msg)) continue;
var message = fileMessages[msg];
if (message.location.file === real_file){
delete fileMessages[msg];
}
}
}
}
module.exports.lint(editor, real_file, real_file);
};
cleanupMessages = function(){
editor_hash = {};
atom.workspace.getTextEditors().forEach( function(entry){
try{
path = entry.getPath()
} catch(err){
}
editor_hash[entry.getPath()] = 1;
});
for (var file in module.exports.messages){
if (!editor_hash.hasOwnProperty(file)){
delete module.exports.messages[file]
}
}
module.exports.linter_gcc.setAllMessages(JSON.parse(JSON.stringify(require("./utility").flattenHash(module.exports.messages))));
};
subs.add(module.exports.linter_gcc)
atom.workspace.observeTextEditors(function(editor) {
subs.add(editor.onDidSave(lintOnSave))
subs.add(editor.onDidStopChanging(lintOnTheFly))
subs.add(editor.onDidDestroy(cleanupMessages))
})
}
}