From 609c4b3b28904c05c17afc9df31f0f2e17d5f4a8 Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Wed, 7 Aug 2013 13:12:05 +0400 Subject: [PATCH] Add notification panel for kotlin files that aren't in 'kotlin' folder (for gradle-android project) --- idea/src/META-INF/plugin.xml | 1 + .../IncorrectSourceRootNameNotification.java | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/IncorrectSourceRootNameNotification.java diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index f03b1b0af04..c242df0734f 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -271,6 +271,7 @@ + diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/IncorrectSourceRootNameNotification.java b/idea/src/org/jetbrains/jet/plugin/quickfix/IncorrectSourceRootNameNotification.java new file mode 100644 index 00000000000..93a089fd2b2 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/IncorrectSourceRootNameNotification.java @@ -0,0 +1,99 @@ +/* + * Copyright 2010-2013 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 org.jetbrains.jet.plugin.quickfix; + +import com.intellij.ide.actions.OpenFileAction; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.compiler.CompilerManager; +import com.intellij.openapi.fileEditor.FileEditor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.util.Key; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.ui.EditorNotificationPanel; +import com.intellij.ui.EditorNotifications; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.plugin.JetFileType; +import org.jetbrains.jet.plugin.JetPluginUtil; + +import java.io.IOException; +import java.util.StringTokenizer; + +public class IncorrectSourceRootNameNotification extends EditorNotifications.Provider { + private static final Key KEY = Key.create("move.kotlin.file.under.kotlin.source.root"); + + private final Project project; + + public IncorrectSourceRootNameNotification(Project project) { + this.project = project; + } + + @Override + @Nullable + public EditorNotificationPanel createNotificationPanel(@NotNull final VirtualFile file, @NotNull FileEditor fileEditor) { + if (file.getFileType() != JetFileType.INSTANCE) return null; + if (CompilerManager.getInstance(project).isExcludedFromCompilation(file)) return null; + + if (!JetPluginUtil.isKtFileInGradleProjectInWrongFolder(file, project)) { + return null; + } + + EditorNotificationPanel panel = new EditorNotificationPanel(); + panel.setText("Kotlin file in Gradle Project should be under source root with name 'kotlin'"); + for (final VirtualFile root : ProjectRootManager.getInstance(project).getContentSourceRoots()) { + if (root.getName().equals("kotlin")) { + VirtualFile sourceRootForFile = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(file); + assert sourceRootForFile != null : "Notification Panel should appear only for files under source root " + + file.getCanonicalPath(); + final String relativePath = VfsUtilCore.getRelativePath(file, sourceRootForFile, '/'); + if (relativePath != null) { + VirtualFile inKotlinDir = VfsUtil.findRelativeFile(root, relativePath.split("/")); + if (inKotlinDir == null) { + panel.createActionLabel("Move file", new Runnable() { + @Override + public void run() { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + try { + VirtualFile newFile = VfsUtil. + copyFileRelative(IncorrectSourceRootNameNotification.class, file, root, relativePath); + file.delete(IncorrectSourceRootNameNotification.class); + OpenFileAction.openFile(newFile, project); + } + catch (IOException ignored) { + } + } + }); + } + }); + break; + } + } + } + } + return panel; + } + + @Override + public Key getKey() { + return KEY; + } +}