Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codefix): codefix for diagnostics invalid casing #21

Merged
merged 4 commits into from
Nov 7, 2016
Merged
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
37 changes: 37 additions & 0 deletions dist/src/client/htmlInvalidCasingCodeActionProvider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/src/client/htmlInvalidCasingCodeActionProvider.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions dist/src/client/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/src/client/main.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions src/client/htmlInvalidCasingCodeActionProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
import vscode = require('vscode');

export default class HtmlInvalidCasingActionProvider implements vscode.CodeActionProvider {
private static commandId: string = 'aurelia-fix-invalid-casing';
private command: vscode.Disposable;

public activate(subscriptions: vscode.Disposable[]) {
this.command = vscode.commands.registerCommand(HtmlInvalidCasingActionProvider.commandId, this.fixInvalidCasing, this);
subscriptions.push(this);
}

public provideCodeActions(
document: vscode.TextDocument,
range: vscode.Range,
context: vscode.CodeActionContext,
token: vscode.CancellationToken): vscode.Command[] {

let diagnostic: vscode.Diagnostic = context.diagnostics[0];
let text = document.getText(diagnostic.range);
const kebabCaseValidationRegex = /(.*)\.(bind|one-way|two-way|one-time|call|delegate|trigger)/;

let result = kebabCaseValidationRegex.exec(text);
let attribute = result[1];
let binding = result[2];
let fixedAttribute = attribute.split(/(?=[A-Z])/).map(s => s.toLowerCase()).join('-');
let fixedText = `${fixedAttribute}.${binding}`;
let commands: vscode.Command[] = [];

commands.push({
arguments: [document, diagnostic.range, fixedText],
command: HtmlInvalidCasingActionProvider.commandId,
title: `Rename ${attribute} to ${fixedAttribute}`,
});

return commands;
}

public fixInvalidCasing(document, range, fixedText) {
let edit = new vscode.WorkspaceEdit();
edit.replace(document.uri, range, fixedText);
return vscode.workspace.applyEdit(edit);
}

public dispose(): void {
this.command.dispose();
}
}
8 changes: 7 additions & 1 deletion src/client/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as path from 'path';
import { ExtensionContext, OutputChannel, window } from 'vscode';
import { ExtensionContext, OutputChannel, window, languages } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient';
import AureliaCliCommands from './aureliaCLICommands';
import htmlInvalidCasingActionProvider from './htmlInvalidCasingCodeActionProvider';

let outputChannel: OutputChannel;

Expand All @@ -14,6 +15,11 @@ export function activate(context: ExtensionContext) {
// Register CLI commands
context.subscriptions.push(AureliaCliCommands.registerCommands(outputChannel));

// Register code fix
const invalidCasingAction = new htmlInvalidCasingActionProvider();
invalidCasingAction.activate(context.subscriptions);
languages.registerCodeActionsProvider('html', invalidCasingAction);

// Register Aurelia language server
const serverModule = context.asAbsolutePath(path.join('dist', 'src', 'server', 'main.js'));
const debugOptions = { execArgv: ['--nolazy', '--debug=6004'] };
Expand Down