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

Commit

Permalink
Use the newly added receiver output from godef to get the correct doc…
Browse files Browse the repository at this point in the history
…umentation (#2223)

* Use the newly added receiver output from godef to get the correct documentation

See rogpeppe/godef#105

Part of #2107

* Review fixes
  • Loading branch information
segevfiner authored and ramya-rao-a committed Jan 9, 2019
1 parent 4b0e018 commit 8133ea3
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/goDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');
import { byteOffsetAt, getBinPath, runGodoc, getWorkspaceFolderPath } from './util';
import { promptForMissingTool } from './goInstallTools';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { getGoVersion, SemVersion, goKeywords, isPositionInString, getToolsEnvVars, getFileArchive, killProcess } from './util';
import { isModSupported, promptToUpdateToolForModules } from './goModules';

Expand Down Expand Up @@ -79,7 +79,7 @@ export function adjustWordPosition(document: vscode.TextDocument, position: vsco
}

const godefImportDefinitionRegex = /^import \(.* ".*"\)$/;
function definitionLocation_godef(input: GoDefinitionInput, token: vscode.CancellationToken): Promise<GoDefinitionInformation> {
function definitionLocation_godef(input: GoDefinitionInput, token: vscode.CancellationToken, useReceivers: boolean = true): Promise<GoDefinitionInformation> {
let godefTool = 'godef';
let godefPath = getBinPath(godefTool);
if (!path.isAbsolute(godefPath)) {
Expand All @@ -95,13 +95,21 @@ function definitionLocation_godef(input: GoDefinitionInput, token: vscode.Cancel

return new Promise<GoDefinitionInformation>((resolve, reject) => {
// Spawn `godef` process
p = cp.execFile(godefPath, ['-t', '-i', '-f', input.document.fileName, '-o', offset.toString()], { env, cwd }, (err, stdout, stderr) => {
const args = ['-t', '-i', '-f', input.document.fileName, '-o', offset.toString()];
if (useReceivers) {
args.push('-r');
}
p = cp.execFile(godefPath, args, { env, cwd }, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
return reject(missingToolMsg + godefTool);
}
if (err) {
return reject(err.message || stderr);
if (stderr.indexOf('flag provided but not defined: -r') !== -1) {
promptForUpdatingTool('godef');
}
p = null;
return definitionLocation_godef(input, token, false).then(resolve, reject);
}
let result = stdout.toString();
let lines = result.split('\n');
Expand All @@ -117,17 +125,16 @@ function definitionLocation_godef(input: GoDefinitionInput, token: vscode.Cancel
file: file,
line: +line - 1,
column: + col - 1,
declarationlines: lines.splice(1),
declarationlines: lines.slice(1),
toolUsed: 'godef',
doc: null,
name: null
};
if (!input.includeDocs || godefImportDefinitionRegex.test(definitionInformation.declarationlines[0])) {
return resolve(definitionInformation);
}
// TODO #2107 Once godef supports printing method receivers we should pass
// the receiver to runGodoc
runGodoc(pkgPath, '', input.word, token).then(doc => {
match = /^\w+ \(\*?(\w+)\)/.exec(lines[1]);
runGodoc(pkgPath, match ? match[1] : '', input.word, token).then(doc => {
if (doc) {
definitionInformation.doc = doc;
}
Expand Down Expand Up @@ -171,7 +178,7 @@ function definitionLocation_gogetdoc(input: GoDefinitionInput, token: vscode.Can
}
if (stderr && stderr.startsWith('flag provided but not defined: -tags')) {
p = null;
return definitionLocation_gogetdoc(input, token, false);
return definitionLocation_gogetdoc(input, token, false).then(resolve, reject);
}
if (err) {
if (input.isMod
Expand Down

0 comments on commit 8133ea3

Please sign in to comment.