Search in sources :

Example 31 with EditorImpl

use of com.intellij.openapi.editor.impl.EditorImpl in project intellij-community by JetBrains.

the class PsiDocumentManagerImplTest method testUndoShouldAddToCommitQueue.

public void testUndoShouldAddToCommitQueue() throws IOException {
    VirtualFile virtualFile = getVirtualFile(createTempFile("X.java", ""));
    PsiFile file = findFile(virtualFile);
    assertEquals("JAVA", file.getFileType().getName());
    assertNotNull(file);
    assertTrue(file.isPhysical());
    Editor editor = FileEditorManager.getInstance(myProject).openTextEditor(new OpenFileDescriptor(getProject(), virtualFile, 0), false);
    ((EditorImpl) editor).setCaretActive();
    //getDocument(file);
    final Document document = editor.getDocument();
    String text = "class X {" + StringUtil.repeat("void fff() {}\n", 1000) + "}";
    WriteCommandAction.runWriteCommandAction(null, () -> document.insertString(0, text));
    for (int i = 0; i < 300; i++) {
        getPsiDocumentManager().commitAllDocuments();
        assertTrue(getPsiDocumentManager().isCommitted(document));
        String insert = "ddfdkjh";
        WriteCommandAction.runWriteCommandAction(getProject(), () -> document.insertString(0, insert));
        TimeoutUtil.sleep(50);
        WriteCommandAction.runWriteCommandAction(getProject(), () -> document.replaceString(0, insert.length(), ""));
        FileDocumentManager.getInstance().saveDocument(document);
        assertEquals(text, editor.getDocument().getText());
        waitTenSecondsForCommit(document);
        assertTrue("Still not committed: " + document, getPsiDocumentManager().isCommitted(document));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) MockPsiFile(com.intellij.mock.MockPsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) MockDocument(com.intellij.mock.MockDocument)

Example 32 with EditorImpl

use of com.intellij.openapi.editor.impl.EditorImpl in project intellij-community by JetBrains.

the class EditorCoordinateMapper method visualLineStartOffset.

private int visualLineStartOffset(int offset, boolean leanForward) {
    EditorImpl editor = myView.getEditor();
    offset = DocumentUtil.alignToCodePointBoundary(myDocument, offset);
    int result = EditorUtil.getNotFoldedLineStartOffset(editor, offset);
    SoftWrapModelImpl softWrapModel = editor.getSoftWrapModel();
    List<? extends SoftWrap> softWraps = softWrapModel.getRegisteredSoftWraps();
    int currentOrPrevWrapIndex = softWrapModel.getSoftWrapIndex(offset);
    SoftWrap currentOrPrevWrap;
    if (currentOrPrevWrapIndex < 0) {
        currentOrPrevWrapIndex = -currentOrPrevWrapIndex - 2;
        currentOrPrevWrap = currentOrPrevWrapIndex < 0 || currentOrPrevWrapIndex >= softWraps.size() ? null : softWraps.get(currentOrPrevWrapIndex);
    } else {
        currentOrPrevWrap = leanForward ? softWraps.get(currentOrPrevWrapIndex) : null;
    }
    if (currentOrPrevWrap != null && currentOrPrevWrap.getStart() > result) {
        result = currentOrPrevWrap.getStart();
    }
    return result;
}
Also used : EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) SoftWrapModelImpl(com.intellij.openapi.editor.impl.SoftWrapModelImpl)

Example 33 with EditorImpl

use of com.intellij.openapi.editor.impl.EditorImpl in project intellij-community by JetBrains.

the class VisualLineFragmentsIterator method init.

private void init(EditorView view, int startOffset, int startLogicalLine, int currentOrPrevWrapIndex, int nextFoldingIndex, @Nullable Runnable quickEvaluationListener) {
    myQuickEvaluationListener = quickEvaluationListener;
    myView = view;
    EditorImpl editor = view.getEditor();
    myDocument = editor.getDocument();
    FoldingModelEx foldingModel = editor.getFoldingModel();
    FoldRegion[] regions = foldingModel.fetchTopLevel();
    myRegions = regions == null ? FoldRegion.EMPTY_ARRAY : regions;
    SoftWrapModelImpl softWrapModel = editor.getSoftWrapModel();
    List<? extends SoftWrap> softWraps = softWrapModel.getRegisteredSoftWraps();
    SoftWrap currentOrPrevWrap = currentOrPrevWrapIndex < 0 || currentOrPrevWrapIndex >= softWraps.size() ? null : softWraps.get(currentOrPrevWrapIndex);
    SoftWrap followingWrap = (currentOrPrevWrapIndex + 1) < 0 || (currentOrPrevWrapIndex + 1) >= softWraps.size() ? null : softWraps.get(currentOrPrevWrapIndex + 1);
    myVisualLineStartOffset = mySegmentStartOffset = startOffset;
    myCurrentFoldRegionIndex = nextFoldingIndex;
    myCurrentEndLogicalLine = startLogicalLine;
    myCurrentX = myView.getInsets().left;
    if (mySegmentStartOffset == 0) {
        myCurrentX += myView.getPrefixTextWidthInPixels();
    } else if (currentOrPrevWrap != null && mySegmentStartOffset == currentOrPrevWrap.getStart()) {
        myCurrentX += currentOrPrevWrap.getIndentInPixels();
        myCurrentVisualColumn = currentOrPrevWrap.getIndentInColumns();
    }
    myNextWrapOffset = followingWrap == null ? Integer.MAX_VALUE : followingWrap.getStart();
    setInlaysAndFragmentIterator();
}
Also used : EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) FoldRegion(com.intellij.openapi.editor.FoldRegion) SoftWrapModelImpl(com.intellij.openapi.editor.impl.SoftWrapModelImpl) SoftWrap(com.intellij.openapi.editor.SoftWrap) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx)

