v0.0.3
This commit is contained in:
parent
ff66c26ad4
commit
1e39924e41
4 changed files with 51 additions and 29 deletions
10
README.md
10
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
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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 => {
|
||||
editBuilder.replace(selection, `if ${errPrefix}${word}; err != nil {\n${space}\rreturn err\n${space}}`);
|
||||
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}}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
// 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"target": "es2020",
|
||||
"outDir": "out",
|
||||
"lib": [
|
||||
"es6"
|
||||
"es2020"
|
||||
],
|
||||
"sourceMap": true,
|
||||
"rootDir": "src",
|
||||
|
|
Loading…
Reference in a new issue