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

Proposal to implement collapsing #500

Merged
merged 6 commits into from
Jan 8, 2018
Merged

Proposal to implement collapsing #500

merged 6 commits into from
Jan 8, 2018

Conversation

benclarkwood
Copy link
Contributor

  1. Add new setting "go.blockImportsOnly"
  2. Add parsing of package name for single import lines
  3. When the user adds an import manually and there is already one or more single imports, collapse it all into a block if go.blockImportsOnly is true.

note: addresses #374

package.json Outdated
@@ -320,6 +320,11 @@
"type": "string",
"default": "",
"description": "The Go build tags to use for all commands that support a `-tags '...'` argument"
},
"go.blockImportsOnly": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If anyone has a better name...

Copy link
Contributor

Choose a reason for hiding this comment

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

I actually think we can just make this the default. No need for a new setting here.

Copy link
Contributor

@lukehoban lukehoban left a comment

Choose a reason for hiding this comment

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

Generally looks good. Couple of comments inline.

src/util.ts Outdated
}
if (line.match(/^(\s)*import(\s)+[^\(]/)) {
ret.imports.push({ kind: 'single', start: i, end: i });
// Pull the package name
let match = line.match(/import "(.+)"/);
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to make sure this never fails. Probably better to just modify the if match to capture the name as well? I ran into failures when a line looked like import "byte" for example (two spaces instead of one).

Copy link
Contributor

Choose a reason for hiding this comment

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

This will also fail in case of dot imports and named imports. Example:

import . "fmt"
import m "math"

package.json Outdated
@@ -320,6 +320,11 @@
"type": "string",
"default": "",
"description": "The Go build tags to use for all commands that support a `-tags '...'` argument"
},
"go.blockImportsOnly": {
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually think we can just make this the default. No need for a new setting here.

src/goImport.ts Outdated
// First, delete all the single packages.
editBuilder.delete(new vscode.Range(new vscode.Position(firstImport.start, 0), new vscode.Position(lastImport.end, 10000)));
// Now, insert the import block.
editBuilder.insert(new vscode.Position(pkg.start + 2, 0), importBlock);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is anchoring this on pkg.start always safe? What if there are comments in front of the imports or two groups of parenthesized imports? Can this just be anchored at firstImport.start instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Like @lukehoban says, this would result in comments that were previously between packages and imports to appear after the new import block.
Also, comments in between single import statements are lost too. Example:

import "math"  //Helps me in math
// This is my custom package
import "mycustom"

How about doing this instead?

  1. Insert (\n\t after the word "import" in the first import
  2. Replace the word "import" with \t for rest of the imports
  3. Insert ) in the line after the last import

@benclarkwood
Copy link
Contributor Author

Oh my gosh this is such a better way of doing this!

Thanks!

On Sep 25, 2016, at 23:03, Ramya Rao notifications@github.com wrote:

@ramya-rao-a commented on this pull request.

In src/goImport.ts:

  •       let firstImport = imports[0];
    
  •     let lastImport = imports[imports.length - 1];
    
  •     // Construct the import block.
    
  •     let importBlock = 'import (\n';
    
  •     imports.forEach(element => {
    
  •         importBlock += '\t"' + element.package + '"\n';
    
  •     });
    
  •     importBlock += '\t"' + imp + '"\n'; // Add the user's new import
    
  •     importBlock += ')\n';
    
  •     return vscode.window.activeTextEditor.edit(editBuilder => {
    
  •         // First, delete all the single packages.
    
  •         editBuilder.delete(new vscode.Range(new vscode.Position(firstImport.start, 0), new vscode.Position(lastImport.end, 10000)));
    
  •         // Now, insert the import block.
    
  •         editBuilder.insert(new vscode.Position(pkg.start + 2, 0), importBlock);
    
    Like @lukehoban says, this would result in comments that were previously between packages and imports to appear after the new import block.
    Also, comments in between single import statements are lost too. Example:

import "math" //Helps me in math
// This is my custom package
import "mycustom"
How about doing this instead?

  1. Insert (\n\t after the word "import" in the first import
  2. Replace the word "import" with \t for rest of the imports
  3. Insert ) in the line after the last import


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

src/goImport.ts Outdated
@@ -31,7 +31,7 @@ function askUserForImport(): Thenable<string> {
});
}

export function addImport(arg: string) {
export function addImport(arg: string, goConfig: vscode.WorkspaceConfiguration) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you shouldn't be needing goConfig here anymore

src/goImport.ts Outdated
editBuilder.insert(new vscode.Position(firstImport.start, 0), 'import (\n');

imports.forEach(element => {
editBuilder.replace(new vscode.Range(new vscode.Position(element.start, 0), new vscode.Position(element.start, importLength)), '\t');
Copy link
Contributor

@ramya-rao-a ramya-rao-a Sep 26, 2016

Choose a reason for hiding this comment

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

Starting from 0 will cause issues when there are spaces/tabs before the word "import".
This shouldn't happen if formatting has been run on the file, but there might be cases where user has not yet saved the file or has turned formatOnSave off or has not formatted the doc at all

Currently parseFilePrelude returns start and end lines. You can update that to return startCharacter as well and use that

src/goImport.ts Outdated
// A comment
import . "dotinclude"
import "apackage2" // A comment explaining the package

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 add a unit test for this?

@benclarkwood
Copy link
Contributor Author

Haven't abandoned this, just been supes busy at work. Will add tests / address additional concerns this weekend hopefully.

@ramya-rao-a
Copy link
Contributor

@benclarkwood There has been some changes to goImport.ts recently, so make sure to get latest from master before you continue

@mattetti
Copy link
Contributor

This PR is stuck in a conflict, @benclarkwood would you mine rebasing?
On a separate note, I find the property name go.blockImportsOnly quite confusing since it reads like we are blocking imports only. Maybe go.groupedImportsOnly?

Ben Wood and others added 4 commits January 7, 2018 20:46
1. Add new setting "go.blockImportsOnly"
2. Add parsing of package name for single import lines
3. When the user adds an import manually and there is already one or more single imports, collapse it all into a block if go.blockImportsOnly is true.
1. Fix linter issues
1. Back out package name parsing
2. Remove setting from package.json (always use block imports)
3. Switch up how block import is created to preserve comments and special imports
@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Jan 8, 2018

@benclarkwood I took the liberty of rebasing and adding tests

@ramya-rao-a ramya-rao-a merged commit 3790d5f into microsoft:master Jan 8, 2018
@benclarkwood
Copy link
Contributor Author

@ramya-rao-a thanks! Sorry, Golang became not my daily driver and I totally forgot about this PR. 🙇 .

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.

6 participants