use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class DocumentBasedReplaceHandler method prepare.
@Override
public void prepare(ReplacementInfo info) {
assert info instanceof ReplacementInfoImpl;
MatchResult result = ((ReplacementInfoImpl) info).getMatchResult();
PsiElement element = result.getMatch();
PsiFile file = element instanceof PsiFile ? (PsiFile) element : element.getContainingFile();
Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
TextRange textRange = result.getMatch().getTextRange();
assert textRange != null;
RangeMarker rangeMarker = document.createRangeMarker(textRange);
rangeMarker.setGreedyToLeft(true);
rangeMarker.setGreedyToRight(true);
myRangeMarkers.put(info, rangeMarker);
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class GroovyCompletionUtil method addImportForClass.
public static int addImportForClass(PsiFile file, int startOffset, int endOffset, PsiClass aClass) throws IncorrectOperationException {
// LOG.assertTrue(CommandProcessor.getInstance().getCurrentCommand() != null);
// LOG.assertTrue(
// ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().getCurrentWriteAction(null) != null);
final PsiManager manager = file.getManager();
final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
int newStartOffset = startOffset;
final PsiReference reference = file.findReferenceAt(endOffset - 1);
if (reference != null) {
final PsiElement resolved = reference.resolve();
if (resolved instanceof PsiClass) {
if (((PsiClass) resolved).getQualifiedName() == null || manager.areElementsEquivalent(aClass, resolved)) {
return newStartOffset;
}
}
}
String name = aClass.getName();
document.replaceString(startOffset, endOffset, name);
final RangeMarker toDelete = JavaCompletionUtil.insertTemporary(endOffset, document, " ");
PsiDocumentManager.getInstance(manager.getProject()).commitAllDocuments();
final PsiReference ref = file.findReferenceAt(startOffset);
if (ref instanceof GrReferenceElement && aClass.isValid()) {
PsiElement newElement = ref.bindToElement(aClass);
RangeMarker marker = document.createRangeMarker(newElement.getTextRange());
CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newElement);
newStartOffset = marker.getStartOffset();
}
if (toDelete.isValid()) {
document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset());
}
return newStartOffset;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class ConstructorInsertHandler method insertParentheses.
public static boolean insertParentheses(InsertionContext context, LookupElement delegate, final PsiClass psiClass, final boolean forAnonymous) {
if (context.getCompletionChar() == '[') {
return false;
}
PsiMethod constructor = JavaConstructorCallElement.extractCalledConstructor(delegate);
final PsiElement place = context.getFile().findElementAt(context.getStartOffset());
assert place != null;
boolean hasParams = constructor != null ? constructor.getParameterList().getParametersCount() > 0 : hasConstructorParameters(psiClass, place);
RangeMarker refEnd = context.getDocument().createRangeMarker(context.getTailOffset(), context.getTailOffset());
JavaCompletionUtil.insertParentheses(context, delegate, false, hasParams, forAnonymous);
if (constructor != null) {
PsiCallExpression call = JavaMethodCallElement.findCallAtOffset(context, refEnd.getStartOffset());
if (call != null) {
CompletionMemory.registerChosenMethod(constructor, call);
}
}
return true;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class JavaCompletionUtil method insertClassReference.
public static int insertClassReference(PsiClass psiClass, PsiFile file, int startOffset, int endOffset) {
final Project project = file.getProject();
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitAllDocuments();
final PsiManager manager = file.getManager();
final Document document = FileDocumentManager.getInstance().getDocument(file.getViewProvider().getVirtualFile());
final PsiReference reference = file.findReferenceAt(startOffset);
if (reference != null) {
final PsiElement resolved = reference.resolve();
if (resolved instanceof PsiClass) {
if (((PsiClass) resolved).getQualifiedName() == null || manager.areElementsEquivalent(psiClass, resolved)) {
return endOffset;
}
}
}
String name = psiClass.getName();
if (name == null) {
return endOffset;
}
assert document != null;
document.replaceString(startOffset, endOffset, name);
int newEndOffset = startOffset + name.length();
final RangeMarker toDelete = insertTemporary(newEndOffset, document, " ");
documentManager.commitAllDocuments();
PsiElement element = file.findElementAt(startOffset);
if (element instanceof PsiIdentifier) {
PsiElement parent = element.getParent();
if (parent instanceof PsiJavaCodeReferenceElement && !((PsiJavaCodeReferenceElement) parent).isQualified() && !(parent.getParent() instanceof PsiPackageStatement)) {
PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement) parent;
if (psiClass.isValid() && !psiClass.getManager().areElementsEquivalent(psiClass, resolveReference(ref))) {
final boolean staticImport = ref instanceof PsiImportStaticReferenceElement;
PsiElement newElement;
try {
newElement = staticImport ? ((PsiImportStaticReferenceElement) ref).bindToTargetClass(psiClass) : ref.bindToElement(psiClass);
} catch (IncorrectOperationException e) {
// can happen if fqn contains reserved words, for example
return endOffset;
}
final RangeMarker rangeMarker = document.createRangeMarker(newElement.getTextRange());
documentManager.doPostponedOperationsAndUnblockDocument(document);
documentManager.commitDocument(document);
newElement = CodeInsightUtilCore.findElementInRange(file, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), PsiJavaCodeReferenceElement.class, JavaLanguage.INSTANCE);
rangeMarker.dispose();
if (newElement != null) {
newEndOffset = newElement.getTextRange().getEndOffset();
if (!(newElement instanceof PsiReferenceExpression)) {
PsiReferenceParameterList parameterList = ((PsiJavaCodeReferenceElement) newElement).getParameterList();
if (parameterList != null) {
newEndOffset = parameterList.getTextRange().getStartOffset();
}
}
if (!staticImport && !psiClass.getManager().areElementsEquivalent(psiClass, resolveReference((PsiReference) newElement)) && !PsiUtil.isInnerClass(psiClass)) {
final String qName = psiClass.getQualifiedName();
if (qName != null) {
document.replaceString(newElement.getTextRange().getStartOffset(), newEndOffset, qName);
newEndOffset = newElement.getTextRange().getStartOffset() + qName.length();
}
}
}
}
}
}
if (toDelete.isValid()) {
document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset());
}
return newEndOffset;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class ExtractMethodObjectHandler method run.
public static void run(@NotNull final Project project, final Editor editor, @NotNull final ExtractMethodObjectProcessor processor, @NotNull final ExtractMethodObjectProcessor.MyExtractMethodProcessor extractProcessor) {
final RangeMarker marker;
if (editor != null) {
final int offset = editor.getCaretModel().getOffset();
marker = editor.getDocument().createRangeMarker(new TextRange(offset, offset));
} else {
marker = null;
}
CommandProcessor.getInstance().executeCommand(project, () -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> {
try {
ApplicationManager.getApplication().runWriteAction(() -> extractProcessor.doRefactoring());
processor.run();
ApplicationManager.getApplication().runWriteAction(() -> processor.runChangeSignature());
} catch (IncorrectOperationException e) {
LOG.error(e);
}
PsiDocumentManager.getInstance(project).commitAllDocuments();
if (processor.isCreateInnerClass()) {
processor.moveUsedMethodsToInner();
PsiDocumentManager.getInstance(project).commitAllDocuments();
if (editor != null) {
DuplicatesImpl.processDuplicates(extractProcessor, project, editor);
}
}
ApplicationManager.getApplication().runWriteAction(() -> {
if (processor.isCreateInnerClass()) {
processor.changeInstanceAccess(project);
}
final PsiElement method = processor.getMethod();
LOG.assertTrue(method != null);
method.delete();
});
}), ExtractMethodObjectProcessor.REFACTORING_NAME, ExtractMethodObjectProcessor.REFACTORING_NAME);
if (editor != null) {
editor.getCaretModel().moveToOffset(marker.getStartOffset());
marker.dispose();
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
}
Aggregations