This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
goBrowsePackages: toString bug fix + do not wait for goListAll #1136
Merged
ramya-rao-a
merged 3 commits into
microsoft:master
from
marwan-at-work:browse-packages-refactor
Aug 17, 2017
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,64 +26,73 @@ export function browsePackages() { | |
selectedText = getImportPath(selectedText); | ||
} | ||
|
||
if (isGoListComplete()) { | ||
return showPackages(selectedText); | ||
showPackageFiles(selectedText); | ||
} | ||
|
||
function showPackageFiles(pkg: string) { | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if at the end of this second, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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+)?\"([^\"]+)\"/); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 rungo list