Skip to content

Commit

Permalink
updated to the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
egamma committed Sep 15, 2015
1 parent 58f067a commit e9b55d6
Show file tree
Hide file tree
Showing 12 changed files with 754 additions and 93 deletions.
15 changes: 6 additions & 9 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# Prereqs Tools

- gorename: go get golang.org/x/tools/cmd/gorename
- gocode: go get -u github.com/nsf/gocode
- goreturns: go get -u sourcegraph.com/sqs/goreturns
- godef: go get -v github.com/rogpeppe/godef
- golint: go get -u github.com/golang/lint/golint




- gorename: `go get golang.org/x/tools/cmd/gorename`
- gocode: `go get -u github.com/nsf/gocode`
- goreturns: `go get -u sourcegraph.com/sqs/goreturns`
- godef: `go get -v github.com/rogpeppe/godef`
- golint: `go get -u github.com/golang/lint/golint`
- go-find-references: `go get -v github.com/redefiance/go-find-references`
164 changes: 164 additions & 0 deletions snippets/go.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
".source.go": {
"package clause": {
"prefix": "pkg",
"body": "package ${1:name}"
},
"single import": {
"prefix": "im",
"body": "import \"${1:package}\""
},
"multiple imports": {
"prefix": "ims",
"body": "import (\n\t\"${1:package}\"\n)"
},
"single constant": {
"prefix": "co",
"body": "const ${1:name} = ${2:value}"
},
"multiple constants": {
"prefix": "cos",
"body": "const (\n\t${1:name} = ${2:value}\n)"
},
"type interface declaration": {
"prefix": "tyi",
"body": "type ${1:name} interface {\n\t$0\n}"
},
"type struct declaration": {
"prefix": "tys",
"body": "type ${1:name} struct {\n\t$0\n}"
},
"main function": {
"prefix": "main",
"body": "func main() {\n\t$0\n}"
},
"function declaration": {
"prefix": "func",
"body": "func $1($2) $3 {\n\t$0\n}"
},
"variable declaration": {
"prefix": "var",
"body": "var ${1:name} ${2:type}"
},
"switch statement": {
"prefix": "switch",
"body": "switch ${1:expression} {\ncase ${2:condition}:\n\t$0\n}"
},
"case clause": {
"prefix": "cs",
"body": "case ${1:condition}:$0"
},
"for statement": {
"prefix": "for",
"body": "for ${1:index} := 0; $1 < ${2:count}; $1${3:++} {\n\t$0\n}"
},
"for range statement": {
"prefix": "forr",
"body": "for ${1:var} := range ${2:var} {\n\t$0\n}"
},
"channel declaration": {
"prefix": "ch",
"body": "chan ${1:type}"
},
"map declaration": {
"prefix": "map",
"body": "map[${1:type}]${2:type}"
},
"empty interface": {
"prefix": "in",
"body": "interface{}"
},
"if statement": {
"prefix": "if",
"body": "if ${1:condition} {\n\t$0\n}"
},
"else branch": {
"prefix": "el",
"body": "else {\n\t$0\n}"
},
"if else statement": {
"prefix": "ie",
"body": "if ${1:condition} {\n\t$2\n} else {\n\t$0\n}"
},
"if err != nil": {
"prefix": "iferr",
"body": "if err != nil {\n\t${1:return}\n}"
},
"fmt.Println": {
"prefix": "fp",
"body": "fmt.Println(\"$1\")"
},
"fmt.Printf": {
"prefix": "ff",
"body": "fmt.Printf(\"$1\", ${2:var})"
},
"log.Println": {
"prefix": "lp",
"body": "log.Println(\"$1\")"
},
"log.Printf": {
"prefix": "lf",
"body": "log.Printf(\"$1\", ${2:var})"
},
"log variable content": {
"prefix": "lv",
"body": "log.Printf(\"${1:var}: %#+v\\\\n\", ${1:var})"
},
"make(...)": {
"prefix": "make",
"body": "make(${1:type}, ${2:0})"
},
"new(...)": {
"prefix": "new",
"body": "new(${1:type})"
},
"panic(...)": {
"prefix": "pn",
"body": "panic(\"$0\")"
},
"http ResponseWriter *Request": {
"prefix": "wr",
"body": "${1:w} http.ResponseWriter, ${2:r} *http.Request"
},
"http.HandleFunc": {
"prefix": "hf",
"body": "${1:http}.HandleFunc(\"${2:/}\", ${3:handler})"
},
"http handler declaration": {
"prefix": "hand",
"body": "func $1(${2:w} http.ResponseWriter, ${3:r} *http.Request) {\n\t$0\n}"
},
"http.Redirect": {
"prefix": "rd",
"body": "http.Redirect(${1:w}, ${2:r}, \"${3:/}\", ${4:http.StatusFound})"
},
"http.Error": {
"prefix": "herr",
"body": "http.Error(${1:w}, ${2:err}.Error(), ${3:http.StatusInternalServerError})"
},
"http.ListenAndServe": {
"prefix": "las",
"body": "http.ListenAndServe(\"${1::8080}\", ${2:nil})"
},
"http.Serve": {
"prefix": "sv",
"body": "http.Serve(\"${1::8080}\", ${2:nil})"
},
"goroutine anonymous function": {
"prefix": "go",
"body": "go func($1) {\n\t$2\n}($0)"
},
"goroutine function": {
"prefix": "gf",
"body": "go ${1:func}($0)"
},
"defer statement": {
"prefix": "df",
"body": "defer ${1:func}($0)"
},
"test function": {
"prefix": "tf",
"body": "func Test$1(t *testing.T) {\n\t$0\n}"
}
}
}
40 changes: 33 additions & 7 deletions src/goCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');
import os = require('os');
import fs = require('fs');

