Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
neonxp committed Mar 28, 2021
1 parent 1e39924 commit b034d85
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "gotools",
"displayName": "Golang Tools",
"description": "Tools for productive work",
"version": "0.0.3",
"version": "0.0.4",
"publisher": "neonxp",
"author": {
"name": "Alexander NeonXP Kiryukhin",
Expand Down
2 changes: 1 addition & 1 deletion snippets/snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"Structure constructor": {
"body": [
"// New instance of $1 type.",
"// New$1 returns new $1.",
"func New${1:type}(${2}) *$1 {",
"\t$3",
"\treturn &$1{$4}",
Expand Down
77 changes: 42 additions & 35 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
import * as vscode from 'vscode';

const fnRegex = /^(\t*)(.*)err(\s?):=(.+?)$/
const fnRegex = /^\t*(.*)err\s?:=.+?$/

export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerCodeActionsProvider('go', new ErrorsWrapper(), {
providedCodeActionKinds: ErrorsWrapper.providedCodeActionKinds
}));
context.subscriptions.push(
vscode.commands.registerCommand('gotools.wrap-error', () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
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}}`
);
}
});
}
}
})
vscode.languages.registerCodeActionsProvider(
'go',
new ErrorsWrapper(),
{ providedCodeActionKinds: ErrorsWrapper.providedCodeActionKinds }
)
);
context.subscriptions.push(
vscode.commands.registerCommand('gotools.wrap-error', wrapError)
);
}

Expand All @@ -53,7 +28,6 @@ export class ErrorsWrapper implements vscode.CodeActionProvider {
if (!editor) {
return undefined;
}
const selection = editor.selection;
const line = document.lineAt(editor.selection.start.line);
if (!fnRegex.test(line.text)) {
return undefined;
Expand All @@ -65,3 +39,36 @@ export class ErrorsWrapper implements vscode.CodeActionProvider {
];
}
}

const wrapError = () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
const document = editor.document;
const line = document.lineAt(editor.selection.start.line);
const matches = line.text.match(fnRegex);
if (matches == null || matches.length == 0) {
return;
}
const extravars = matches[1].split(',').map(x => x.trim()).filter(x => x);
if (extravars.filter(x => x != "_").length > 0) {
editor.insertSnippet(
new vscode.SnippetString(`\nif err != nil {\n\t\${1:return \${2:nil, }\${3:err}}\n}\n`),
new vscode.Position(line.range.end.line, line.range.end.character + line.firstNonWhitespaceCharacterIndex),
)
} else {
editor.insertSnippet(
new vscode.SnippetString(`if `),
new vscode.Position(line.range.start.line, line.range.start.character + line.firstNonWhitespaceCharacterIndex),
)
editor.insertSnippet(
new vscode.SnippetString(`; err != nil {\n\t\${1:return \${2:nil, }\${3:err}}\n}\n`),
new vscode.Position(line.range.end.line, line.range.end.character + line.firstNonWhitespaceCharacterIndex + 3),
{
undoStopAfter: true,
undoStopBefore: false,
}
)
}
};

0 comments on commit b034d85

Please sign in to comment.