diff --git a/CHANGELOG.md b/CHANGELOG.md index 0713337..ebcc7a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.1.0 +Now we support Cargo! + # 0.0.3 Check original file, not a tmp copy. See #1. diff --git a/README.md b/README.md index c0fcdd5..98ccc31 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/lib/init.coffee b/lib/init.coffee index 36472ef..bd7ae6e 100644 --- a/lib/init.coffee +++ b/lib/init.coffee @@ -3,7 +3,11 @@ module.exports = executablePath: type: 'string' default: 'rustc' - description: 'Path to rust compiller.' + description: 'Path to rust compiler' + executablePath2: + type: 'string' + default: 'cargo' + description: 'Path to rust package manager' activate: -> console.log 'Linter-Rust: package loaded, diff --git a/lib/linter-rust.coffee b/lib/linter-rust.coffee index 5187dc3..3aaeded 100644 --- a/lib/linter-rust.coffee +++ b/lib/linter-rust.coffee @@ -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: '^(.+):(?\\d+):(?\\d+):\\s*(\\d+):(\\d+)\\s+((?error|fatal error)|(?warning)):\\s+(?.+)\n' + regex: '^(?.+):(?\\d+):(?\\d+):\\s*(\\d+):(\\d+)\\s+((?error|fatal error)|(?warning)):\\s+(?.+)\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 diff --git a/package.json b/package.json index d54d014..ab528d4 100644 --- a/package.json +++ b/package.json @@ -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": {