use of com.intellij.util.IncorrectOperationException in project Main by SpartanRefactoring.
the class SpartanizerAnnotator method annotate.
@Override
public void annotate(@NotNull final PsiElement e, @NotNull AnnotationHolder h) {
try {
if (!Spartanizer.canTip(e) || e.getContainingFile().getName().contains("Spartanizer"))
return;
Annotation annotation = h.createInfoAnnotation(e, "Spartanize This!");
annotation.registerFix(new IntentionAction() {
@SuppressWarnings("unchecked")
@Nls
@NotNull
@Override
public String getText() {
return Toolbox.getInstance().getTipper(e).description(e);
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return "SpartanizerAction";
}
@Override
public boolean isAvailable(@NotNull Project __, Editor e, PsiFile f) {
return true;
}
@Override
public void invoke(@NotNull Project p, Editor ed, PsiFile f) throws IncorrectOperationException {
Spartanizer.spartanizeElement(e);
}
@Override
public boolean startInWriteAction() {
return false;
}
});
TextAttributesKey.createTextAttributesKey("");
annotation.setEnforcedTextAttributes((new TextAttributes(null, null, JBColor.BLUE, EffectType.WAVE_UNDERSCORE, 0)));
} catch (Throwable t) {
Logger l = new Logger(this.getClass());
l.error("", t);
}
}
use of com.intellij.util.IncorrectOperationException in project kotlin by JetBrains.
the class AbstractFormatterTest method doTextTest.
public void doTextTest(final Action action, final String text, File fileAfter, String extension) throws IncorrectOperationException {
final PsiFile file = createFile("A" + extension, text);
if (myLineRange != null) {
DocumentImpl document = new DocumentImpl(text);
myTextRange = new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset()));
}
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file);
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
document.replaceString(0, document.getTextLength(), text);
manager.commitDocument(document);
try {
TextRange rangeToUse = myTextRange;
if (rangeToUse == null) {
rangeToUse = file.getTextRange();
}
ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
} catch (IncorrectOperationException e) {
assertTrue(e.getLocalizedMessage(), false);
}
}
});
}
}, "", "");
if (document == null) {
fail("Don't expect the document to be null");
return;
}
KotlinTestUtils.assertEqualsToFile(fileAfter, document.getText());
manager.commitDocument(document);
KotlinTestUtils.assertEqualsToFile(fileAfter, file.getText());
}
use of com.intellij.util.IncorrectOperationException in project kotlin by JetBrains.
the class MoveKotlinTopLevelDeclarationsDialog method doAction.
@Override
protected void doAction() {
KotlinMoveTarget target = selectMoveTarget();
if (target == null)
return;
saveRefactoringSettings();
List<KtNamedDeclaration> elementsToMove = getSelectedElementsToMove();
List<KtFile> sourceFiles = getSourceFiles(elementsToMove);
final PsiDirectory sourceDirectory = getSourceDirectory(sourceFiles);
for (PsiElement element : elementsToMove) {
String message = target.verify(element.getContainingFile());
if (message != null) {
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), message, null, myProject);
return;
}
}
try {
boolean deleteSourceFile = false;
if (isFullFileMove()) {
if (isMoveToPackage()) {
Pair<VirtualFile, ? extends MoveDestination> sourceRootWithMoveDestination = selectPackageBasedTargetDirAndDestination(false);
//noinspection ConstantConditions
final MoveDestination moveDestination = sourceRootWithMoveDestination.getSecond();
PsiDirectory targetDir = moveDestination.getTargetIfExists(sourceDirectory);
String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
List<PsiFile> filesExistingInTargetDir = getFilesExistingInTargetDir(sourceFiles, targetFileName, targetDir);
if (filesExistingInTargetDir.isEmpty()) {
PsiDirectory targetDirectory = ApplicationUtilsKt.runWriteAction(new Function0<PsiDirectory>() {
@Override
public PsiDirectory invoke() {
return moveDestination.getTargetDirectory(sourceDirectory);
}
});
for (KtFile sourceFile : sourceFiles) {
MoveUtilsKt.setUpdatePackageDirective(sourceFile, cbUpdatePackageDirective.isSelected());
}
invokeRefactoring(new MoveFilesWithDeclarationsProcessor(myProject, sourceFiles, targetDirectory, targetFileName, isSearchInComments(), isSearchInNonJavaFiles(), moveCallback));
return;
}
}
int ret = Messages.showYesNoCancelDialog(myProject, "You are about to move all declarations out of the source file(s). Do you want to delete empty files?", RefactoringBundle.message("move.title"), Messages.getQuestionIcon());
if (ret == Messages.CANCEL)
return;
deleteSourceFile = ret == Messages.YES;
}
MoveDeclarationsDescriptor options = new MoveDeclarationsDescriptor(elementsToMove, target, MoveDeclarationsDelegate.TopLevel.INSTANCE, isSearchInComments(), isSearchInNonJavaFiles(), true, deleteSourceFile, moveCallback, false);
invokeRefactoring(new MoveKotlinDeclarationsProcessor(myProject, options, Mover.Default.INSTANCE));
} catch (IncorrectOperationException e) {
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), null, myProject);
}
}
use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.
the class DomRootInvocationHandler method setEmptyXmlTag.
@Override
protected XmlTag setEmptyXmlTag() {
final XmlTag[] result = new XmlTag[] { null };
getManager().runChange(() -> {
try {
final String namespace = getXmlElementNamespace();
@NonNls final String nsDecl = StringUtil.isEmpty(namespace) ? "" : " xmlns=\"" + namespace + "\"";
final XmlFile xmlFile = getFile();
final XmlTag tag = XmlElementFactory.getInstance(xmlFile.getProject()).createTagFromText("<" + getXmlElementName() + nsDecl + "/>");
result[0] = ((XmlDocument) xmlFile.getDocument().replace(((XmlFile) tag.getContainingFile()).getDocument())).getRootTag();
} catch (IncorrectOperationException e) {
LOG.error(e);
}
});
return result[0];
}
use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.
the class IndexedElementInvocationHandler method setEmptyXmlTag.
@Override
protected XmlTag setEmptyXmlTag() {
final DomInvocationHandler parent = getParentHandler();
assert parent != null : "write operations should be performed on the DOM having a parent, your DOM may be not very fresh";
final FixedChildDescriptionImpl description = getChildDescription();
final XmlFile xmlFile = getFile();
parent.createFixedChildrenTags(getXmlName(), description, myIndex);
final List<XmlTag> tags = DomImplUtil.findSubTags(parent.getXmlTag(), getXmlName(), xmlFile);
if (tags.size() > myIndex) {
return tags.get(myIndex);
}
final XmlTag[] newTag = new XmlTag[1];
getManager().runChange(() -> {
try {
final XmlTag parentTag = parent.getXmlTag();
newTag[0] = (XmlTag) parentTag.add(parent.createChildTag(getXmlName()));
} catch (IncorrectOperationException e) {
LOG.error(e);
}
});
return newTag[0];
}
Aggregations