From 135b5b49eba6d5ac176f27b928b89b20fca8cf99 Mon Sep 17 00:00:00 2001 From: liigo Date: Tue, 16 Dec 2014 08:59:40 +0800 Subject: [PATCH 1/4] initial work on support cargo --- lib/linter-rust.coffee | 43 +++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/linter-rust.coffee b/lib/linter-rust.coffee index 5187dc3..6f910b3 100644 --- a/lib/linter-rust.coffee +++ b/lib/linter-rust.coffee @@ -2,7 +2,7 @@ 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' @@ -20,26 +20,47 @@ class LinterRust extends Linter exec "#{@executablePath} --version", @executionCheckHandler 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 rust " + versionRegEx.exec(stdout)[1] + 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) => + # @cmd = "#{@executablePath} --no-trans --color never" + # @cmd = "#{@executablePath} build --manifest-path" + dir = path.dirname editing_file + cargofile = findFile(dir, "Cargo.toml") + log("find cargofile: ", cargofile) + if cargofile + @cwd = path.dirname cargofile + @cmd = "cargo build --verbose" + else + @cwd = path.dirname editing_file + @cmd = "rustc --no-trans --color never" 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 user is editing + editing_file = @editor.getPath() + log("lintFile", editing_file) + @initCmd editing_file + super(editing_file, callback) + + beforeSpawnProcess: (command, args, options) => + { + command: "cargo", # will be configable as executablePath2? + args: args[0..-2], # remove the last .rs file, we only need Cargo.toml + options: options # keep it as is + } formatMessage: (match) -> type = if match.error then match.error else match.warning From cd9bf8f9ff28e86eddb9af5c8c21f8224a619de3 Mon Sep 17 00:00:00 2001 From: liigo Date: Thu, 18 Dec 2014 15:52:15 +0800 Subject: [PATCH 2/4] searching file Cargo.toml to decide which command we use, rustc or cargo --- lib/init.coffee | 6 +++++- lib/linter-rust.coffee | 45 +++++++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 19 deletions(-) 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 6f910b3..47e2195 100644 --- a/lib/linter-rust.coffee +++ b/lib/linter-rust.coffee @@ -9,6 +9,8 @@ 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' @@ -18,6 +20,8 @@ class LinterRust extends Linter 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|cargo) ([\d\.]+)/ @@ -29,41 +33,46 @@ class LinterRust extends Linter \"#{result}\". Please, check executable path in the linter settings." else @enabled = true - # log "Linter-Rust: found rust " + versionRegEx.exec(stdout)[1] log "Linter-Rust: found " + stdout log 'Linter-Rust: initialization completed' initCmd: (editing_file) => - # @cmd = "#{@executablePath} --no-trans --color never" - # @cmd = "#{@executablePath} build --manifest-path" + # search for Cargo.toml in container directoies dir = path.dirname editing_file - cargofile = findFile(dir, "Cargo.toml") - log("find cargofile: ", cargofile) - if cargofile - @cwd = path.dirname cargofile - @cmd = "cargo build --verbose" + @cargoManifestPath = findFile(dir, "Cargo.toml") + if @cargoManifestPath + log "found Cargo.toml: #{@cargoManifestPath}" + @cmd = "cargo build" + @cwd = path.dirname @cargoManifestPath else - @cwd = path.dirname editing_file @cmd = "rustc --no-trans --color never" + @cwd = path.dirname editing_file lintFile: (filePath, callback) => if not @enabled return - # filePath is in tmp dir, not the real one user is editing + # filePath is in tmp dir, not the real one that user is editing editing_file = @editor.getPath() - log("lintFile", editing_file) @initCmd editing_file - super(editing_file, callback) + if @cargoManifestPath + super(editing_file, callback) + else + super(filePath, callback) beforeSpawnProcess: (command, args, options) => - { - command: "cargo", # will be configable as executablePath2? - args: args[0..-2], # remove the last .rs file, we only need Cargo.toml - options: options # keep it as is - } + # 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 From 02a945de1a09623c0646124ee1d5d557d8507c35 Mon Sep 17 00:00:00 2001 From: liigo Date: Fri, 19 Dec 2014 16:27:41 +0800 Subject: [PATCH 3/4] more meta info --- CHANGELOG.md | 3 +++ README.md | 4 ++-- lib/linter-rust.coffee | 2 +- package.json | 4 ++-- 4 files changed, 8 insertions(+), 5 deletions(-) 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/linter-rust.coffee b/lib/linter-rust.coffee index 47e2195..b2ff4cd 100644 --- a/lib/linter-rust.coffee +++ b/lib/linter-rust.coffee @@ -13,7 +13,7 @@ class LinterRust extends Linter @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 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": { From d16985cbc53fc5bcc762464d161c687d3410ca78 Mon Sep 17 00:00:00 2001 From: Liigo Zhuang Date: Thu, 12 Mar 2015 13:46:17 +0800 Subject: [PATCH 4/4] update no-trans arg --- lib/linter-rust.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linter-rust.coffee b/lib/linter-rust.coffee index b2ff4cd..3aaeded 100644 --- a/lib/linter-rust.coffee +++ b/lib/linter-rust.coffee @@ -45,7 +45,7 @@ class LinterRust extends Linter @cmd = "cargo build" @cwd = path.dirname @cargoManifestPath else - @cmd = "rustc --no-trans --color never" + @cmd = "rustc -Z no-trans --color never" @cwd = path.dirname editing_file lintFile: (filePath, callback) =>