//TODO: Less hacky?
var go : string;
if(process.env.GOROOT) {
go = path.join(process.env["GOROOT"], "bin", "go");
} else if(process.env.PATH) {
var pathparts = (<string>process.env.PATH).split((<any>path).delimiter);
go = pathparts.map(dir => path.join(dir, 'go' + (os.platform() == "win32" ? ".exe" : ""))).filter(candidate => fs.existsSync(candidate))[0];
}
if(!go) {
vscode.shell.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT. Set location manual in 'go.goroot' setting.");
}

export interface ICheckResult {
file: string;
Expand All @@ -16,16 +29,20 @@ export interface ICheckResult {
severity: string;
}

export function check(filename: string): Promise<ICheckResult[]> {
var gobuild = new Promise((resolve, reject) => {
export function check(filename: string, buildOnSave = true, lintOnSave = true, vetOnSave = true): Promise<ICheckResult[]> {
var gobuild = !buildOnSave ? Promise.resolve([]) : new Promise((resolve, reject) => {
var tmppath = path.normalize(path.join(os.tmpdir(), "go-code-check"))
var cwd = path.dirname(filename)
var args = ["build", "-o", tmppath, "."];
if(filename.match(/_test.go$/i)) {
args = ['test', '-copybinary', '-o', tmppath, '-c', '.']
}
var process = cp.execFile("go", args, {cwd: cwd}, (err, stdout, stderr) => {
cp.execFile(go, args, {cwd: cwd}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'go' compiler is not available. Install Go from http://golang.org/dl/.");
return resolve([]);
}
var lines = stderr.toString().split('\n');
var ret: ICheckResult[] = [];
for(var i = 1; i < lines.length; i++) {
Expand All @@ -42,10 +59,15 @@ export function check(filename: string): Promise<ICheckResult[]> {
});
});

var golint = new Promise((resolve, reject) => {
var golint = !lintOnSave ? Promise.resolve([]) : new Promise((resolve, reject) => {
var cwd = path.dirname(filename)
var process = cp.execFile("golint", [filename], {cwd: cwd}, (err, stdout, stderr) => {
var golint = path.join(process.env["GOPATH"], "bin", "golint");
cp.execFile("golint", [filename], {cwd: cwd}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'golint' command is not available. Use 'go get -u github.com/golang/lint/golint' to install.");
return resolve([]);
}
var lines = stdout.toString().split('\n');
var ret: ICheckResult[] = [];
for(var i = 0; i < lines.length; i++) {
Expand All @@ -62,10 +84,14 @@ export function check(filename: string): Promise<ICheckResult[]> {
});
});

var govet = new Promise((resolve, reject) => {
var govet = !vetOnSave ? Promise.resolve([]) : new Promise((resolve, reject) => {
var cwd = path.dirname(filename)
var process = cp.execFile("go", ["tool", "vet", filename], {cwd: cwd}, (err, stdout, stderr) => {
cp.execFile(go, ["tool", "vet", filename], {cwd: cwd}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'go tool vet' compiler is not available. Install Go from http://golang.org/dl/.");
return resolve([]);
}
var lines = stdout.toString().split('\n');
var ret: ICheckResult[] = [];
for(var i = 0; i < lines.length; i++) {
Expand Down
22 changes: 15 additions & 7 deletions src/goDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');

class DeclartionSupport implements vscode.Modes.IDeclarationSupport {

Expand All @@ -18,19 +19,26 @@ class DeclartionSupport implements vscode.Modes.IDeclarationSupport {
public findDeclaration(resource:vscode.URI, position:vscode.IPosition, token: vscode.CancellationToken):Promise<vscode.Modes.IReference> {

return new Promise((resolve, reject) => {
var path = resource.fsPath;
var filename = resource.fsPath;
var model = this.modelService.getModel(resource);
var wordAtPosition = model.getWordAtPosition(position);

// compute the file offset for position
var offset = position.column;
for (var row = 1; row < position.lineNumber; row++) {
offset += model.getLineMaxColumn(row);
}
var offset = model.getValueInRange({
startLineNumber: 0,
startColumn: 0,
endLineNumber: position.lineNumber,
endColumn: position.column
}).length;

var godef = path.join(process.env["GOPATH"], "bin", "godef");

// Spawn `godef` process
var process = cp.execFile("godef", ["-t", "-i", "-f", path, "-o", offset.toString()], {}, (err, stdout, stderr) => {
var p = cp.execFile(godef, ["-t", "-i", "-f", filename, "-o", offset.toString()], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'godef' command is not available. Use 'go get -u github.com/rogpeppe/godef' to install.");
}
if (err) return resolve(null);
var result = stdout.toString();
var lines = result.split('\n');
Expand All @@ -49,7 +57,7 @@ class DeclartionSupport implements vscode.Modes.IDeclarationSupport {
reject(e);
}
});
process.stdin.end(model.getValue());
p.stdin.end(model.getValue());
});
}
}
Expand Down
22 changes: 15 additions & 7 deletions src/goExtraInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');

class ExtraInfoSupport implements vscode.Modes.IExtraInfoSupport {

Expand All @@ -18,19 +19,26 @@ class ExtraInfoSupport implements vscode.Modes.IExtraInfoSupport {
public computeInfo(resource:vscode.URI, position:vscode.IPosition, token: vscode.CancellationToken): Promise<vscode.Modes.IComputeExtraInfoResult> {

return new Promise((resolve, reject) => {
var path = resource.fsPath;
var filename = resource.fsPath;
var model = this.modelService.getModel(resource);
var wordAtPosition = model.getWordAtPosition(position);

// compute the file offset for position
var offset = position.column;
for (var row = 1; row < position.lineNumber; row++) {
offset += model.getLineMaxColumn(row);
}
var offset = model.getValueInRange({
startLineNumber: 0,
startColumn: 0,
endLineNumber: position.lineNumber,
endColumn: position.column
}).length;

var godef = path.join(process.env["GOPATH"], "bin", "godef");

// Spawn `godef` process
var process = cp.execFile("godef", ["-t", "-i", "-f", path, "-o", offset.toString()], {}, (err, stdout, stderr) => {
var p = cp.execFile(godef, ["-t", "-i", "-f", filename, "-o", offset.toString()], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'godef' command is not available. Use 'go get -u github.com/rogpeppe/godef' to install.");
}
if (err) return resolve(null);
var result = stdout.toString();
var lines = result.split('\n');
Expand All @@ -51,7 +59,7 @@ class ExtraInfoSupport implements vscode.Modes.IExtraInfoSupport {
reject(e);
}
});
process.stdin.end(model.getValue());
p.stdin.end(model.getValue());
});
}
}
Expand Down
Loading

0 comments on commit e9b55d6

Please sign in to comment.