-
Notifications
You must be signed in to change notification settings - Fork 645
Conversation
Thanks for the PR @freman Any reason to prompt the user to choose among the imported packages in current document rather than say choose the package under the cursor like we do for the |
Hi @ramya-rao-a no reason really, I was just excited to be getting a feature that worked around the macos path selection box. It did have the benefit of working no matter where one was in the file. I've taken your suggestion and expanded on it, it'll now use the current selected import line, or if the user has selected text it'll try to use that (reasoning being you might be importing foo/bar/package but it makes more sense to work in foo/bar) It'll also try to prevent adding redundant workspaces, one future improvement I could see is removing redundant workspaces (ie you add foo/bar/package but then later add foo/bar it might be worth removing foo/bar/package automatically) |
Errm, I don't think I broke travis... did I? |
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.
Sorry for taking a while to get to this PR.
I have left a few comments, please do take a look.
src/goImport.ts
Outdated
|
||
let importPath = ''; | ||
if (selection.isSingleLine) { | ||
// Attempt to load a partial import path based on currently selected text |
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.
Can you provide an example for the case that you are attempting to cover here?
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.
Absolutely. The easiest example that comes to mind.
import "github.com/aws/aws-sdk-go/service/rds/rdsutils"
Realistically being able to select github.com/aws/aws-sdk-go
and add that to the workspace is probably going to be what you want more than github.com/aws/aws-sdk-go/service/rds/rdsutils
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.
Good point. I would change the if (selection.isSingleLine)
to if (!selection.isEmpty)
.
There could be a package with a very long import path or someone with a very zoomed in window.
Also trim the selected text before adding the "
in the beginning and end. Else the selected text could be "github.com/golang/example"
and this code here will change it to " "github.com/golang/example"
src/goImport.ts
Outdated
|
||
let filePath = fixDriveCasingInWindows(editor.document.fileName); | ||
let fileDirPath = path.dirname(filePath); | ||
let currentWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), fileDirPath); |
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.
Take the case of multiple gopaths. Someone might have GOPATH set to /Users/folderA;/Users/folderB
.
The current workspace is under /Users.folderA
The package in question could be under /Users/folderB
In this case, the current logic wont work.
How about passing the import path to go list -f {{.Dir}}
? This would return the actual path for the package. You wouldnt need to to a fs.exists
check either.
See showPackageFiles
function in goBrowsePackage.ts
for a reference on how to use go list
.
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.
True, I'll sort this out.
src/goImport.ts
Outdated
const existingWorkspaceFolder = getRootWorkspaceFolder(importPathUri); | ||
if (existingWorkspaceFolder !== undefined) { | ||
vscode.window.showInformationMessage('Already available under ' + existingWorkspaceFolder.name); | ||
return; |
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.
What happens when we pass in a uri for an already existing folder to vscode.workspace.updateWorkspaceFolders
? If updateWorkspaceFolders
gracefully handles this case, then we dont need to do this check beforehand
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.
I can't remember if it did or didn't, I think my motivation was to let the user know why nothing happened. I can double check but I still think it's nice to let the user know that the workspace is already imported.
IE: they try to add github.com/aws/aws-sdk-go/service/rds/rdsutils
after having github.com/aws/aws-sdk-go
loaded.
In an ideal world I'd actually make the workspace navigate/expand the path but I've no idea how to do this in vscode
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.
I double checked.
Adding a subfolder of a folder in already in the work space just creates a new folder pointing to that subfolder in the workspace.
eg.
if you already have github.com/aws/aws-sdk-go/
then activate the command on github.com/aws/aws-sdk-go/service/rds/rdsutils
you end up with aws-sdk-go
and rdsutils
as workspace folders.
Ideally I'd have it navigate to and highlight the added package but I have no idea how to do that.
src/goMain.ts
Outdated
@@ -274,6 +274,10 @@ export function activate(ctx: vscode.ExtensionContext): void { | |||
return addImport(typeof arg === 'string' ? arg : null); | |||
})); | |||
|
|||
ctx.subscriptions.push(vscode.commands.registerCommand('go.import.workspace', (arg: string) => { |
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.
What is the scenario where an arg would be passed via the command?
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.
Uh that's a bit of left over code, it probably wouldn't at this point, I'll remove that.
package.json
Outdated
@@ -154,6 +154,10 @@ | |||
"title": "Go: Add Import", | |||
"description": "Add an import declaration" | |||
}, | |||
{ | |||
"command": "go.import.workspace", | |||
"title": "Go: Add import to workspace" |
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.
The name is not very self explanatory....
How about go.add.package.workspace
and Go: Add Package to Workspace
?
@ramya-rao-a is there any way to trigger travis without pushing again? |
src/goImport.ts
Outdated
} | ||
|
||
const goRuntimePath = getBinPath('go'); | ||
const env = Object.assign({}, process.env, { GOPATH: getCurrentGoPath() }); |
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.
Use getToolsEnvVars()
src/util.ts
Outdated
let workspaceFolder = vscode.workspace.getWorkspaceFolder(forPathUri); | ||
let previousWorkspaceFolder: vscode.WorkspaceFolder; | ||
while (workspaceFolder !== undefined) { |
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.
Can you explain the scenario that would need this loop?
Taking your example of aws, if github.com/aws/aws-sdk-go
is already part of the workspace and we run the command for github.com/aws/aws-sdk-go/service/rds/rdsutils
, wouldnt vscode.workspace.getWorkspaceFolder(forPathUri)
give you the uri of the parent workspace folder? And isnt that enough?
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.
Looks good. I have a left a few more comments. Please take a look.
Thanks @ramya-rao-a, I've addressed those, the original thought behind that loop is lost to time, I suspect I just didn't understand how that call worked |
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.
Awesome, thanks for your patience with me :)
Make it possible to add the path for the selected import, or part there of to the current workspace for convenience.
Resolves #1733