use of com.intellij.openapi.fileEditor.FileEditorManager in project intellij-community by JetBrains.
the class AddAllOpenFilesToFavorites method getFilesToAdd.
static ArrayList<PsiFile> getFilesToAdd(Project project) {
ArrayList<PsiFile> result = new ArrayList<>();
final FileEditorManager editorManager = FileEditorManager.getInstance(project);
final PsiManager psiManager = PsiManager.getInstance(project);
for (VirtualFile openFile : editorManager.getOpenFiles()) {
if (!openFile.isValid())
continue;
final PsiFile psiFile = psiManager.findFile(openFile);
if (psiFile != null) {
result.add(psiFile);
}
}
return result;
}
use of com.intellij.openapi.fileEditor.FileEditorManager in project intellij-community by JetBrains.
the class DvcsUtil method getSelectedFile.
/**
* Returns the currently selected file, based on which VcsBranch or StatusBar components will identify the current repository root.
*/
@Nullable
@CalledInAwt
public static VirtualFile getSelectedFile(@NotNull Project project) {
StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
final FileEditor fileEditor = StatusBarUtil.getCurrentFileEditor(project, statusBar);
VirtualFile result = null;
if (fileEditor != null) {
if (fileEditor instanceof TextEditor) {
Document document = ((TextEditor) fileEditor).getEditor().getDocument();
result = FileDocumentManager.getInstance().getFile(document);
} else if (fileEditor instanceof ImageFileEditor) {
result = ((ImageFileEditor) fileEditor).getImageEditor().getFile();
}
}
if (result == null) {
final FileEditorManager manager = FileEditorManager.getInstance(project);
if (manager != null) {
Editor editor = manager.getSelectedTextEditor();
if (editor != null) {
result = FileDocumentManager.getInstance().getFile(editor.getDocument());
}
}
}
return result;
}
use of com.intellij.openapi.fileEditor.FileEditorManager in project intellij-community by JetBrains.
the class OverrideImplementUtil method overrideOrImplement.
public static List<PsiGenerationInfo<PsiMethod>> overrideOrImplement(PsiClass psiClass, @NotNull PsiMethod baseMethod) throws IncorrectOperationException {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(baseMethod.getProject());
List<PsiGenerationInfo<PsiMethod>> results = new ArrayList<>();
try {
List<PsiGenerationInfo<PsiMethod>> prototypes = convert2GenerationInfos(overrideOrImplementMethod(psiClass, baseMethod, false));
if (prototypes.isEmpty())
return null;
PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(baseMethod.getContainingClass(), psiClass, PsiSubstitutor.EMPTY);
PsiElement anchor = getDefaultAnchorToOverrideOrImplement(psiClass, baseMethod, substitutor);
results = GenerateMembersUtil.insertMembersBeforeAnchor(psiClass, anchor, prototypes);
return results;
} finally {
PsiFile psiFile = psiClass.getContainingFile();
Editor editor = fileEditorManager.openTextEditor(new OpenFileDescriptor(psiFile.getProject(), psiFile.getVirtualFile()), true);
if (editor != null && !results.isEmpty()) {
results.get(0).positionCaret(editor, true);
editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
}
}
}
use of com.intellij.openapi.fileEditor.FileEditorManager in project intellij-community by JetBrains.
the class CopyAbstractMethodImplementationHandler method copyImplementation.
private void copyImplementation(final PsiMethod sourceMethod) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(sourceMethod))
return;
final List<PsiMethod> generatedMethods = new ArrayList<>();
new WriteCommandAction(myProject, getTargetFiles()) {
@Override
protected void run(@NotNull final Result result) throws Throwable {
for (PsiEnumConstant enumConstant : myTargetEnumConstants) {
PsiClass initializingClass = enumConstant.getOrCreateInitializingClass();
myTargetClasses.add(initializingClass);
}
for (PsiClass psiClass : myTargetClasses) {
final Collection<PsiMethod> methods = OverrideImplementUtil.overrideOrImplementMethod(psiClass, myMethod, true);
final Iterator<PsiMethod> iterator = methods.iterator();
if (!iterator.hasNext())
continue;
PsiMethod overriddenMethod = iterator.next();
final PsiCodeBlock body = overriddenMethod.getBody();
final PsiCodeBlock sourceBody = sourceMethod.getBody();
assert body != null && sourceBody != null;
ChangeContextUtil.encodeContextInfo(sourceBody, true);
final PsiElement newBody = body.replace(sourceBody.copy());
ChangeContextUtil.decodeContextInfo(newBody, psiClass, null);
PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(mySourceClass, psiClass, PsiSubstitutor.EMPTY);
PsiElement anchor = OverrideImplementUtil.getDefaultAnchorToOverrideOrImplement(psiClass, sourceMethod, substitutor);
try {
if (anchor != null) {
overriddenMethod = (PsiMethod) anchor.getParent().addBefore(overriddenMethod, anchor);
} else {
overriddenMethod = (PsiMethod) psiClass.addBefore(overriddenMethod, psiClass.getRBrace());
}
generatedMethods.add(overriddenMethod);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
}.execute();
if (!generatedMethods.isEmpty()) {
PsiMethod target = generatedMethods.get(0);
PsiFile psiFile = target.getContainingFile();
FileEditorManager fileEditorManager = FileEditorManager.getInstance(psiFile.getProject());
Editor editor = fileEditorManager.openTextEditor(new OpenFileDescriptor(psiFile.getProject(), psiFile.getVirtualFile()), false);
if (editor != null) {
GenerateMembersUtil.positionCaret(editor, target, true);
editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
}
}
}
use of com.intellij.openapi.fileEditor.FileEditorManager in project intellij-community by JetBrains.
the class CodeInsightTestCase method createEditor.
protected Editor createEditor(@NotNull VirtualFile file) {
final FileEditorManager instance = FileEditorManager.getInstance(myProject);
if (file.getFileType().isBinary())
return null;
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
Editor editor = instance.openTextEditor(new OpenFileDescriptor(myProject, file, 0), false);
((EditorImpl) editor).setCaretActive();
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
DaemonCodeAnalyzer.getInstance(getProject()).restart();
return editor;
}
Aggregations