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

goBrowsePackages: toString bug fix + do not wait for goListAll #1136

Merged
merged 3 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 51 additions & 42 deletions src/goBrowsePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,73 @@ export function browsePackages() {
selectedText = getImportPath(selectedText);
}

if (isGoListComplete()) {
return showPackages(selectedText);
showPackageFiles(selectedText);
}

function showPackageFiles(pkg: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if pkg is empty string which can happen when the command is run without any editor open, then we will be needlessly creating a process to run go list

const goRuntimePath = getGoRuntimePath();
if (!goRuntimePath) {
return vscode.window.showErrorMessage('Could not locate Go path. Make sure you have Go installed');
}

// `go list all` has not completed. Wait for a second which is an acceptable duration of delay.
setTimeout(() => {
// `go list all` still not complete. It takes a long time on slower machines or when there are way too many folders in GOPATH
if (!isGoListComplete()) {
vscode.window.showInformationMessage('Finding packages... Try after sometime.');
return;
cp.execFile(goRuntimePath, ['list', '-f', '{{.Dir}}:{{.GoFiles}}:{{.TestGoFiles}}:{{.XTestGoFiles}}', pkg], (err, stdout, stderr) => {
if (!stdout || stdout.indexOf(':') === -1) {
if (isGoListComplete()) {
return showPackageList();
}

return showTryAgainLater();
}
showPackages(selectedText);
}, 1000);

let matches = stdout && stdout.match(/(.*):\[(.*)\]:\[(.*)\]:\[(.*)\]/);
if (matches) {
let dir = matches[1];
let files = matches[2] ? matches[2].split(' ') : [];
let testfiles = matches[3] ? matches[3].split(' ') : [];
let xtestfiles = matches[4] ? matches[4].split(' ') : [];
files = files.concat(testfiles);
files = files.concat(xtestfiles);
vscode.window.showQuickPick(files, { placeHolder: `Below are Go files from ${pkg}` }).then(file => {
// if user abandoned list, file will be null and path.join will error out.
// therefore return.
if (!file) return;

vscode.workspace.openTextDocument(path.join(dir, file)).then(document => {
vscode.window.showTextDocument(document);
});
});
}
});
}

function showPackages(selectedText: string) {
const goRuntimePath = getGoRuntimePath();
if (!goRuntimePath) {
return;
}
function showPackageList() {
goListAll().then(pkgMap => {
const pkgs: string[] = Array.from(pkgMap.keys());
if (!pkgs || pkgs.length === 0) {
return vscode.window.showErrorMessage('Could not find packages. Ensure `go list all` runs successfully.');
}
let selectPkgPromise: Thenable<string> = Promise.resolve(selectedText);
if (!selectedText || pkgs.indexOf(selectedText) === -1) {
selectPkgPromise = vscode.window.showQuickPick(pkgs, { placeHolder: 'Select a package to browse' });
}
selectPkgPromise.then(pkg => {
cp.execFile(goRuntimePath, ['list', '-f', '{{.Dir}}:{{.GoFiles}}:{{.TestGoFiles}}:{{.XTestGoFiles}}', pkg], (err, stdout, stderr) => {
if (!stdout || stdout.indexOf(':') === -1) {
return;
}
let matches = stdout.match(/(.*):\[(.*)\]:\[(.*)\]:\[(.*)\]/);
if (matches) {
let dir = matches[1];
let files = matches[2] ? matches[2].split(' ') : [];
let testfiles = matches[3] ? matches[3].split(' ') : [];
let xtestfiles = matches[4] ? matches[4].split(' ') : [];
files = files.concat(testfiles);
files = files.concat(xtestfiles);
vscode.window.showQuickPick(files, { placeHolder: `Below are Go files from ${pkg}` }).then(file => {
// if user abandoned list, file will be null and path.join will error out.
// therefore return.
if (!file) return;

vscode.workspace.openTextDocument(path.join(dir, file)).then(document => {
vscode.window.showTextDocument(document);
});
});
}
vscode
.window
.showQuickPick(pkgs, { placeHolder: 'Select a package to browse' })
.then(pkgFromDropdown => {
if (!pkgFromDropdown) return;
showPackageFiles(pkgFromDropdown);
});
});
});
}

function showTryAgainLater() {
// `go list all` has not completed. Wait for a second which is an acceptable duration of delay.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if at the end of this second, go list completes, we won't be showing anything. Was that intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so that we don't get stuck in an infinite loop. And since we say try after sometime, it shouldn't be a surprise.

setTimeout(() => {
// `go list all` still not complete. It takes a long time on slower machines or when there are way too many folders in GOPATH
if (!isGoListComplete()) {
vscode.window.showInformationMessage('Finding packages... Try after sometime.');
return;
}
}, 1000);
}

function getImportPath(text: string): string {
// Catch cases like `import alias "importpath"` and `import "importpath"`
let singleLineImportMatches = text.match(/^\s*import\s+([a-z,A-Z,_,\.]\w*\s+)?\"([^\"]+)\"/);
Expand Down
2 changes: 1 addition & 1 deletion src/goPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function goListAll(): Promise<Map<string, string>> {
});

cmd.on('close', (status) => {
chunks.toString().split('\n').forEach(pkgDetail => {
chunks.join('').split('\n').forEach(pkgDetail => {
if (!pkgDetail || !pkgDetail.trim() || pkgDetail.indexOf(';') === -1) return;
let [pkgName, pkgPath] = pkgDetail.trim().split(';');
allPkgs.set(pkgPath, pkgName);
Expand Down