use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class GroovyMethodInliner method getCurrentEditorIfApplicable.
@Nullable
private static Editor getCurrentEditorIfApplicable(@NotNull PsiElement element) {
final Project project = element.getProject();
final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor != null && editor.getDocument() == PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile())) {
return editor;
}
return null;
}
use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class FormClassAnnotator method annotateFormField.
private static void annotateFormField(final PsiField field, final PsiFile boundForm, final AnnotationHolder holder) {
Annotation boundFieldAnnotation = holder.createInfoAnnotation(field, null);
boundFieldAnnotation.setGutterIconRenderer(new BoundIconRenderer(field));
LOG.assertTrue(boundForm instanceof PsiPlainTextFile);
final PsiType guiComponentType = FormReferenceProvider.getGUIComponentType((PsiPlainTextFile) boundForm, field.getName());
if (guiComponentType != null) {
final PsiType fieldType = field.getType();
if (!fieldType.isAssignableFrom(guiComponentType)) {
String message = UIDesignerBundle.message("bound.field.type.mismatch", guiComponentType.getCanonicalText(), fieldType.getCanonicalText());
Annotation annotation = holder.createErrorAnnotation(field.getTypeElement(), message);
annotation.registerFix(new ChangeFormComponentTypeFix((PsiPlainTextFile) boundForm, field.getName(), field.getType()), null, null);
annotation.registerFix(new ChangeBoundFieldTypeFix(field, guiComponentType), null, null);
}
}
if (field.hasInitializer()) {
final String message = UIDesignerBundle.message("field.is.overwritten.by.generated.code", field.getName());
Annotation annotation = holder.createWarningAnnotation(field.getInitializer(), message);
annotation.registerFix(new IntentionAction() {
@Override
@NotNull
public String getText() {
return message;
}
@Override
@NotNull
public String getFamilyName() {
return UIBundle.message("remove.field.initializer.quick.fix");
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return field.getInitializer() != null;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final PsiExpression initializer = field.getInitializer();
LOG.assertTrue(initializer != null);
initializer.delete();
}
@Override
public boolean startInWriteAction() {
return true;
}
});
}
}
use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class GenerateMainAction method actionPerformed.
public void actionPerformed(AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
assert project != null;
final Editor editor = e.getData(CommonDataKeys.EDITOR);
assert editor != null;
final int offset = editor.getCaretModel().getOffset();
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
PsiClass psiClass = PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiClass.class);
assert psiClass != null;
if (!PsiUtil.hasDefaultConstructor(psiClass)) {
Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.default.constructor"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
return;
}
final List<PsiFile> boundForms = FormClassIndex.findFormsBoundToClass(project, psiClass.getQualifiedName());
final LwRootContainer rootContainer;
try {
rootContainer = Utils.getRootContainer(boundForms.get(0).getText(), null);
} catch (AlienFormFileException ex) {
Messages.showMessageDialog(project, "The form bound to the class is not a valid IntelliJ IDEA form", UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
return;
} catch (Exception ex) {
LOG.error(ex);
return;
}
if (rootContainer.getComponentCount() == 0) {
Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.empty.form"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
return;
}
String rootBinding = rootContainer.getComponent(0).getBinding();
if (rootBinding == null || psiClass.findFieldByName(rootBinding, true) == null) {
Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.root.binding"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
return;
}
@NonNls final StringBuilder mainBuilder = new StringBuilder("public static void main(String[] args) { ");
final JavaCodeStyleManager csm = JavaCodeStyleManager.getInstance(project);
SuggestedNameInfo nameInfo = csm.suggestVariableName(VariableKind.LOCAL_VARIABLE, "frame", null, null);
String varName = nameInfo.names[0];
mainBuilder.append(JFrame.class.getName()).append(" ").append(varName).append("= new ").append(JFrame.class.getName());
mainBuilder.append("(\"").append(psiClass.getName()).append("\");");
mainBuilder.append(varName).append(".setContentPane(new ").append(psiClass.getQualifiedName()).append("().").append(rootBinding).append(");");
mainBuilder.append(varName).append(".setDefaultCloseOperation(").append(JFrame.class.getName()).append(".EXIT_ON_CLOSE);");
mainBuilder.append(varName).append(".pack();");
mainBuilder.append(varName).append(".setVisible(true);");
mainBuilder.append("}\n");
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
try {
PsiMethod method = JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createMethodFromText(mainBuilder.toString(), file);
List<PsiGenerationInfo<PsiMethod>> infos = Collections.singletonList(new PsiGenerationInfo<>(method));
List<PsiGenerationInfo<PsiMethod>> resultMembers = GenerateMembersUtil.insertMembersAtOffset(file, offset, infos);
resultMembers.get(0).positionCaret(editor, false);
} catch (IncorrectOperationException e1) {
LOG.error(e1);
}
}), null, null);
}
use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class StudyRefreshTaskFileAction method refreshFile.
private static void refreshFile(@NotNull final StudyState studyState, @NotNull final Project project) {
final Editor editor = studyState.getEditor();
final TaskFile taskFile = studyState.getTaskFile();
final Task task = taskFile.getTask();
if (task.hasSubtasks()) {
for (AnswerPlaceholder placeholder : taskFile.getActivePlaceholders()) {
StudySubtaskUtils.refreshPlaceholder(editor, placeholder);
}
} else {
if (!resetTaskFile(editor.getDocument(), project, taskFile, studyState.getVirtualFile().getName())) {
Messages.showInfoMessage("The initial text of task file is unavailable", "Failed to Refresh Task File");
return;
}
if (task.isChoiceTask()) {
final StudyToolWindow window = StudyUtils.getStudyToolWindow(project);
if (window != null) {
window.setBottomComponent(new StudyChoiceVariantsPanel(task));
}
}
}
WolfTheProblemSolver.getInstance(project).clearProblems(studyState.getVirtualFile());
taskFile.setHighlightErrors(false);
StudyUtils.drawAllAnswerPlaceholders(editor, taskFile);
EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile);
ApplicationManager.getApplication().invokeLater(() -> IdeFocusManager.getInstance(project).requestFocus(editor.getContentComponent(), true));
StudyNavigator.navigateToFirstAnswerPlaceholder(editor, taskFile);
showBalloon(project, "You can start again now", MessageType.INFO);
}
use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class StudyRunAction method run.
public void run(@NotNull final Project project) {
if (myHandler != null && !myHandler.isProcessTerminated())
return;
Editor selectedEditor = StudyUtils.getSelectedEditor(project);
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
assert selectedEditor != null;
VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
if (openedFile != null && openedFile.getCanonicalPath() != null) {
String filePath = openedFile.getCanonicalPath();
executeFile(project, openedFile, filePath);
}
}
Aggregations