public void testPsi2DocMergeReplaceAfterAdd() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker(buffer.toString(), 2, 5); synchronizer.startTransaction(getProject(), document, psiFile); synchronizer.insertString(document, 1, "a"); buffer.insert(1, "a"); synchronizer.replaceString(document, 3, 4, "a"); buffer.replace(3, 4, "a"); synchronizer.replaceString(document, 3, 5, "bb"); buffer.replace(3, 5, "bb"); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(document); assertSize(2, transaction.getAffectedFragments().keySet()); synchronizer.commitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 3, 6); }
public void testPsi2DocMergeReplaceWithMultipleAdditions() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker(buffer.toString(), 2, 5); synchronizer.startTransaction(getProject(), document, psiFile); synchronizer.replaceString(document, 0, 10, "0"); buffer.replace(0, 10, "0"); for (int i = 1; i < 10; i++) { synchronizer.insertString(document, i, "" + i); buffer.insert(i, "" + i); } final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(document); assertSize(1, transaction.getAffectedFragments().keySet()); synchronizer.commitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 2, 5); }
public void testPsi2DocMergeMultipleAdditionsWithReplace() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker(buffer.toString(), 2, 5); synchronizer.startTransaction(getProject(), document, psiFile); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(document); assertNotNull(transaction); for (int i = 0; i < 10; i++) { synchronizer.insertString(document, i, "" + i); buffer.insert(i, "" + i); } assertSize(1, transaction.getAffectedFragments().keySet()); synchronizer.replaceString(document, 0, 20, "0123456789"); buffer.replace(0, 20, "0123456789"); assertSize(1, transaction.getAffectedFragments().keySet()); synchronizer.commitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 2, 5); }
public void testPsi2DocSurround() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker(buffer.toString(), 2, 5); synchronizer.startTransaction(getProject(), document, psiFile); synchronizer.replaceString(document, 3, 5, "3a4"); buffer.replace(3, 5, "3a4"); synchronizer.insertString(document, 3, "b"); buffer.insert(3, "b"); synchronizer.insertString(document, 7, "d"); buffer.insert(7, "d"); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(document); assertSize(3, transaction.getAffectedFragments().keySet()); synchronizer.commitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 2, 7); }
public void testDocSynchronizerPrefersLineBoundaryChanges() throws Exception { RangeMarker marker = createMarker("import java.awt.List;\n" + "[import java.util.ArrayList;\n]" + "import java.util.HashMap;\n" + "import java.util.Map;"); PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer(); Document document = marker.getDocument(); synchronizer.startTransaction(getProject(), document, null); String newText = StringUtil.replaceSubstring(document.getText(), TextRange.create(marker), ""); synchronizer.replaceString(document, 0, document.getTextLength(), newText); final List<DocumentEvent> events = new ArrayList<DocumentEvent>(); document.addDocumentListener(new DocumentAdapter() { @Override public void documentChanged(DocumentEvent e) { events.add(e); } }); synchronizer.doCommitTransaction(document); assertEquals(newText, document.getText()); DocumentEvent event = assertOneElement(events); assertEquals("DocumentEventImpl[myOffset=22, myOldLength=28, myNewLength=0, myOldString='import java.util.ArrayList;\n', myNewString=''].", event.toString()); }
public void testPsi2DocReplaceAfterAdd() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker("0123456789", 2, 5); PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer(); synchronizer.startTransaction(getProject(), marker.getDocument(), null); synchronizer.insertString(marker.getDocument(), 1, "a"); buffer.insert(1, "a"); synchronizer.replaceString(marker.getDocument(), 3, 4, "a"); buffer.replace(3, 4, "a"); synchronizer.doCommitTransaction(marker.getDocument()); assertEquals(buffer.toString(), marker.getDocument().getText()); assertValidMarker(marker, 3, 6); }
public void testPsi2DocMergeReplaceAfterAdd() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker("0123456789", 2, 5); PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer(); synchronizer.startTransaction(getProject(), marker.getDocument(), null); synchronizer.insertString(marker.getDocument(), 1, "a"); buffer.insert(1, "a"); synchronizer.replaceString(marker.getDocument(), 3, 4, "a"); buffer.replace(3, 4, "a"); synchronizer.replaceString(marker.getDocument(), 3, 5, "bb"); buffer.replace(3, 5, "bb"); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(marker.getDocument()); final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments = transaction.getAffectedFragments(); assertEquals(affectedFragments.size(), 2); synchronizer.doCommitTransaction(marker.getDocument()); assertEquals(buffer.toString(), marker.getDocument().getText()); assertValidMarker(marker, 3, 6); }
public void testPsi2DocMergeReplaceWithMultipleAdditions() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker("0123456789", 2, 5); PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer(); synchronizer.startTransaction(getProject(), marker.getDocument(), null); synchronizer.replaceString(marker.getDocument(), 0, 10, "0"); buffer.replace(0, 10, "0"); for (int i = 1; i < 10; i++) { synchronizer.insertString(marker.getDocument(), i, "" + i); buffer.insert(i, "" + i); } final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(marker.getDocument()); final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments = transaction.getAffectedFragments(); assertEquals(1, affectedFragments.size()); synchronizer.doCommitTransaction(marker.getDocument()); assertEquals(buffer.toString(), marker.getDocument().getText()); assertValidMarker(marker, 2, 5); }
public void testPsi2DocMergeMultipleAdditionsWithReplace() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker("0123456789", 2, 5); PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer(); synchronizer.startTransaction(getProject(), marker.getDocument(), null); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(marker.getDocument()); final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments = transaction.getAffectedFragments(); for (int i = 0; i < 10; i++) { synchronizer.insertString(marker.getDocument(), i, "" + i); buffer.insert(i, "" + i); } assertEquals(1, affectedFragments.size()); synchronizer.replaceString(marker.getDocument(), 0, 20, "0123456789"); buffer.replace(0, 20, "0123456789"); assertEquals(1, affectedFragments.size()); synchronizer.doCommitTransaction(marker.getDocument()); assertEquals(buffer.toString(), marker.getDocument().getText()); assertValidMarker(marker, 2, 5); }
public void testPsi2DocSurround() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker("0123456789", 2, 5); PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer(); synchronizer.startTransaction(getProject(), marker.getDocument(), null); synchronizer.replaceString(marker.getDocument(), 3, 5, "3a4"); buffer.replace(3, 5, "3a4"); synchronizer.insertString(marker.getDocument(), 3, "b"); buffer.insert(3, "b"); synchronizer.insertString(marker.getDocument(), 7, "d"); buffer.insert(7, "d"); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(marker.getDocument()); final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments = transaction.getAffectedFragments(); assertEquals(3, affectedFragments.size()); synchronizer.doCommitTransaction(marker.getDocument()); assertEquals(buffer.toString(), marker.getDocument().getText()); assertValidMarker(marker, 2, 7); }
public void testPsi2DocForwardRangesChanges() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker("0123456789", 2, 5); PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer(); Document document = marker.getDocument(); synchronizer.startTransaction(getProject(), document, null); synchronizer.replaceString(document, 4, 5, "3a4"); buffer.replace(4, 5, "3a4"); synchronizer.insertString(document, 7, "b"); buffer.insert(7, "b"); synchronizer.insertString(document, 1, "b"); buffer.insert(1, "b"); synchronizer.doCommitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 3, 8); }
public void testPsi2DocMergeReplaceAfterAdd() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker(buffer.toString(), 2, 5); synchronizer.startTransaction(getProject(), document, psiFile); synchronizer.insertString(document, 1, "a"); buffer.insert(1, "a"); synchronizer.replaceString(document, 3, 4, "a"); buffer.replace(3, 4, "a"); synchronizer.replaceString(document, 3, 5, "bb"); buffer.replace(3, 5, "bb"); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(document); final Map<TextRange, CharSequence> affectedFragments = transaction.getAffectedFragments(); assertEquals(affectedFragments.size(), 2); synchronizer.commitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 3, 6); }
public void testPsi2DocMergeReplaceWithMultipleAdditions() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker(buffer.toString(), 2, 5); synchronizer.startTransaction(getProject(), document, psiFile); synchronizer.replaceString(document, 0, 10, "0"); buffer.replace(0, 10, "0"); for (int i = 1; i < 10; i++) { synchronizer.insertString(document, i, "" + i); buffer.insert(i, "" + i); } final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(document); final Map<TextRange, CharSequence> affectedFragments = transaction.getAffectedFragments(); assertEquals(1, affectedFragments.size()); synchronizer.commitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 2, 5); }
public void testPsi2DocMergeMultipleAdditionsWithReplace() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker(buffer.toString(), 2, 5); synchronizer.startTransaction(getProject(), document, psiFile); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(document); final Map<TextRange, CharSequence> affectedFragments = transaction.getAffectedFragments(); for (int i = 0; i < 10; i++) { synchronizer.insertString(document, i, "" + i); buffer.insert(i, "" + i); } assertEquals(1, affectedFragments.size()); synchronizer.replaceString(document, 0, 20, "0123456789"); buffer.replace(0, 20, "0123456789"); assertEquals(1, affectedFragments.size()); synchronizer.commitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 2, 5); }
public void testPsi2DocSurround() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker(buffer.toString(), 2, 5); synchronizer.startTransaction(getProject(), document, psiFile); synchronizer.replaceString(document, 3, 5, "3a4"); buffer.replace(3, 5, "3a4"); synchronizer.insertString(document, 3, "b"); buffer.insert(3, "b"); synchronizer.insertString(document, 7, "d"); buffer.insert(7, "d"); final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction = synchronizer.getTransaction(document); final Map<TextRange, CharSequence> affectedFragments = transaction.getAffectedFragments(); assertEquals(3, affectedFragments.size()); synchronizer.commitTransaction(document); assertEquals(buffer.toString(), document.getText()); assertValidMarker(marker, 2, 7); }
@Nullable public static Document getDocumentToBeUsedFor(final PsiFile file) { final Project project = file.getProject(); final Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (document == null) return null; if (PsiDocumentManager.getInstance(project).isUncommited(document)) return null; PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project)).getSynchronizer(); if (synchronizer.isDocumentAffectedByTransactions(document)) return null; return document; }
public void testPsi2Doc1() throws Exception { StringBuilder buffer = new StringBuilder("0123456789"); RangeMarker marker = createMarker("0123456789", 2, 5); PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(getProject())).getSynchronizer(); synchronizer.startTransaction(getProject(), marker.getDocument(), null); synchronizer.insertString(marker.getDocument(), 3, "a"); buffer.insert(3, "a"); synchronizer.doCommitTransaction(marker.getDocument()); assertEquals(buffer.toString(), marker.getDocument().getText()); assertValidMarker(marker, 2, 6); }
@Nullable public static Document getDocumentToBeUsedFor(final PsiFile file) { final Project project = file.getProject(); final Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (document == null) return null; if (PsiDocumentManager.getInstance(project).isUncommited(document)) return null; PsiToDocumentSynchronizer synchronizer = ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(file.getProject())).getSynchronizer(); if (synchronizer.isDocumentAffectedByTransactions(document)) return null; return document; }
private static void promoteNonPhysicalChangesToDocument(ASTNode rootElement, PsiFile file) { if (file instanceof DummyHolder) return; if (((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(file.getProject())).isCommitInProgress()) return; VirtualFile vFile = file.getViewProvider().getVirtualFile(); if (vFile instanceof LightVirtualFile && !(vFile instanceof VirtualFileWindow)) { Document document = FileDocumentManager.getInstance().getCachedDocument(vFile); if (document != null) { CharSequence text = rootElement.getChars(); PsiToDocumentSynchronizer.performAtomically(file, () -> document.replaceString(0, document.getTextLength(), text)); } } }