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 all commits
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
92 changes: 51 additions & 41 deletions src/goBrowsePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,74 @@ export function browsePackages() {
selectedText = getImportPath(selectedText);
}

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

function showPackageFiles(pkg: string, showAllPkgsIfPkgNotFound: boolean) {
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.');
if (!pkg && showAllPkgsIfPkgNotFound) {
return showPackageList();
}

cp.execFile(goRuntimePath, ['list', '-f', '{{.Dir}}:{{.GoFiles}}:{{.TestGoFiles}}:{{.XTestGoFiles}}', pkg], (err, stdout, stderr) => {
if (!stdout || stdout.indexOf(':') === -1) {
if (showAllPkgsIfPkgNotFound) {
return showPackageList();
}

return;
}
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() {
if (!isGoListComplete()) {
return showTryAgainLater();
}

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, false);
});
});
});
}

function showTryAgainLater() {
vscode.window.showInformationMessage('Finding packages... Try after sometime.');
}

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