/*
* Copyright 2005, Sixth and Red River Software
*
* 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.sixrr.metrics.utils;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
class PluginPsiUtil {
private PluginPsiUtil() {
super();
}
@Nullable
private static VirtualFile getVirtualFile(PsiElement psiElement) {
if (psiElement == null) {
return null;
}
final PsiFile containingFile = psiElement.getContainingFile();
if (containingFile == null) {
return null;
}
return containingFile.getVirtualFile();
}
public static boolean isElementInSelectedFile(Project project,
PsiElement psiElement) {
final VirtualFile elementFile = getVirtualFile(psiElement);
if (elementFile == null) {
return false;
}
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
final VirtualFile[] currentEditedFiles = fileEditorManager.getSelectedFiles();
for (final VirtualFile file : currentEditedFiles) {
if (elementFile.equals(file)) {
return true;
}
}
return false;
}
@Nullable
public static Editor getEditorIfSelected(Project project,
PsiElement psiElement) {
final VirtualFile elementFile = getVirtualFile(psiElement);
if (elementFile == null) {
return null;
}
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
final FileEditor fileEditor = fileEditorManager.getSelectedEditor(elementFile);
Editor editor = null;
if (fileEditor != null && fileEditor instanceof TextEditor) {
editor = ((TextEditor) fileEditor).getEditor();
}
return editor;
}
}