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

Add Import to Workspace #1745

Merged
merged 4 commits into from
Jul 17, 2018
Merged

Add Import to Workspace #1745

merged 4 commits into from
Jul 17, 2018

Conversation

freman
Copy link
Contributor

@freman freman commented Jun 20, 2018

Make it possible to add the path for the selected import, or part there of to the current workspace for convenience.

Resolves #1733

@msftclas
Copy link

msftclas commented Jun 20, 2018

CLA assistant check
All CLA requirements met.

@ramya-rao-a
Copy link
Contributor

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 Go: Get Package command?

@freman
Copy link
Contributor Author

freman commented Jun 20, 2018

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)

@freman freman changed the title Initial work on adding an import folder to workspace Add Import to Workspace Jun 22, 2018
@freman
Copy link
Contributor Author

freman commented Jun 28, 2018

Errm, I don't think I broke travis... did I?

Copy link
Contributor

@ramya-rao-a ramya-rao-a left a 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
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

@ramya-rao-a ramya-rao-a Jul 16, 2018

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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;
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor Author

@freman freman Jul 2, 2018

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) => {
Copy link
Contributor

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?

Copy link
Contributor Author

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"
Copy link
Contributor

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 ?

@freman
Copy link
Contributor Author

freman commented Jul 9, 2018

@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() });
Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Contributor

@ramya-rao-a ramya-rao-a left a 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.

@freman
Copy link
Contributor Author

freman commented Jul 17, 2018

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

Copy link
Contributor

@ramya-rao-a ramya-rao-a left a 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 :)

@ramya-rao-a ramya-rao-a merged commit 81e7053 into microsoft:master Jul 17, 2018
@freman freman deleted the feat-1733 branch July 17, 2018 23:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants