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

Commit

Permalink
Option to Ignore folders while workspace symbol search (#795)
Browse files Browse the repository at this point in the history
* Option to Ignore folders while workspace symbol search

* Use the right flag
  • Loading branch information
ramya-rao-a authored Mar 29, 2017
1 parent dea3135 commit d2489dc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ install:
- go get -u -v sourcegraph.com/sqs/goreturns
- go get -u -v golang.org/x/tools/cmd/gorename
- go get -u -v github.com/tpng/gopkgs
- go get -u -v github.com/newhook/go-symbols
- go get -u -v github.com/acroca/go-symbols
- go get -u -v github.com/alecthomas/gometalinter
- go get -u -v github.com/cweill/gotests/...
- GO15VENDOREXPERIMENT=1
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ If you wish to have the extension use a separate GOPATH for its tools, provide t
- goreturns: `go get -u -v sourcegraph.com/sqs/goreturns`
- gorename: `go get -u -v golang.org/x/tools/cmd/gorename`
- gopkgs: `go get -u -v github.com/tpng/gopkgs`
- go-symbols: `go get -u -v github.com/newhook/go-symbols`
- go-symbols: `go get -u -v github.com/acroca/go-symbols`
- guru: `go get -u -v golang.org/x/tools/cmd/guru`
- gotests: `go get -u -v github.com/cweill/gotests/...`
- godoc: `go get -u -v golang.org/x/tools/cmd/godoc`
Expand All @@ -214,7 +214,7 @@ go get -u -v github.com/lukehoban/go-outline
go get -u -v sourcegraph.com/sqs/goreturns
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v github.com/tpng/gopkgs
go get -u -v github.com/newhook/go-symbols
go get -u -v github.com/acroca/go-symbols
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v github.com/cweill/gotests/...
go get -u -v golang.org/x/tools/cmd/godoc
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,14 @@
"testCoverage": false
},
"description": "Enable/Disable entries from the context menu in the editor."
},
"go.gotoSymbol.ignoreFolders": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"description": "Folder names (not paths) to ignore while using Go to Symbol in Workspace feature"
}
}
},
Expand Down Expand Up @@ -637,7 +645,7 @@
"when": "editorTextFocus && config.go.editorContextMenuCommands.generateTestForPackage && resourceLangId == go",
"command": "go.test.generate.package"
},
{
{
"when": "editorTextFocus && config.go.editorContextMenuCommands.addImport && resourceLangId == go",
"command": "go.import.add"
},
Expand Down
2 changes: 1 addition & 1 deletion src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getTools(goVersion: SemVersion): { [key: string]: string } {
'gocode': 'github.com/nsf/gocode',
'gopkgs': 'github.com/tpng/gopkgs',
'go-outline': 'github.com/lukehoban/go-outline',
'go-symbols': 'github.com/newhook/go-symbols',
'go-symbols': 'github.com/acroca/go-symbols',
'guru': 'golang.org/x/tools/cmd/guru',
'gorename': 'golang.org/x/tools/cmd/gorename',
'gomodifytags': 'github.com/fatih/gomodifytags'
Expand Down
36 changes: 26 additions & 10 deletions src/goSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import vscode = require('vscode');
import cp = require('child_process');
import { getBinPath } from './util';
import { promptForMissingTool } from './goInstallTools';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';

// Keep in sync with github.com/newhook/go-symbols'
// Keep in sync with github.com/acroca/go-symbols'
interface GoSymbolDeclaration {
name: string;
kind: string;
Expand Down Expand Up @@ -47,28 +47,44 @@ export class GoWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider
symbols.push(symbolInfo);
});
};
let symArgs = vscode.workspace.getConfiguration('go')['symbols'];
let args = [vscode.workspace.rootPath, query];
if (symArgs !== undefined && symArgs !== '') {
args.unshift(symArgs);

return getWorkspaceSymbols(vscode.workspace.rootPath, query).then(results => {
let symbols: vscode.SymbolInformation[] = [];
convertToCodeSymbols(results, symbols);
return symbols;
});
}
}

