Skip to content

Commit

Permalink
Refactor HoverProvider to use definitionLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehoban committed Feb 7, 2016
1 parent 15a6d1b commit edb88b9
Showing 1 changed file with 21 additions and 42 deletions.
63 changes: 21 additions & 42 deletions src/goExtraInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,32 @@

'use strict';

import { window, HoverProvider, Hover, TextDocument, Position, Range, CancellationToken } from 'vscode';
import cp = require('child_process');
import path = require('path');
import { getBinPath } from './goPath'
import { byteOffsetAt } from './util'
import { HoverProvider, Hover, TextDocument, Position, CancellationToken } from 'vscode';

export class GoHoverProvider implements HoverProvider {

public provideHover(document: TextDocument, position: Position, token: CancellationToken): Thenable<Hover> {

return new Promise((resolve, reject) => {
let filename = document.fileName;
let offset = byteOffsetAt(document, position);
import { definitionLocation } from './goDeclaration'

var godef = getBinPath("godef");

// Spawn `godef` process
let p = cp.execFile(godef, ["-t", "-i", "-f", filename, "-o", offset.toString()], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
window.showInformationMessage("The 'godef' command is not available. Use 'go get -u github.com/rogpeppe/godef' to install.");
}
if (err) return resolve(null);
let result = stdout.toString();
let lines = result.split('\n');
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 text;
if (lines.length > 1) {
text = lines.slice(1, 10).join('\n');
text = text.replace(/\n+$/, '');
} else {
text = lines[0]
}
let hover = new Hover({ language: 'go', value: text });
return resolve(hover);
} catch (e) {
reject(e);
export class GoHoverProvider implements HoverProvider {
public provideHover(document: TextDocument, position: Position, token: CancellationToken): Thenable<Hover> {
return definitionLocation(document, position).then(definitionInfo => {
let lines = definitionInfo.lines;
lines = lines.map(line => {
if (line.indexOf('\t') == 0) {
line = line.slice(1)
}
return line.replace(/\t/g, ' ')
});
p.stdin.end(document.getText());
lines = lines.filter(line => line.length != 0);
if (lines.length > 10) lines[9] = "...";
let text;
if (lines.length > 1) {
text = lines.slice(1, 10).join('\n');
text = text.replace(/\n+$/, '');
} else {
text = lines[0]
}
let hover = new Hover({ language: 'go', value: text });
return hover;
});
}
}

0 comments on commit edb88b9

Please sign in to comment.