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

Use gogetdoc instead of godef and godoc #622

Merged
merged 11 commits into from
Nov 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ install:
- npm run vscode:prepublish
- go get -u -v github.com/nsf/gocode
- go get -u -v github.com/rogpeppe/godef
- if [[ "$(go version)" =~ "go version go1.5" ]]; then echo hello; else go get -u -v github.com/zmb3/gogetdoc; fi
- if [[ "$(go version)" =~ "go version go1.5" ]]; then echo cannot get golint; else go get -u -v github.com/golang/lint/golint; fi
- go get -u -v github.com/lukehoban/go-outline
- go get -u -v sourcegraph.com/sqs/goreturns
Expand All @@ -32,8 +33,8 @@ install:
- go get -u -v golang.org/x/tools/cmd/guru
- go get -u -v github.com/alecthomas/gometalinter
- go get -u -v github.com/cweill/gotests/...
- GO15VENDOREXPERIMENT=1
- if [[ "$(go version)" =~ "go version go1.5" ]]; then echo skipping gometalinter; else gometalinter --install; fi
- GO15VENDOREXPERIMENT=1
- if [[ "$(go version)" =~ "go version go1.5" ]]; then echo skipping gometalinter; else gometalinter --install; fi

script:
- npm run lint
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Read the [Release Notes](https://github.com/Microsoft/vscode-go/wiki/Release-Not
This extension adds rich language support for the Go language to VS Code, including:

- Completion Lists (using `gocode`)
- Signature Help (using `godoc`)
- Signature Help (using `gogetdoc` or `godef`+`godoc`)
- Snippets
- Quick Info (using `godef`)
- Goto Definition (using `godef`)
- Quick Info (using `gogetdoc` or `godef`+`godoc`)
- Goto Definition (using `gogetdoc` or `godef`+`godoc`)
- Find References (using `guru`)
- File outline (using `go-outline`)
- Workspace symbol search (using `go-symbols`)
Expand Down Expand Up @@ -199,6 +199,7 @@ The extension uses the following tools, installed in the current GOPATH. If any

- gocode: `go get -u -v github.com/nsf/gocode`
- godef: `go get -u -v github.com/rogpeppe/godef`
- gogetdoc: `go get -u -v github.com/zmb3/gogetdoc`
- golint: `go get -u -v github.com/golang/lint/golint`
- go-outline: `go get -u -v github.com/lukehoban/go-outline`
- goreturns: `go get -u -v sourcegraph.com/sqs/goreturns`
Expand All @@ -212,6 +213,7 @@ To install them just paste and run:
```bash
go get -u -v github.com/nsf/gocode
go get -u -v github.com/rogpeppe/godef
go get -u -v github.com/zmb3/gogetdoc
go get -u -v github.com/golang/lint/golint
go get -u -v github.com/lukehoban/go-outline
go get -u -v sourcegraph.com/sqs/goreturns
Expand Down
23 changes: 21 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@
"default": "."
},
"mode": {
"enum": ["debug", "remote", "test", "exec"],
"enum": [
"debug",
"remote",
"test",
"exec"
],
"description": "One of 'debug', 'remote', 'test', 'exec'.",
"default": "debug"
},
Expand Down Expand Up @@ -295,7 +300,12 @@
"go.formatTool": {
"type": "string",
"default": "goreturns",
"description": "Pick 'gofmt', 'goimports' or 'goreturns' to run on format."
"description": "Pick 'gofmt', 'goimports' or 'goreturns' to run on format.",
"enum": [
"gofmt",
"goimports",
"goreturns"
]
},
"go.formatFlags": {
"type": "array",
Expand Down Expand Up @@ -365,6 +375,15 @@
"type": "boolean",
"default": true,
"description": "Autocomplete members from unimported packages."
},
"go.docsTool": {
"type": "string",
"default": "godoc",
"description": "Pick 'godoc' or 'gogetdoc' to get documentation. In Go 1.5, godoc is used regardless of the choice here.",
"enum": [
"godoc",
"gogetdoc"
]
}
}
}
Expand Down
90 changes: 81 additions & 9 deletions src/goDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,31 @@ import path = require('path');
import { getBinPath } from './goPath';
import { byteOffsetAt } from './util';
import { promptForMissingTool } from './goInstallTools';
import { getGoVersion, SemVersion } from './util';

export interface GoDefinitionInformtation {
file: string;
line: number;
col: number;
lines: string[];
column: number;
doc: string;
declarationlines: string[];
name: string;
toolUsed: string;
}

export function definitionLocation(document: vscode.TextDocument, position: vscode.Position, includeDocs = true): Promise<GoDefinitionInformtation> {
export function definitionLocation(document: vscode.TextDocument, position: vscode.Position, toolForDocs: string, includeDocs = true): Promise<GoDefinitionInformtation> {
return getGoVersion().then((ver: SemVersion) => {
if (!ver) {
return Promise.resolve(null);
}
if (toolForDocs === 'godoc' || ver.major < 1 || (ver.major === 1 && ver.minor < 6)) {
return definitionLocation_godef(document, position, includeDocs);
}
return definitionLocation_gogetdoc(document, position);
});
}

function definitionLocation_godef(document: vscode.TextDocument, position: vscode.Position, includeDocs = true): Promise<GoDefinitionInformtation> {
return new Promise<GoDefinitionInformtation>((resolve, reject) => {

let wordAtPosition = document.getWordRangeAtPosition(position);
Expand Down Expand Up @@ -50,9 +65,11 @@ export function definitionLocation(document: vscode.TextDocument, position: vsco
let definitionInformation: GoDefinitionInformtation = {
file: file,
line: +line - 1,
col: + col - 1,
lines,
doc: undefined
column: + col - 1,
declarationlines: lines.splice(1),
toolUsed: 'godef',
doc: null,
name: null
};
if (!includeDocs) {
return resolve(definitionInformation);
Expand Down Expand Up @@ -87,15 +104,70 @@ export function definitionLocation(document: vscode.TextDocument, position: vsco
});
}

function definitionLocation_gogetdoc(document: vscode.TextDocument, position: vscode.Position): Promise<GoDefinitionInformtation> {
return new Promise<GoDefinitionInformtation>((resolve, reject) => {
let wordAtPosition = document.getWordRangeAtPosition(position);
let offset = byteOffsetAt(document, position);
let gogetdoc = getBinPath('gogetdoc');
let p = cp.execFile(gogetdoc, ['-u', '-json', '-modified', '-pos', document.fileName + ':#' + offset.toString()], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gogetdoc');
}
if (err) return resolve(null);
let goGetDocOutput = <GoGetDocOuput>JSON.parse(stdout.toString());
let match = /(.*):(\d+):(\d+)/.exec(goGetDocOutput.pos);
let definitionInfo = {
file: null,
line: 0,
column: 0,
toolUsed: 'gogetdoc',
declarationlines: goGetDocOutput.decl.split('\n'),
doc: goGetDocOutput.doc,
name: goGetDocOutput.name
};
if (!match) {
return resolve(definitionInfo);
}
let [_, file, line, col] = match;
definitionInfo.file = match[1];
definitionInfo.line = +match[2] - 1;
definitionInfo.column = +match[3] - 1;
return resolve(definitionInfo);

} catch (e) {
reject(e);
}
});
let documentText = document.getText();
let documentArchive = document.fileName + '\n';
documentArchive = documentArchive + Buffer.byteLength(documentText) + '\n';
documentArchive = documentArchive + documentText;
p.stdin.end(documentArchive);
});
}

export class GoDefinitionProvider implements vscode.DefinitionProvider {
private toolForDocs = 'godoc';

constructor(toolForDocs: string) {
this.toolForDocs = toolForDocs;
}

public provideDefinition(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.Location> {
return definitionLocation(document, position, false).then(definitionInfo => {
if (definitionInfo == null) return null;
return definitionLocation(document, position, this.toolForDocs, false).then(definitionInfo => {
if (definitionInfo == null || definitionInfo.file == null) return null;
let definitionResource = vscode.Uri.file(definitionInfo.file);
let pos = new vscode.Position(definitionInfo.line, definitionInfo.col);
let pos = new vscode.Position(definitionInfo.line, definitionInfo.column);
return new vscode.Location(definitionResource, pos);
});
}
}

interface GoGetDocOuput {
name: string;
import: string;
decl: string;
doc: string;
pos: string;
}
29 changes: 12 additions & 17 deletions src/goExtraInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,25 @@ import { HoverProvider, Hover, MarkedString, TextDocument, Position, Cancellatio
import { definitionLocation } from './goDeclaration';

export class GoHoverProvider implements HoverProvider {
private toolForDocs = 'godoc';

constructor(toolForDocs: string) {
this.toolForDocs = toolForDocs;
}

public provideHover(document: TextDocument, position: Position, token: CancellationToken): Thenable<Hover> {
return definitionLocation(document, position, true).then(definitionInfo => {
return definitionLocation(document, position, this.toolForDocs, true).then(definitionInfo => {
if (definitionInfo == null) return null;
let lines = definitionInfo.lines;
lines = lines.map(line => {
if (line.indexOf('\t') === 0) {
line = line.slice(1);
}
return line.replace(/\t/g, ' ');
});
lines = lines.filter(line => line.length !== 0);
if (lines.length > 10) lines[9] = '...';
let lines = definitionInfo.declarationlines
.filter(line => !line.startsWith('\t//') && line !== '')
.map(line => line.replace(/\t/g, ' '));
let text;
if (lines.length > 1) {
text = lines.slice(1, 10).join('\n');
text = text.replace(/\n+$/, '');
} else {
text = lines[0];
}
text = lines.join('\n').replace(/\n+$/, '');
let hoverTexts: MarkedString[] = [];
hoverTexts.push({ language: 'go', value: text });
if (definitionInfo.doc != null) {
hoverTexts.push(definitionInfo.doc);
}
hoverTexts.push({ language: 'go', value: text});
let hover = new Hover(hoverTexts);
return hover;
});
Expand Down
8 changes: 7 additions & 1 deletion src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ function getTools(goVersion: SemVersion): { [key: string]: string } {
let tools: { [key: string]: string } = {
'gocode': 'github.com/nsf/gocode',
'gopkgs': 'github.com/tpng/gopkgs',
'godef': 'github.com/rogpeppe/godef',
'go-outline': 'github.com/lukehoban/go-outline',
'go-symbols': 'github.com/newhook/go-symbols',
'guru': 'golang.org/x/tools/cmd/guru',
'gorename': 'golang.org/x/tools/cmd/gorename'
};

// Install the doc/def tool that was chosen by the user
if (goConfig['docsTool'] === 'godoc') {
tools['godef'] = 'github.com/rogpeppe/godef';
} else if (goConfig['docsTool'] === 'gogetdoc') {
tools['gogetdoc'] = 'github.com/zmb3/gogetdoc';
}

// Install the formattool that was chosen by the user
if (goConfig['formatTool'] === 'goimports') {
tools['goimports'] = 'golang.org/x/tools/cmd/goimports';
Expand Down
6 changes: 3 additions & 3 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ let goFormatOnSaveDeprecated = true;

export function activate(ctx: vscode.ExtensionContext): void {

ctx.subscriptions.push(vscode.languages.registerHoverProvider(GO_MODE, new GoHoverProvider()));
ctx.subscriptions.push(vscode.languages.registerHoverProvider(GO_MODE, new GoHoverProvider(vscode.workspace.getConfiguration('go')['docsTool'])));
ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
ctx.subscriptions.push(vscode.languages.registerDefinitionProvider(GO_MODE, new GoDefinitionProvider()));
ctx.subscriptions.push(vscode.languages.registerDefinitionProvider(GO_MODE, new GoDefinitionProvider(vscode.workspace.getConfiguration('go')['docsTool'])));
ctx.subscriptions.push(vscode.languages.registerReferenceProvider(GO_MODE, new GoReferenceProvider()));
ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GO_MODE, new GoDocumentFormattingEditProvider()));
ctx.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(GO_MODE, new GoDocumentSymbolProvider()));
ctx.subscriptions.push(vscode.languages.registerWorkspaceSymbolProvider(new GoWorkspaceSymbolProvider()));
ctx.subscriptions.push(vscode.languages.registerRenameProvider(GO_MODE, new GoRenameProvider()));
ctx.subscriptions.push(vscode.languages.registerSignatureHelpProvider(GO_MODE, new GoSignatureHelpProvider(), '(', ','));
ctx.subscriptions.push(vscode.languages.registerSignatureHelpProvider(GO_MODE, new GoSignatureHelpProvider(vscode.workspace.getConfiguration('go')['docsTool']), '(', ','));
ctx.subscriptions.push(vscode.languages.registerCodeActionsProvider(GO_MODE, new GoCodeActionProvider()));

diagnosticCollection = vscode.languages.createDiagnosticCollection('go');
Expand Down
Loading