use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class DocumentFoldingInfo method loadFromEditor.
void loadFromEditor(@NotNull Editor editor) {
assertDispatchThread();
LOG.assertTrue(!editor.isDisposed());
clear();
PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(editor.getDocument());
SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
EditorFoldingInfo info = EditorFoldingInfo.get(editor);
FoldRegion[] foldRegions = editor.getFoldingModel().getAllFoldRegions();
for (FoldRegion region : foldRegions) {
if (!region.isValid())
continue;
PsiElement element = info.getPsiElement(region);
boolean expanded = region.isExpanded();
boolean collapseByDefault = element != null && FoldingPolicy.isCollapseByDefault(element) && !FoldingUtil.caretInsideRange(editor, TextRange.create(region));
if (collapseByDefault == expanded || element == null) {
FoldingInfo fi = new FoldingInfo(region.getPlaceholderText(), expanded);
if (element != null) {
myPsiElements.add(smartPointerManager.createSmartPsiElementPointer(element, file));
element.putUserData(FOLDING_INFO_KEY, fi);
} else {
RangeMarker marker = editor.getDocument().createRangeMarker(region.getStartOffset(), region.getEndOffset());
myRangeMarkers.add(marker);
marker.putUserData(FOLDING_INFO_KEY, fi);
}
}
}
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class DocumentFoldingInfo method equals.
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DocumentFoldingInfo info = (DocumentFoldingInfo) o;
if (myFile != null ? !myFile.equals(info.myFile) : info.myFile != null) {
return false;
}
if (!myProject.equals(info.myProject) || !myPsiElements.equals(info.myPsiElements) || !mySerializedElements.equals(info.mySerializedElements)) {
return false;
}
if (myRangeMarkers.size() != info.myRangeMarkers.size())
return false;
for (int i = 0; i < myRangeMarkers.size(); i++) {
RangeMarker marker = myRangeMarkers.get(i);
RangeMarker other = info.myRangeMarkers.get(i);
if (marker == other || !marker.isValid() || !other.isValid()) {
continue;
}
if (!TextRange.areSegmentsEqual(marker, other))
return false;
FoldingInfo fi = marker.getUserData(FOLDING_INFO_KEY);
FoldingInfo ofi = other.getUserData(FOLDING_INFO_KEY);
if (!Comparing.equal(fi, ofi))
return false;
}
return true;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class JavaVariableInplaceIntroducer method appendTypeCasts.
private static void appendTypeCasts(List<RangeMarker> occurrenceMarkers, PsiFile file, Project project, @Nullable PsiVariable psiVariable) {
if (occurrenceMarkers != null) {
for (RangeMarker occurrenceMarker : occurrenceMarkers) {
final PsiElement refVariableElement = file.findElementAt(occurrenceMarker.getStartOffset());
final PsiReferenceExpression referenceExpression = PsiTreeUtil.getParentOfType(refVariableElement, PsiReferenceExpression.class);
if (referenceExpression != null) {
final PsiElement parent = referenceExpression.getParent();
if (parent instanceof PsiVariable) {
createCastInVariableDeclaration(project, (PsiVariable) parent);
} else if (parent instanceof PsiReferenceExpression && psiVariable != null) {
final PsiExpression initializer = psiVariable.getInitializer();
LOG.assertTrue(initializer != null);
final PsiType type = initializer.getType();
if (((PsiReferenceExpression) parent).resolve() == null && type != null) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
final PsiExpression castedExpr = elementFactory.createExpressionFromText("((" + type.getCanonicalText() + ")" + referenceExpression.getText() + ")", parent);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(referenceExpression.replace(castedExpr));
}
}
}
}
}
if (psiVariable != null && psiVariable.isValid()) {
createCastInVariableDeclaration(project, psiVariable);
}
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class GroovyMethodInliner method inlineReferenceImpl.
@Nullable
static RangeMarker inlineReferenceImpl(@NotNull GrCallExpression call, @NotNull GrMethod method, boolean resultOfCallExplicitlyUsed, boolean isTailMethodCall, @Nullable Editor editor) {
try {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(call.getProject());
final Project project = call.getProject();
// Variable declaration for qualifier expression
GrVariableDeclaration qualifierDeclaration = null;
GrReferenceExpression innerQualifier = null;
GrExpression qualifier = null;
if (call instanceof GrMethodCallExpression && ((GrMethodCallExpression) call).getInvokedExpression() != null) {
GrExpression invoked = ((GrMethodCallExpression) call).getInvokedExpression();
if (invoked instanceof GrReferenceExpression && ((GrReferenceExpression) invoked).getQualifierExpression() != null) {
qualifier = ((GrReferenceExpression) invoked).getQualifierExpression();
if (PsiUtil.isSuperReference(qualifier)) {
qualifier = null;
} else if (!GroovyInlineMethodUtil.isSimpleReference(qualifier)) {
String qualName = generateQualifierName(call, method, project, qualifier);
qualifier = (GrExpression) PsiUtil.skipParentheses(qualifier, false);
qualifierDeclaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, qualifier, null, qualName);
innerQualifier = (GrReferenceExpression) factory.createExpressionFromText(qualName);
} else {
innerQualifier = (GrReferenceExpression) qualifier;
}
}
}
GrMethod _method = prepareNewMethod(call, method, qualifier);
GrExpression result = getAloneResultExpression(_method);
if (result != null) {
GrExpression expression = call.replaceWithExpression(result, false);
TextRange range = expression.getTextRange();
return editor != null ? editor.getDocument().createRangeMarker(range.getStartOffset(), range.getEndOffset(), true) : null;
}
GrMethod newMethod = prepareNewMethod(call, method, innerQualifier);
String resultName = InlineMethodConflictSolver.suggestNewName("result", newMethod, call);
// Add variable for method result
Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(newMethod.getBlock());
final int returnCount = returnStatements.size();
PsiType methodType = method.getInferredReturnType();
GrOpenBlock body = newMethod.getBlock();
assert body != null;
GrExpression replaced;
if (resultOfCallExplicitlyUsed && !isTailMethodCall) {
GrExpression resultExpr = null;
if (PsiType.VOID.equals(methodType)) {
resultExpr = factory.createExpressionFromText("null");
} else if (returnCount == 1) {
final GrExpression returnExpression = ControlFlowUtils.extractReturnExpression(returnStatements.iterator().next());
if (returnExpression != null) {
resultExpr = factory.createExpressionFromText(returnExpression.getText());
}
} else if (returnCount > 1) {
resultExpr = factory.createExpressionFromText(resultName);
}
if (resultExpr == null) {
resultExpr = factory.createExpressionFromText("null");
}
replaced = call.replaceWithExpression(resultExpr, false);
} else {
replaced = call;
}
// Calculate anchor to insert before
GrExpression enclosingExpr = GroovyRefactoringUtil.addBlockIntoParent(replaced);
GrVariableDeclarationOwner owner = PsiTreeUtil.getParentOfType(enclosingExpr, GrVariableDeclarationOwner.class);
assert owner != null;
PsiElement element = enclosingExpr;
while (element != null && element.getParent() != owner) {
element = element.getParent();
}
assert element != null && element instanceof GrStatement;
GrStatement anchor = (GrStatement) element;
if (!resultOfCallExplicitlyUsed) {
assert anchor == enclosingExpr;
}
// add qualifier reference declaration
if (qualifierDeclaration != null) {
owner.addVariableDeclarationBefore(qualifierDeclaration, anchor);
}
// Process method return statements
if (returnCount > 1 && !PsiType.VOID.equals(methodType) && !isTailMethodCall) {
PsiType type = methodType != null && methodType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ? null : methodType;
GrVariableDeclaration resultDecl = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, resultName);
GrStatement statement = ((GrStatementOwner) owner).addStatementBefore(resultDecl, anchor);
JavaCodeStyleManager.getInstance(statement.getProject()).shortenClassReferences(statement);
// Replace all return statements with assignments to 'result' variable
for (GrStatement returnStatement : returnStatements) {
GrExpression value = ControlFlowUtils.extractReturnExpression(returnStatement);
if (value != null) {
GrExpression assignment = factory.createExpressionFromText(resultName + " = " + value.getText());
returnStatement.replaceWithStatement(assignment);
} else {
returnStatement.replaceWithStatement(factory.createExpressionFromText(resultName + " = null"));
}
}
}
if (!isTailMethodCall && resultOfCallExplicitlyUsed && returnCount == 1) {
returnStatements.iterator().next().removeStatement();
} else if (!isTailMethodCall && (PsiType.VOID.equals(methodType) || returnCount == 1)) {
for (GrStatement returnStatement : returnStatements) {
if (returnStatement instanceof GrReturnStatement) {
final GrExpression returnValue = ((GrReturnStatement) returnStatement).getReturnValue();
if (returnValue != null && GroovyRefactoringUtil.hasSideEffect(returnValue)) {
returnStatement.replaceWithStatement(returnValue);
continue;
}
} else if (GroovyRefactoringUtil.hasSideEffect(returnStatement)) {
continue;
}
returnStatement.removeStatement();
}
}
// Add all method statements
GrStatement[] statements = body.getStatements();
for (GrStatement statement : statements) {
((GrStatementOwner) owner).addStatementBefore(statement, anchor);
}
if (resultOfCallExplicitlyUsed && !isTailMethodCall) {
TextRange range = replaced.getTextRange();
RangeMarker marker = editor != null ? editor.getDocument().createRangeMarker(range.getStartOffset(), range.getEndOffset(), true) : null;
reformatOwner(owner);
return marker;
} else {
GrStatement stmt;
if (isTailMethodCall && enclosingExpr.getParent() instanceof GrReturnStatement) {
stmt = (GrReturnStatement) enclosingExpr.getParent();
} else {
stmt = enclosingExpr;
}
stmt.removeStatement();
reformatOwner(owner);
return null;
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return null;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class TodoPanel method updatePreviewPanel.
private void updatePreviewPanel() {
if (myProject == null || myProject.isDisposed())
return;
List<UsageInfo> infos = new ArrayList<>();
final TreePath path = myTree.getSelectionPath();
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
Object userObject = node.getUserObject();
if (userObject instanceof NodeDescriptor) {
Object element = ((NodeDescriptor) userObject).getElement();
TodoItemNode pointer = myTodoTreeBuilder.getFirstPointerForElement(element);
if (pointer != null) {
final SmartTodoItemPointer value = pointer.getValue();
final Document document = value.getDocument();
final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
final RangeMarker rangeMarker = value.getRangeMarker();
if (psiFile != null) {
infos.add(new UsageInfo(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset()));
}
}
}
}
myUsagePreviewPanel.updateLayout(infos.isEmpty() ? null : infos);
}
Aggregations