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

Commit

Permalink
Do not show blank identifiers in file outline (#1893)
Browse files Browse the repository at this point in the history
* Do not show blank identifiers in file outline

* Address PR feedback and add tests
  • Loading branch information
ragurney authored and ramya-rao-a committed Aug 31, 2018
1 parent eb1279b commit 311208a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/goOutline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,14 @@ export class GoDocumentSymbolProvider implements vscode.DocumentSymbolProvider {

let gotoSymbolConfig = vscode.workspace.getConfiguration('go', document.uri)['gotoSymbol'];
let includeImports = gotoSymbolConfig ? gotoSymbolConfig['includeImports'] : false;

(decls || []).forEach(decl => {
if (!includeImports && decl.type === 'import') return;

let label = decl.label;

if (label === '_' && decl.type === 'variable') return;

if (decl.receiverType) {
label = '(' + decl.receiverType + ').' + label;
}
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/outlineTest/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"
)

var _ string = "foobar"

func print(txt string) {
fmt.Println(txt)
}
func main() {
print("Hello")
}
28 changes: 25 additions & 3 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getEditsFromUnifiedDiffStr, getEdits } from '../src/diffUtils';
import jsDiff = require('diff');
import { testCurrentFile } from '../src/goTest';
import { getBinPath, getGoVersion, isVendorSupported } from '../src/util';
import { documentSymbols } from '../src/goOutline';
import { documentSymbols, GoDocumentSymbolProvider } from '../src/goOutline';
import { listPackages, getTextEditForAddImport } from '../src/goImport';
import { generateTestCurrentFile, generateTestCurrentFunction, generateTestCurrentPackage } from '../src/goGenerateTests';
import { getAllPackages } from '../src/goPackages';
Expand Down Expand Up @@ -76,6 +76,7 @@ suite('Go Extension Tests', () => {
fs.copySync(path.join(fixtureSourcePath, 'fillStruct', 'input_2.go'), path.join(fixturePath, 'fillStruct', 'input_2.go'));
fs.copySync(path.join(fixtureSourcePath, 'fillStruct', 'golden_2.go'), path.join(fixturePath, 'fillStruct', 'golden_2.go'));
fs.copySync(path.join(fixtureSourcePath, 'fillStruct', 'input_2.go'), path.join(fixturePath, 'fillStruct', 'input_3.go'));
fs.copySync(path.join(fixtureSourcePath, 'outlineTest', 'test.go'), path.join(fixturePath, 'outlineTest', 'test.go'));
});

suiteTeardown(() => {
Expand Down Expand Up @@ -551,7 +552,7 @@ It returns the number of bytes written and any write error encountered.
});

test('Test Outline', (done) => {
let filePath = path.join(fixturePath, 'test.go');
let filePath = path.join(fixturePath, 'outlineTest', 'test.go');
let options = { fileName: filePath };
documentSymbols(options, null).then(outlines => {
let packageOutline = outlines[0];
Expand All @@ -569,7 +570,7 @@ It returns the number of bytes written and any write error encountered.
});

test('Test Outline imports only', (done) => {
let filePath = path.join(fixturePath, 'test.go');
let filePath = path.join(fixturePath, 'outlineTest', 'test.go');
let options = { fileName: filePath, importsOnly: true };
documentSymbols(options, null).then(outlines => {
let packageOutline = outlines[0];
Expand All @@ -586,6 +587,27 @@ It returns the number of bytes written and any write error encountered.
}, done);
});

test('Test Outline document symbols', (done) => {
let uri = vscode.Uri.file(path.join(fixturePath, 'outlineTest', 'test.go'));
vscode.workspace.openTextDocument(uri).then(document => {
new GoDocumentSymbolProvider().provideDocumentSymbols(document, null).then(symbols => {
let groupedSymbolNames = symbols.reduce(function (map, symbol) {
map[symbol.kind] = (map[symbol.kind] || []).concat([symbol.name]);
return map;
}, {});

let packageNames = groupedSymbolNames[vscode.SymbolKind.Package];
let variableNames = groupedSymbolNames[vscode.SymbolKind.Variable];
let functionNames = groupedSymbolNames[vscode.SymbolKind.Function];

assert.equal(packageNames[0], 'main');
assert.equal(variableNames, undefined);
assert.equal(functionNames[0], 'print');
assert.equal(functionNames[1], 'main');
});
}).then(() => done(), done);
});

test('Test listPackages', (done) => {
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
vscode.workspace.openTextDocument(uri).then(document => {
Expand Down

0 comments on commit 311208a

Please sign in to comment.