Skip to content

Commit

Permalink
Action 'Optimize imports' in XQuery removes unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
arnostv committed May 21, 2014
1 parent b363495 commit 06b2614
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -8,3 +8,4 @@ Arnost Valicek <arnost.valicek@gmail.com> - Issue #91 - XQuery context for live
Dirk Kirsten <dk@basex.org> - Issue #91 - Live templates
Mateusz Rudnicki <nicklamer@gmail.com> - Issue #93 - Enhance next/previous method navigation
Arnost Valicek <arnost.valicek@gmail.com> - Issue #21 - Unused variables highlighting
Arnost Valicek <arnost.valicek@gmail.com> - Issue #100 - Optimize imports in xquery files
@@ -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();
}
}
};
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Expand Up @@ -53,6 +53,7 @@
<completion.contributor language="XQuery" implementationClass="org.intellij.xquery.completion.XQueryCompletionContributor"/>
<lang.psiStructureViewFactory language="XQuery" implementationClass="org.intellij.xquery.structure.XQueryStructureViewFactory"/>
<lang.formatter language="XQuery" implementationClass="org.intellij.xquery.formatter.XQueryFormattingModelBuilder"/>
<lang.importOptimizer language="XQuery" implementationClass="org.intellij.xquery.formatter.XQueryImportOptimizer"/>
<lang.foldingBuilder language="XQuery" implementationClass="org.intellij.xquery.folding.XQueryFoldingBuilder"/>
<codeStyleSettingsProvider implementation="org.intellij.xquery.formatter.settings.XQueryCodeStyleSettingsProvider"/>

Expand Down
@@ -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");
}


}
@@ -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()
};
@@ -0,0 +1,8 @@
module namespace local = "local";

import module "used" at "file2.xq";
declare default function namespace "used";

declare function local:testOptimizeRemovesUnusedImport() {
test()
};

0 comments on commit 06b2614

Please sign in to comment.