Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 34ae462

Browse files
committed
Added cargo lint. See #5
1 parent ca1c9e6 commit 34ae462

File tree

5 files changed

+75
-61
lines changed

5 files changed

+75
-61
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.0
2+
* Remove linting "on the fly".
3+
* Support Cargo. See #5. Thanks @liigo for ideas.
4+
15
# 0.0.13
26
Added linting on the fly (experimental).
37

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# linter-rust
22

3-
Linting your Rust-files on the fly using [rustc](http://www.rust-lang.org) with [Atom](https://atom.io).
3+
Linting your Rust-files in [Atom](https://atom.io), using [rustc](http://www.rust-lang.org) and [cargo](https://crates.io).
4+
Files will be checked when you open or save them.
45

56
## Installation
67

7-
* Install [Rust](http://www.rust-lang.org).
8+
* Install [Rust](http://www.rust-lang.org) and/or [Cargo](https://crates.io).
89
* `$ apm install linter` (if you don't have [AtomLinter/Linter](https://github.com/AtomLinter/Linter) installed).
910
* `$ apm install linter-rust`
1011

lib/init.coffee

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,27 @@ module.exports =
44
type: 'string'
55
default: 'rustc'
66
description: 'Path to rust compiller'
7+
cargoExecutablePath:
8+
type: 'string'
9+
default: 'cargo'
10+
description: 'Path to Cargo package manager'
11+
useCargo:
12+
type: 'boolean'
13+
default: true
14+
description: 'Use Cargo if it\'s possible'
715
cargoManifestFilename:
816
type: 'string'
917
default: 'Cargo.toml'
1018
description: 'Cargo manifest filename'
11-
lintOnChange:
12-
type: 'boolean'
13-
default: true
14-
description: 'Lint file on change (experimental)'
19+
jobsNumber:
20+
type: 'integer'
21+
default: 2
22+
description: 'Number of jobs to run Cargo in parallel'
23+
executionTimeout:
24+
type: 'integer'
25+
default: 10000
26+
description: 'Linting execution timeout'
27+
1528

1629
activate: ->
1730
console.log 'Linter-Rust: package loaded,

lib/linter-rust.coffee

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,89 @@ Linter = require "#{linterPath}/lib/linter"
55
{log, warn, findFile} = require "#{linterPath}/lib/utils"
66
fs = require 'fs'
77
path = require 'path'
8-
tmp = require('tmp')
98

109

1110
class LinterRust extends Linter
12-
@enabled: false
11+
regex: '(?<file>.+):(?<line>\\d+):(?<col>\\d+):\\s*(\\d+):(\\d+)\\s+((?<error>error|fatal error)|(?<warning>warning)|(?<info>note)):\\s+(?<message>.+)\n'
1312
@syntax: 'source.rust'
14-
rustcPath: ''
1513
linterName: 'rust'
1614
errorStream: 'stderr'
17-
regex: '(?<file>.+):(?<line>\\d+):(?<col>\\d+):\\s*(\\d+):(\\d+)\\s+((?<error>error|fatal error)|(?<warning>warning)|(?<info>note)):\\s+(?<message>.+)\n'
15+
16+
rustcPath: ''
17+
cargoPath: ''
1818
cargoManifestFilename: ''
19-
dependencyDir: "target/debug/deps"
20-
tmpFile: null
21-
lintOnChange: false
19+
cargoDependencyDir: "target/debug/deps"
20+
useCargo: true
21+
jobsNumber: 2
22+
23+
baseOptions: []
24+
executionTimeout: 10000
2225

2326

2427
constructor: (@editor) ->
2528
super @editor
2629
atom.config.observe 'linter-rust.executablePath', =>
27-
rustcPath = atom.config.get 'linter-rust.executablePath'
28-
if rustcPath != @rustcPath
29-
@enabled = false
30-
@rustcPath = rustcPath
31-
exec "\"#{@rustcPath}\" --version", @executionCheckHandler
30+
@rustcPath = atom.config.get 'linter-rust.executablePath'
31+
exec "\"#{@rustcPath}\" --version", @executionCheckHandler
32+
atom.config.observe 'linter-rust.cargoExecutablePath', =>
33+
@cargoPath = atom.config.get 'linter-rust.cargoExecutablePath'
34+
exec "\"#{@cargoPath}\" --version", @executionCheckHandler
3235
atom.config.observe 'linter-rust.cargoManifestFilename', =>
33-
@cargoManifestFilename = atom.config.get 'linter-rust.cargoMainifestFilename'
34-
atom.config.observe 'linter-rust.lintOnChange', =>
35-
@lintOnChange = atom.config.get 'linter-rust.lintOnChange'
36+
@cargoManifestFilename = atom.config.get 'linter-rust.cargoManifestFilename'
37+
atom.config.observe 'linter-rust.useCargo', =>
38+
@useCargo = atom.config.get 'linter-rust.useCargo'
39+
atom.config.observe 'linter-rust.jobsNumber', =>
40+
@jobsNumber = atom.config.get 'jobsNumber'
41+
atom.config.observe 'linter-rust.executionTimeout', =>
42+
@executionTimeout = atom.config.get 'linter-rust.executionTimeout'
3643

3744

3845
executionCheckHandler: (error, stdout, stderr) =>
39-
versionRegEx = /rustc ([\d\.]+)/
40-
if not versionRegEx.test(stdout)
46+
executable = if /^rustc/.test stdout then ['rustc', @rustcPath] else ['cargo', @cargoPath]
47+
versionRegEx = /(rustc|cargo) ([\d\.]+)/
48+
if not versionRegEx.test stdout
4149
result = if error? then '#' + error.code + ': ' else ''
4250
result += 'stdout: ' + stdout if stdout.length > 0
4351
result += 'stderr: ' + stderr if stderr.length > 0
44-
console.error "Linter-Rust: \"#{@rustcPath}\" was not executable: \
52+
console.error "Linter-Rust: \"#{executable[1]}\" was not executable: \
4553
\"#{result}\". Please, check executable path in the linter settings."
54+
if 'rustc' == executable[0] then @rustcPath='' else @cargoPath=''
4655
else
47-
@enabled = true
48-
log "Linter-Rust: found rust " + versionRegEx.exec(stdout)[1]
49-
do @initCmd
56+
log "Linter-Rust: found #{executable[0]}"
5057

5158

52-
initCmd: =>
53-
@cmd = [@rustcPath, '-Z', 'no-trans', '--color', 'never']
54-
cargoPath = do @locateCargoManifest
55-
if cargoPath
56-
@cmd.push '-L'
57-
@cmd.push path.join cargoPath, @dependencyDir
58-
log 'Linter-Rust: initialization completed'
59+
initCmd: (editingFile) =>
60+
cargoManifestPath = do @locateCargoManifest
61+
if not @cargoPath or not @useCargo or not cargoManifestPath
62+
@cmd = [@rustcPath, '-Z', 'no-trans', '--color', 'never']
63+
if cargoManifestPath
64+
@cmd.push '-L'
65+
@cmd.push path.join path.dirname(cargoManifestPath), @cargoDependencyDir
66+
return editingFile
67+
else
68+
@cmd = [@cargoPath, 'build', '-j', 2, '--manifest-path']
69+
return cargoManifestPath
5970

6071

6172
lintFile: (filePath, callback) =>
62-
if @enabled
63-
fileName = path.basename do @editor.getPath
64-
if @lintOnChange
65-
cur_dir = path.dirname do @editor.getPath
66-
@tmpFile = tmp.fileSync {dir: cur_dir, postfix: "-#{fileName}"}
67-
fs.writeFileSync @tmpFile.name, @editor.getText()
68-
super @tmpFile.name, callback
69-
else
70-
super fileName, callback
73+
editingFile = @initCmd path.basename do @editor.getPath
74+
if @rustcPath or (@cargoPath and @useCargo)
75+
super editingFile, callback
7176

7277

7378
locateCargoManifest: ->
7479
cur_dir = path.resolve path.dirname do @editor.getPath
75-
return findFile(cur_dir, @cargoManifestFilename)
76-
80+
findFile(cur_dir, @cargoManifestFilename)
7781

78-
processMessage: (message, callback) ->
79-
if @tmpFile
80-
@tmpFile.removeCallback()
81-
@tmpFile = null
82-
super message, callback
8382

84-
85-
formatMessage: (match) ->
86-
fileName = path.basename do @editor.getPath
87-
fileNameRE = RegExp "-#{fileName}$"
83+
formatMessage: (match) =>
8884
type = if match.error then match.error else if match.warning then match.warning else match.info
89-
if fileNameRE.test(match.file) or fileName == match.file
90-
"#{type}: #{match.message}"
91-
else
85+
fileName = path.basename do @editor.getPath
86+
if match.file isnt fileName
87+
match.col = match.line = 0
9288
"#{type} in #{match.file}: #{match.message}"
89+
else
90+
"#{type}: #{match.message}"
9391

9492

9593
module.exports = LinterRust

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
"linter-package": true,
44
"main": "./lib/init",
55
"version": "0.0.13",
6-
"description": "Lint Rust on the fly, using rustc",
6+
"description": "Lint Rust-files, using rustc and/or cargo",
77
"repository": "https://github.com/AtomLinter/linter-rust",
88
"license": "MIT",
99
"engines": {
1010
"atom": ">0.50.0"
1111
},
12-
"dependencies": {
13-
"tmp": "0.0.26"
14-
}
12+
"dependencies": {}
1513
}

0 commit comments

Comments
 (0)