This commit is contained in:
Александр Кирюхин 2021-03-22 05:17:45 +03:00
parent ff66c26ad4
commit 1e39924e41
No known key found for this signature in database
GPG key ID: 6DF7A2910D0699E9
4 changed files with 51 additions and 29 deletions

View file

@ -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) [![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) [![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) [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 :) Extension in active development! Your contribution is always welcome :)
# Snippets
| Prefix| Description | Example | | Prefix| Description | Example |
| :---- |:-----------:| -------:| | :---- |:-----------:| -------:|
| `pkg` | Package header line | `package test` | | `pkg` | Package header line | `package test` |
| `construct` | Constructor for structure type | see in action | | `construct` | Constructor for structure type | see in action |
| `var` | Variable with type and value | `var test string = "hello"` | | `var` | Variable with type and value | `var test string = "hello"` |
| `stack` | Stack from array of types | see in action | | `stack` | Stack from array of types | see in action |
# Code actions
* Add error checking - adds stub error checking to current line

View file

@ -1,8 +1,8 @@
{ {
"name": "gotools", "name": "gotools",
"displayName": "Golang tools", "displayName": "Golang Tools",
"description": "Tools for productive work", "description": "Tools for productive work",
"version": "0.0.2", "version": "0.0.3",
"publisher": "neonxp", "publisher": "neonxp",
"author": { "author": {
"name": "Alexander NeonXP Kiryukhin", "name": "Alexander NeonXP Kiryukhin",

View file

@ -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'; import * as vscode from 'vscode';
// this method is called when your extension is activated const fnRegex = /^(\t*)(.*)err(\s?):=(.+?)$/
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerCodeActionsProvider('go', new ErrorsWrapper(), { context.subscriptions.push(vscode.languages.registerCodeActionsProvider('go', new ErrorsWrapper(), {
providedCodeActionKinds: ErrorsWrapper.providedCodeActionKinds providedCodeActionKinds: ErrorsWrapper.providedCodeActionKinds
@ -11,28 +9,37 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push( context.subscriptions.push(
vscode.commands.registerCommand('gotools.wrap-error', () => { vscode.commands.registerCommand('gotools.wrap-error', () => {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
if (editor) { if (editor) {
const document = editor.document; const document = editor.document;
const selection = editor.selection; const line = document.lineAt(editor.selection.start.line);
const line = document.lineAt(editor.selection.start.line) const matches = line.text.match(fnRegex);
const idx = line.firstNonWhitespaceCharacterIndex if (matches == null) {
const word = document.getText(selection); return;
const space = "\t".repeat(idx) }
let errPrefix = "" if (matches.length > 0) {
if (word.indexOf("=") == -1) { const intendation = matches[1];
errPrefix = "err :=" 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 function deactivate() { }
export class ErrorsWrapper implements vscode.CodeActionProvider { 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 { public provideCodeActions(document: vscode.TextDocument, range: vscode.Range): vscode.CodeAction[] | undefined {
const action = new vscode.CodeAction('Wrap error', vscode.CodeActionKind.RefactorRewrite); const editor = vscode.window.activeTextEditor;
action.command = { command: 'gotools.wrap-error', title: 'Wrap with error block', tooltip: '' }; 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 [ return [
action action,
] ];
} }
} }

View file

@ -1,14 +1,14 @@
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"target": "es6", "target": "es2020",
"outDir": "out", "outDir": "out",
"lib": [ "lib": [
"es6" "es2020"
], ],
"sourceMap": true, "sourceMap": true,
"rootDir": "src", "rootDir": "src",
"strict": true /* enable all strict type-checking options */ "strict": true /* enable all strict type-checking options */
/* Additional Checks */ /* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */