Search in sources :

Example 71 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class IntroduceParameterHandler method introduceWrappedCodeBlockParameter.

private void introduceWrappedCodeBlockParameter(PsiMethod methodToIntroduceParameter, PsiMethod methodToSearchFor, Editor editor, final Project project, final PsiType selectedType, final MyExtractMethodProcessor processor, final PsiElement[] elements) {
    final PsiElement commonParent = findCommonParent(elements);
    if (commonParent == null) {
        LOG.error("Should have common parent:" + Arrays.toString(elements));
        return;
    }
    final RangeMarker marker = editor.getDocument().createRangeMarker(commonParent.getTextRange());
    final PsiElement[] copyElements = processor.getElements();
    final PsiElement containerCopy = findCommonParent(copyElements);
    if (containerCopy == null) {
        LOG.error("Should have common parent:" + Arrays.toString(copyElements));
        return;
    }
    final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(selectedType);
    final PsiClass wrapperClass = resolveResult.getElement();
    LOG.assertTrue(wrapperClass != null);
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
    final Ref<String> suffixText = new Ref<>();
    final Ref<String> prefixText = new Ref<>();
    final Ref<String> methodText = new Ref<>();
    WriteCommandAction.runWriteCommandAction(project, () -> {
        final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(wrapperClass);
        LOG.assertTrue(method != null);
        final String interfaceMethodName = method.getName();
        processor.setMethodName(interfaceMethodName);
        if (copyElements.length == 1 && copyElements[0].getUserData(ElementToWorkOn.PARENT) == null) {
            copyElements[0].putUserData(ElementToWorkOn.REPLACE_NON_PHYSICAL, true);
        }
        processor.doExtract();
        final PsiMethod extractedMethod = processor.getExtractedMethod();
        final PsiParameter[] parameters = extractedMethod.getParameterList().getParameters();
        final PsiParameter[] interfaceParameters = method.getParameterList().getParameters();
        final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
        for (int i = 0; i < interfaceParameters.length; i++) {
            final PsiTypeElement typeAfterInterface = factory.createTypeElement(substitutor.substitute(interfaceParameters[i].getType()));
            final PsiTypeElement typeElement = parameters[i].getTypeElement();
            if (typeElement != null) {
                typeElement.replace(typeAfterInterface);
            }
        }
        methodText.set(extractedMethod.getText());
        final PsiMethodCallExpression methodCall = processor.getMethodCall();
        prefixText.set(containerCopy.getText().substring(0, methodCall.getTextRange().getStartOffset() - containerCopy.getTextRange().getStartOffset()));
        suffixText.set("." + methodCall.getText() + containerCopy.getText().substring(methodCall.getTextRange().getEndOffset() - containerCopy.getTextRange().getStartOffset()));
    });
    PsiExpression expression = factory.createExpressionFromText("new " + selectedType.getCanonicalText() + "() {" + methodText.get() + "}", elements[0]);
    expression = (PsiExpression) JavaCodeStyleManager.getInstance(project).shortenClassReferences(expression);
    expression.putUserData(ElementToWorkOn.PARENT, commonParent);
    expression.putUserData(ElementToWorkOn.PREFIX, prefixText.get());
    expression.putUserData(ElementToWorkOn.SUFFIX, suffixText.get());
    expression.putUserData(ElementToWorkOn.TEXT_RANGE, marker);
    expression.putUserData(ElementToWorkOn.EXPR_RANGE, elements.length == 1 ? elements[0].getTextRange() : null);
    new Introducer(project, expression, null, editor).introduceParameter(methodToIntroduceParameter, methodToSearchFor);
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) Ref(com.intellij.openapi.util.Ref) AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer)

Example 72 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class OffsetMap method saveOffset.

