Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
{
"command": "editor.action.formatDocument",
"title": "Ruby: autocorrect by rubocop"
},
{
"command": "ruby.rubocop.restart_server",
"title": "Ruby: restart rubocop server"
}
],
"keybindings": [
Expand Down Expand Up @@ -86,6 +90,11 @@
"default": false,
"description": "execute rubocop using bundler (ie 'bundle exec rubocop')"
},
"ruby.rubocop.useServer": {
"type": "boolean",
"default": false,
"description": "execute rubocop in server mode (ie 'rubocop --server')"
},
"ruby.rubocop.suppressRubocopWarnings": {
"type": "boolean",
"default": false,
Expand Down
3 changes: 3 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface RubocopConfig {
onSave: boolean;
configFilePath: string;
useBundler: boolean;
useServer: boolean;
suppressRubocopWarnings: boolean;
}

Expand Down Expand Up @@ -48,6 +49,7 @@ export const getConfig: () => RubocopConfig = () => {
const cmd = win32 ? 'rubocop.bat' : 'rubocop';
const conf = vs.workspace.getConfiguration('ruby.rubocop');
let useBundler = conf.get('useBundler', false);
let useServer = conf.get('useServer', false);
const configPath = conf.get('executePath', '');
const suppressRubocopWarnings = conf.get('suppressRubocopWarnings', false);
let command: string;
Expand All @@ -73,6 +75,7 @@ export const getConfig: () => RubocopConfig = () => {
configFilePath: conf.get('configFilePath', ''),
onSave: conf.get('onSave', true),
useBundler,
useServer,
suppressRubocopWarnings,
};
};
Expand Down
19 changes: 18 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@ import * as vscode from 'vscode';
import { Rubocop, RubocopAutocorrectProvider } from './rubocop';
import { onDidChangeConfiguration } from './configuration';

let rubocop: Rubocop;

// entry point of extension
export function activate(context: vscode.ExtensionContext): void {
'use strict';

const diag = vscode.languages.createDiagnosticCollection('ruby');
context.subscriptions.push(diag);

const rubocop = new Rubocop(diag);
rubocop = new Rubocop(diag);
const disposable = vscode.commands.registerCommand('ruby.rubocop', () => {
const document = vscode.window.activeTextEditor.document;
rubocop.execute(document);
});

context.subscriptions.push(disposable);

const restart = vscode.commands.registerCommand(
'ruby.rubocop.restart_server',
() => {
rubocop.restart();
}
);

context.subscriptions.push(restart);

const ws = vscode.workspace;

ws.onDidChangeConfiguration(onDidChangeConfiguration(rubocop));
Expand Down Expand Up @@ -48,3 +59,9 @@ export function activate(context: vscode.ExtensionContext): void {
formattingProvider
);
}

export function deactivate(): void {
if (rubocop) {
rubocop.stop();
}
}
21 changes: 21 additions & 0 deletions src/rubocop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function getCurrentPath(fileUri: vscode.Uri): string {
function getCommandArguments(fileName: string): string[] {
let commandArguments = ['--stdin', fileName, '--force-exclusion'];
const extensionConfig = getConfig();
if (extensionConfig.useServer) {
commandArguments = commandArguments.concat('--server');
}
if (extensionConfig.configFilePath !== '') {
const found = [extensionConfig.configFilePath]
.concat(
Expand Down Expand Up @@ -212,6 +215,24 @@ export class Rubocop {
}
}

public restart(): void {
const folders = vscode.workspace.workspaceFolders;
if (this.config.useServer && folders && folders.length) {
cp.execSync(`${this.config.command} --restart-server`, {
cwd: folders[0].uri.fsPath,
});
}
}

public stop(): void {
const folders = vscode.workspace.workspaceFolders;
if (this.config.useServer && folders && folders.length) {
cp.execSync(`${this.config.command} --stop-server`, {
cwd: folders[0].uri.fsPath,
});
}
}

// execute rubocop
private executeRubocop(
args: string[],
Expand Down
1 change: 1 addition & 0 deletions test/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ vsStub.workspace.getConfiguration = (
executePath: '',
onSave: true,
useBundler: false,
useServer: false,
suppressRubocopWarnings: false,
};

Expand Down