protected void restoreState(@NotNull final V psiField) { if (!ReadonlyStatusHandler.ensureDocumentWritable( myProject, InjectedLanguageUtil.getTopLevelEditor(myEditor).getDocument())) return; ApplicationManager.getApplication() .runWriteAction( () -> { final PsiFile containingFile = psiField.getContainingFile(); final RangeMarker exprMarker = getExprMarker(); if (exprMarker != null) { myExpr = restoreExpression(containingFile, psiField, exprMarker, myExprText); } if (myLocalMarker != null) { final PsiElement refVariableElement = containingFile.findElementAt(myLocalMarker.getStartOffset()); if (refVariableElement != null) { final PsiElement parent = refVariableElement.getParent(); if (parent instanceof PsiNamedElement) { ((PsiNamedElement) parent).setName(myLocalName); } } final V localVariable = getLocalVariable(); if (localVariable != null && localVariable.isPhysical()) { myLocalVariable = localVariable; final PsiElement nameIdentifier = localVariable.getNameIdentifier(); if (nameIdentifier != null) { myLocalMarker = createMarker(nameIdentifier); } } } final List<RangeMarker> occurrenceMarkers = getOccurrenceMarkers(); for (int i = 0, occurrenceMarkersSize = occurrenceMarkers.size(); i < occurrenceMarkersSize; i++) { RangeMarker marker = occurrenceMarkers.get(i); if (getExprMarker() != null && marker.getStartOffset() == getExprMarker().getStartOffset() && myExpr != null) { myOccurrences[i] = myExpr; continue; } final E psiExpression = restoreExpression( containingFile, psiField, marker, getLocalVariable() != null ? myLocalName : myExprText); if (psiExpression != null) { myOccurrences[i] = psiExpression; } } if (myExpr != null && myExpr.isPhysical()) { myExprMarker = createMarker(myExpr); } myOccurrenceMarkers = null; deleteTemplateField(psiField); }); }
public void testNestedAfter() { RangeMarker marker1 = createMarker("0[12345678]90123"); Document document = marker1.getDocument(); RangeMarker marker2 = document.createRangeMarker(2, 5); RangeMarker marker3 = document.createRangeMarker(3, 4); document.insertString(10, "x"); assertEquals(1, marker1.getStartOffset()); assertEquals(2, marker2.getStartOffset()); assertEquals(3, marker3.getStartOffset()); }
public void testCreation() throws Exception { RangeMarker marker = createMarker("0123456789", 2, 5); assertEquals(2, marker.getStartOffset()); assertEquals(5, marker.getEndOffset()); assertTrue(marker.isValid()); }
@Override protected void moveOffsetAfter(boolean success) { if (getLocalVariable() != null && getLocalVariable().isValid()) { myEditor.getCaretModel().moveToOffset(getLocalVariable().getTextOffset()); myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); } else if (getExprMarker() != null) { final RangeMarker exprMarker = getExprMarker(); if (exprMarker.isValid()) { myEditor.getCaretModel().moveToOffset(exprMarker.getStartOffset()); myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); } } super.moveOffsetAfter(success); if (myLocalMarker != null && !isRestart()) { myLocalMarker.dispose(); } if (success) { performPostIntroduceTasks(); final String refactoringId = getRefactoringId(); if (refactoringId != null) { final RefactoringEventData afterData = new RefactoringEventData(); afterData.addElement(getVariable()); myProject .getMessageBus() .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC) .refactoringDone(refactoringId, afterData); } } }
public void testNested3() { RangeMarker marker1 = createMarker("01[23]4567890123"); DocumentEx document = (DocumentEx) marker1.getDocument(); RangeMarker marker2 = document.createRangeMarker(9, 11); RangeMarker marker3 = document.createRangeMarker(1, 12); marker3.dispose(); document.deleteString(marker1.getEndOffset(), marker2.getStartOffset()); }
public void testInsertIntoEnd() throws Exception { RangeMarker marker = createMarker("0123456789", 2, 5); marker.getDocument().insertString(5, "xxx"); assertEquals(2, marker.getStartOffset()); assertEquals(5, marker.getEndOffset()); assertTrue(marker.isValid()); }
public void testDeleteBeforeStart() throws Exception { RangeMarker marker = createMarker("01[234]56789"); marker.getDocument().deleteString(0, 1); assertEquals(1, marker.getStartOffset()); assertEquals(4, marker.getEndOffset()); assertTrue(marker.isValid()); }
public void testInsertIntoStartExpandToLeft() throws Exception { RangeMarker marker = createMarker("0123456789", 2, 5); marker.setGreedyToLeft(true); marker.getDocument().insertString(2, "xxx"); assertEquals(2, marker.getStartOffset()); assertEquals(8, marker.getEndOffset()); assertTrue(marker.isValid()); }
public void testUpdateInvalid() throws Exception { RangeMarker marker = createMarker("01[]23456789"); marker.getDocument().deleteString(1, 3); assertFalse(marker.isValid()); marker.getDocument().insertString(2, "xxx"); assertEquals(2, marker.getStartOffset()); assertEquals(2, marker.getEndOffset()); assertFalse(marker.isValid()); }
public void testNoNegative() throws Exception { RangeMarker marker = createMarker( "package safd;\n\n[import javax.swing.JPanel;]\nimport java.util.ArrayList;\n\nclass T{}"); marker .getDocument() .replaceString( 15, 15 + "import javax.swing.JPanel;\nimport java.util.ArrayList;".length(), "import java.util.ArrayList;"); assertEquals(15, marker.getStartOffset()); }
protected int getCaretOffset() { RangeMarker r; if (myLocalMarker != null) { final PsiReference reference = myExpr != null ? myExpr.getReference() : null; if (reference != null && reference.resolve() == myLocalVariable) { r = myExprMarker; } else { r = myLocalMarker; } } else { r = myExprMarker; } return r != null ? r.getStartOffset() : 0; }
public boolean startsOnTheSameElement(E expr, V localVariable) { if (myExprMarker != null && myExprMarker.isValid() && expr != null && myExprMarker.getStartOffset() == expr.getTextOffset()) { return true; } if (myLocalMarker != null && myLocalMarker.isValid() && localVariable != null && myLocalMarker.getStartOffset() == localVariable.getTextOffset()) { return true; } return isRestart(); }
@Override protected void addHighlights( @NotNull Map<TextRange, TextAttributes> ranges, @NotNull Editor editor, @NotNull Collection<RangeHighlighter> highlighters, @NotNull HighlightManager highlightManager) { final TextAttributes attributes = EditorColorsManager.getInstance() .getGlobalScheme() .getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES); final V variable = getVariable(); if (variable != null) { final String name = variable.getName(); LOG.assertTrue(name != null, variable); final int variableNameLength = name.length(); if (isReplaceAllOccurrences()) { for (RangeMarker marker : getOccurrenceMarkers()) { final int startOffset = marker.getStartOffset(); highlightManager.addOccurrenceHighlight( editor, startOffset, startOffset + variableNameLength, attributes, 0, highlighters, null); } } else if (getExpr() != null) { final int startOffset = getExprMarker().getStartOffset(); highlightManager.addOccurrenceHighlight( editor, startOffset, startOffset + variableNameLength, attributes, 0, highlighters, null); } } for (RangeHighlighter highlighter : highlighters) { highlighter.setGreedyToLeft(true); highlighter.setGreedyToRight(true); } }
public V getLocalVariable() { if (myLocalVariable != null && myLocalVariable.isValid()) { return myLocalVariable; } if (myLocalMarker != null) { V variable = getVariable(); PsiFile containingFile; if (variable != null) { containingFile = variable.getContainingFile(); } else { containingFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument()); } PsiNameIdentifierOwner identifierOwner = PsiTreeUtil.getParentOfType( containingFile.findElementAt(myLocalMarker.getStartOffset()), PsiNameIdentifierOwner.class, false); return identifierOwner != null && identifierOwner.getClass() == myLocalVariable.getClass() ? (V) identifierOwner : null; } return myLocalVariable; }
public ASTNode processRange(final ASTNode element, final int startOffset, final int endOffset) { final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(element); assert psiElement != null; final PsiFile file = psiElement.getContainingFile(); final Document document = file.getViewProvider().getDocument(); final RangeMarker rangeMarker = document != null && endOffset < document.getTextLength() ? document.createRangeMarker(startOffset, endOffset) : null; PsiElement elementToFormat = document instanceof DocumentWindow ? InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file) : psiElement; final PsiFile fileToFormat = elementToFormat.getContainingFile(); final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(fileToFormat); if (builder != null) { TextRange range = preprocess(element, TextRange.create(startOffset, endOffset)); if (document instanceof DocumentWindow) { DocumentWindow documentWindow = (DocumentWindow) document; range = documentWindow.injectedToHost(range); } // final SmartPsiElementPointer pointer = // SmartPointerManager.getInstance(psiElement.getProject()).createSmartPsiElementPointer(psiElement); final FormattingModel model = CoreFormatterUtil.buildModel( builder, elementToFormat, mySettings, FormattingMode.REFORMAT); if (file.getTextLength() > 0) { try { FormatterEx.getInstanceEx() .format( model, mySettings, mySettings.getIndentOptions(fileToFormat.getFileType()), new FormatTextRanges(range, true)); wrapLongLinesIfNecessary(file, document, startOffset, endOffset); } catch (IncorrectOperationException e) { LOG.error(e); } } if (!psiElement.isValid()) { if (rangeMarker != null) { final PsiElement at = file.findElementAt(rangeMarker.getStartOffset()); final PsiElement result = PsiTreeUtil.getParentOfType(at, psiElement.getClass(), false); assert result != null; rangeMarker.dispose(); return result.getNode(); } else { assert false; } } // return SourceTreeToPsiMap.psiElementToTree(pointer.getElement()); } if (rangeMarker != null) { rangeMarker.dispose(); } return element; }
private static void assertValidMarker(@NotNull RangeMarker marker, int start, int end) { assertTrue(marker.isValid()); assertEquals(start, marker.getStartOffset()); assertEquals(end, marker.getEndOffset()); }