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

Commit

Permalink
Add references codelens support
Browse files Browse the repository at this point in the history
  • Loading branch information
theSoenke committed Apr 19, 2017
1 parent 6ea85f0 commit 0c4b211
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This extension adds rich language support for the Go language to VS Code, includ
- Quick Info (using `gogetdoc` or `godef`+`godoc`)
- Goto Definition (using `gogetdoc` or `godef`+`godoc`)
- Find References (using `guru`)
- References CodeLens
- File outline (using `go-outline`)
- Workspace symbol search (using `go-symbols`)
- Rename (using `gorename`. Note: For Undo after rename to work in Windows you need to have `diff` tool in your path)
Expand Down Expand Up @@ -49,7 +50,7 @@ The Go extension is ready to use on the get go. If you want to customize the fea


### Go Language Server (Experimental)
Set `go.useLanguageServer` to `true` to use the Go language server from [Sourcegraph](https://github.com/sourcegraph/go-langserver) for features like Hover, Definition, Find All References, Signature Help, Go to Symbol in File and Workspace.
Set `go.useLanguageServer` to `true` to use the Go language server from [Sourcegraph](https://github.com/sourcegraph/go-langserver) for features like Hover, Definition, Find All References, Signature Help, Go to Symbol in File and Workspace.
* This is an experimental feature and is not available in Windows yet.
* If set to true, you will be prompted to install the Go language server. Once installed, you will have to reload VS Code window. The language server will then be run by the Go extension in the background to provide services needed for the above mentioned features.
* Everytime you change the value of the setting `go.useLanguageServer`, you need to reload the VS Code window for it to take effect.
Expand Down Expand Up @@ -112,7 +113,7 @@ For more read [Debugging Go Code Using VS Code](https://github.com/Microsoft/vsc

#### Remote Debugging

To remote debug using VS Code, read [Remote Debugging](https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code#remote-debugging)
To remote debug using VS Code, read [Remote Debugging](https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code#remote-debugging)

## Building and Debugging the Extension

Expand Down
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,21 @@
"default": false,
"description": "If false, the import statements will be excluded while using the Go to Symbol in File feature"
},
"go.referencesCodeLens.enabled": {
"type": "boolean",
"default": false,
"description": "Enable/disable references CodeLens"
},
"go.addTags": {
"type": "object",
"properties": {
"promptForTags": {
"type":"boolean",
"type": "boolean",
"default": false,
"description": "If true, Go: Add Tags command will prompt the user to provide tags and options instead of using the configured values"
},
"tags": {
"type":"string",
"type": "string",
"default": "json",
"description": "Comma separated tags to be used by Go: Add Tags command"
},
Expand Down Expand Up @@ -559,7 +564,7 @@
"default": 500,
"description": "The number of milliseconds to delay before execution. Resets with each keystroke."
}
},
},
"default": {
"enabled": false,
"delay": 500
Expand All @@ -570,12 +575,12 @@
"type": "object",
"properties": {
"promptForTags": {
"type":"boolean",
"type": "boolean",
"default": false,
"description": "If true, Go: Remove Tags command will prompt the user to provide tags and options instead of using the configured values"
},
"tags": {
"type":"string",
"type": "string",
"default": "json",
"description": "Comma separated tags to be used by Go: Remove Tags command"
},
Expand Down Expand Up @@ -624,7 +629,7 @@
"type": "boolean",
"default": true,
"description": "If true, adds command to run all tests in the current package to the editor context menu"
},
},
"generateTestForFunction": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -701,7 +706,7 @@
{
"when": "editorTextFocus && config.go.editorContextMenuCommands.testPackage && resourceLangId == go",
"command": "go.test.package"
},
},
{
"when": "editorTextFocus && config.go.editorContextMenuCommands.generateTestForFunction && resourceLangId == go",
"command": "go.test.generate.function"
Expand Down
66 changes: 66 additions & 0 deletions src/goCodelens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'

import vscode = require('vscode');
import { CodeLensProvider, SymbolInformation, SymbolKind, TextDocument, CancellationToken, CodeLens, Range, Command, Location, commands } from 'vscode';
import { documentSymbols, GoDocumentSymbolProvider } from './goOutline';
import { GoReferenceProvider } from "./goReferences";

export class GoCodeLensProvider implements CodeLensProvider {
public provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
let codelensEnabled = vscode.workspace.getConfiguration('go').get('referencesCodeLens.enabled');
if (!codelensEnabled) {
return Promise.resolve([]);
}

return this.provideDocumentSymbols(document, token).then(symbols => {
let symbolReferences = symbols.map(symbol => this.provideSymbolReferences(document, symbol, token));
return Promise.all(symbolReferences).then(values => {
let codelenses = [];
values.forEach(lens => {
if (lens) {
codelenses.push(lens);
}
});
return codelenses;
});
});
}

private provideDocumentSymbols(document: TextDocument, token: CancellationToken): Thenable<vscode.SymbolInformation[]> {
let symbolProvider = new GoDocumentSymbolProvider();
return symbolProvider.provideDocumentSymbols(document, token).then(symbols => {
return symbols.filter(symbol =>
symbol.kind === vscode.SymbolKind.Function ||
symbol.kind === vscode.SymbolKind.Interface)
});
}

private provideSymbolReferences(document: TextDocument, symbol: SymbolInformation, token: CancellationToken): Thenable<CodeLens> {
if (token.isCancellationRequested) {
return Promise.resolve(null);
}

let options = {
includeDeclaration: false
};
let position = symbol.location.range.start;
if (symbol.kind === vscode.SymbolKind.Function) {
position = position.translate(0, 5);
}
let referenceProvider = new GoReferenceProvider();
return referenceProvider.doFindReferences(document, position, options, token).then(references => {
if (!references) {
return Promise.resolve(null);
}

let showReferences: Command = {
title: references.length === 1
? '1 reference'
: references.length + ' references',
command: 'editor.action.showReferences',
arguments: [document.uri, position, references]
};
return new CodeLens(symbol.location.range, showReferences);
});
}
}
2 changes: 2 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { LanguageClient } from 'vscode-languageclient';
import { clearCacheForTools } from './goPath';
import { addTags, removeTags } from './goModifytags';
import { parseLiveFile } from './goLiveErrors';
import { GoCodeLensProvider } from './goCodelens'

export let errorDiagnosticCollection: vscode.DiagnosticCollection;
let warningDiagnosticCollection: vscode.DiagnosticCollection;
Expand Down Expand Up @@ -82,6 +83,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GO_MODE, new GoDocumentFormattingEditProvider()));
ctx.subscriptions.push(vscode.languages.registerRenameProvider(GO_MODE, new GoRenameProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeActionsProvider(GO_MODE, new GoCodeActionProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, new GoCodeLensProvider()));

errorDiagnosticCollection = vscode.languages.createDiagnosticCollection('go-error');
ctx.subscriptions.push(errorDiagnosticCollection);
Expand Down
10 changes: 9 additions & 1 deletion src/goReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class GoReferenceProvider implements vscode.ReferenceProvider {
});
}

private doFindReferences(document: vscode.TextDocument, position: vscode.Position, options: { includeDeclaration: boolean }, token: vscode.CancellationToken): Thenable<vscode.Location[]> {
public doFindReferences(document: vscode.TextDocument, position: vscode.Position, options: { includeDeclaration: boolean }, token: vscode.CancellationToken): Thenable<vscode.Location[]> {
return new Promise((resolve, reject) => {
let filename = canonicalizeGOPATHPrefix(document.fileName);
let cwd = path.dirname(filename);
Expand Down Expand Up @@ -53,6 +53,14 @@ export class GoReferenceProvider implements vscode.ReferenceProvider {
if (!match) continue;
let [_, file, lineStartStr, colStartStr, lineEndStr, colEndStr] = match;
let referenceResource = vscode.Uri.file(path.resolve(cwd, file));

if (!options.includeDeclaration) {
if (document.uri.fsPath === referenceResource.fsPath &&
position.line === Number(lineStartStr) - 1) {
continue;
}
}

let range = new vscode.Range(
+lineStartStr - 1, +colStartStr - 1, +lineEndStr - 1, +colEndStr
);
Expand Down

0 comments on commit 0c4b211

Please sign in to comment.