Example 34 with EditorImpl

use of com.intellij.openapi.editor.impl.EditorImpl in project intellij-community by JetBrains.

the class ArgumentHintLayer method paintComponent.

@Override
protected void paintComponent(final Graphics g) {
    super.paintComponent(g);
    final Pair<Boolean, Argument> nextArg = myNextArg;
    if (nextArg == null) {
        // Nothing to show
        return;
    }
    /**
     *
     * We should display argument right after trimmed (spaces removed) document end or cursor position what ever comes first.
     *
     * We also need to add prompt length.
     */
    final EditorImpl consoleEditor = PyUtil.as(myConsole.getConsoleEditor(), EditorImpl.class);
    if (consoleEditor == null) {
        /**
       * We can't calculate anything if editor is not {@link EditorImpl})
       */
        Logger.getInstance(ArgumentHintLayer.class).warn("Bad editor: " + myConsole.getConsoleEditor());
        return;
    }
    final int consoleFontType = consoleEditor.getCaretModel().getTextAttributes().getFontType();
    final FontMetrics consoleFontMetrics = consoleEditor.getFontMetrics(consoleFontType);
    final Font consoleFont = consoleFontMetrics.getFont();
    // Copy rendering hints
    final Graphics2D sourceGraphics2 = PyUtil.as(consoleEditor.getComponent().getGraphics(), Graphics2D.class);
    if (sourceGraphics2 != null && g instanceof Graphics2D) {
        ((Graphics2D) g).setRenderingHints(sourceGraphics2.getRenderingHints());
    }
    final boolean argumentRequired = nextArg.first;
    final String argumentText = nextArg.second.getHelp().getHelpString();
    g.setFont(consoleFont);
    g.setColor(argumentRequired ? myRequiredColor : myOptionalColor);
    final String textToShow = wrapBracesIfNeeded(argumentRequired, StringUtil.isEmpty(argumentText) ? PyBundle.message("commandLine.argumentHint.defaultName") : argumentText);
    // Update caret position (if known)
    final CaretRectangle[] locations = consoleEditor.getCaretLocations(true);
    if (locations != null) {
        final CaretRectangle rectangle = locations[0];
        myCaretPositionPx = rectangle.myPoint.x;
    }
    final int consoleEditorTop = consoleEditor.getComponent().getLocation().y;
    final double textHeight = Math.floor(consoleFont.getStringBounds(textToShow, consoleFontMetrics.getFontRenderContext()).getY());
    // pixels in position should be integer, anyway
    @SuppressWarnings("NumericCastThatLosesPrecision") final int y = (int) (consoleEditorTop - textHeight);
    // We should take scrolling into account to prevent argument "flying" over text when user scrolls it, like "position:fixed" in css
    final Point scrollLocation = consoleEditor.getContentComponent().getLocation();
    final int spaceWidth = EditorUtil.getSpaceWidth(consoleFontType, consoleEditor);
    // Remove whitespaces on the end of document
    /**
     * TODO: This is actually copy/paste with {@link com.intellij.openapi.editor.actions.EditorActionUtil#moveCaretToLineEnd}.
     * Need to merge somehow.
     */
    final String trimmedDocument = StringUtil.trimTrailing(consoleEditor.getDocument().getText());
    final double trimmedDocumentWidth = consoleFont.getStringBounds(trimmedDocument, consoleFontMetrics.getFontRenderContext()).getWidth();
    // pixels in position should be integer, anyway
    @SuppressWarnings("NumericCastThatLosesPrecision") final int contentWidth = (int) Math.ceil(trimmedDocumentWidth + consoleEditor.getPrefixTextWidthInPixels());
    g.drawString(textToShow, Math.max(myCaretPositionPx, contentWidth) + scrollLocation.x + spaceWidth, y + scrollLocation.y);
}
Also used : Argument(com.jetbrains.commandInterface.command.Argument) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) CaretRectangle(com.intellij.openapi.editor.impl.EditorImpl.CaretRectangle)

Aggregations

EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)34 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)8 Editor (com.intellij.openapi.editor.Editor)7 LightweightHint (com.intellij.ui.LightweightHint)6 Document (com.intellij.openapi.editor.Document)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 EditorMouseFixture (com.intellij.testFramework.fixtures.EditorMouseFixture)3 NotNull (org.jetbrains.annotations.NotNull)3 IntentionHintComponent (com.intellij.codeInsight.intention.impl.IntentionHintComponent)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 SoftWrap (com.intellij.openapi.editor.SoftWrap)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2 FoldingModelEx (com.intellij.openapi.editor.ex.FoldingModelEx)2 SoftWrapModelImpl (com.intellij.openapi.editor.impl.SoftWrapModelImpl)2 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2 Project (com.intellij.openapi.project.Project)2 HintHint (com.intellij.ui.HintHint)2 EditorHintListener (com.intellij.codeInsight.hint.EditorHintListener)1