private static Editor createEditor() { EditorFactory editorFactory = EditorFactory.getInstance(); Document document = editorFactory.createDocument(""); EditorEx editor = (EditorEx) editorFactory.createEditor(document); reinitSettings(editor); return editor; }
public LanguageConsoleImpl( Project project, String title, LightVirtualFile lightFile, boolean initComponents) { myProject = project; myTitle = title; myVirtualFile = lightFile; EditorFactory editorFactory = EditorFactory.getInstance(); myHistoryFile = new LightVirtualFile(getTitle() + ".history.txt", FileTypes.PLAIN_TEXT, ""); myEditorDocument = FileDocumentManager.getInstance().getDocument(lightFile); reparsePsiFile(); assert myEditorDocument != null; myConsoleEditor = (EditorEx) editorFactory.createEditor(myEditorDocument, myProject); myConsoleEditor.addFocusListener(myFocusListener); myCurrentEditor = myConsoleEditor; myHistoryViewer = (EditorEx) editorFactory.createViewer( ((EditorFactoryImpl) editorFactory).createDocument(true), myProject); myUpdateQueue = new MergingUpdateQueue("ConsoleUpdateQueue", 300, true, null); Disposer.register(this, myUpdateQueue); // action shortcuts are not yet registered ApplicationManager.getApplication() .invokeLater( new Runnable() { @Override public void run() { installEditorFactoryListener(); } }); if (initComponents) { initComponents(); } }
public void dispose() { final EditorFactory editorFactory = EditorFactory.getInstance(); editorFactory.releaseEditor(myConsoleEditor); editorFactory.releaseEditor(myHistoryViewer); final FileEditorManager editorManager = FileEditorManager.getInstance(getProject()); final boolean isOpen = editorManager.isFileOpen(myVirtualFile); if (isOpen) { editorManager.closeFile(myVirtualFile); } }
public void testRangeHighlighterLinesInRangeForLongLinePerformance() throws Exception { final int N = 50000; Document document = EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol('x', 2 * N)); final MarkupModelEx markupModel = (MarkupModelEx) DocumentMarkupModel.forDocument(document, ourProject, true); for (int i = 0; i < N - 1; i++) { markupModel.addRangeHighlighter(2 * i, 2 * i + 1, 0, null, HighlighterTargetArea.EXACT_RANGE); } markupModel.addRangeHighlighter( N / 2, N / 2 + 1, 0, null, HighlighterTargetArea.LINES_IN_RANGE); PlatformTestUtil.startPerformanceTest( "slow highlighters lookup", (int) (N * Math.log(N) / 1000), new ThrowableRunnable() { @Override public void run() { List<RangeHighlighterEx> list = new ArrayList<RangeHighlighterEx>(); CommonProcessors.CollectProcessor<RangeHighlighterEx> coll = new CommonProcessors.CollectProcessor<RangeHighlighterEx>(list); for (int i = 0; i < N - 1; i++) { list.clear(); markupModel.processRangeHighlightersOverlappingWith(2 * i, 2 * i + 1, coll); assertEquals(2, list.size()); // 1 line plus one exact range marker } } }) .assertTiming(); }
public void testE5() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10)); List<RangeMarker> mm = add(document, 9, 9, 4, 4, 1, 7, 7, 7, 4, 7); edit(document, 1, 5, 0); delete(mm, 3); }
public void testE13() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10)); List<RangeMarker> mm = add(document, 5, 9, 9, 9, 7, 7, 6, 8); edit(document, 2, 1, 0); delete(mm, 0, 2); }
public void testRangeMarkersAreLazyCreated() throws Exception { final Document document = EditorFactory.getInstance().createDocument("[xxxxxxxxxxxxxx]"); RangeMarker m1 = document.createRangeMarker(2, 4); RangeMarker m2 = document.createRangeMarker(2, 4); assertEquals(2, ((DocumentImpl) document).getRangeMarkersSize()); assertEquals(1, ((DocumentImpl) document).getRangeMarkersNodeSize()); RangeMarker m3 = document.createRangeMarker(2, 5); assertEquals(2, ((DocumentImpl) document).getRangeMarkersNodeSize()); document.deleteString(4, 5); assertTrue(m1.isValid()); assertTrue(m2.isValid()); assertTrue(m3.isValid()); assertEquals(1, ((DocumentImpl) document).getRangeMarkersNodeSize()); m1.setGreedyToLeft(true); assertTrue(m1.isValid()); assertEquals(3, ((DocumentImpl) document).getRangeMarkersSize()); assertEquals(2, ((DocumentImpl) document).getRangeMarkersNodeSize()); m3.dispose(); assertTrue(m1.isValid()); assertTrue(m2.isValid()); assertFalse(m3.isValid()); assertEquals(2, ((DocumentImpl) document).getRangeMarkersSize()); assertEquals(2, ((DocumentImpl) document).getRangeMarkersNodeSize()); }
public void testE4() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10)); List<RangeMarker> mm = add(document, 3, 5, 5, 6, 4, 8, 6, 9, 8, 9); edit(document, 6, 0, 0, 3, 0, 2); delete(mm, 1, 0); }
public void testE11() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10)); List<RangeMarker> mm = add(document, 9, 9, 7, 7, 1, 6, 3, 7); // edit(document, 0,0,0); delete(mm, 1); }
@Override protected void releaseResources() { super.releaseResources(); if (myPreview == null) return; EditorFactory.getInstance().releaseEditor(myPreview); myPreview = null; }
public void testRangeMarkersAreWeakReferenced_NoVerify() throws Exception { final Document document = EditorFactory.getInstance().createDocument("[xxxxxxxxxxxxxx]"); for (int i = 0; i < 10; i++) { document.createRangeMarker(0, document.getTextLength()); } LeakHunter.checkLeak(document, RangeMarker.class); }
private void releaseAllEditors() { for (final Editor editor : myEditors.values()) { if (!editor.isDisposed()) { EditorFactory.getInstance().releaseEditor(editor); } } myEditors.clear(); }
public void testE15() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 100)); List<RangeMarker> mm = add( document, 90, 93, 0, 9, 44, 79, 4, 48, 44, 99, 53, 64, 59, 82, 12, 99, 81, 86, 8, 40, 24, 55, 32, 50, 74, 79, 14, 94, 7, 14); edit(document, 34, 0, 4, 99, 0, 3); }
public void testE14() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 100)); List<RangeMarker> mm = add( document, 6, 11, 2, 13, 17, 17, 13, 19, 2, 3, 9, 10, 10, 11, 14, 14, 1, 3, 4, 12, 14, 15, 3, 10, 14, 14, 4, 4, 4, 8, 6, 14, 8, 16, 2, 12, 11, 19, 10, 13); edit(document, 19, 0, 0, 7, 3, 0, 16, 0, 3); }
public void testE16() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 100)); List<RangeMarker> mm = add( document, 29, 63, 47, 52, 72, 86, 19, 86, 13, 55, 18, 57, 92, 95, 83, 99, 41, 80, 53, 85, 10, 30, 28, 44, 23, 32, 70, 95, 14, 28); edit(document, 67, 5, 0, 1, 0, 4); delete(mm, 11); }
public void testPersistent() throws Exception { String text = "xxx\nzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; Document document = EditorFactory.getInstance().createDocument(text); int startOffset = text.indexOf('z'); int endOffset = text.lastIndexOf('z'); RangeMarker marker = document.createRangeMarker(startOffset, endOffset, true); document.replaceString(startOffset + 1, endOffset - 1, "ccc"); assertTrue(marker.isValid()); }
public void testE17() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 100)); List<RangeMarker> mm = add( document, 15, 85, 79, 88, 90, 94, 43, 67, 54, 89, 81, 98, 1, 34, 58, 93, 22, 23, 44, 45, 63, 84, 45, 76, 58, 87, 40, 59, 5, 81, 95, 95, 12, 61, 52, 65, 80, 95, 6, 16, 7, 67, 59, 63, 91, 96, 99, 99, 50, 96, 72, 78, 78, 78, 85, 85, 5, 51, 90, 91); edit(document, 20, 26, 0, 15, 0, 4, 64, 4, 0); }
public void _testRandomAddRemove() { int N = 100; for (int ti = 0; ; ti++) { if (ti % 10000 == 0) System.out.println(ti); DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', N)); Random gen = new Random(); List<Pair<RangeMarker, TextRange>> adds = new ArrayList<Pair<RangeMarker, TextRange>>(); List<Pair<RangeMarker, TextRange>> dels = new ArrayList<Pair<RangeMarker, TextRange>>(); try { for (int i = 0; i < 30; i++) { int x = gen.nextInt(N); int y = x + gen.nextInt(N - x); if (gen.nextBoolean()) { x = 0; y = document.getTextLength(); } RangeMarkerEx r = (RangeMarkerEx) document.createRangeMarker(x, y); adds.add(Pair.create((RangeMarker) r, TextRange.create(r))); } List<Pair<RangeMarker, TextRange>> candidates = new ArrayList<Pair<RangeMarker, TextRange>>(adds); while (!candidates.isEmpty()) { int size = candidates.size(); int x = gen.nextInt(size); Pair<RangeMarker, TextRange> c = candidates.remove(x); RangeMarkerEx r = (RangeMarkerEx) c.first; assertEquals(size - 1, candidates.size()); dels.add(c); r.dispose(); } } catch (AssertionError e) { String s = "adds: "; for (Pair<RangeMarker, TextRange> c : adds) { TextRange t = c.second; s += t.getStartOffset() + "," + t.getEndOffset() + ", "; } s += "\ndels: "; for (Pair<RangeMarker, TextRange> c : dels) { int index = adds.indexOf(c); assertSame(c, adds.get(index)); s += index + ", "; } System.err.println(s); throw e; } } }
public void testRangeHighlighterDisposeVsRemoveAllConflict() throws Exception { Document document = EditorFactory.getInstance().createDocument("[xxxxxxxxxxxxxx]"); MarkupModel markupModel = DocumentMarkupModel.forDocument(document, ourProject, true); RangeMarker m = markupModel.addRangeHighlighter(1, 6, 0, null, HighlighterTargetArea.EXACT_RANGE); assertTrue(m.isValid()); markupModel.removeAllHighlighters(); assertFalse(m.isValid()); assertEmpty(markupModel.getAllHighlighters()); m.dispose(); assertFalse(m.isValid()); }
public void testRangeHighlightersRecreateBug() throws Exception { Document document = EditorFactory.getInstance().createDocument("[xxxxxxxxxxxxxx]"); MarkupModel markupModel = DocumentMarkupModel.forDocument(document, ourProject, true); for (int i = 0; i < 2; i++) { RangeMarker m = markupModel.addRangeHighlighter(1, 6, 0, null, HighlighterTargetArea.EXACT_RANGE); RangeMarker m2 = markupModel.addRangeHighlighter(2, 7, 0, null, HighlighterTargetArea.EXACT_RANGE); RangeMarker m3 = markupModel.addRangeHighlighter(1, 6, 0, null, HighlighterTargetArea.EXACT_RANGE); markupModel.removeAllHighlighters(); } }
public void testRangeHighlighterIteratorOrder() throws Exception { Document document = EditorFactory.getInstance().createDocument("1234567890"); final MarkupModelEx markupModel = (MarkupModelEx) DocumentMarkupModel.forDocument(document, ourProject, true); RangeHighlighter exact = markupModel.addRangeHighlighter(3, 6, 0, null, HighlighterTargetArea.EXACT_RANGE); RangeHighlighter line = markupModel.addRangeHighlighter(4, 5, 0, null, HighlighterTargetArea.LINES_IN_RANGE); List<RangeHighlighter> list = new ArrayList<RangeHighlighter>(); markupModel.processRangeHighlightersOverlappingWith( 2, 9, new CommonProcessors.CollectProcessor<RangeHighlighter>(list)); assertEquals(Arrays.asList(line, exact), list); }
public void testValidationBug() throws Exception { Document document = EditorFactory.getInstance().createDocument("[xxxxxxxxxxxxxx]"); final Editor editor = EditorFactory.getInstance().createEditor(document); try { final FoldRegion[] fold = new FoldRegion[1]; editor .getFoldingModel() .runBatchFoldingOperation( new Runnable() { @Override public void run() { fold[0] = editor.getFoldingModel().addFoldRegion(0, 2, ""); } }); RangeMarker marker = document.createRangeMarker(0, 2); document.deleteString(1, 2); assertTrue(marker.isValid()); // assertFalse(fold[0].isValid()); } finally { EditorFactory.getInstance().releaseEditor(editor); } }
public TemplateManagerImpl(Project project) { myProject = project; final EditorFactoryListener myEditorFactoryListener = new EditorFactoryAdapter() { @Override public void editorReleased(@NotNull EditorFactoryEvent event) { Editor editor = event.getEditor(); if (editor.getProject() != null && editor.getProject() != myProject) return; if (myProject.isDisposed() || !myProject.isOpen()) return; TemplateState state = getTemplateState(editor); if (state != null) { state.gotoEnd(); } clearTemplateState(editor); } }; EditorFactory.getInstance().addEditorFactoryListener(myEditorFactoryListener, myProject); }
@Override public void removeNotify() { super.removeNotify(); EditorFactory.getInstance().releaseEditor(myEditor); disposeNonTextEditor(); }
public ImplementationViewComponent(PsiElement[] elements, final int index) { super(new BorderLayout()); final Project project = elements.length > 0 ? elements[0].getProject() : null; EditorFactory factory = EditorFactory.getInstance(); Document doc = factory.createDocument(""); doc.setReadOnly(true); myEditor = factory.createEditor(doc, project); ((EditorEx) myEditor).setBackgroundColor(EditorFragmentComponent.getBackgroundColor(myEditor)); final EditorSettings settings = myEditor.getSettings(); settings.setAdditionalLinesCount(1); settings.setAdditionalColumnsCount(1); settings.setLineMarkerAreaShown(false); settings.setIndentGuidesShown(false); settings.setLineNumbersShown(false); settings.setFoldingOutlineShown(false); myBinarySwitch = new CardLayout(); myViewingPanel = new JPanel(myBinarySwitch); myEditor.setBorder(null); ((EditorEx) myEditor).getScrollPane().setViewportBorder(JBScrollPane.createIndentBorder()); myViewingPanel.add(myEditor.getComponent(), TEXT_PAGE_KEY); myBinaryPanel = new JPanel(new BorderLayout()); myViewingPanel.add(myBinaryPanel, BINARY_PAGE_KEY); add(myViewingPanel, BorderLayout.CENTER); myToolbar = createToolbar(); myLocationLabel = new JLabel(); myCountLabel = new JLabel(); final JPanel header = new JPanel(new BorderLayout(2, 0)); header.setBorder( BorderFactory.createCompoundBorder( IdeBorderFactory.createBorder(SideBorder.BOTTOM), IdeBorderFactory.createEmptyBorder(0, 0, 0, 5))); final JPanel toolbarPanel = new JPanel(new GridBagLayout()); final GridBagConstraints gc = new GridBagConstraints( GridBagConstraints.RELATIVE, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0); toolbarPanel.add(myToolbar.getComponent(), gc); setPreferredSize(new Dimension(600, 400)); update( elements, new PairFunction<PsiElement[], List<FileDescriptor>, Boolean>() { @Override public Boolean fun( final PsiElement[] psiElements, final List<FileDescriptor> fileDescriptors) { if (psiElements.length == 0) return false; myElements = psiElements; myIndex = index < myElements.length ? index : 0; PsiFile psiFile = getContainingFile(myElements[myIndex]); VirtualFile virtualFile = psiFile.getVirtualFile(); EditorHighlighter highlighter; if (virtualFile != null) highlighter = HighlighterFactory.createHighlighter(project, virtualFile); else { String fileName = psiFile.getName(); // some artificial psi file, lets do best we can highlighter = HighlighterFactory.createHighlighter(project, fileName); } ((EditorEx) myEditor).setHighlighter(highlighter); gc.fill = GridBagConstraints.HORIZONTAL; gc.weightx = 1; if (myElements.length > 1) { myFileChooser = new ComboBox( fileDescriptors.toArray(new FileDescriptor[fileDescriptors.size()]), 250); updateRenderer(project); myFileChooser.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int index = myFileChooser.getSelectedIndex(); if (myIndex != index) { myIndex = index; updateControls(); } } }); myLabel = new JLabel(); myLabel.setVisible(false); toolbarPanel.add(myFileChooser, gc); } else { myFileChooser = new ComboBox(); myFileChooser.setVisible(false); myCountLabel.setVisible(false); myLabel = new JLabel(); VirtualFile file = psiFile.getVirtualFile(); if (file != null) { myLabel.setIcon(getIconForFile(psiFile)); myLabel.setForeground( FileStatusManager.getInstance(project).getStatus(file).getColor()); myLabel.setText(file.getPresentableName()); myLabel.setBorder( new CompoundBorder( IdeBorderFactory.createRoundedBorder(), IdeBorderFactory.createEmptyBorder(0, 0, 0, 5))); } toolbarPanel.add(myLabel, gc); } gc.fill = GridBagConstraints.NONE; gc.weightx = 0; toolbarPanel.add(myCountLabel, gc); header.add(toolbarPanel, BorderLayout.CENTER); header.add(myLocationLabel, BorderLayout.EAST); add(header, BorderLayout.NORTH); updateControls(); return true; } }); }
private void recreateEditorsPanel() { myValuesPanel.removeAll(); myValuesPanel.setLayout(new CardLayout()); if (!myProject.isOpen()) return; JPanel valuesPanelComponent = new MyJPanel(new GridBagLayout()); myValuesPanel.add( new JBScrollPane(valuesPanelComponent) { @Override public void updateUI() { super.updateUI(); getViewport().setBackground(UIUtil.getPanelBackground()); } }, VALUES); myValuesPanel.add(myNoPropertySelectedPanel, NO_PROPERTY_SELECTED); List<PropertiesFile> propertiesFiles = myResourceBundle.getPropertiesFiles(); GridBagConstraints gc = new GridBagConstraints( 0, 0, 0, 0, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0); releaseAllEditors(); myTitledPanels.clear(); int y = 0; Editor previousEditor = null; Editor firstEditor = null; for (final PropertiesFile propertiesFile : propertiesFiles) { final Editor editor = createEditor(); final Editor oldEditor = myEditors.put(propertiesFile, editor); if (firstEditor == null) { firstEditor = editor; } if (previousEditor != null) { editor.putUserData( ChooseSubsequentPropertyValueEditorAction.PREV_EDITOR_KEY, previousEditor); previousEditor.putUserData( ChooseSubsequentPropertyValueEditorAction.NEXT_EDITOR_KEY, editor); } previousEditor = editor; if (oldEditor != null) { EditorFactory.getInstance().releaseEditor(oldEditor); } ((EditorEx) editor) .addFocusListener( new FocusChangeListener() { @Override public void focusGained(final Editor editor) { mySelectedEditor = editor; } @Override public void focusLost(final Editor eventEditor) { writeEditorPropertyValue(editor, propertiesFile, null); } }); gc.gridx = 0; gc.gridy = y++; gc.gridheight = 1; gc.gridwidth = GridBagConstraints.REMAINDER; gc.weightx = 1; gc.weighty = 1; gc.anchor = GridBagConstraints.CENTER; Locale locale = propertiesFile.getLocale(); List<String> names = new ArrayList<String>(); if (!Comparing.strEqual(locale.getDisplayLanguage(), null)) { names.add(locale.getDisplayLanguage()); } if (!Comparing.strEqual(locale.getDisplayCountry(), null)) { names.add(locale.getDisplayCountry()); } if (!Comparing.strEqual(locale.getDisplayVariant(), null)) { names.add(locale.getDisplayVariant()); } String title = propertiesFile.getName(); if (!names.isEmpty()) { title += " (" + StringUtil.join(names, "/") + ")"; } JComponent comp = new JPanel(new BorderLayout()) { @Override public Dimension getPreferredSize() { Insets insets = getBorder().getBorderInsets(this); return new Dimension(100, editor.getLineHeight() * 4 + insets.top + insets.bottom); } }; comp.add(editor.getComponent(), BorderLayout.CENTER); comp.setBorder(IdeBorderFactory.createTitledBorder(title, true)); myTitledPanels.put(propertiesFile, (JPanel) comp); valuesPanelComponent.add(comp, gc); } if (previousEditor != null) { previousEditor.putUserData( ChooseSubsequentPropertyValueEditorAction.NEXT_EDITOR_KEY, firstEditor); firstEditor.putUserData( ChooseSubsequentPropertyValueEditorAction.PREV_EDITOR_KEY, previousEditor); } gc.gridx = 0; gc.gridy = y; gc.gridheight = GridBagConstraints.REMAINDER; gc.gridwidth = GridBagConstraints.REMAINDER; gc.weightx = 10; gc.weighty = 1; valuesPanelComponent.add(new JPanel(), gc); selectionChanged(); myValuesPanel.repaint(); UIUtil.invokeAndWaitIfNeeded( new Runnable() { @Override public void run() { updateEditorsFromProperties(); } }); }
/** * Inspects all lines of the given document and wraps all of them that exceed {@link * CodeStyleSettings#RIGHT_MARGIN right margin}. * * <p>I.e. the algorithm is to do the following for every line: * * <p> * * <pre> * <ol> * <li> * Check if the line exceeds {@link CodeStyleSettings#RIGHT_MARGIN right margin}. Go to the next line in the case of * negative answer; * </li> * <li>Determine line wrap position; </li> * <li> * Perform 'smart wrap', i.e. not only wrap the line but insert additional characters over than line feed if necessary. * For example consider that we wrap a single-line comment - we need to insert comment symbols on a start of the wrapped * part as well. Generally, we get the same behavior as during pressing 'Enter' at wrap position during editing document; * </li> * </ol> * </pre> * * @param file file that holds parsed document tree * @param document target document * @param startOffset start offset of the first line to check for wrapping (inclusive) * @param endOffset end offset of the first line to check for wrapping (exclusive) */ private void wrapLongLinesIfNecessary( @NotNull final PsiFile file, @Nullable final Document document, final int startOffset, final int endOffset) { if (!mySettings.getCommonSettings(file.getLanguage()).WRAP_LONG_LINES || PostprocessReformattingAspect.getInstance(file.getProject()) .isViewProviderLocked(file.getViewProvider()) || document == null) { return; } final VirtualFile vFile = FileDocumentManager.getInstance().getFile(document); if ((vFile == null || vFile instanceof LightVirtualFile) && !ApplicationManager.getApplication().isUnitTestMode()) { // we assume that control flow reaches this place when the document is backed by a "virtual" // file so any changes made by // a formatter affect only PSI and it is out of sync with a document text return; } Editor editor = PsiUtilBase.findEditor(file); EditorFactory editorFactory = null; if (editor == null) { if (!ApplicationManager.getApplication().isDispatchThread()) { return; } editorFactory = EditorFactory.getInstance(); editor = editorFactory.createEditor(document, file.getProject()); } try { final Editor editorToUse = editor; ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { final CaretModel caretModel = editorToUse.getCaretModel(); final int caretOffset = caretModel.getOffset(); final RangeMarker caretMarker = editorToUse.getDocument().createRangeMarker(caretOffset, caretOffset); doWrapLongLinesIfNecessary( editorToUse, file.getProject(), editorToUse.getDocument(), startOffset, endOffset); if (caretMarker.isValid() && caretModel.getOffset() != caretMarker.getStartOffset()) { caretModel.moveToOffset(caretMarker.getStartOffset()); } } }); } finally { PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject()); if (documentManager.isUncommited(document)) documentManager.commitDocument(document); if (editorFactory != null) { editorFactory.releaseEditor(editor); } } }
public void testE9() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10)); List<RangeMarker> mm = add(document, 4, 5, 9, 9, 1, 2, 0, 3); edit(document, 0, 3, 0); }
public void testRandomEdit_NoCommand() { final int N = 100; final Random gen = new Random(); int N_TRIES = Timings.adjustAccordingToMySpeed(7000, false); System.out.println("N_TRIES = " + N_TRIES); DocumentEx document = null; for (int tryn = 0; tryn < N_TRIES; tryn++) { ((UndoManagerImpl) UndoManager.getInstance(getProject())).flushCurrentCommandMerger(); ((UndoManagerImpl) UndoManager.getGlobalInstance()).flushCurrentCommandMerger(); if (document != null) { ((UndoManagerImpl) UndoManager.getInstance(getProject())) .clearUndoRedoQueueInTests(document); ((UndoManagerImpl) UndoManager.getGlobalInstance()).clearUndoRedoQueueInTests(document); } if (tryn % 10000 == 0) { System.out.println(tryn); } document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', N)); final DocumentEx finalDocument = document; new WriteCommandAction(getProject()) { @Override protected void run(@NotNull Result result) throws Exception { List<Pair<RangeMarker, TextRange>> adds = new ArrayList<Pair<RangeMarker, TextRange>>(); List<Pair<RangeMarker, TextRange>> dels = new ArrayList<Pair<RangeMarker, TextRange>>(); List<Trinity<Integer, Integer, Integer>> edits = new ArrayList<Trinity<Integer, Integer, Integer>>(); try { for (int i = 0; i < 30; i++) { int x = gen.nextInt(N); int y = x + gen.nextInt(N - x); RangeMarkerEx r = (RangeMarkerEx) finalDocument.createRangeMarker(x, y); adds.add(Pair.create((RangeMarker) r, TextRange.create(r))); } for (int i = 0; i < 10; i++) { int offset = gen.nextInt(finalDocument.getTextLength()); if (gen.nextBoolean()) { int length = gen.nextInt(5); edits.add(Trinity.create(offset, 0, length)); finalDocument.insertString(offset, StringUtil.repeatSymbol(' ', length)); } else { int length = gen.nextInt(finalDocument.getTextLength() - offset); edits.add(Trinity.create(offset, length, 0)); finalDocument.deleteString(offset, offset + length); } } List<Pair<RangeMarker, TextRange>> candidates = new ArrayList<Pair<RangeMarker, TextRange>>(adds); while (!candidates.isEmpty()) { int size = candidates.size(); int x = gen.nextInt(size); Pair<RangeMarker, TextRange> c = candidates.remove(x); RangeMarkerEx r = (RangeMarkerEx) c.first; assertEquals(size - 1, candidates.size()); dels.add(c); r.dispose(); } } catch (AssertionError e) { String s = "adds: "; for (Pair<RangeMarker, TextRange> c : adds) { TextRange t = c.second; s += t.getStartOffset() + "," + t.getEndOffset() + ", "; } s += "\nedits: "; for (Trinity<Integer, Integer, Integer> edit : edits) { s += edit.first + "," + edit.second + "," + edit.third + ", "; } s += "\ndels: "; for (Pair<RangeMarker, TextRange> c : dels) { int index = adds.indexOf(c); assertSame(c, adds.get(index)); s += index + ", "; } System.err.println(s); throw e; } } }.execute(); } }
public void testE10() { DocumentEx document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10)); List<RangeMarker> mm = add(document, 9, 9, 6, 8, 8, 8, 5, 9); edit(document, 2, 6, 0, 2, 0, 4); }