Skip to content

Commit

Permalink
Refactored to use IssueCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmecham committed Oct 30, 2019
1 parent b77f73f commit 49587e3
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 96 deletions.
74 changes: 74 additions & 0 deletions Scripts/RuboCop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// RuboCop Extension for Nova
// RuboCop.js
//
// Copyright © 2019 Justin Mecham. All rights reserved.
//

const RuboCopProcess = require("RuboCopProcess");

class RuboCop {

constructor() {
this.disposables = new CompositeDisposable();
this.issues = new IssueCollection();
this.editors = [];

this.disposables.add(this.issues);

this.disposables.add(nova.workspace.onDidAddTextEditor(this.startWatchingTextEditor.bind(this)));
}

startWatchingTextEditor(editor) {
const document = editor.document;

if (document.syntax != "ruby") {
console.info(`[startWatchingTextEditor] Skipping ${document.path} (Syntax: ${document.syntax})`);
return;
}

console.info(`[startWatchingTextEditor] Adding ${document.path} (Syntax: ${document.syntax})`);

editor.onDidSave(this.processDocument.bind(this));
editor.onDidStopChanging(this.processDocument.bind(this));
editor.onDidDestroy(this.stopWatchingTextEditor.bind(this));

this.editors.push(editor);

this.processDocument(editor);
}

stopWatchingTextEditor(editor) {
const index = this.editors.indexOf(editor);
if (index) {
const uri = `file://${editor.document.path}`;
const document = editor.document;
console.info(`[stopWatchingTextEditor] Removing editor for ${document.path}`);
this.editors.splice(this.editors.index, 1);
this.issues.remove(uri);
} else {
console.warn("[stopWatchingTextEditor] Attempted to remove an unknown text editor...")
}
}

processDocument(editor) {
const relativePath = nova.workspace.relativizePath(editor.document.path);
const uri = `file://${editor.document.path}`;
const contentRange = new Range(0, editor.document.length);
const content = editor.document.getTextInRange(contentRange);
const process = new RuboCopProcess(relativePath, content);

process.onComplete((offenses) => {
this.issues.set(uri, offenses.map(offense => offense.issue));
});

process.execute();
}

dispose() {
this.disposables.dispose();
}

}

module.exports = RuboCop;
2 changes: 1 addition & 1 deletion Scripts/offense.js → Scripts/RuboCopOffense.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// RuboCop Extension for Nova
// offense.js
// RuboCopOffense.js
//
// Copyright © 2019 Justin Mecham. All rights reserved.
//
Expand Down
32 changes: 16 additions & 16 deletions Scripts/rubocop.js → Scripts/RuboCopProcess.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// RuboCop Extension for Nova
// rubocop.js
// RuboCopProcess.js
//
// Copyright © 2019 Justin Mecham. All rights reserved.
//

const RuboCopOffense = require("offense");
const Offense = require("RuboCopOffense");

class RuboCopProcess {

Expand All @@ -14,18 +14,6 @@ class RuboCopProcess {
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() {
if (this._process) { return this._process }

Expand All @@ -41,6 +29,18 @@ class RuboCopProcess {
return (this._process = process);
}

execute() {
this.process.start();

const channel = this.process.stdin;
const writer = channel.getWriter();

writer.ready.then(() => {
writer.write(this.content);
writer.close();
});
}

handleError(error) {
console.error(error);
}
Expand All @@ -51,11 +51,11 @@ class RuboCopProcess {

console.info(JSON.stringify(offenses, null, " "));

this.offenses = offenses.map(offense => new RuboCopOffense(offense))
this.offenses = offenses.map(offense => new Offense(offense));

if (this._onCompleteCallback) {
this._onCompleteCallback(this.offenses);
}
}
}

onComplete(callback) {
Expand Down
70 changes: 0 additions & 70 deletions Scripts/issuesProvider.js

This file was deleted.

13 changes: 4 additions & 9 deletions Scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
// Copyright © 2019 Justin Mecham. All rights reserved.
//

const RuboCopIssuesProvider = require("issuesProvider");
let disposables = new CompositeDisposable();
const disposables = new CompositeDisposable();
const RuboCop = require("RuboCop");

exports.activate = function() {
console.log("Activating");

const issuesProvider = new RuboCopIssuesProvider();
nova.workspace.onDidAddTextEditor(issuesProvider.addTextEditor.bind(issuesProvider));
disposables.add(nova.assistants.registerIssueAssistant("ruby", issuesProvider));
const ruboCopInstance = new RuboCop();
disposables.add(ruboCopInstance);
}

exports.deactivate = function() {
console.log("Deactivating");

disposables.dispose();
}

0 comments on commit 49587e3

Please sign in to comment.