This repository has been archived by the owner on Nov 6, 2023. It is now read-only.
forked from misogi/vscode-ruby-rubocop
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Verseth/feature/quick_fixes
Implement quick fixes for Rubocop
- Loading branch information
Showing
9 changed files
with
772 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as vscode from 'vscode'; | ||
|
||
import { getConfig } from './configuration'; | ||
|
||
export function isFileUri(uri: vscode.Uri): boolean { | ||
return uri.scheme === 'file'; | ||
} | ||
|
||
export function getCurrentPath(fileUri: vscode.Uri): string { | ||
const wsfolder = vscode.workspace.getWorkspaceFolder(fileUri); | ||
return (wsfolder && wsfolder.uri.fsPath) || path.dirname(fileUri.fsPath); | ||
} | ||
|
||
// extract argument to an array | ||
export function getCommandArguments(fileName: string): string[] { | ||
let commandArguments = ['--stdin', fileName, '--force-exclusion']; | ||
const extensionConfig = getConfig(); | ||
if (extensionConfig.configFilePath !== '') { | ||
const found = [extensionConfig.configFilePath] | ||
.concat( | ||
(vscode.workspace.workspaceFolders || []).map((ws) => | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as vscode from 'vscode'; | ||
import * as cp from 'child_process'; | ||
|
||
import { getConfig } from './configuration'; | ||
import { getCommandArguments, getCurrentPath } from './helper'; | ||
|
||
export default class RubocopAutocorrectProvider | ||
implements vscode.DocumentFormattingEditProvider | ||
{ | ||
public provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] { | ||
return this.getAutocorrectEdits(document); | ||
} | ||
|
||
public getAutocorrectEdits(document: vscode.TextDocument, additionalArguments: string[] = []): vscode.TextEdit[] { | ||
const config = getConfig(); | ||
try { | ||
const args = [...getCommandArguments(document.fileName), ...additionalArguments]; | ||
if(additionalArguments.length === 0) args.push('--autocorrect'); | ||
|
||
if (config.useServer) { | ||
args.push('--server'); | ||
} | ||
|
||
const options = { | ||
cwd: getCurrentPath(document.uri), | ||
input: document.getText(), | ||
}; | ||
let stdout; | ||
if (config.useBundler) { | ||
stdout = cp.execSync(`${config.command} ${args.join(' ')}`, options); | ||
} else { | ||
stdout = cp.execFileSync(config.command, args, options); | ||
} | ||
|
||
return this.onSuccess(document, stdout); | ||
} catch (e) { | ||
// if there are still some offences not fixed RuboCop will return status 1 | ||
if (e.status !== 1) { | ||
vscode.window.showWarningMessage( | ||
'An error occurred during auto-correction' | ||
); | ||
console.log(e); | ||
return []; | ||
} else { | ||
return this.onSuccess(document, e.stdout); | ||
} | ||
} | ||
} | ||
|
||
// Output of auto-correction 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 auto-corrected ruby | ||
private onSuccess(document: vscode.TextDocument, stdout: Buffer) { | ||
const stringOut = stdout.toString(); | ||
const autoCorrection = stringOut.match( | ||
/^.*\n====================(?:\n|\r\n)([.\s\S]*)/m | ||
); | ||
if (!autoCorrection) { | ||
throw new Error(`Error parsing auto-correction from CLI: ${stringOut}`); | ||
} | ||
return [ | ||
new vscode.TextEdit(this.getFullRange(document), autoCorrection.pop()), | ||
]; | ||
} | ||
|
||
private getFullRange(document: vscode.TextDocument): vscode.Range { | ||
return new vscode.Range( | ||
new vscode.Position(0, 0), | ||
document.lineAt(document.lineCount - 1).range.end | ||
); | ||
} | ||
} |
Oops, something went wrong.