From f8508b40f80a3ce812201ba5eadc268225684b61 Mon Sep 17 00:00:00 2001 From: Luke Hoban Date: Wed, 16 Dec 2015 10:51:51 -0800 Subject: [PATCH] Add a "package" smart snippet This is a rough implementation for now, which we can expand on if we decide to use this "smart snippets" approach more generally. Fixes #155. --- snippets/go.json | 4 ---- src/goImport.ts | 2 +- src/goSuggest.ts | 20 +++++++++++++++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/snippets/go.json b/snippets/go.json index 86e5b4910..747fcad61 100644 --- a/snippets/go.json +++ b/snippets/go.json @@ -1,9 +1,5 @@ { ".source.go": { - "package clause": { - "prefix": "pkg", - "body": "package ${1:name}" - }, "single import": { "prefix": "im", "body": "import \"${1:package}\"" diff --git a/src/goImport.ts b/src/goImport.ts index d9821470c..dfedc70eb 100644 --- a/src/goImport.ts +++ b/src/goImport.ts @@ -45,7 +45,7 @@ export function addImport(arg: string) { return vscode.window.activeTextEditor.edit(editBuilder => { editBuilder.insert(new vscode.Position(lastSingleImport + 1, 0), 'import "' + imp + '"\n'); }); - } else if(pkg.start >= 0) { + } else if(pkg && pkg.start >= 0) { // There are no import declarations, but there is a package declaration return vscode.window.activeTextEditor.edit(editBuilder => { editBuilder.insert(new vscode.Position(pkg.start + 1, 0), '\nimport (\n\t"' + imp + '"\n)\n'); diff --git a/src/goSuggest.ts b/src/goSuggest.ts index 394b19b09..ae4ce44b2 100644 --- a/src/goSuggest.ts +++ b/src/goSuggest.ts @@ -6,7 +6,7 @@ import vscode = require('vscode'); import cp = require('child_process'); -import path = require('path'); +import { dirname, basename } from 'path'; import { getBinPath } from './goPath' function vscodeKindFromGoCodeClass(kind: string): vscode.CompletionItemKind { @@ -35,7 +35,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider { public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable { return this.ensureGoCodeConfigured().then(() => { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { var filename = document.fileName; if (document.lineAt(position.line).text.match(/^\s*\/\//)) { @@ -73,7 +73,21 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider { } if (err) return reject(err); var results = <[number, GoCodeSuggestion[]]>JSON.parse(stdout.toString()); - if (!results[1]) return resolve([]); + if (!results[1]) { + // "Smart Snippet" for package clause + // TODO: Factor this out into a general mechanism + if(!document.getText().match(/package\s+(\w+)/)) { + let defaultPackageName = + basename(document.fileName) == "main.go" + ? "main" + : basename(dirname(document.fileName)); + let packageItem = new vscode.CompletionItem("package " + defaultPackageName ); + packageItem.kind = vscode.CompletionItemKind.Snippet; + packageItem.insertText = "package " + defaultPackageName + "\r\n\r\n"; + return resolve([packageItem]); + } + return resolve([]); + } var suggestions = results[1].map(suggest => { var item = new vscode.CompletionItem(suggest.name); item.kind = vscodeKindFromGoCodeClass(suggest.class);