Skip to content

Commit

Permalink
feat(codefix): codefix for diagnostics invalid casing (#21)
Browse files Browse the repository at this point in the history
* feat(codefix): codefix for diagnostics invalid casing

* fix(codefix): remove language server code fix setting
  • Loading branch information
Erik Lieben authored Nov 7, 2016
1 parent 7d15884 commit 8d23232
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 2 deletions.
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

0 comments on commit 8d23232

Please sign in to comment.