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

Commit

Permalink
Do not get definition info if not identifier (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a authored Jan 5, 2017
1 parent e11d3ee commit 148c744
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 19 deletions.
24 changes: 13 additions & 11 deletions src/goDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import path = require('path');
import { getBinPath } from './goPath';
import { byteOffsetAt } from './util';
import { promptForMissingTool } from './goInstallTools';
import { getGoVersion, SemVersion } from './util';
import { getGoVersion, SemVersion, goKeywords, isPositionInString } from './util';

export interface GoDefinitionInformtation {
file: string;
Expand All @@ -24,22 +24,26 @@ export interface GoDefinitionInformtation {
}

export function definitionLocation(document: vscode.TextDocument, position: vscode.Position, toolForDocs: string, includeDocs = true): Promise<GoDefinitionInformtation> {
let wordRange = document.getWordRangeAtPosition(position);
let lineText = document.lineAt(position.line).text;
let word = wordRange ? document.getText(wordRange) : '';
if (!wordRange || lineText.startsWith('//') || isPositionInString(document, position) || word.match(/^\d+.?\d+$/) || goKeywords.indexOf(word) > 0) {
return Promise.resolve(null);
}

let offset = byteOffsetAt(document, position);
return getGoVersion().then((ver: SemVersion) => {
// If no Go version can be parsed, it means it's a non-tagged one.
// Assume it's > Go 1.5
if (toolForDocs === 'godoc' || (ver && (ver.major < 1 || (ver.major === 1 && ver.minor < 6)))) {
return definitionLocation_godef(document, position, includeDocs);
return definitionLocation_godef(document, position, offset, includeDocs);
}
return definitionLocation_gogetdoc(document, position);
return definitionLocation_gogetdoc(document, position, offset);
});
}

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

let wordAtPosition = document.getWordRangeAtPosition(position);
let offset = byteOffsetAt(document, position);

let godef = getBinPath('godef');

// Spawn `godef` process
Expand Down Expand Up @@ -106,10 +110,8 @@ function definitionLocation_godef(document: vscode.TextDocument, position: vscod
});
}

function definitionLocation_gogetdoc(document: vscode.TextDocument, position: vscode.Position): Promise<GoDefinitionInformtation> {
function definitionLocation_gogetdoc(document: vscode.TextDocument, position: vscode.Position, offset: number): 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 {
Expand Down
8 changes: 2 additions & 6 deletions src/goSuggest.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 { dirname, basename } from 'path';
import { getBinPath } from './goPath';
import { parameters, parseFilePrelude } from './util';
import { parameters, parseFilePrelude, isPositionInString } from './util';
import { promptForMissingTool } from './goInstallTools';
import { listPackages, getTextEditForAddImport } from './goImport';

Expand Down Expand Up @@ -61,11 +61,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
return resolve([]);
}

// Count the number of double quotes in the line till current position. Ignore escaped double quotes
let doubleQuotesCnt = (lineTillCurrentPosition.match(/[^\\]\"/g) || []).length;
doubleQuotesCnt += lineTillCurrentPosition.startsWith('\"') ? 1 : 0;
let inString = (doubleQuotesCnt % 2 === 1);

let inString = isPositionInString(document, position);
if (!inString && lineTillCurrentPosition.endsWith('\"')) {
return resolve([]);
}
Expand Down
40 changes: 39 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ const extensionId: string = 'lukehoban.Go';
const extensionVersion: string = vscode.extensions.getExtension(extensionId).packageJSON.version;
const aiKey: string = 'AIF-d9b70cd4-b9f9-4d70-929b-a071c400b217';

export const goKeywords: string[] = [
'break',
'case',
'chan',
'const',
'continue',
'default',
'defer',
'else',
'fallthrough',
'for',
'func',
'go',
'goto',
'if',
'import',
'interface',
'map',
'package',
'range',
'return',
'select',
'struct',
'switch',
'type',
'var'
];

export interface SemVersion {
major: number;
minor: number;
Expand Down Expand Up @@ -205,4 +233,14 @@ export function sendTelemetryEvent(eventName: string, properties?: {

telemtryReporter = telemtryReporter ? telemtryReporter : new TelemetryReporter(extensionId, extensionVersion, aiKey);
telemtryReporter.sendTelemetryEvent(eventName, properties, measures);
}
}

export function isPositionInString(document: vscode.TextDocument, position: vscode.Position): boolean {
let lineText = document.lineAt(position.line).text;
let lineTillCurrentPosition = lineText.substr(0, position.character);

// Count the number of double quotes in the line till current position. Ignore escaped double quotes
let doubleQuotesCnt = (lineTillCurrentPosition.match(/[^\\]\"/g) || []).length;
doubleQuotesCnt += lineTillCurrentPosition.startsWith('\"') ? 1 : 0;
return doubleQuotesCnt % 2 === 1;
}
2 changes: 1 addition & 1 deletion test/fixtures/gogetdocTestData/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {

// Hello is a method on the struct ABC. Will signature help understand this correctly
func (abcd *ABC) Hello(s string, exclaim bool) string {
net.CIDRMask(1, 2)
net.CIDRMask(10, 20)
if exclaim {
s = s + "!"
}
Expand Down
12 changes: 12 additions & 0 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ suite('Go Extension Tests', () => {
// assert.equal(res.contents.length, 2);
// assert.equal(expectedDocumentation, <string>(res.contents[0]));
// }
if (expectedSignature === null && expectedDocumentation === null) {
assert.equal(res, null);
return;
}
assert.equal(expectedSignature, (<{ language: string; value: string }>res.contents[0]).value);
})
);
Expand Down Expand Up @@ -165,6 +169,10 @@ encountered.
`;
let testCases: [vscode.Position, string, string][] = [
// [new vscode.Position(3,3), '/usr/local/go/src/fmt'],
[new vscode.Position(0, 3), null, null], // keyword
[new vscode.Position(23, 14), null, null], // inside a string
[new vscode.Position(20, 0), null, null], // just a }
[new vscode.Position(28, 16), null, null], // inside a number
[new vscode.Position(22, 5), 'main func()', null],
[new vscode.Position(40, 23), 'import (math "math")', null],
[new vscode.Position(19, 6), 'Println func(a ...interface{}) (n int, err error)', printlnDoc],
Expand All @@ -179,6 +187,10 @@ Spaces are always added between operands and a newline is appended.
It returns the number of bytes written and any write error encountered.
`;
let testCases: [vscode.Position, string, string][] = [
[new vscode.Position(0, 3), null, null], // keyword
[new vscode.Position(23, 11), null, null], // inside a string
[new vscode.Position(20, 0), null, null], // just a }
[new vscode.Position(28, 16), null, null], // inside a number
[new vscode.Position(22, 5), 'func main()', ''],
[new vscode.Position(23, 4), 'func print(txt string)', 'This is an unexported function so couldnt get this comment on hover :(\nNot anymore!! gogetdoc to the rescue\n'],
[new vscode.Position(40, 23), 'package math', 'Package math provides basic constants and mathematical functions.\n'],
Expand Down

0 comments on commit 148c744

Please sign in to comment.