Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into Gokuldroid/master
Browse files Browse the repository at this point in the history
  • Loading branch information
misogi committed Sep 20, 2020
2 parents 430a5f6 + c99fb81 commit bd931a4
Show file tree
Hide file tree
Showing 9 changed files with 1,092 additions and 717 deletions.
13 changes: 3 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
# See https://code.visualstudio.com/Docs/extensions/testing-extensions

sudo: false

os:
- osx
- linux

services:
- xvfb

language: node_js
node_js:
- node

cache:
yarn: true

# Ensure xvfb is available on Linux test hosts
before_install:
- if [ $TRAVIS_OS_NAME == "linux" ]; then
export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
sh -e /etc/init.d/xvfb start;
sleep 3;
fi

before_script:
- npm run vscode:prepublish

Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# 0.8.3

- Using relative config file path

# 0.8.2

- update packages that have secirity issues

# 0.8.1

- add `suppressRubocopWarnings` option that ignore warning

# 0.8.0

- add useBundler config. We can set `true` to override to `bundle exec rubocop`

# 0.7.1

- Fix autoCorrection find on windows
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ Visual Studio Code でrubocopを実行するエクステンションです。

This extension provides interfaces to rubocop for vscode.

[rubocop](https://github.com/bbatsov/rubocop) is code analyzer for ruby.
[rubocop](https://github.com/bbatsov/rubocop) is a code analyzer for ruby.

[ruby rubocop in Code Market Place](https://marketplace.visualstudio.com/items/misogi.ruby-rubocop)

![exec on save](./images/onsave.gif)

## problems
## Problems

This extension may have a problem when using a rvm or chruby environment.
This extension may have problems when using a rvm or chruby environment.
We recommend [vscode-ruby](https://marketplace.visualstudio.com/items?itemName=rebornix.Ruby). It can also lint ruby code.

When autoCorrect, History of changing file is broken.
When autoCorrect is enabled, the history of changing file is broken.

## features
## Features

- lint by execute command "Ruby: lint by rubocop" (cmd+shift+p and type command)
- lint by executing the command "Ruby: lint by rubocop" (cmd+shift+p and type command)
- auto invoke when saving file
- auto correct command "Ruby: autocorrect by rubocop"

### Exclude file

An extension forces rubocop's `force-exclusion` option.
The extension forces rubocop's `force-exclusion` option.

If you want not to execute rubocop on some file, You could add AllCops/Exclude on rubocop.yml. This can saving without executing rubocop.
If you do not want rubocop to be executed on some file, you can add AllCops/Exclude in rubocop.yml. The file can be saved without executing rubocop.

# Installation

Expand Down Expand Up @@ -71,7 +71,7 @@ Specify configuration (via navigating to `File > Preferences > Workspace Setting

### Keybindings

You can change keybinding (via editing `keybindings.json`)
You can change the keybinding (via editing `keybindings.json`)

```javascript
{ "key": "ctrl+alt+l", "command": "ruby.rubocopAutocorrect",
Expand All @@ -80,7 +80,7 @@ You can change keybinding (via editing `keybindings.json`)

# todo

- more configurable command line option (like -R)
- more configurable command line options (like -R)
- integration with rbenv
- testing & CI support

Expand Down
26 changes: 21 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ruby-rubocop",
"version": "0.7.1",
"version": "0.8.3",
"publisher": "misogi",
"displayName": "ruby-rubocop",
"description": "execute rubocop for current Ruby code.",
Expand Down Expand Up @@ -35,6 +35,12 @@
"extensions": [
".rb"
]
},
{
"id": "gemfile",
"filenamePatterns": [
"Gemfile"
]
}
],
"commands": [
Expand All @@ -51,7 +57,7 @@
{
"key": "shift+ctrl+r",
"command": "editor.action.formatDocument",
"when": "editorLangId == 'ruby'"
"when": "editorLangId == 'ruby' || editorLangId == 'gemfile'"
}
],
"configuration": {
Expand All @@ -72,20 +78,30 @@
"type": "boolean",
"default": true,
"description": "execute rubocop on save."
},
"ruby.rubocop.useBundler": {
"type": "boolean",
"default": false,
"description": "execute rubocop using bundler (ie 'bundle exec rubocop')"
},
"ruby.rubocop.suppressRubocopWarnings": {
"type": "boolean",
"default": false,
"description": "Suppress warnings from rubocop and attempt to run regardless. (Useful if you share a rubocop.yml file and run into unrecognized cop errors you know are okay.)"
}
}
}
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/chai": "^4.2.9",
"@types/mocha": "^5.2.6",
"@types/node": "^11.11.6",
"@types/node": "^13.7.7",
"chai": "^4.2.0",
"mocha": "^6.0.2",
"proxyquire": "^2.1.0",
"sinon": "^7.3.0",
"tslint": "^5.1.0",
"typescript": "3.3.4000",
"typescript": "^3.8.3",
"typescript-formatter": "^7.2.2",
"vscode": "^1.1.0"
}
Expand Down
76 changes: 40 additions & 36 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,34 @@ export interface RubocopConfig {
onSave: boolean;
configFilePath: string;
useBundler: boolean;
suppressRubocopWarnings: boolean
}

export const onDidChangeConfiguration: (rubocop: Rubocop) => () => void = (rubocop) => {
return () => rubocop.config = getConfig();
const detectBundledRubocop: () => boolean = () => {
try {
cp.execSync('bundle show rubocop', { cwd: vs.workspace.rootPath });
return true;
} catch (e) {
return false;
}
};

const autodetectExecutePath: (cmd: string) => string = cmd => {
const key: string = 'PATH';
let paths = process.env[key];
if (!paths) {
return '';
}

let pathparts = paths.split(path.delimiter);
for (let i = 0; i < pathparts.length; i++) {
let binpath = path.join(pathparts[i], cmd);
if (fs.existsSync(binpath)) {
return pathparts[i] + path.sep;
}
}

return '';
};

/**
Expand All @@ -23,54 +47,34 @@ export const getConfig: () => RubocopConfig = () => {
const win32 = process.platform === 'win32';
const cmd = win32 ? 'rubocop.bat' : 'rubocop';
const conf = vs.workspace.getConfiguration('ruby.rubocop');
let useBundler;
let path = conf.get('executePath', '');
let useBundler = conf.get('useBundler', false);
let configPath = conf.get('executePath', '');
let suppressRubocopWarnings = conf.get('suppressRubocopWarnings', false)
let command;

// if executePath is present in workspace config, use it.
if (path.length !== 0) {
command = path + cmd;
} else if (detectBundledRubocop()) {
if (configPath.length !== 0) {
command = configPath + cmd;
} else if (useBundler || detectBundledRubocop()) {
useBundler = true;
command = `bundle exec ${cmd}`;
} else {
path = autodetectExecutePath(cmd);
if (0 === path.length) {
const detectedPath = autodetectExecutePath(cmd);
if (0 === detectedPath.length) {
vs.window.showWarningMessage('execute path is empty! please check ruby.rubocop.executePath');
}
command = path + cmd;
command = detectedPath + cmd;
}

return {
useBundler,
command,
configFilePath: conf.get('configFilePath', ''),
onSave: conf.get('onSave', true),
useBundler,
suppressRubocopWarnings,
};
};

const detectBundledRubocop: () => boolean = () => {
try {
cp.execSync('bundle show rubocop', { cwd: vs.workspace.rootPath });
return true;
} catch (e) {
return false;
}
};

const autodetectExecutePath: (cmd: string) => string = (cmd) => {
const key: string = 'PATH';
let paths = process.env[key];
if (!paths) {
return '';
}

let pathparts = paths.split(path.delimiter);
for (let i = 0; i < pathparts.length; i++) {
let binpath = path.join(pathparts[i], cmd);
if (fs.existsSync(binpath)) {
return pathparts[i] + path.sep;
}
}

return '';
export const onDidChangeConfiguration: (rubocop: Rubocop) => () => void = rubocop => {
return () => (rubocop.config = getConfig());
};
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import * as vscode from 'vscode';
import { Rubocop, RubocopAutocorrectProvider } from './rubocop';
import { onDidChangeConfiguration } from './configuration';
Expand Down Expand Up @@ -39,6 +38,7 @@ export function activate(context: vscode.ExtensionContext): void {
ws.onDidCloseTextDocument((e: vscode.TextDocument) => {
rubocop.clear(e);
});
const formattingProvider = new RubocopAutocorrectProvider;
const formattingProvider = new RubocopAutocorrectProvider();
vscode.languages.registerDocumentFormattingEditProvider('ruby', formattingProvider);
vscode.languages.registerDocumentFormattingEditProvider('gemfile', formattingProvider);
}
36 changes: 24 additions & 12 deletions src/rubocop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export class RubocopAutocorrectProvider implements vscode.DocumentFormattingEdit


// Output of autocorrection looks like this:
//
//
// {"metadata": ... {"offense_count":5,"target_file_count":1,"inspected_file_count":1}}====================
// def a
// 3
// end
//
//
// So we need to parse out the actual autocorrected ruby
private onSuccess(document: vscode.TextDocument, stdout: Buffer) {
const stringOut = stdout.toString()
Expand Down Expand Up @@ -83,13 +83,21 @@ function getCommandArguments(fileName: string): string[] {
let commandArguments = ['--stdin', fileName, '--format', 'json', '--force-exclusion'];
const extensionConfig = getConfig();
if (extensionConfig.configFilePath !== '') {
if (fs.existsSync(extensionConfig.configFilePath)) {
const config = ['--config', extensionConfig.configFilePath];
commandArguments = commandArguments.concat(config);
} else {
vscode.window.showWarningMessage(`${extensionConfig.configFilePath} file does not exist. Ignoring...`);

let found = [extensionConfig.configFilePath].concat(
(vscode.workspace.workspaceFolders || []).map((ws: any) => path.join(ws.uri.path, extensionConfig.configFilePath))
).filter((p: string) => fs.existsSync(p));

if (found.length == 0) {
vscode.window.showWarningMessage(`${extensionConfig.configFilePath} file does not exist. Ignoring...`);
} else {
if (found.length > 1) {
vscode.window.showWarningMessage(`Found multiple files (${found}) will use ${found[0]}`);
}
}
const config = ['--config', found[0]];
commandArguments = commandArguments.concat(config);
}
}

return commandArguments;
}
Expand All @@ -110,7 +118,11 @@ export class Rubocop {
}

public execute(document: vscode.TextDocument, onComplete?: () => void): void {
if (document.languageId !== 'ruby' || document.isUntitled || !isFileUri(document.uri)) {
if (
(document.languageId !== 'gemfile' && document.languageId !== 'ruby') ||
document.isUntitled ||
!isFileUri(document.uri)
) {
// git diff has ruby-mode. but it is Untitled file.
return;
}
Expand Down Expand Up @@ -183,7 +195,7 @@ export class Rubocop {
private executeRubocop(
args: string[],
fileContents: string,
options: cp.ExecFileOptions,
options: cp.ExecOptions,
cb: (err: Error, stdout: string, stderr: string) => void): cp.ChildProcess {
let child;
if (this.config.useBundler) {
Expand Down Expand Up @@ -231,8 +243,8 @@ export class Rubocop {
} else if (error && (<any>error).code === 127) {
vscode.window.showWarningMessage(stderr);
return true;
} else if (errorOutput.length > 0) {
vscode.window.showErrorMessage(stderr);
} else if (errorOutput.length > 0 && !this.config.suppressRubocopWarnings) {
vscode.window.showWarningMessage(stderr);
return true;
}

Expand Down
Loading

0 comments on commit bd931a4

Please sign in to comment.