forked from ligasgr/intellij-xquery
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Action 'Optimize imports' in XQuery removes unused imports
Closes ligasgr#100
- Loading branch information
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
55 changes: 55 additions & 0 deletions
55
src/main/java/org/intellij/xquery/formatter/XQueryImportOptimizer.java
This file contains 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,55 @@ | ||
/* | ||
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors | ||
* (see the CONTRIBUTORS file). | ||
* | ||
* 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 org.intellij.xquery.formatter; | ||
|
||
import com.intellij.lang.ImportOptimizer; | ||
import com.intellij.psi.PsiFile; | ||
import org.intellij.xquery.inspection.imports.FunctionNamespacesExtractor; | ||
import org.intellij.xquery.inspection.imports.UnusedImportsFinder; | ||
import org.intellij.xquery.inspection.imports.VariableNamespacesExtractor; | ||
import org.intellij.xquery.psi.XQueryFile; | ||
import org.intellij.xquery.psi.XQueryModuleImport; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collection; | ||
|
||
public class XQueryImportOptimizer implements ImportOptimizer { | ||
@Override | ||
public boolean supports(PsiFile psiFile) { | ||
return psiFile instanceof XQueryFile; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Runnable processFile(final PsiFile psiFile) { | ||
return new Runnable() { | ||
@Override | ||
public void run() { | ||
FunctionNamespacesExtractor functionNamespacesExtractor = new FunctionNamespacesExtractor(); | ||
VariableNamespacesExtractor variableNamespacesExtractor = new VariableNamespacesExtractor(); | ||
UnusedImportsFinder unusedImportsFinder = new UnusedImportsFinder(functionNamespacesExtractor, variableNamespacesExtractor); | ||
|
||
Collection<XQueryModuleImport> unusedImports = unusedImportsFinder.getUnusedImports((XQueryFile) psiFile); | ||
|
||
for (XQueryModuleImport unusedImport : unusedImports) { | ||
unusedImport.delete(); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains 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
40 changes: 40 additions & 0 deletions
40
...testFunctional/java/org/intellij/xquery/inspection/optimizeImport/OptimizeImportTest.java
This file contains 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,40 @@ | ||
/* | ||
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors | ||
* (see the CONTRIBUTORS file). | ||
* | ||
* 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 org.intellij.xquery.inspection.optimizeImport; | ||
|
||
import com.intellij.codeInsight.actions.OptimizeImportsAction; | ||
import com.intellij.ide.DataManager; | ||
import org.intellij.xquery.BaseFunctionalTestCase; | ||
|
||
public class OptimizeImportTest extends BaseFunctionalTestCase { | ||
|
||
@Override | ||
protected String getTestDataPath() { | ||
return "src/testFunctional/testData/org/intellij/xquery/inspection/organizeImports"; | ||
} | ||
|
||
|
||
public void testOptimizeRemovesUnusedImports() { | ||
String tstname = getTestName(false) + ".xq"; | ||
myFixture.configureByFile(tstname); | ||
OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent())); | ||
myFixture.checkResultByFile(getTestName(false) + "_after.xq"); | ||
} | ||
|
||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...l/testData/org/intellij/xquery/inspection/organizeImports/OptimizeRemovesUnusedImports.xq
This file contains 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 @@ | ||
module namespace local = "local"; | ||
|
||
import module "firstUnused" at "file1.xq"; | ||
import module "used" at "file2.xq"; | ||
import module "secondUnused" at "file3.xq"; | ||
declare default function namespace "used"; | ||
|
||
declare function local:testOptimizeRemovesUnusedImport() { | ||
test() | ||
}; |
8 changes: 8 additions & 0 deletions
8
...Data/org/intellij/xquery/inspection/organizeImports/OptimizeRemovesUnusedImports_after.xq
This file contains 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,8 @@ | ||
module namespace local = "local"; | ||
|
||
import module "used" at "file2.xq"; | ||
declare default function namespace "used"; | ||
|
||
declare function local:testOptimizeRemovesUnusedImport() { | ||
test() | ||
}; |