This repository was archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmode.coffee
More file actions
251 lines (223 loc) · 8.01 KB
/
Copy pathmode.coffee
File metadata and controls
251 lines (223 loc) · 8.01 KB
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
path = require 'path'
atom_linter = require 'atom-linter'
XRegExp = require 'xregexp'
pattern = XRegExp('(?<file>[^\n\r]+):(?<from_line>\\d+):(?<from_col>\\d+):\\s*\
(?<to_line>\\d+):(?<to_col>\\d+)\\s+\
((?<error>error|fatal error)|(?<warning>warning)|(?<info>note|help)):\\s+\
(?<message>.+?)[\n\r]+($|(?=[^\n\r]+:\\d+))', 's')
parseOldMessages = (output, {disabledWarnings, textEditor}) ->
elements = []
XRegExp.forEach output, pattern, (match) ->
range = if match.from_col == match.to_col and match.from_line == match.to_line
atom_linter.generateRange(textEditor, Number.parseInt(match.from_line, 10) - 1, Number.parseInt(match.from_col, 10) - 1)
else
[
[match.from_line - 1, match.from_col - 1],
[match.to_line - 1, match.to_col - 1]
]
level = if match.error then 'error'
else if match.warning then 'warning'
else if match.info then 'info'
else if match.trace then 'trace'
else if match.note then 'note'
element =
type: level
message: match.message
file: match.file
range: range
elements.push element
buildMessages elements, disabledWarnings
parseJsonMessages = (messages, {disabledWarnings}) ->
elements = []
for input in messages
continue unless input and input.spans
primary_span = input.spans.find (span) -> span.is_primary
continue unless primary_span
while primary_span.expansion and primary_span.expansion.span
primary_span = primary_span.expansion.span
range = [
[primary_span.line_start - 1, primary_span.column_start - 1],
[primary_span.line_end - 1, primary_span.column_end - 1]
]
input.level = 'error' if input.level == 'fatal error'
element =
type: input.level
message: input.message
file: primary_span.file_name
range: range
children: input.children
for span in input.spans
unless span.is_primary
element.children.push
range: [
[span.line_start - 1, span.column_start - 1],
[span.line_end - 1, span.column_end - 1]
]
elements.push element
buildMessages elements, disabledWarnings
parseJsonOutput = (output, {disabledWarnings, additionalFilter} ) ->
results = output.split('\n').map (message) ->
message = message.trim()
if message.startsWith '{'
json = JSON.parse message
if additionalFilter?
additionalFilter(json)
else
json
.filter (m) -> m?
parseJsonMessages results, {disabledWarnings}
buildMessages = (elements, disabledWarnings) ->
messages = []
lastMessage = null
for element in elements
switch element.type
when 'info', 'trace', 'note'
# Add only if there is a last message
if lastMessage
lastMessage.reference = {
file: element.file
position: element.range
}
when 'warning'
# If the message is warning and user enabled disabling warnings
# Check if this warning is disabled
if disabledWarnings and disabledWarnings.length > 0
messageIsDisabledLint = false
for disabledWarning in disabledWarnings
# Find a disabled lint in warning message
if element.message.indexOf(disabledWarning) >= 0
messageIsDisabledLint = true
lastMessage = null
break
if not messageIsDisabledLint
lastMessage = constructMessage "Warning", element
messages.push lastMessage
else
lastMessage = constructMessage "Warning" , element
messages.push lastMessage
when 'error', 'fatal error'
lastMessage = constructMessage "Error", element
messages.push lastMessage
return messages
constructMessage = (type, element) ->
message =
severity: type.toLowerCase()
excerpt: element.message
location: {
file: element.file
position: element.range
}
# children exists only in JSON messages
if element.children
for children in element.children
if children.range
# NOTE: Will only save the first valid reference
message.reference = {
file: element.file
position: children.range
}
break
message
buildRustcArguments = (linter, paths) ->
[editingFile, cargoManifestPath] = paths
Promise.resolve().then () =>
rustcArgs = switch linter.rustcBuildTest
when true then ['--cfg', 'test']
else []
rustcArgs = rustcArgs.concat ['--color', 'never']
cmd = [linter.rustcPath]
.concat rustcArgs
if cargoManifestPath
cmd.push '-L'
cmd.push path.join path.dirname(cargoManifestPath), linter.cargoDependencyDir
compilationFeatures = linter.compilationFeatures(false)
cmd = cmd.concat compilationFeatures if compilationFeatures
cmd = cmd.concat [editingFile]
[editingFile, cmd]
cachedUsingMultitoolForClippy = null
buildCargoArguments = (linter, cargoManifestPath) ->
buildCargoPath = (cargoPath, cargoCommand) ->
# the result is cached to avoid delays
if cachedUsingMultitoolForClippy? and linter.allowedToCacheVersions
Promise.resolve().then () =>
cachedUsingMultitoolForClippy
else
# Decide if should use older multirust or newer rustup
usingMultitoolForClippy =
atom_linter.exec 'rustup', ['--version'], {ignoreExitCode: true}
.then ->
result: true, tool: 'rustup'
.catch ->
# Try to use older multirust at least
atom_linter.exec 'multirust', ['--version'], {ignoreExitCode: true}
.then ->
result: true, tool: 'multirust'
.catch ->
result: false
usingMultitoolForClippy.then (canUseMultirust) ->
if (cargoCommand == 'clippy' or cargoCommand == 'clippy all') and canUseMultirust.result
[canUseMultirust.tool, 'run', 'nightly', 'cargo']
else
[cargoPath]
.then (cached) =>
cachedUsingMultitoolForClippy = cached
cached
cargoArgs = switch linter.cargoCommand
when 'check' then ['check']
when 'check all' then ['check', '--all', '--all-targets']
when 'check tests' then ['check', '--tests']
when 'test' then ['test', '--no-run']
when 'test all' then ['test', '--no-run', '--all', '--all-targets']
when 'rustc' then ['rustc', '--color', 'never']
when 'clippy' then ['clippy']
when 'clippy all' then ['clippy', '--workspace', '--all-targets']
else ['build']
compilationFeatures = linter.compilationFeatures(true)
buildCargoPath(linter.cargoPath, linter.cargoCommand).then (cmd) ->
cmd = cmd
.concat cargoArgs
.concat ['-j', linter.jobsNumber]
cmd = cmd.concat compilationFeatures if compilationFeatures
cmd.push "--manifest-path=#{cargoManifestPath}"
[cargoManifestPath, cmd]
# These define the behabiour of each error mode linter-rust has
errorModes =
JSON_RUSTC:
neededOutput: (stdout, stderr) ->
stderr
parse: (output, options) =>
parseJsonOutput output, options
buildArguments: (linter, file) ->
buildRustcArguments(linter, file).then (cmd_res) ->
[file, cmd] = cmd_res
cmd = cmd.concat ['--error-format=json']
[file, cmd]
JSON_CARGO:
neededOutput: (stdout, stderr) ->
stdout
parse: (output, options) ->
options.additionalFilter = (json) ->
if json? and json.reason == "compiler-message"
json.message
parseJsonOutput output, options
buildArguments: (linter, file) ->
buildCargoArguments(linter, file).then (cmd_res) ->
[file, cmd] = cmd_res
cmd = cmd.concat ['--message-format', 'json']
[file, cmd]
FLAGS_JSON_CARGO:
neededOutput: (stdout, stderr) ->
stderr
parse: parseJsonOutput
buildArguments: buildCargoArguments
OLD_RUSTC:
neededOutput: (stdout, stderr) ->
stderr
parse: parseOldMessages
buildArguments: buildRustcArguments
OLD_CARGO:
neededOutput: (stdout, stderr) ->
stderr
parse: parseOldMessages
buildArguments: buildCargoArguments
module.exports = errorModes