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

Commit

Permalink
Fixes references codelens on methods #962
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Jun 9, 2017
1 parent a409440 commit efa448f
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/goCodelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { CodeLensProvider, SymbolInformation, SymbolKind, TextDocument, Cancella
import { documentSymbols, GoDocumentSymbolProvider } from './goOutline';
import { GoReferenceProvider } from './goReferences';

const methodRegex = /^func\s+\(\s*\w+\s+\*?\w+\s*\)\s+/;

class ReferencesCodeLens extends CodeLens {
constructor(
public document: TextDocument,
Expand All @@ -25,7 +27,15 @@ export class GoCodeLensProvider implements CodeLensProvider {

return this.provideDocumentSymbols(document, token).then(symbols => {
return symbols.map(symbol => {
return new ReferencesCodeLens(document, symbol, symbol.location.range);
let position = symbol.location.range.start;

// Add offset for functions as go-outline returns position at the keyword func instead of func name
if (symbol.kind === vscode.SymbolKind.Function) {
let funcDecl = document.lineAt(position.line).text.substr(position.character);
let match = methodRegex.exec(funcDecl);
position = position.translate(0, match ? match[0].length : 5);
}
return new ReferencesCodeLens(document, symbol, new vscode.Range(position, position));
});
});
}
Expand All @@ -40,21 +50,15 @@ export class GoCodeLensProvider implements CodeLensProvider {
let options = {
includeDeclaration: false
};
let position = codeLens.symbol.location.range.start;

// Add offset for functions due to go parser returns always 1 as the start character in a line
if (codeLens.symbol.kind === vscode.SymbolKind.Function) {
position = position.translate(0, 5);
}
let referenceProvider = new GoReferenceProvider();
return referenceProvider.provideReferences(codeLens.document, position, options, token).then(references => {
return referenceProvider.provideReferences(codeLens.document, codeLens.range.start, options, token).then(references => {
if (references) {
codeLens.command = {
title: references.length === 1
? '1 reference'
: references.length + ' references',
command: 'editor.action.showReferences',
arguments: [codeLens.document.uri, position, references]
arguments: [codeLens.document.uri, codeLens.range.start, references]
};
} else {
codeLens.command = {
Expand Down

0 comments on commit efa448f

Please sign in to comment.