-
Notifications
You must be signed in to change notification settings - Fork 645
Conversation
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": { |
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 anyone has a better name...
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 actually think we can just make this the default. No need for a new setting 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.
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 "(.+)"/); |
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.
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).
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.
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": { |
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 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); |
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.
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?
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.
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?
- Insert
(\n\t
after the word "import" in the first import - Replace the word "import" with
\t
for rest of the imports - Insert
)
in the line after the last import
Oh my gosh this is such a better way of doing this! Thanks!
|
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) { |
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.
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'); |
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.
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 | ||
|
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 add a unit test for this?
Haven't abandoned this, just been supes busy at work. Will add tests / address additional concerns this weekend hopefully. |
@benclarkwood There has been some changes to |
0b2155b
to
7aa5605
Compare
This PR is stuck in a conflict, @benclarkwood would you mine rebasing? |
d5299a5
to
38331b7
Compare
7259f77
to
f6936fa
Compare
89e20b7
to
3d8a1f0
Compare
7d591f1
to
a8a68e2
Compare
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. 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
@benclarkwood I took the liberty of rebasing and adding tests |
@ramya-rao-a thanks! Sorry, Golang became not my daily driver and I totally forgot about this PR. 🙇 . |
note: addresses #374