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

Commit

Permalink
Case when no workpsace is open
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Sep 18, 2017
1 parent f6dafa4 commit 0335692
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/goImplementations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ interface GuruImplementsOutput {

export class GoImplementationProvider implements vscode.ImplementationProvider {
public provideImplementation(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.Definition> {
// To keep `guru implements` fast we want to restrict the scope of the search to current workpsace
// If no workpsace is open, then no-op
let root = vscode.workspace.rootPath;
if (vscode.workspace.getWorkspaceFolder(document.uri)) {
root = vscode.workspace.getWorkspaceFolder(document.uri).uri.fsPath;
}
if (!root) {
vscode.window.showInformationMessage('Cannot find implementations when there is no workspace open.');
return;
}

return new Promise<vscode.Definition>((resolve, reject) => {
if (token.isCancellationRequested) {
return resolve(null);
}
let env = getToolsEnvVars();
let cwd = vscode.workspace.getWorkspaceFolder(document.uri).uri.fsPath;
let listProcess = cp.execFile(getGoRuntimePath(), ['list', '-e', '-json'], { cwd, env }, (err, stdout, stderr) => {
let listProcess = cp.execFile(getGoRuntimePath(), ['list', '-e', '-json'], { cwd: root, env }, (err, stdout, stderr) => {
if (err) {
return reject(err);
}
Expand All @@ -47,7 +57,7 @@ export class GoImplementationProvider implements vscode.ImplementationProvider {
let buildTags = '"' + vscode.workspace.getConfiguration('go', document.uri)['buildTags'] + '"';
let args = ['-scope', `${scope}/...`, '-json', '-tags', buildTags, 'implements', `${filename}:#${offset.toString()}`];

let guruProcess = cp.execFile(goGuru, args, {env}, (err, stdout, stderr) => {
let guruProcess = cp.execFile(goGuru, args, { env }, (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('guru');
return resolve(null);
Expand Down
7 changes: 5 additions & 2 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,13 @@ export function activate(ctx: vscode.ExtensionContext): void {
let gopath = getCurrentGoPath();

let wasInfered = vscode.workspace.getConfiguration('go', vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri : null)['inferGopath'];
let root = vscode.window.activeTextEditor ? (vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || vscode.window.activeTextEditor.document).uri.fsPath : vscode.workspace.rootPath;
let root = vscode.workspace.rootPath;
if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) {
root = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath;
}

// not only if it was configured, but if it was successful.
if (wasInfered && root.indexOf(gopath) === 0) {
if (wasInfered && root && root.indexOf(gopath) === 0) {
const inferredFrom = vscode.window.activeTextEditor ? 'current folder' : 'workspace root';
vscode.window.showInformationMessage(`Current GOPATH is inferred from ${inferredFrom}: ${gopath}`);
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/goPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export function parseEnvFile(path: string): { [key: string]: string } {

// Walks up given folder path to return the closest ancestor that has `src` as a child
export function getInferredGopath(folderPath: string): string {
if (!folderPath) {
return;
}

let dirs = folderPath.toLowerCase().split(path.sep);

// find src directory closest to given folder path
Expand Down
2 changes: 1 addition & 1 deletion src/goSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class GoSignatureHelpProvider implements SignatureHelpProvider {
}

public provideSignatureHelp(document: TextDocument, position: Position, token: CancellationToken): Promise<SignatureHelp> {
let goConfig = this.goConfig;
if (!this.goConfig) {
this.goConfig = vscode.workspace.getConfiguration('go', document.uri);
}
Expand All @@ -30,6 +29,7 @@ export class GoSignatureHelpProvider implements SignatureHelpProvider {
}
let callerPos = this.previousTokenPosition(document, theCall.openParen);
// Temporary fix to fall back to godoc if guru is the set docsTool
let goConfig = this.goConfig;
if (goConfig['docsTool'] === 'guru') {
goConfig = Object.assign({}, goConfig, { 'docsTool': 'godoc' });
}
Expand Down
9 changes: 8 additions & 1 deletion src/goSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ export class GoWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider
symbols.push(symbolInfo);
});
};
let root = vscode.window.activeTextEditor ? (vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || vscode.window.activeTextEditor.document).uri.fsPath : vscode.workspace.rootPath;
let root = vscode.workspace.rootPath;
if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) {
root = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath;
}
if (!root) {
vscode.window.showInformationMessage('No workspace is open to find symbols.');
return;
}

return getWorkspaceSymbols(root, query).then(results => {
let symbols: vscode.SymbolInformation[] = [];
Expand Down
10 changes: 9 additions & 1 deletion src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, args
* @param goConfig Configuration for the Go extension.
*/
export function testWorkspace(goConfig: vscode.WorkspaceConfiguration, args: any) {
let dir = vscode.workspace.rootPath;
if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) {
dir = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath;
}
if (!dir) {
vscode.window.showInformationMessage('No workspace is open to run tests.');
return;
}
const testConfig = {
goConfig: goConfig,
dir: vscode.window.activeTextEditor ? (vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || vscode.window.activeTextEditor.document).uri.fsPath : vscode.workspace.rootPath,
dir: dir,
flags: getTestFlags(goConfig, args),
includeSubDirectories: true
};
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ export function getToolsEnvVars(): any {
}

export function getCurrentGoPath(workspaceUri?: vscode.Uri): string {
if (!workspaceUri && vscode.window.activeTextEditor) {
workspaceUri = (vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || vscode.window.activeTextEditor.document).uri;
if (!workspaceUri && vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) {
workspaceUri = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri;
}
const config = vscode.workspace.getConfiguration('go', workspaceUri);
const currentRoot = workspaceUri ? workspaceUri.fsPath : vscode.workspace.rootPath;
Expand Down

0 comments on commit 0335692

Please sign in to comment.