diff --git a/README.md b/README.md index 6c1785e..079c1db 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,23 @@ -# Golang snippets +# Golang Tools [![Version](https://vsmarketplacebadge.apphb.com/version/neonxp.gotools.svg)](https://marketplace.visualstudio.com/items?itemName=neonxp.gotools) [![Installs](https://vsmarketplacebadge.apphb.com/installs/neonxp.gotools.svg)](https://marketplace.visualstudio.com/items?itemName=neonxp.gotools) [Install extension](https://marketplace.visualstudio.com/items?itemName=neonxp.gotools) -Extensions with frequently used snippets for productive go development. +Extensions with frequently used snippets and code actions for productive go development. Extension in active development! Your contribution is always welcome :) +# Snippets + | Prefix| Description | Example | | :---- |:-----------:| -------:| | `pkg` | Package header line | `package test` | | `construct` | Constructor for structure type | see in action | | `var` | Variable with type and value | `var test string = "hello"` | | `stack` | Stack from array of types | see in action | + +# Code actions + +* Add error checking - adds stub error checking to current line diff --git a/package.json b/package.json index a4c3698..bc57ad3 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "gotools", - "displayName": "Golang tools", + "displayName": "Golang Tools", "description": "Tools for productive work", - "version": "0.0.2", + "version": "0.0.3", "publisher": "neonxp", "author": { "name": "Alexander NeonXP Kiryukhin", diff --git a/src/extension.ts b/src/extension.ts index 3a1de6b..3bc0995 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,9 +1,7 @@ -// The module 'vscode' contains the VS Code extensibility API -// Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; -// this method is called when your extension is activated -// your extension is activated the very first time the command is executed +const fnRegex = /^(\t*)(.*)err(\s?):=(.+?)$/ + export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.languages.registerCodeActionsProvider('go', new ErrorsWrapper(), { providedCodeActionKinds: ErrorsWrapper.providedCodeActionKinds @@ -11,28 +9,37 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand('gotools.wrap-error', () => { const editor = vscode.window.activeTextEditor; - if (editor) { const document = editor.document; - const selection = editor.selection; - const line = document.lineAt(editor.selection.start.line) - const idx = line.firstNonWhitespaceCharacterIndex - const word = document.getText(selection); - const space = "\t".repeat(idx) - let errPrefix = "" - if (word.indexOf("=") == -1) { - errPrefix = "err :=" + const line = document.lineAt(editor.selection.start.line); + const matches = line.text.match(fnRegex); + if (matches == null) { + return; + } + if (matches.length > 0) { + const intendation = matches[1]; + const extravars = matches[2].split(',').map(x => x.trim()).filter(x => x); + const rest = matches[4].trim(); + editor.edit(editBuilder => { + if (extravars.filter(x => x != "_").length > 0) { + editBuilder.insert( + new vscode.Position(line.lineNumber + 1, 0), + `${intendation}if err != nil {\n${intendation}\treturn err\n${intendation}}` + ); + } else { + extravars.push("err"); + editBuilder.replace( + line.range, + `${intendation}if ${extravars.join(", ")} := ${rest}; err != nil {\n${intendation}\treturn err\n${intendation}}` + ); + } + }); } - editor.edit(editBuilder => { - editBuilder.replace(selection, `if ${errPrefix}${word}; err != nil {\n${space}\rreturn err\n${space}}`); - }); } }) ); - } -// this method is called when your extension is deactivated export function deactivate() { } export class ErrorsWrapper implements vscode.CodeActionProvider { @@ -42,10 +49,19 @@ export class ErrorsWrapper implements vscode.CodeActionProvider { ]; public provideCodeActions(document: vscode.TextDocument, range: vscode.Range): vscode.CodeAction[] | undefined { - const action = new vscode.CodeAction('Wrap error', vscode.CodeActionKind.RefactorRewrite); - action.command = { command: 'gotools.wrap-error', title: 'Wrap with error block', tooltip: '' }; + const editor = vscode.window.activeTextEditor; + if (!editor) { + return undefined; + } + const selection = editor.selection; + const line = document.lineAt(editor.selection.start.line); + if (!fnRegex.test(line.text)) { + return undefined; + } + const action = new vscode.CodeAction('Add error checking', vscode.CodeActionKind.RefactorRewrite); + action.command = { command: 'gotools.wrap-error', title: 'Add error checking block', tooltip: '' }; return [ - action - ] + action, + ]; } } diff --git a/tsconfig.json b/tsconfig.json index b65c745..df5ac7f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,14 @@ { "compilerOptions": { "module": "commonjs", - "target": "es6", + "target": "es2020", "outDir": "out", "lib": [ - "es6" + "es2020" ], "sourceMap": true, "rootDir": "src", - "strict": true /* enable all strict type-checking options */ + "strict": true /* enable all strict type-checking options */ /* Additional Checks */ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */