diff --git a/Scripts/issuesProvider.js b/Scripts/issuesProvider.js index fb4cb96..40d10c1 100644 --- a/Scripts/issuesProvider.js +++ b/Scripts/issuesProvider.js @@ -5,7 +5,7 @@ // Copyright © 2019 Justin Mecham. All rights reserved. // -const RuboCopOffense = require("offense"); +const RuboCopProcess = require("rubocop"); class RuboCopIssuesProvider { @@ -18,31 +18,30 @@ class RuboCopIssuesProvider { const document = editor.document; if (document.syntax != "ruby") { - console.log(`[addTextEditor] Skipping ${document.path} (Syntax: ${document.syntax})`); + console.info(`[addTextEditor] Skipping ${document.path} (Syntax: ${document.syntax})`); return; } - console.log(`[addTextEditor] Adding ${document.path} (Syntax: ${document.syntax})`); + console.info(`[addTextEditor] Adding ${document.path} (Syntax: ${document.syntax})`); - const relativePath = nova.workspace.relativizePath(document.path); - this.processFile(relativePath, editor); - - editor.onDidSave(this.updateFile.bind(this)); - editor.onDidStopChanging(this.updateFile.bind(this)); + editor.onDidSave(this.processDocument.bind(this)); + editor.onDidStopChanging(this.processDocument.bind(this)); editor.onDidDestroy(this.removeTextEditor.bind(this)); this.editors.push(editor); + + this.processDocument(editor); } removeTextEditor(editor) { const index = this.editors.indexOf(editor); if (index) { const document = editor.document; - console.log(`[removeTextEditor] Removing editor for ${document.path}`); + console.info(`[removeTextEditor] Removing editor for ${document.path}`); this.editors.splice(this.editors.index, 1); delete this.offenses[editor.document.path]; } else { - console.log.warn("[removeTextEditor] Attempted to remove an unknown text editor...") + console.warn("[removeTextEditor] Attempted to remove an unknown text editor...") } } @@ -52,49 +51,20 @@ class RuboCopIssuesProvider { return this.offenses[relativePath].map(offense => offense.issue); } - - updateFile(editor) { - const relativePath = nova.workspace.relativizePath(editor.document.path); - this.processFile(relativePath, editor); - } - - // TODO: Extract this into its own class - processFile(path, editor) { - const options = { - args: ["rubocop", "-o /tmp/foo", "--format=json", "--stdin", path], - cwd: nova.workspace.path, - stdio: "pipe", - }; - - const process = new Process("/usr/bin/env", options); - - process.onStdout(this.processResult.bind(this)); - - process.onStderr(function(line) { - console.log("Error! ", line); - }); - - process.start(); + + processDocument(editor) { + const relativePath = nova.workspace.relativizePath(editor.document.path); + const contentRange = new Range(0, editor.document.length); + const content = editor.document.getTextInRange(contentRange); + const rubocop = new RuboCopProcess(relativePath, content); - const writableStream = process.stdio[0]; - const defaultWriter = writableStream.getWriter(); - - defaultWriter.ready.then(() => { - defaultWriter.write(editor.document.getTextInRange(new Range(0, editor.document.length))); - defaultWriter.close(); + rubocop.onComplete =((offenses) => { + this.offenses[relativePath] = rubocop.offenses; }); - } - - processResult(result) { - const parsedResult = JSON.parse(result); - console.log("processResult ", result); - const path = parsedResult["files"][0]["path"]; - const rawOffenses = parsedResult["files"][0]["offenses"]; - const offenses = rawOffenses.map(rawOffense => new RuboCopOffense(rawOffense)) - - this.offenses[path] = offenses; + rubocop.execute(); } + } module.exports = RuboCopIssuesProvider; diff --git a/Scripts/rubocop.js b/Scripts/rubocop.js new file mode 100644 index 0000000..89fe195 --- /dev/null +++ b/Scripts/rubocop.js @@ -0,0 +1,57 @@ +// +// RuboCop for Nova +// rubocop.js +// +// Copyright © 2019 Justin Mecham. All rights reserved. +// + +const RuboCopOffense = require("offense"); + +class RuboCopProcess { + + constructor(path, content = null) { + this.path = path; + this.content = content; + } + + execute() { + this.process.start(); + + const channel = this.process.stdin; + const writer = channel.getWriter(); + + writer.ready.then(() => { + writer.write(this.content); + writer.close(); + }); + } + + get process() { + const process = new Process("/usr/bin/env", { + args: ["rubocop", "--format=json", "--stdin", this.path], + cwd: nova.workspace.path, + stdio: "pipe", + }); + + process.onStdout(this.handleOutput.bind(this)); + process.onStderr(this.handleError.bind(this)); + + return process; + } + + handleError(error) { + console.error(error); + } + + handleOutput(output) { + const parsedOutput = JSON.parse(result); + const offenses = parsedOutput["files"][0]["offenses"]; + + console.info(JSON.stringify(offenses, null, " ")); + + this.offenses = offenses.map(offense => new RuboCopOffense(offense)) + } + +} + +module.exports = RuboCopProcess; diff --git a/Tests/test.rb b/Tests/test.rb index 002b285..d3b36a7 100644 --- a/Tests/test.rb +++ b/Tests/test.rb @@ -2,3 +2,7 @@ class Test end +] + + +