Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Add a "package" smart snippet
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lukehoban committed Dec 16, 2015
1 parent c14dfb2 commit f8508b4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 0 additions & 4 deletions snippets/go.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
".source.go": {
"package clause": {
"prefix": "pkg",
"body": "package ${1:name}"
},
"single import": {
"prefix": "im",
"body": "import \"${1:package}\""
Expand Down
2 changes: 1 addition & 1 deletion src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
20 changes: 17 additions & 3 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -35,7 +35,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {

public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> {
return this.ensureGoCodeConfigured().then(() => {
return new Promise((resolve, reject) => {
return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
var filename = document.fileName;

if (document.lineAt(position.line).text.match(/^\s*\/\//)) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f8508b4

Please sign in to comment.