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));
}
}
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;
}
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();
}
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);
}
Aggregations