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

Support Cargo #5

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.1.0
Now we support Cargo!

# 0.0.3
Check original file, not a tmp copy. See #1.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# linter-rust

This package will lint your Rust-files in Atom, using [rustc](http://www.rust-lang.org).
This package will lint your Rust-files in Atom, using [rustc](http://www.rust-lang.org) and [cargo](https://crates.io).
Files will be checked when you open or save them.

## Installation

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

Expand Down
6 changes: 5 additions & 1 deletion lib/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module.exports =
executablePath:
type: 'string'
default: 'rustc'
description: 'Path to rust compiller.'
description: 'Path to rust compiler'
executablePath2:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should use more understandable names of config variables, e.g. rustcPath and cargoPath

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmist executablePath is used by upstream Linter. If i remove executablePath (and add rustcPath, cargoPath as you suggested) here, Linter will always add another executablePath in settings page, which looks weird. Maybe I'm wrong.

type: 'string'
default: 'cargo'
description: 'Path to rust package manager'

activate: ->
console.log 'Linter-Rust: package loaded,
Expand Down
56 changes: 43 additions & 13 deletions lib/linter-rust.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,77 @@ linterPath = atom.packages.getLoadedPackage("linter").path
Linter = require "#{linterPath}/lib/linter"

{exec} = require 'child_process'
{log, warn} = require "#{linterPath}/lib/utils"
{log, warn, findFile} = require "#{linterPath}/lib/utils"
path = require 'path'


class LinterRust extends Linter
@enable: false
@syntax: 'source.rust'
@cargoPath: 'cargo'
@cargoManifestPath: null
linterName: 'rust'
errorStream: 'stderr'
regex: '^(.+):(?<line>\\d+):(?<col>\\d+):\\s*(\\d+):(\\d+)\\s+((?<error>error|fatal error)|(?<warning>warning)):\\s+(?<message>.+)\n'
regex: '^(?<file>.+):(?<line>\\d+):(?<col>\\d+):\\s*(\\d+):(\\d+)\\s+((?<error>error|fatal error)|(?<warning>warning)):\\s+(?<message>.+)\n'

constructor: (@editor) ->
super @editor
atom.config.observe 'linter-rust.executablePath', =>
@executablePath = atom.config.get 'linter-rust.executablePath'
exec "#{@executablePath} --version", @executionCheckHandler
atom.config.observe 'linter-rust.executablePath2', =>
@cargoPath = atom.config.get 'linter-rust.executablePath2'

executionCheckHandler: (error, stdout, stderr) =>
versionRegEx = /rustc ([\d\.]+)/
versionRegEx = /(rustc|cargo) ([\d\.]+)/
if not versionRegEx.test(stdout)
result = if error? then '#' + error.code + ': ' else ''
result += 'stdout: ' + stdout if stdout.length > 0
result += 'stderr: ' + stderr if stderr.length > 0
console.error "Linter-Rust: \"#{@executablePath}\" was not executable: \
console.error "Linter-Rust: \"#{@executablePath}\" was invalid: \
\"#{result}\". Please, check executable path in the linter settings."
else
@enabled = true
log "Linter-Rust: found rust " + versionRegEx.exec(stdout)[1]
do @initCmd
log "Linter-Rust: found " + stdout
log 'Linter-Rust: initialization completed'

initCmd: =>
@cmd = "#{@executablePath} --no-trans --color never"
log 'Linter-Rust: initialization completed'
initCmd: (editing_file) =>
# search for Cargo.toml in container directoies
dir = path.dirname editing_file
@cargoManifestPath = findFile(dir, "Cargo.toml")
if @cargoManifestPath
log "found Cargo.toml: #{@cargoManifestPath}"
@cmd = "cargo build"
@cwd = path.dirname @cargoManifestPath
else
@cmd = "rustc -Z no-trans --color never"
@cwd = path.dirname editing_file

lintFile: (filePath, callback) =>
if @enabled
origin_file = path.basename @editor.getPath()
super(origin_file, callback)
if not @enabled
return
# filePath is in tmp dir, not the real one that user is editing
editing_file = @editor.getPath()
@initCmd editing_file
if @cargoManifestPath
super(editing_file, callback)
else
super(filePath, callback)

beforeSpawnProcess: (command, args, options) =>
# is there a Cargo.toml file?
if @cargoManifestPath
return {
command: @cargoPath, # we build package using cargo
args: args[0..-2], # remove the last .rs file that Linter always appends
options: options # keep it as is
}
else
# we compile .rs file using rustc
return { command: command, args: args, options:options }

formatMessage: (match) ->
type = if match.error then match.error else match.warning
"#{type} #{match.message}"
"#{type}: #{match.message}"

module.exports = LinterRust
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"linter-package": true,
"activationEvents": [],
"main": "./lib/init",
"version": "0.0.3",
"description": "Lint Rust on the fly, using rustc",
"version": "0.1.0",
"description": "Lint Rust on the fly, using rustc and cargo",
"repository": "https://github.com/AtomLinter/linter-rust",
"license": "MIT",
"engines": {
Expand Down