From 148c744c3a5933aa97dec87dcaab8c6a26a8c272 Mon Sep 17 00:00:00 2001 From: Ramya Rao Date: Thu, 5 Jan 2017 14:43:04 -0800 Subject: [PATCH] Do not get definition info if not identifier (#711) --- src/goDeclaration.ts | 24 +++++++++------- src/goSuggest.ts | 8 ++---- src/util.ts | 40 +++++++++++++++++++++++++- test/fixtures/gogetdocTestData/test.go | 2 +- test/go.test.ts | 12 ++++++++ 5 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/goDeclaration.ts b/src/goDeclaration.ts index e7a95be61..b98374923 100644 --- a/src/goDeclaration.ts +++ b/src/goDeclaration.ts @@ -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; @@ -24,22 +24,26 @@ export interface GoDefinitionInformtation { } export function definitionLocation(document: vscode.TextDocument, position: vscode.Position, toolForDocs: string, includeDocs = true): Promise { + 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 { +function definitionLocation_godef(document: vscode.TextDocument, position: vscode.Position, offset: number, includeDocs = true): Promise { return new Promise((resolve, reject) => { - - let wordAtPosition = document.getWordRangeAtPosition(position); - let offset = byteOffsetAt(document, position); - let godef = getBinPath('godef'); // Spawn `godef` process @@ -106,10 +110,8 @@ function definitionLocation_godef(document: vscode.TextDocument, position: vscod }); } -function definitionLocation_gogetdoc(document: vscode.TextDocument, position: vscode.Position): Promise { +function definitionLocation_gogetdoc(document: vscode.TextDocument, position: vscode.Position, offset: number): Promise { return new Promise((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 { diff --git a/src/goSuggest.ts b/src/goSuggest.ts index bea9e6d54..5bcc8d33e 100644 --- a/src/goSuggest.ts +++ b/src/goSuggest.ts @@ -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'; @@ -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([]); } diff --git a/src/util.ts b/src/util.ts index 4f74f1de8..bc5ee5e68 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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; @@ -205,4 +233,14 @@ export function sendTelemetryEvent(eventName: string, properties?: { telemtryReporter = telemtryReporter ? telemtryReporter : new TelemetryReporter(extensionId, extensionVersion, aiKey); telemtryReporter.sendTelemetryEvent(eventName, properties, measures); -} \ No newline at end of file +} + +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; +} diff --git a/test/fixtures/gogetdocTestData/test.go b/test/fixtures/gogetdocTestData/test.go index 6f69b5531..7a337b1bb 100644 --- a/test/fixtures/gogetdocTestData/test.go +++ b/test/fixtures/gogetdocTestData/test.go @@ -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 + "!" } diff --git a/test/go.test.ts b/test/go.test.ts index cab092053..1e0664874 100644 --- a/test/go.test.ts +++ b/test/go.test.ts @@ -102,6 +102,10 @@ suite('Go Extension Tests', () => { // assert.equal(res.contents.length, 2); // assert.equal(expectedDocumentation, (res.contents[0])); // } + if (expectedSignature === null && expectedDocumentation === null) { + assert.equal(res, null); + return; + } assert.equal(expectedSignature, (<{ language: string; value: string }>res.contents[0]).value); }) ); @@ -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], @@ -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'],