private void saveOffset(OffsetKey key, int offset, boolean externally) {
    assert !myDisposed;
    if (externally && myMap.containsKey(key)) {
        myModified.add(key);
    }
    RangeMarker old = myMap.get(key);
    if (old != null)
        old.dispose();
    final RangeMarker marker = myDocument.createRangeMarker(offset, offset);
    marker.setGreedyToRight(key.isMoveableToRight());
    myMap.put(key, marker);
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 73 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class OffsetMap method removeOffset.

public void removeOffset(OffsetKey key) {
    synchronized (myMap) {
        ProgressManager.checkCanceled();
        assert !myDisposed;
        myModified.add(key);
        RangeMarker old = myMap.get(key);
        if (old != null)
            old.dispose();
        myMap.remove(key);
    }
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 74 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class DocumentFoldingInfo method readExternal.

@Override
public void readExternal(final Element element) {
    ApplicationManager.getApplication().runReadAction(() -> {
        clear();
        if (!myFile.isValid())
            return;
        final Document document = FileDocumentManager.getInstance().getDocument(myFile);
        if (document == null)
            return;
        PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getCachedPsiFile(document);
        String date = null;
        boolean canRestoreElement = psiFile != null && (!DumbService.getInstance(myProject).isDumb() || FoldingUpdate.supportsDumbModeFolding(psiFile));
        for (final Object o : element.getChildren()) {
            Element e = (Element) o;
            Boolean expanded = Boolean.valueOf(e.getAttributeValue(EXPANDED_ATT));
            if (ELEMENT_TAG.equals(e.getName())) {
                String signature = e.getAttributeValue(SIGNATURE_ATT);
                if (signature == null) {
                    continue;
                }
                FoldingInfo fi = new FoldingInfo(DEFAULT_PLACEHOLDER, expanded);
                if (canRestoreElement) {
                    PsiElement restoredElement = FoldingPolicy.restoreBySignature(psiFile, signature);
                    if (restoredElement != null && restoredElement.isValid()) {
                        myPsiElements.add(SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(restoredElement));
                        restoredElement.putUserData(FOLDING_INFO_KEY, fi);
                    }
                } else {
                    // Postponed initialization
                    mySerializedElements.add(new SerializedPsiElement(signature, fi));
                }
            } else if (MARKER_TAG.equals(e.getName())) {
                if (date == null) {
                    date = getTimeStamp();
                }
                if (date.isEmpty())
                    continue;
                if (!date.equals(e.getAttributeValue(DATE_ATT)) || FileDocumentManager.getInstance().isDocumentUnsaved(document))
                    continue;
                StringTokenizer tokenizer = new StringTokenizer(e.getAttributeValue(SIGNATURE_ATT), ":");
                try {
                    int start = Integer.valueOf(tokenizer.nextToken()).intValue();
                    int end = Integer.valueOf(tokenizer.nextToken()).intValue();
                    if (start < 0 || end >= document.getTextLength() || start > end)
                        continue;
                    RangeMarker marker = document.createRangeMarker(start, end);
                    myRangeMarkers.add(marker);
                    String placeholderAttributeValue = e.getAttributeValue(PLACEHOLDER_ATT);
                    String placeHolderText = placeholderAttributeValue == null ? DEFAULT_PLACEHOLDER : XmlStringUtil.unescapeIllegalXmlChars(placeholderAttributeValue);
                    FoldingInfo fi = new FoldingInfo(placeHolderText, expanded);
                    marker.putUserData(FOLDING_INFO_KEY, fi);
                } catch (NoSuchElementException exc) {
                    LOG.error(exc);
                }
            } else {
                throw new IllegalStateException("unknown tag: " + e.getName());
            }
        }
    });
}
Also used : Element(org.jdom.Element) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) StringTokenizer(com.intellij.util.text.StringTokenizer)

Example 75 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class DocumentFoldingInfo method clear.

void clear() {
    myPsiElements.clear();
    for (RangeMarker marker : myRangeMarkers) {
        marker.dispose();
    }
    myRangeMarkers.clear();
    mySerializedElements.clear();
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker)

Aggregations

RangeMarker (com.intellij.openapi.editor.RangeMarker)111 Document (com.intellij.openapi.editor.Document)33 TextRange (com.intellij.openapi.util.TextRange)20 Project (com.intellij.openapi.project.Project)19 PsiFile (com.intellij.psi.PsiFile)14 PsiElement (com.intellij.psi.PsiElement)13 IncorrectOperationException (com.intellij.util.IncorrectOperationException)13 Editor (com.intellij.openapi.editor.Editor)11 Nullable (org.jetbrains.annotations.Nullable)11 NotNull (org.jetbrains.annotations.NotNull)10 Template (com.intellij.codeInsight.template.Template)8 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)7 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)6 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)6 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)5 THashMap (gnu.trove.THashMap)5 GutterMark (com.intellij.codeInsight.daemon.GutterMark)4 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)4 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4