-
Notifications
You must be signed in to change notification settings - Fork 645
automatic import adder should not separate first import from its comment #1701
Comments
Can you share what happens when you just type out Also, do you know of other cases like this where the import statement should not be updated? |
If vscode-go is supposed to add imports on "Format Document", then it doesn't seem to be working. Only if I set the formatTool to "goimports" does "Format Document" add imports. As for goimports' behaviour, it simply adds another import line without switching to the parentheses form, so it does not break the code. |
I am not aware of any other magic packages aside from "C". |
Having a brief a look at the code. The culprit seems to be src/goImport.ts:getTextEditForAddImport. As the code is currently written it simply inserts "import (" before the first import and then replaces "import" with "\t". In a case like this
that process works fine. It's only when the "C" is the first import that this breaks because it inserts the "import (" between the "C" and the comment. That's the actual issue. If the first package is preceded by a comment line, then the "import (" must be inserted before the comment. I don't even see this as a special case for "C". If someone writes code like this
converting it to
also breaks the association between the package and the comment. So the solution would look something like this: Current code:
Fixed code (pseudo-code as I don't know vscode-go well enough):
This only handles // comments but I think that's fine. All of the examples in the documentation of cgo use these comments, so I'm sure 99% of programmers do that, too. |
This is a feature request for @heschik: Would you support a change like this or is this working as intended? |
Seems someone got to it in the intervening 2 years.
|
Great! Closing this issue since it is now fixed. |
Ah, my mistake - I didn't realize that VS Code had extra code for handling |
getTextEditForAddImport tries to insert a newly added package into an existing import group. If no import group exists, it tries to merge single line import statements and create a group. In this process, import statements like import "C" is a pseudo import and shouldn't be selected for import grouping. This CL changes parseFilePrelude to detect such pseudo imports and excludes such imports from the grouping logic. Fixes microsoft#1701
getTextEditForAddImport tries to insert a newly added package into an existing import group. If no import group exists, it tries to merge single line import statements and create a group. In this process, import statements like import "C" are pseudo imports and shouldn't be grouped with other imports. This CL changes parseFilePrelude to detect such pseudo imports and excludes such imports from the grouping logic. Fixes microsoft#1701
getTextEditForAddImport tries to insert a newly added package into an existing import group. If no import group exists, it tries to merge single line import statements and create a group. In this process, import statements like import "C" are pseudo imports and shouldn't be grouped with other imports. This CL changes parseFilePrelude to detect such pseudo imports and excludes such imports from the grouping logic. Fixes microsoft#1701
* goImports: avoid collapsing new imports into pseudo import line getTextEditForAddImport tries to insert a newly added package into an existing import group. If no import group exists, it tries to merge single line import statements and create a group. In this process, import statements like import "C" are pseudo imports and shouldn't be grouped with other imports. This CL changes parseFilePrelude to detect such pseudo imports and excludes such imports from the grouping logic. Fixes #1701 * correct the file name mac and vsccode ignored casing, but linux and git were unhappy.
The latest update 0.14.2 has the fix for this, Thanks @hyangah |
Relevant Settings:
Type the following program
However this breaks the magic of import "C" which uses the preceding comment.
When converting import lines to a parenthesized import block, vscode-go should insert the "import (" before the comment lines immediately preceding (i.e. with no empty lines in between) the first import (if there are any).
The text was updated successfully, but these errors were encountered: