-
Notifications
You must be signed in to change notification settings - Fork 570
Optimize imports on the fly #1807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
auduchinok
wants to merge
3
commits into
go-lang-plugin-org:master
Choose a base branch
from
auduchinok:on_the_fly_optimize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/com/goide/codeInsight/imports/GoOptimizeImportsPass.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Mihai Toader, Florin Patan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.goide.codeInsight.imports; | ||
|
||
import com.intellij.codeInsight.daemon.impl.DefaultHighlightInfoProcessor; | ||
import com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.progress.ProgressIndicator; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.DocumentUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class GoOptimizeImportsPass extends ProgressableTextEditorHighlightingPass { | ||
@NotNull private final PsiFile myFile; | ||
|
||
public GoOptimizeImportsPass(@NotNull Project project, @NotNull PsiFile file, @NotNull Editor editor) { | ||
super(project, editor.getDocument(), "Go Optimize Imports Pass", file, editor, file.getTextRange(), false, | ||
new DefaultHighlightInfoProcessor()); | ||
myFile = file; | ||
} | ||
|
||
@Override | ||
protected void collectInformationWithProgress(@NotNull ProgressIndicator progress) { | ||
progress.checkCanceled(); | ||
} | ||
|
||
@Override | ||
protected void applyInformationWithProgress() { | ||
final Runnable runnable = new GoImportOptimizer().processFile(myFile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So are you going to calculate all imports to remove right in EDT? Don't you think it's not really efficient? |
||
DocumentUtil.writeInRunUndoTransparentAction(runnable); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/com/goide/codeInsight/imports/GoOptimizeImportsPassFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Mihai Toader, Florin Patan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.goide.codeInsight.imports; | ||
|
||
import com.goide.psi.GoFile; | ||
import com.intellij.codeHighlighting.Pass; | ||
import com.intellij.codeHighlighting.TextEditorHighlightingPass; | ||
import com.intellij.codeHighlighting.TextEditorHighlightingPassFactory; | ||
import com.intellij.codeHighlighting.TextEditorHighlightingPassRegistrar; | ||
import com.intellij.codeInsight.daemon.impl.FileStatusMap; | ||
import com.intellij.openapi.components.AbstractProjectComponent; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiFile; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class GoOptimizeImportsPassFactory extends AbstractProjectComponent implements TextEditorHighlightingPassFactory { | ||
protected GoOptimizeImportsPassFactory(Project project, TextEditorHighlightingPassRegistrar highlightingPassRegistrar) { | ||
super(project); | ||
highlightingPassRegistrar.registerTextEditorHighlightingPass(this, new int[]{Pass.UPDATE_ALL}, null, false, -1); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public TextEditorHighlightingPass createHighlightingPass(@NotNull PsiFile file, @NotNull Editor editor) { | ||
TextRange range = FileStatusMap.getDirtyTextRange(editor, Pass.UPDATE_ALL); | ||
if (range != null && file instanceof GoFile && GoCodeInsightSettings.getInstance().isOptimizeImportsOnTheFly()) { | ||
return new GoOptimizeImportsPass(file.getProject(), file, editor); | ||
} | ||
return null; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getComponentName() { | ||
return "OptimizeImportsPassFactory"; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
testData/imports/optimize/on-the-fly/redeclaredImport-after.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package a | ||
import ( | ||
"fmt" | ||
"http" | ||
) | ||
|
||
func b() { | ||
http.get("123") | ||
fmt.Println() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package a | ||
import ( | ||
fmt "fmt" | ||
"fmt" | ||
"http" | ||
) | ||
|
||
func b() { | ||
http.get("123") | ||
fmt.Println() | ||
} |
10 changes: 10 additions & 0 deletions
10
testData/imports/optimize/on-the-fly/redundantImport-after.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package a | ||
import ( | ||
"fmt" | ||
"http" | ||
) | ||
|
||
func b() { | ||
http.get("123") | ||
fmt.Println() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package a | ||
import ( | ||
fmt "fmt" | ||
"http" | ||
) | ||
|
||
func b() { | ||
http.get("123") | ||
fmt.Println() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package a | ||
|
||
func b() { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package a | ||
import "fmt" | ||
|
||
func b() { | ||
} |
63 changes: 63 additions & 0 deletions
63
tests/com/goide/codeInsight/imports/GoOptimizeImportsOnTheFlyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Mihai Toader, Florin Patan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.goide.codeInsight.imports; | ||
|
||
import com.goide.GoCodeInsightFixtureTestCase; | ||
import com.intellij.testFramework.LightProjectDescriptor; | ||
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl; | ||
|
||
public class GoOptimizeImportsOnTheFlyTest extends GoCodeInsightFixtureTestCase { | ||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
setUpProjectSdk(); | ||
((CodeInsightTestFixtureImpl)myFixture).canChangeDocumentDuringHighlighting(true); | ||
GoCodeInsightSettings.getInstance().setOptimizeImportsOnTheFly(true); | ||
} | ||
|
||
@Override | ||
protected void tearDown() throws Exception { | ||
GoCodeInsightSettings.getInstance().setOptimizeImportsOnTheFly(false); | ||
super.tearDown(); | ||
} | ||
|
||
@Override | ||
protected String getBasePath() { | ||
return "imports/optimize/on-the-fly"; | ||
} | ||
|
||
@Override | ||
protected LightProjectDescriptor getProjectDescriptor() { | ||
return createMockProjectDescriptor(); | ||
} | ||
|
||
@Override | ||
protected boolean isWriteActionRequired() { | ||
return false; | ||
} | ||
|
||
private void doTest() { | ||
String testName = getTestName(true); | ||
myFixture.configureByFile(testName + ".go"); | ||
myFixture.doHighlighting(); | ||
myFixture.checkResultByFile(testName + "-after.go"); | ||
} | ||
|
||
public void testUnusedImport() { doTest(); } | ||
public void testRedundantImport() { doTest(); } | ||
public void testRedeclaredImport() { doTest(); } | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
enable by default