aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/extension.js88
-rw-r--r--src/extension.ts78
-rw-r--r--src/test/runTest.ts23
-rw-r--r--src/test/suite/extension.test.ts15
-rw-r--r--src/test/suite/index.ts38
5 files changed, 88 insertions, 154 deletions
diff --git a/src/extension.js b/src/extension.js
new file mode 100644
index 0000000..45a30cd
--- /dev/null
+++ b/src/extension.js
@@ -0,0 +1,88 @@
+// The module 'vscode' contains the VS Code extensibility API
+// Import the module and reference it with the alias vscode in your code below
+const vscode = require('vscode');
+const fnRegex = /^(\t*)(.*)err\s?:?=.+?$/;
+
+// This method is called when your extension is activated
+// Your extension is activated the very first time the command is executed
+
+/**
+ * @param {vscode.ExtensionContext} context
+ */
+function activate(context) {
+ let wrapErrorCommand = vscode.commands.registerCommand('gotools.wrap-error', wrapError);
+ context.subscriptions.push(
+ vscode.languages.registerCodeActionsProvider(
+ 'go',
+ new ErrorsWrapper(),
+ {
+ providedCodeActionKinds: [
+ vscode.CodeActionKind.RefactorRewrite
+ ]
+ }
+ )
+ );
+ context.subscriptions.push(wrapErrorCommand);
+}
+
+// This method is called when your extension is deactivated
+function deactivate() { }
+
+class ErrorsWrapper {
+
+ provideCodeActions(document, range) {
+ const editor = vscode.window.activeTextEditor;
+ if (!editor) {
+ return undefined;
+ }
+
+ const line = document.lineAt(editor.selection.start.line);
+ if (!fnRegex.test(line.text)) {
+ vscode.commands.executeCommand('setContext', 'allowWrapIferr', false);
+ return undefined;
+ }
+ vscode.commands.executeCommand('setContext', 'allowWrapIferr', true);
+ 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,
+ ];
+ }
+}
+
+module.exports = {
+ activate,
+ deactivate
+}
+
+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[2].split(',').map(x => x.trim()).filter(x => x);
+ if (extravars.filter(x => x !== "_").length > 0) {
+ editor.insertSnippet(
+ new vscode.SnippetString(`\nif err != nil {\n\treturn \${1:nil, }\${2:err}\n}\n`),
+ new vscode.Position(line.range.end.line, line.range.end.character + line.firstNonWhitespaceCharacterIndex),
+ );
+ } else {
+ const tabs = matches[1];
+ const original = matches[0].trimStart()
+ editor.insertSnippet(
+ new vscode.SnippetString(`${tabs}if ${original}; err != nil {\n${tabs}\treturn \${2:nil, }\${3:err}\n${tabs}}\n`),
+ line.range,
+ {
+ undoStopBefore: true,
+ undoStopAfter: true
+ }
+ );
+ }
+};
diff --git a/src/extension.ts b/src/extension.ts
deleted file mode 100644
index a60f216..0000000
--- a/src/extension.ts
+++ /dev/null
@@ -1,78 +0,0 @@
-import * as vscode from 'vscode';
-
-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', wrapError)
- );
-}
-
-export function deactivate() { }
-
-export class ErrorsWrapper implements vscode.CodeActionProvider {
-
- public static readonly providedCodeActionKinds = [
- vscode.CodeActionKind.RefactorRewrite
- ];
-
- public provideCodeActions(document: vscode.TextDocument, range: vscode.Range): vscode.CodeAction[] | undefined {
- const editor = vscode.window.activeTextEditor;
- if (!editor) {
- return undefined;
- }
-
- const line = document.lineAt(editor.selection.start.line);
- if (!fnRegex.test(line.text)) {
- vscode.commands.executeCommand('setContext', 'allowWrapIferr', false);
- return undefined;
- }
- vscode.commands.executeCommand('setContext', 'allowWrapIferr', true);
- 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,
- ];
- }
-}
-
-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,
- }
- )
- }
-};
diff --git a/src/test/runTest.ts b/src/test/runTest.ts
deleted file mode 100644
index 1eabfa3..0000000
--- a/src/test/runTest.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import * as path from 'path';
-
-import { runTests } from 'vscode-test';
-
-async function main() {
- try {
- // The folder containing the Extension Manifest package.json
- // Passed to `--extensionDevelopmentPath`
- const extensionDevelopmentPath = path.resolve(__dirname, '../../');
-
- // The path to test runner
- // Passed to --extensionTestsPath
- const extensionTestsPath = path.resolve(__dirname, './suite/index');
-
- // Download VS Code, unzip it and run the integration test
- await runTests({ extensionDevelopmentPath, extensionTestsPath });
- } catch (err) {
- console.error('Failed to run tests');
- process.exit(1);
- }
-}
-
-main();
diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts
deleted file mode 100644
index 4ca0ab4..0000000
--- a/src/test/suite/extension.test.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import * as assert from 'assert';
-
-// You can import and use all API from the 'vscode' module
-// as well as import your extension to test it
-import * as vscode from 'vscode';
-// import * as myExtension from '../../extension';
-
-suite('Extension Test Suite', () => {
- vscode.window.showInformationMessage('Start all tests.');
-
- test('Sample test', () => {
- assert.strictEqual(-1, [1, 2, 3].indexOf(5));
- assert.strictEqual(-1, [1, 2, 3].indexOf(0));
- });
-});
diff --git a/src/test/suite/index.ts b/src/test/suite/index.ts
deleted file mode 100644
index 7029e38..0000000
--- a/src/test/suite/index.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import * as path from 'path';
-import * as Mocha from 'mocha';
-import * as glob from 'glob';
-
-export function run(): Promise<void> {
- // Create the mocha test
- const mocha = new Mocha({
- ui: 'tdd',
- color: true
- });
-
- const testsRoot = path.resolve(__dirname, '..');
-
- return new Promise((c, e) => {
- glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
- if (err) {
- return e(err);
- }
-
- // Add files to the test suite
- files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
-
- try {
- // Run the mocha test
- mocha.run(failures => {
- if (failures > 0) {
- e(new Error(`${failures} tests failed.`));
- } else {
- c();
- }
- });
- } catch (err) {
- console.error(err);
- e(err);
- }
- });
- });
-}