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);
}
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);
}
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);
}
}
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());
}
}
});
}
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();
}
Aggregations