Proposal to implement collapsing#500
Proposal to implement collapsing#500ramya-rao-a merged 6 commits intomicrosoft:masterfrom benclarkwood:master
Conversation
package.json
Outdated
| "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.
If anyone has a better name...
There was a problem hiding this comment.
I actually think we can just make this the default. No need for a new setting here.
lukehoban
left a comment
There was a problem hiding this comment.
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.
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.
This will also fail in case of dot imports and named imports. Example:
import . "fmt"
import m "math"
package.json
Outdated
| "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.
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.
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.
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\tafter the word "import" in the first import - Replace the word "import" with
\tfor 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
| } | ||
|
|
||
| export function addImport(arg: string) { | ||
| export function addImport(arg: string, goConfig: vscode.WorkspaceConfiguration) { |
There was a problem hiding this comment.
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.
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.
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