export function getWorkspaceSymbols(workspacePath: string, query: string, goConfig?: vscode.WorkspaceConfiguration, ignoreFolderFeatureOn: boolean = true): Thenable<GoSymbolDeclaration[]> {
if (!goConfig) {
goConfig = vscode.workspace.getConfiguration('go');
}
let gotoSymbolConfig = goConfig['gotoSymbol'];
let ignoreFolders: string[] = gotoSymbolConfig ? gotoSymbolConfig['ignoreFolders'] : [];
let args = (ignoreFolderFeatureOn && ignoreFolders && ignoreFolders.length > 0) ? ['-ignore', ignoreFolders.join(',')] : [];
args.push(workspacePath);
args.push(query);
let gosyms = getBinPath('go-symbols');
return new Promise((resolve, reject) => {
let p = cp.execFile(gosyms, args, { maxBuffer: 1024 * 1024 }, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('go-symbols');
}
if (err && stderr && stderr.startsWith('flag provided but not defined: -ignore')) {
promptForUpdatingTool('go-symbols');
return getWorkspaceSymbols(workspacePath, query, goConfig, false).then(results => {
return resolve(results);
});
}
if (err) return resolve(null);
let result = stdout.toString();
let decls = <GoSymbolDeclaration[]>JSON.parse(result);
let symbols: vscode.SymbolInformation[] = [];
convertToCodeSymbols(decls, symbols);
return resolve(symbols);
return resolve(decls);
} catch (e) {
reject(e);
}
});
});
}
}
33 changes: 33 additions & 0 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { GoHoverProvider } from '../src/goExtraInfo';
import { GoCompletionItemProvider } from '../src/goSuggest';
import { GoSignatureHelpProvider } from '../src/goSignature';
import { GoDefinitionProvider } from '../src/goDeclaration';
import { getWorkspaceSymbols } from '../src/goSymbol';
import { check } from '../src/goCheck';
import cp = require('child_process');
import { getEditsFromUnifiedDiffStr, getEdits } from '../src/diffUtils';
Expand Down Expand Up @@ -709,4 +710,36 @@ It returns the number of bytes written and any write error encountered.
return Promise.all<void>([gopkgsPromise, listPkgPromise]);
}).then(() => done(), done);
});

test('Workspace Symbols', (done) => {
// This test needs a go project that has vendor folder and vendor packages
// Since the Go extension takes a dependency on the godef tool at github.com/rogpeppe/godef
// which has vendor packages, we are using it here to test the "replace vendor packages with relative path" feature.
// If the extension ever stops depending on godef tool or if godef ever stops having vendor packages, then this test
// will fail and will have to be replaced with any other go project with vendor packages

let workspacePath = path.join(process.env['GOPATH'], 'src', 'github.com', 'rogpeppe', 'godef');
let configWithoutIgnoringFolders = Object.create(vscode.workspace.getConfiguration('go'), {
'gotoSymbol': {
value: {
'ignoreFolders': []
}
}
});
let configWithIgnoringFolders = Object.create(vscode.workspace.getConfiguration('go'), {
'gotoSymbol': {
value: {
'ignoreFolders': ['vendor']
}
}
});
let withoutIgnoringFolders = getWorkspaceSymbols(workspacePath, 'WinInfo', configWithoutIgnoringFolders).then(results => {
assert.equal(results[0].name, 'WinInfo');
assert.equal(results[0].path, path.join(workspacePath, 'vendor/9fans.net/go/acme/acme.go'));
});
let withIgnoringFolders = getWorkspaceSymbols(workspacePath, 'WinInfo', configWithIgnoringFolders).then(results => {
assert.equal(results.length, 0);
});
Promise.all([withIgnoringFolders, withoutIgnoringFolders]).then(() => done(), done);
});
});

0 comments on commit d2489dc

Please sign in to comment.