Provided extension point for detecting if source file is test data file. On this "test data" files during "before commit" actions stage no "Reformat, Rearrange and Optimize imports" actions will be performed (part of IDEA-140314)

This commit is contained in:
Yaroslav Lepenkin
2015-05-18 18:04:53 +03:00
parent 1bd313a730
commit 254f33cb65
5 changed files with 97 additions and 8 deletions
@@ -0,0 +1,40 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* 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.intellij.openapi.projectRoots;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
public class JavaLanguageTestDataChecker implements LanguageTestDataChecker {
@Override
@NotNull
public FileType getFileType() {
return StdFileTypes.JAVA;
}
@Override
public boolean isTestData(@NotNull Project project, @NotNull VirtualFile virtualFile) {
ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
return !index.isUnderSourceRootOfType(virtualFile, JavaModuleSourceRootTypes.SOURCES);
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* 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.intellij.openapi.projectRoots;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
/**
* Files for which #isTestData returns true won't be processed by
* Optimize Imports, Reformat Code and Rearrange Code actions during before-commit stage.
*
* When developing language plugin it is improperly to perform any modification actions
* for language test data files (files that looks like source code, but are used only as test data)
* on commit, even if appropriate checkboxes in commit dialog are turned on.
*/
public interface LanguageTestDataChecker {
ExtensionPointName<LanguageTestDataChecker> EP_NAME =
new ExtensionPointName<LanguageTestDataChecker>("com.intellij.languageTestDataChecker");
@NotNull
FileType getFileType();
boolean isTestData(@NotNull Project project, @NotNull VirtualFile virtualFile);
}
@@ -254,6 +254,7 @@
<extensionPoint name="useScopeOptimizer" interface="com.intellij.psi.search.UseScopeOptimizer"/>
<extensionPoint name="generatedSourcesFilter" interface="com.intellij.openapi.roots.GeneratedSourcesFilter"/>
<extensionPoint name="languageTestDataChecker" interface="com.intellij.openapi.projectRoots.LanguageTestDataChecker"/>
<extensionPoint name="gotoClassContributor"
interface="com.intellij.navigation.ChooseByNameContributor"/>
@@ -16,10 +16,10 @@
package com.intellij.openapi.vcs.checkin;
import com.intellij.openapi.components.StorageScheme;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ex.ProjectEx;
import com.intellij.openapi.projectRoots.LanguageTestDataChecker;
import com.intellij.openapi.roots.GeneratedSourcesFilter;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
@@ -30,7 +30,6 @@ import com.intellij.psi.PsiManager;
import com.intellij.psi.util.PsiUtilCore;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.util.ArrayList;
@@ -67,7 +66,8 @@ public class CheckinHandlerUtil {
for (VirtualFile file : selectedFiles) {
if (file.isValid()) {
if (isUnderProjectFileDir(projectFileDir, file) || !isFileUnderSourceRoot(project, file)) {
if (isUnderProjectFileDir(projectFileDir, file) || !isFileUnderSourceRoot(project, file)
|| isLanguageTestData(project, file)) {
continue;
}
PsiFile psiFile = psiManager.findFile(file);
@@ -83,12 +83,17 @@ public class CheckinHandlerUtil {
private static boolean isFileUnderSourceRoot(@NotNull Project project, @NotNull VirtualFile file) {
ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
if (StdFileTypes.JAVA == file.getFileType()) {
return index.isUnderSourceRootOfType(file, JavaModuleSourceRootTypes.SOURCES) && !index.isInLibrarySource(file);
}
else {
return index.isInContent(file) && !index.isInLibrarySource(file) ;
return index.isInContent(file) && !index.isInLibrarySource(file);
}
private static boolean isLanguageTestData(@NotNull Project project, @NotNull VirtualFile file) {
for (LanguageTestDataChecker checker : LanguageTestDataChecker.EP_NAME.getExtensions()) {
if (checker.getFileType() == file.getFileType()
&& checker.isTestData(project, file)) {
return true;
}
}
return false;
}
static void disableWhenDumb(@NotNull Project project, @NotNull JCheckBox checkBox, @NotNull String tooltip) {
+1
View File
@@ -1621,6 +1621,7 @@
<hierarchy.referenceProcessor implementation="com.intellij.ide.hierarchy.call.JavaCallReferenceProcessor"/>
<projectTemplate projectType="JAVA_MODULE" templatePath="resources/projectTemplates/Java/Command_Line_App.zip"/>
<generatedSourcesFilter implementation="com.intellij.openapi.roots.JavaGeneratedSourcesFilter"/>
<languageTestDataChecker implementation="com.intellij.openapi.projectRoots.JavaLanguageTestDataChecker"/>
<refactoring.elementListenerProvider implementation="com.intellij.codeInspection.ex.AdditionalAnnotationsRefactoringElementListener"/>
<projectOpenProcessor implementation="com.intellij.platform.PlatformProjectOpenProcessor" order="last"/>
<projectService serviceInterface="com.intellij.platform.ProjectBaseDirectory"