This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Go to Type Definition command implementation
- Loading branch information
Dima
committed
Nov 20, 2018
1 parent
f26c88f
commit 4332269
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import vscode = require('vscode'); | ||
import cp = require('child_process'); | ||
import path = require('path'); | ||
import { byteOffsetAt, getBinPath, canonicalizeGOPATHPrefix, getFileArchive, killTree } from './util'; | ||
import { promptForMissingTool } from './goInstallTools'; | ||
import { getToolsEnvVars } from './util'; | ||
|
||
interface GuruDescribeOutput { | ||
desc: string; | ||
pos: string; | ||
detail: string; | ||
value: GuruDescribeValueOutput; | ||
} | ||
|
||
interface GuruDescribeValueOutput { | ||
type: string; | ||
value: string; | ||
objpos: string; | ||
typespos: GuruDefinitionOutput[]; | ||
} | ||
|
||
interface GuruDefinitionOutput { | ||
objpos: string; | ||
desc: string; | ||
} | ||
|
||
export class GoTypeDefinitionProvider implements vscode.TypeDefinitionProvider { | ||
provideTypeDefinition(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Definition> { | ||
return new Promise<vscode.Location[]>((resolve, reject) => { | ||
let goGuru = getBinPath('guru'); | ||
if (!path.isAbsolute(goGuru)) { | ||
promptForMissingTool('guru'); | ||
return reject('Cannot find tool "guru" to find references.'); | ||
} | ||
|
||
let filename = canonicalizeGOPATHPrefix(document.fileName); | ||
let cwd = path.dirname(filename); | ||
let offset = byteOffsetAt(document, position); | ||
let env = getToolsEnvVars(); | ||
let buildTags = vscode.workspace.getConfiguration('go', document.uri)['buildTags']; | ||
let args = buildTags ? ['-tags', buildTags] : []; | ||
args.push('-json', '-modified', 'describe', `${filename}:#${offset.toString()}`); | ||
|
||
let process = cp.execFile(goGuru, args, { env }, (err, stdout, stderr) => { | ||
try { | ||
if (err && (<any>err).code === 'ENOENT') { | ||
promptForMissingTool('guru'); | ||
return resolve(null); | ||
} | ||
|
||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
let guruOutput = <GuruDescribeOutput>JSON.parse(stdout.toString()); | ||
let results: vscode.Location[] = []; | ||
|
||
if (!guruOutput.value || !guruOutput.value.typespos) { | ||
return resolve(null); | ||
} | ||
|
||
guruOutput.value.typespos.forEach(ref => { | ||
let match = /^(.*):(\d+):(\d+)/.exec(ref.objpos); | ||
if (!match) return; | ||
let [_, file, lineStartStr, colStartStr] = match; | ||
let referenceResource = vscode.Uri.file(path.resolve(cwd, file)); | ||
let range = new vscode.Range( | ||
+lineStartStr - 1, +colStartStr - 1, +lineStartStr - 1, +colStartStr | ||
); | ||
results.push(new vscode.Location(referenceResource, range)); | ||
}); | ||
|
||
resolve(results); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
if (process.pid) { | ||
process.stdin.end(getFileArchive(document)); | ||
} | ||
token.onCancellationRequested(() => | ||
killTree(process.pid) | ||
); | ||
}); | ||
} | ||
} |