Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmecham committed Oct 23, 2019
1 parent 08c728d commit ebfe1c6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 49 deletions.
68 changes: 19 additions & 49 deletions Scripts/issuesProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copyright © 2019 Justin Mecham. All rights reserved.
//

const RuboCopOffense = require("offense");
const RuboCopProcess = require("rubocop");

class RuboCopIssuesProvider {

Expand All @@ -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...")
}
}

Expand All @@ -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;
57 changes: 57 additions & 0 deletions Scripts/rubocop.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions Tests/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

class Test
end
]



0 comments on commit ebfe1c6

Please sign in to comment.