From 254f33cb657d6969ec37edc20dde08e5368af63b Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Mon, 18 May 2015 17:32:23 +0300 Subject: [PATCH] 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) --- .../JavaLanguageTestDataChecker.java | 40 ++++++++++++++++++ .../projectRoots/LanguageTestDataChecker.java | 42 +++++++++++++++++++ .../src/META-INF/LangExtensionPoints.xml | 1 + .../vcs/checkin/CheckinHandlerUtil.java | 21 ++++++---- resources/src/META-INF/IdeaPlugin.xml | 1 + 5 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 java/java-impl/src/com/intellij/openapi/projectRoots/JavaLanguageTestDataChecker.java create mode 100644 platform/lang-api/src/com/intellij/openapi/projectRoots/LanguageTestDataChecker.java diff --git a/java/java-impl/src/com/intellij/openapi/projectRoots/JavaLanguageTestDataChecker.java b/java/java-impl/src/com/intellij/openapi/projectRoots/JavaLanguageTestDataChecker.java new file mode 100644 index 000000000000..f3eafb51fbea --- /dev/null +++ b/java/java-impl/src/com/intellij/openapi/projectRoots/JavaLanguageTestDataChecker.java @@ -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); + } +} diff --git a/platform/lang-api/src/com/intellij/openapi/projectRoots/LanguageTestDataChecker.java b/platform/lang-api/src/com/intellij/openapi/projectRoots/LanguageTestDataChecker.java new file mode 100644 index 000000000000..bca5e35211ee --- /dev/null +++ b/platform/lang-api/src/com/intellij/openapi/projectRoots/LanguageTestDataChecker.java @@ -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 EP_NAME = + new ExtensionPointName("com.intellij.languageTestDataChecker"); + + @NotNull + FileType getFileType(); + + boolean isTestData(@NotNull Project project, @NotNull VirtualFile virtualFile); + +} diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index faea9a0a61ee..2aa532f8303d 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -254,6 +254,7 @@ + diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/checkin/CheckinHandlerUtil.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/checkin/CheckinHandlerUtil.java index 4a5a644d4fbe..0a7389951b29 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/checkin/CheckinHandlerUtil.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/checkin/CheckinHandlerUtil.java @@ -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) { diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index a518d3a5e771..2f1aa5b7bb32 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1621,6 +1621,7 @@ +