use of com.intellij.openapi.fileEditor.OpenFileDescriptor 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.OpenFileDescriptor 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;
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.
the class CreateFromUsageBaseFix method positionCursor.
@Nullable("null means unable to open the editor")
protected static Editor positionCursor(@NotNull Project project, @NotNull PsiFile targetFile, @NotNull PsiElement element) {
TextRange range = element.getTextRange();
LOG.assertTrue(range != null, element.getClass());
int textOffset = range.getStartOffset();
VirtualFile file = targetFile.getVirtualFile();
if (file == null) {
file = PsiUtilCore.getVirtualFile(element);
if (file == null)
return null;
}
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, textOffset);
// avoid centering caret in editor if it's already visible
descriptor.setScrollType(ScrollType.MAKE_VISIBLE);
return FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.
the class OpenRepositoryVersionAction method openRepositoryVersion.
private static void openRepositoryVersion(@NotNull Project project, @NotNull Change[] changes) {
for (Change change : changes) {
ContentRevision revision = change.getAfterRevision();
if (revision == null || revision.getFile().isDirectory())
continue;
VirtualFile vFile = ContentRevisionVirtualFile.create(revision);
Navigatable navigatable = new OpenFileDescriptor(project, vFile);
navigatable.navigate(true);
}
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.
the class CommandLineProcessor method doOpenFile.
@Nullable
private static Project doOpenFile(VirtualFile file, int line, boolean tempProject) {
Project[] projects = ProjectManager.getInstance().getOpenProjects();
if (projects.length == 0 || tempProject) {
PlatformProjectOpenProcessor processor = PlatformProjectOpenProcessor.getInstanceIfItExists();
if (processor != null) {
EnumSet<PlatformProjectOpenProcessor.Option> options = EnumSet.noneOf(PlatformProjectOpenProcessor.Option.class);
if (tempProject) {
options.add(PlatformProjectOpenProcessor.Option.TEMP_PROJECT);
options.add(PlatformProjectOpenProcessor.Option.FORCE_NEW_FRAME);
}
return PlatformProjectOpenProcessor.doOpenProject(file, null, line, null, options);
}
Messages.showErrorDialog("No project found to open file in", "Cannot Open File");
return null;
} else {
NonProjectFileWritingAccessProvider.allowWriting(file);
Project project = findBestProject(file, projects);
OpenFileDescriptor descriptor = line == -1 ? new OpenFileDescriptor(project, file) : new OpenFileDescriptor(project, file, line - 1, 0);
descriptor.navigate(true);
return project;
}
}
Aggregations