public void handleInsert(InsertionContext context, LookupElement item) { super.handleInsert(context, item); context.setAddCompletionChar(false); final CaretModel caretModel = context.getEditor().getCaretModel(); context.getEditor().getDocument().insertString(caretModel.getOffset(), ";"); caretModel.moveToOffset(caretModel.getOffset() + 1); }
private void handleNoSelection(Editor editor) { CaretModel caretModel = editor.getCaretModel(); Document doc = editor.getDocument(); if ((caretModel == null) || (doc == null) || (doc.getTextLength() == 0)) { return; } char[] allChars = doc.getChars(); int maxOffset = allChars.length; int startOffset = caretModel.getOffset(); while ((startOffset < maxOffset) && (!Character.isLetterOrDigit(allChars[startOffset]))) { startOffset++; } StringBuffer word = new StringBuffer(); int i = startOffset; while ((i < maxOffset) && (Character.isLetterOrDigit(allChars[i]))) { word.append(allChars[i]); i++; } if (word.length() > 0) { this.m_transformer.transform(word); int newOffset = startOffset + word.length(); doc.replaceString(startOffset, newOffset, word.toString()); caretModel.moveToOffset(newOffset); } }
@Override public void actionPerformed(AnActionEvent anActionEvent) { final Document document = editor.getDocument(); CaretModel caretModel = editor.getCaretModel(); final int offset = caretModel.getOffset(); new PopupChooserBuilder(QUERY_OPERATOR_LIST) .setMovable(false) .setCancelKeyEnabled(true) .setItemChoosenCallback( new Runnable() { public void run() { final String selectedQueryOperator = (String) QUERY_OPERATOR_LIST.getSelectedValue(); if (selectedQueryOperator == null) return; new WriteCommandAction(project, MONGO_OPERATOR_COMPLETION) { @Override protected void run(@NotNull Result result) throws Throwable { document.insertString(offset, selectedQueryOperator); } }.execute(); } }) .createPopup() .showInBestPositionFor(editor); }
public void testCaretPositionAfterLineRemoval() throws IOException { final String text = "123456\n" + "123456\n" + "line towrapbecauseitislong\n" + "923456"; init(8, text); final CaretModel caretModel = myEditor.getCaretModel(); caretModel.moveToOffset(text.indexOf("e")); final VisualPosition position = caretModel.getVisualPosition(); deleteLine(); assertEquals( text.substring(0, text.indexOf("line")) + text.substring(text.indexOf('9')), myEditor.getDocument().getText()); assertEquals(position, caretModel.getVisualPosition()); }
public void attach(EditorSource editorSource) { if (myEditor != null) hide(); myEditor = editorSource.getEditor(); if (myEditor == null) return; final CaretModel caretModel = myEditor.getCaretModel(); caretModel.addCaretListener(this); editorSource.addDisposable( new Disposable() { public void dispose() { caretModel.removeCaretListener(CurrentLineMarker.this); } }); }
public void testVerticalCaretShiftOnLineComment() throws IOException { String text = "1. just a line that is long enough to be soft wrapped\n" + "2. just a line that is long enough to be soft wrapped\n" + "3. just a line that is long enough to be soft wrapped\n" + "4. just a line that is long enough to be soft wrapped"; init(15, text); CaretModel caretModel = myEditor.getCaretModel(); caretModel.moveToOffset(text.indexOf("2.") + 2); lineComment(); assertEquals( myEditor.offsetToLogicalPosition(text.indexOf("3.") + 2), caretModel.getLogicalPosition()); }
@Nullable private static PyStatementList getStatementList(PsiElement psiElement, Editor editor) { if (psiElement instanceof PyStatementListContainer) { return ((PyStatementListContainer) psiElement).getStatementList(); } else { final CaretModel caretModel = editor.getCaretModel(); final PsiElement atCaret = psiElement.getContainingFile().findElementAt(caretModel.getOffset()); final PyStatementPart statementPart = PsiTreeUtil.getParentOfType(atCaret, PyStatementPart.class); if (statementPart != null) { return statementPart.getStatementList(); } } return null; }
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { final CaretModel caretModel = editor.getCaretModel(); final int position = caretModel.getOffset(); final PsiElement element = file.findElementAt(position); Pair<PsiVariable, PsiType> pair = findVariable(element); if (pair == null) return; PsiVariable variable = pair.getFirst(); PsiType type = pair.getSecond(); variable .getTypeElement() .replace( JavaPsiFacade.getInstance(variable.getProject()) .getElementFactory() .createTypeElement(type)); }
public void testDeleteWhenCaretBeforeSoftWrap() throws IOException { final String text = "text 1234"; init(7, text); final int offset = text.indexOf("123"); checkSoftWraps(offset); final CaretModel caretModel = myEditor.getCaretModel(); caretModel.moveToOffset(offset); caretModel.moveCaretRelatively(-1, 0, false, false, false); assertEquals( offset, caretModel .getOffset()); // Navigating from 'after soft wrap' to the 'before soft wrap' position. delete(); assertEquals("text 234", myEditor.getDocument().getText()); }
public void testNonSmartHome() throws IOException { String text = " this is a string that starts with white space and is long enough to be soft-wrapped\n" + " this is a 'prefix' text before collapsed multi-line folding that is long enough to be soft-wrapped first fold line\n" + "second fold line"; init(30, text); addCollapsedFoldRegion(text.indexOf("first fold line"), text.length(), "..."); List<? extends SoftWrap> softWraps = getSoftWrapModel().getRegisteredSoftWraps(); assertTrue(!softWraps.isEmpty()); CaretModel caretModel = myEditor.getCaretModel(); SoftWrap softWrap = softWraps.get(0); // Test non-smart home myEditor.getSettings().setSmartHome(false); caretModel.moveToOffset(softWrap.getStart() + 1); int visLine = caretModel.getVisualPosition().line; home(); assertEquals(new VisualPosition(visLine, 0), caretModel.getVisualPosition()); caretModel.moveToOffset(text.length()); home(); visLine = caretModel.getVisualPosition().line; assertEquals(new VisualPosition(visLine, 0), caretModel.getVisualPosition()); }
@Override public void beforeDocumentChangeAtCaret() { CaretModel caretModel = myEditor.getCaretModel(); VisualPosition visualCaretPosition = caretModel.getVisualPosition(); if (!isInsideSoftWrap(visualCaretPosition)) { return; } SoftWrap softWrap = myStorage.getSoftWrap(caretModel.getOffset()); if (softWrap == null) { return; } myEditor .getDocument() .replaceString(softWrap.getStart(), softWrap.getEnd(), softWrap.getText()); caretModel.moveToVisualPosition(visualCaretPosition); }
private static boolean isTestClass(PsiFile file, Editor editor) { PsiElement element1 = null; final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { element1 = file.findElementAt(selectionModel.getSelectionStart()); } else { final CaretModel caretModel = editor.getCaretModel(); final Document document = editor.getDocument(); int lineNumber = document.getLineNumber(caretModel.getOffset()); if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) { element1 = file.findElementAt(document.getLineStartOffset(lineNumber)); } } if (element1 != null) { final PyClass clazz = PyUtil.getContainingClassOrSelf(element1); if (clazz != null && PythonUnitTestUtil.isTestCaseClass(clazz, null)) return true; } return false; }
@Nullable private static Point getVisibleBestPopupLocation(@NotNull Editor editor) { VisualPosition visualPosition = editor.getUserData(ANCHOR_POPUP_POSITION); if (visualPosition == null) { CaretModel caretModel = editor.getCaretModel(); if (caretModel.isUpToDate()) { visualPosition = caretModel.getVisualPosition(); } else { visualPosition = editor.offsetToVisualPosition(caretModel.getOffset()); } } Point p = editor.visualPositionToXY( new VisualPosition(visualPosition.line + 1, visualPosition.column)); final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea(); return visibleArea.contains(p) ? p : null; }
public void testHomeProcessing() throws IOException { String text = "class Test {\n" + " public String s = \"this is a long string literal that is expected to be soft-wrapped into multiple visual lines\";\n" + "}"; init(30, text); myEditor.getCaretModel().moveToOffset(text.indexOf("}") - 1); List<? extends SoftWrap> softWraps = new ArrayList<SoftWrap>(getSoftWrapModel().getRegisteredSoftWraps()); assertTrue(!softWraps.isEmpty()); CaretModel caretModel = myEditor.getCaretModel(); int expectedVisualLine = caretModel.getVisualPosition().line; while (!softWraps.isEmpty()) { SoftWrap softWrap = softWraps.get(softWraps.size() - 1); int caretOffsetBefore = caretModel.getOffset(); home(); // Expecting the caret to be moved at the nearest soft wrap start offset. int caretOffset = caretModel.getOffset(); assertTrue(caretOffset < caretOffsetBefore); assertEquals(softWrap.getStart(), caretOffset); assertEquals( new VisualPosition(expectedVisualLine, softWrap.getIndentInColumns()), caretModel.getVisualPosition()); // Expected that caret is moved to visual line start when it's located on soft wrap start // offset at the moment. home(); assertEquals(softWrap.getStart(), caretModel.getOffset()); assertEquals(new VisualPosition(expectedVisualLine, 0), caretModel.getVisualPosition()); softWraps.remove(softWraps.size() - 1); expectedVisualLine--; } // Expecting caret to be located on the first non-white space symbol of non-soft wrapped line. home(); assertEquals(text.indexOf("public"), caretModel.getOffset()); assertEquals( new VisualPosition(expectedVisualLine, text.indexOf("public") - text.indexOf("{\n") - 2), caretModel.getVisualPosition()); }
/** * Applies given caret/selection state to the editor. Editor text must have been set up * previously. */ public static void setCaretsAndSelection(Editor editor, CaretAndSelectionState caretsState) { CaretModel caretModel = editor.getCaretModel(); if (caretModel.supportsMultipleCarets()) { List<CaretState> states = new ArrayList<CaretState>(caretsState.carets.size()); for (CaretInfo caret : caretsState.carets) { states.add( new CaretState( caret.position == null ? null : editor.offsetToLogicalPosition(caret.getCaretOffset(editor.getDocument())), caret.selection == null ? null : editor.offsetToLogicalPosition(caret.selection.getStartOffset()), caret.selection == null ? null : editor.offsetToLogicalPosition(caret.selection.getEndOffset()))); } caretModel.setCaretsAndSelections(states); } else { assertEquals("Multiple carets are not supported by the model", 1, caretsState.carets.size()); CaretInfo caret = caretsState.carets.get(0); if (caret.position != null) { caretModel.moveToOffset(caret.getCaretOffset(editor.getDocument())); } if (caret.selection != null) { editor .getSelectionModel() .setSelection(caret.selection.getStartOffset(), caret.selection.getEndOffset()); } else { editor.getSelectionModel().removeSelection(); } } if (caretsState.blockSelection != null) { editor .getSelectionModel() .setBlockSelection( editor.offsetToLogicalPosition(caretsState.blockSelection.getStartOffset()), editor.offsetToLogicalPosition(caretsState.blockSelection.getEndOffset())); } }
public void navigateIn(@NotNull Editor e) { final int offset = getOffset(); CaretModel caretModel = e.getCaretModel(); boolean caretMoved = false; if (myLogicalLine >= 0) { LogicalPosition pos = new LogicalPosition(myLogicalLine, Math.max(myLogicalColumn, 0)); if (offset < 0 || offset == e.logicalPositionToOffset(pos)) { caretModel.moveToLogicalPosition(pos); caretMoved = true; } } if (!caretMoved && offset >= 0) { caretModel.moveToOffset(Math.min(offset, e.getDocument().getTextLength())); caretMoved = true; } if (caretMoved) { e.getSelectionModel().removeSelection(); scrollToCaret(e); unfoldCurrentLine(e); } }
public void testCaretPositionOnFoldRegionExpand() throws IOException { // We had a problem that caret preserved its visual position instead of offset. This test checks // that. String text = "/**\n" + " * This is a test comment\n" + " */\n" + "public class Test {\n" + "}"; init(100, text); addCollapsedFoldRegion(0, text.indexOf("public") - 1, "/**...*/"); int offset = text.indexOf("class"); CaretModel caretModel = myEditor.getCaretModel(); caretModel.moveToOffset(offset); assertEquals(offset, caretModel.getOffset()); assertEquals(1, caretModel.getVisualPosition().line); toggleFoldRegionState(getFoldRegion(0), true); assertEquals(3, caretModel.getVisualPosition().line); assertEquals(offset, caretModel.getOffset()); }
@Override @SuppressWarnings({"AssignmentToForLoopParameter"}) public void paint( @NotNull Editor editor, @NotNull RangeHighlighter highlighter, @NotNull Graphics g) { int startOffset = highlighter.getStartOffset(); final Document doc = highlighter.getDocument(); if (startOffset >= doc.getTextLength()) return; final int endOffset = highlighter.getEndOffset(); final int endLine = doc.getLineNumber(endOffset); int off; int startLine = doc.getLineNumber(startOffset); IndentGuideDescriptor descriptor = editor.getIndentsModel().getDescriptor(startLine, endLine); final CharSequence chars = doc.getCharsSequence(); do { int start = doc.getLineStartOffset(startLine); int end = doc.getLineEndOffset(startLine); off = CharArrayUtil.shiftForward(chars, start, end, " \t"); startLine--; } while (startLine > 1 && off < doc.getTextLength() && chars.charAt(off) == '\n'); final VisualPosition startPosition = editor.offsetToVisualPosition(off); int indentColumn = startPosition.column; // It's considered that indent guide can cross not only white space but comments, javadocs // etc. Hence, there is a possible // case that the first indent guide line is, say, single-line comment where comment // symbols ('//') are located at the first // visual column. We need to calculate correct indent guide column then. int lineShift = 1; if (indentColumn <= 0 && descriptor != null) { indentColumn = descriptor.indentLevel; lineShift = 0; } if (indentColumn <= 0) return; final FoldingModel foldingModel = editor.getFoldingModel(); if (foldingModel.isOffsetCollapsed(off)) return; final FoldRegion headerRegion = foldingModel.getCollapsedRegionAtOffset(doc.getLineEndOffset(doc.getLineNumber(off))); final FoldRegion tailRegion = foldingModel.getCollapsedRegionAtOffset( doc.getLineStartOffset(doc.getLineNumber(endOffset))); if (tailRegion != null && tailRegion == headerRegion) return; final boolean selected; final IndentGuideDescriptor guide = editor.getIndentsModel().getCaretIndentGuide(); if (guide != null) { final CaretModel caretModel = editor.getCaretModel(); final int caretOffset = caretModel.getOffset(); selected = caretOffset >= off && caretOffset < endOffset && caretModel.getLogicalPosition().column == indentColumn; } else { selected = false; } Point start = editor.visualPositionToXY( new VisualPosition(startPosition.line + lineShift, indentColumn)); final VisualPosition endPosition = editor.offsetToVisualPosition(endOffset); Point end = editor.visualPositionToXY(new VisualPosition(endPosition.line, endPosition.column)); int maxY = end.y; if (endPosition.line == editor.offsetToVisualPosition(doc.getTextLength()).line) { maxY += editor.getLineHeight(); } Rectangle clip = g.getClipBounds(); if (clip != null) { if (clip.y >= maxY || clip.y + clip.height <= start.y) { return; } maxY = Math.min(maxY, clip.y + clip.height); } final EditorColorsScheme scheme = editor.getColorsScheme(); g.setColor( selected ? scheme.getColor(EditorColors.SELECTED_INDENT_GUIDE_COLOR) : scheme.getColor(EditorColors.INDENT_GUIDE_COLOR)); // There is a possible case that indent line intersects soft wrap-introduced text. // Example: // this is a long line <soft-wrap> // that| is soft-wrapped // | // | <- vertical indent // // Also it's possible that no additional intersections are added because of soft wrap: // this is a long line <soft-wrap> // | that is soft-wrapped // | // | <- vertical indent // We want to use the following approach then: // 1. Show only active indent if it crosses soft wrap-introduced text; // 2. Show indent as is if it doesn't intersect with soft wrap-introduced text; if (selected) { g.drawLine(start.x + 2, start.y, start.x + 2, maxY); } else { int y = start.y; int newY = start.y; SoftWrapModel softWrapModel = editor.getSoftWrapModel(); int lineHeight = editor.getLineHeight(); for (int i = Math.max(0, startLine + lineShift); i < endLine && newY < maxY; i++) { List<? extends SoftWrap> softWraps = softWrapModel.getSoftWrapsForLine(i); int logicalLineHeight = softWraps.size() * lineHeight; if (i > startLine + lineShift) { logicalLineHeight += lineHeight; // We assume that initial 'y' value points just below the target // line. } if (!softWraps.isEmpty() && softWraps.get(0).getIndentInColumns() < indentColumn) { if (y < newY || i > startLine + lineShift) { // There is a possible case that soft wrap is located on // indent start line. g.drawLine(start.x + 2, y, start.x + 2, newY + lineHeight); } newY += logicalLineHeight; y = newY; } else { newY += logicalLineHeight; } FoldRegion foldRegion = foldingModel.getCollapsedRegionAtOffset(doc.getLineEndOffset(i)); if (foldRegion != null && foldRegion.getEndOffset() < doc.getTextLength()) { i = doc.getLineNumber(foldRegion.getEndOffset()); } } if (y < maxY) { g.drawLine(start.x + 2, y, start.x + 2, maxY); } } }
public void performAction(IntroduceOperation operation) { final PsiFile file = operation.getFile(); if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) { return; } final Editor editor = operation.getEditor(); if (editor.getSettings().isVariableInplaceRenameEnabled()) { final TemplateState templateState = TemplateManagerImpl.getTemplateState(operation.getEditor()); if (templateState != null && !templateState.isFinished()) { return; } } PsiElement element1 = null; PsiElement element2 = null; final SelectionModel selectionModel = editor.getSelectionModel(); boolean singleElementSelection = false; if (selectionModel.hasSelection()) { element1 = file.findElementAt(selectionModel.getSelectionStart()); element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1); if (element1 instanceof PsiWhiteSpace) { int startOffset = element1.getTextRange().getEndOffset(); element1 = file.findElementAt(startOffset); } if (element2 instanceof PsiWhiteSpace) { int endOffset = element2.getTextRange().getStartOffset(); element2 = file.findElementAt(endOffset - 1); } if (element1 == element2) { singleElementSelection = true; } } else { if (smartIntroduce(operation)) { return; } final CaretModel caretModel = editor.getCaretModel(); final Document document = editor.getDocument(); int lineNumber = document.getLineNumber(caretModel.getOffset()); if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) { element1 = file.findElementAt(document.getLineStartOffset(lineNumber)); element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1); } } final Project project = operation.getProject(); if (element1 == null || element2 == null) { showCannotPerformError(project, editor); return; } element1 = PyRefactoringUtil.getSelectedExpression(project, file, element1, element2); if (element1 == null) { showCannotPerformError(project, editor); return; } if (singleElementSelection && element1 instanceof PyStringLiteralExpression) { final PyStringLiteralExpression literal = (PyStringLiteralExpression) element1; // Currently introduce for substrings of a multi-part string literals is not supported if (literal.getStringNodes().size() > 1) { showCannotPerformError(project, editor); return; } final int offset = element1.getTextOffset(); final TextRange selectionRange = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); final TextRange elementRange = element1.getTextRange(); if (!elementRange.equals(selectionRange) && elementRange.contains(selectionRange)) { final TextRange innerRange = literal.getStringValueTextRange(); final TextRange intersection = selectionRange.shiftRight(-offset).intersection(innerRange); final TextRange finalRange = intersection != null ? intersection : selectionRange; final String text = literal.getText(); if (getFormatValueExpression(literal) != null && breaksStringFormatting(text, finalRange) || getNewStyleFormatValueExpression(literal) != null && breaksNewStyleStringFormatting(text, finalRange) || breaksStringEscaping(text, finalRange)) { showCannotPerformError(project, editor); return; } element1.putUserData( PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE, Pair.create(element1, finalRange)); } } if (!checkIntroduceContext(file, editor, element1)) { return; } operation.setElement(element1); performActionOnElement(operation); }
@Override public void run() { CaretModel caretModel = myEditor.getCaretModel(); try { final CharSequence chars = myDocument.getCharsSequence(); int i = CharArrayUtil.shiftBackwardUntil(chars, myOffset - 1, LINE_SEPARATOR) - 1; i = CharArrayUtil.shiftBackwardUntil(chars, i, LINE_SEPARATOR) + 1; if (i < 0) i = 0; int lineStart = CharArrayUtil.shiftForward(chars, i, " \t"); CodeDocumentationUtil.CommentContext commentContext = CodeDocumentationUtil.tryParseCommentContext(myFile, chars, myOffset, lineStart); PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(getProject()); if (commentContext.docStart) { PsiElement element = myFile.findElementAt(commentContext.lineStart); final String text = element.getText(); final PsiElement parent = element.getParent(); if (text.equals(commentContext.commenter.getDocumentationCommentPrefix()) && isDocComment(parent, commentContext.commenter) || text.startsWith(commentContext.commenter.getDocumentationCommentPrefix()) && element instanceof PsiComment) { PsiComment comment = isDocComment(parent, commentContext.commenter) ? (PsiComment) parent : (PsiComment) element; int commentEnd = comment.getTextRange().getEndOffset(); if (myOffset >= commentEnd) { commentContext.docStart = false; } else { if (isCommentComplete(comment, commentContext.commenter, myEditor)) { if (myOffset >= commentEnd) { commentContext.docAsterisk = false; commentContext.docStart = false; } else { commentContext.docAsterisk = true; commentContext.docStart = false; } } else { generateJavadoc(commentContext.commenter); } } } else { commentContext.docStart = false; } } else if (commentContext.cStyleStart) { PsiElement element = myFile.findElementAt(commentContext.lineStart); if (element instanceof PsiComment && commentContext.commenter.getBlockCommentTokenType() == ((PsiComment) element).getTokenType()) { final PsiComment comment = (PsiComment) element; int commentEnd = comment.getTextRange().getEndOffset(); if (myOffset >= commentEnd && myOffset < myFile.getTextRange().getEndOffset()) { commentContext.docStart = false; } else { if (isCommentComplete(comment, commentContext.commenter, myEditor)) { if (myOffset >= commentEnd) { commentContext.docAsterisk = false; commentContext.docStart = false; } else { commentContext.docAsterisk = true; commentContext.docStart = false; } } else { final int currentEndOfLine = CharArrayUtil.shiftForwardUntil(chars, myOffset, "\n"); myDocument.insertString( currentEndOfLine, " " + commentContext.commenter.getBlockCommentSuffix()); int lstart = CharArrayUtil.shiftBackwardUntil(chars, myOffset, "\n"); myDocument.insertString(currentEndOfLine, chars.subSequence(lstart, myOffset)); psiDocumentManager.commitDocument(myDocument); } } } else { commentContext.docStart = false; } } String indentInsideJavadoc = null; if (myOffset < myDocument.getTextLength()) { final int line = myDocument.getLineNumber(myOffset); if (line > 0 && (commentContext.docAsterisk || commentContext.docStart)) { indentInsideJavadoc = CodeDocumentationUtil.getIndentInsideJavadoc( myDocument, myDocument.getLineStartOffset(line - 1)); } } if (commentContext.docAsterisk) { commentContext.docAsterisk = insertDocAsterisk( commentContext.lineStart, commentContext.docAsterisk, !StringUtil.isEmpty(indentInsideJavadoc), commentContext.commenter); } boolean docIndentApplied = false; CodeInsightSettings codeInsightSettings = CodeInsightSettings.getInstance(); if (codeInsightSettings.SMART_INDENT_ON_ENTER || myForceIndent || commentContext.docStart || commentContext.docAsterisk || commentContext.slashSlash) { final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject()); myOffset = codeStyleManager.adjustLineIndent(myFile, myOffset); psiDocumentManager.commitAllDocuments(); if (!StringUtil.isEmpty(indentInsideJavadoc) && myOffset < myDocument.getTextLength()) { myDocument.insertString(myOffset + 1, indentInsideJavadoc); myOffset += indentInsideJavadoc.length(); docIndentApplied = true; } if (myForceIndent && indentInsideJavadoc != null) { int indentSize = CodeStyleSettingsManager.getSettings(myProject).getIndentSize(myFile.getFileType()); myDocument.insertString(myOffset + 1, StringUtil.repeatSymbol(' ', indentSize)); myCaretAdvance += indentSize; } } if ((commentContext.docAsterisk || commentContext.docStart || commentContext.slashSlash) && !docIndentApplied) { if (myInsertSpace) { if (myOffset == myDocument.getTextLength()) { myDocument.insertString(myOffset, " "); } myDocument.insertString(myOffset + 1, " "); } final char c = myDocument.getCharsSequence().charAt(myOffset); if (c != '\n') { myOffset += 1; } } if ((commentContext.docAsterisk || commentContext.slashSlash) && !commentContext.docStart) { myCaretAdvance += commentContext.slashSlash ? commentContext.commenter.getLineCommentPrefix().length() : 1; } } catch (IncorrectOperationException e) { LOG.error(e); } myOffset = Math.min(myOffset, myDocument.getTextLength()); caretModel.moveToOffset(myOffset); myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); myEditor.getSelectionModel().removeSelection(); if (myCaretAdvance != 0) { LogicalPosition caretPosition = caretModel.getLogicalPosition(); LogicalPosition pos = new LogicalPosition(caretPosition.line, caretPosition.column + myCaretAdvance); caretModel.moveToLogicalPosition(pos); } }
private static void doPaste( final Editor editor, final Project project, final PsiFile file, final Document document, final Producer<Transferable> producer) { Transferable content = null; if (producer != null) { content = producer.produce(); } else { CopyPasteManager manager = CopyPasteManager.getInstance(); if (manager.areDataFlavorsAvailable(DataFlavor.stringFlavor)) { content = manager.getContents(); if (content != null) { manager.stopKillRings(); } } } if (content != null) { String text = null; try { text = (String) content.getTransferData(DataFlavor.stringFlavor); } catch (Exception e) { editor.getComponent().getToolkit().beep(); } if (text == null) return; final CodeInsightSettings settings = CodeInsightSettings.getInstance(); final Map<CopyPastePostProcessor, TextBlockTransferableData> extraData = new HashMap<CopyPastePostProcessor, TextBlockTransferableData>(); for (CopyPastePostProcessor processor : Extensions.getExtensions(CopyPastePostProcessor.EP_NAME)) { TextBlockTransferableData data = processor.extractTransferableData(content); if (data != null) { extraData.put(processor, data); } } text = TextBlockTransferable.convertLineSeparators(text, "\n", extraData.values()); final CaretModel caretModel = editor.getCaretModel(); final SelectionModel selectionModel = editor.getSelectionModel(); final int col = caretModel.getLogicalPosition().column; // There is a possible case that we want to perform paste while there is an active selection // at the editor and caret is located // inside it (e.g. Ctrl+A is pressed while caret is not at the zero column). We want to insert // the text at selection start column // then, hence, inserted block of text should be indented according to the selection start as // well. final int blockIndentAnchorColumn; final int caretOffset = caretModel.getOffset(); if (selectionModel.hasSelection() && caretOffset >= selectionModel.getSelectionStart()) { blockIndentAnchorColumn = editor.offsetToLogicalPosition(selectionModel.getSelectionStart()).column; } else { blockIndentAnchorColumn = col; } // We assume that EditorModificationUtil.insertStringAtCaret() is smart enough to remove // currently selected text (if any). RawText rawText = RawText.fromTransferable(content); String newText = text; for (CopyPastePreProcessor preProcessor : Extensions.getExtensions(CopyPastePreProcessor.EP_NAME)) { newText = preProcessor.preprocessOnPaste(project, file, editor, newText, rawText); } int indentOptions = text.equals(newText) ? settings.REFORMAT_ON_PASTE : CodeInsightSettings.REFORMAT_BLOCK; text = newText; if (LanguageFormatting.INSTANCE.forContext(file) == null && indentOptions != CodeInsightSettings.NO_REFORMAT) { indentOptions = CodeInsightSettings.INDENT_BLOCK; } final String _text = text; ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { EditorModificationUtil.insertStringAtCaret(editor, _text, false, true); } }); int length = text.length(); int offset = caretModel.getOffset() - length; if (offset < 0) { length += offset; offset = 0; } final RangeMarker bounds = document.createRangeMarker(offset, offset + length); caretModel.moveToOffset(bounds.getEndOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); selectionModel.removeSelection(); final Ref<Boolean> indented = new Ref<Boolean>(Boolean.FALSE); for (Map.Entry<CopyPastePostProcessor, TextBlockTransferableData> e : extraData.entrySet()) { //noinspection unchecked e.getKey() .processTransferableData(project, editor, bounds, caretOffset, indented, e.getValue()); } boolean pastedTextContainsWhiteSpacesOnly = CharArrayUtil.shiftForward(document.getCharsSequence(), bounds.getStartOffset(), " \n\t") >= bounds.getEndOffset(); VirtualFile virtualFile = file.getVirtualFile(); if (!pastedTextContainsWhiteSpacesOnly && (virtualFile == null || !SingleRootFileViewProvider.isTooLargeForIntelligence(virtualFile))) { final int indentOptions1 = indentOptions; ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { switch (indentOptions1) { case CodeInsightSettings.INDENT_BLOCK: if (!indented.get()) { indentBlock( project, editor, bounds.getStartOffset(), bounds.getEndOffset(), blockIndentAnchorColumn); } break; case CodeInsightSettings.INDENT_EACH_LINE: if (!indented.get()) { indentEachLine( project, editor, bounds.getStartOffset(), bounds.getEndOffset()); } break; case CodeInsightSettings.REFORMAT_BLOCK: indentEachLine( project, editor, bounds.getStartOffset(), bounds .getEndOffset()); // this is needed for example when inserting a // comment before method reformatBlock( project, editor, bounds.getStartOffset(), bounds.getEndOffset()); break; } } }); } if (bounds.isValid()) { caretModel.moveToOffset(bounds.getEndOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); selectionModel.removeSelection(); editor.putUserData(EditorEx.LAST_PASTED_REGION, TextRange.create(bounds)); } } }
/** * Generates a comment if possible. * * <p>It's assumed that this method {@link PsiDocumentManager#commitDocument(Document) syncs} all * PSI-document changes during the processing. * * @param anchor target element for which a comment should be generated * @param editor target editor * @param commenter commenter to use * @param project current project */ private static void generateComment( @NotNull PsiElement anchor, @NotNull Editor editor, @NotNull CodeDocumentationProvider documentationProvider, @NotNull CodeDocumentationAwareCommenter commenter, @NotNull Project project) { Document document = editor.getDocument(); int commentStartOffset = anchor.getTextRange().getStartOffset(); int lineStartOffset = document.getLineStartOffset(document.getLineNumber(commentStartOffset)); if (lineStartOffset > 0 && lineStartOffset < commentStartOffset) { // Example: // void test1() { // } // void test2() { // <offset> // } // We want to insert the comment at the start of the line where 'test2()' is declared. int nonWhiteSpaceOffset = CharArrayUtil.shiftBackward(document.getCharsSequence(), commentStartOffset - 1, " \t"); commentStartOffset = Math.max(nonWhiteSpaceOffset, lineStartOffset); } int commentBodyRelativeOffset = 0; int caretOffsetToSet = -1; StringBuilder buffer = new StringBuilder(); String commentPrefix = commenter.getDocumentationCommentPrefix(); if (commentPrefix != null) { buffer.append(commentPrefix).append("\n"); commentBodyRelativeOffset += commentPrefix.length() + 1; } String linePrefix = commenter.getDocumentationCommentLinePrefix(); if (linePrefix != null) { buffer.append(linePrefix); commentBodyRelativeOffset += linePrefix.length(); caretOffsetToSet = commentStartOffset + commentBodyRelativeOffset; } buffer.append("\n"); commentBodyRelativeOffset++; String commentSuffix = commenter.getDocumentationCommentSuffix(); if (commentSuffix != null) { buffer.append(commentSuffix).append("\n"); } if (buffer.length() <= 0) { return; } document.insertString(commentStartOffset, buffer); PsiDocumentManager docManager = PsiDocumentManager.getInstance(project); docManager.commitDocument(document); Pair<PsiElement, PsiComment> pair = documentationProvider.parseContext(anchor); if (pair == null || pair.second == null) { return; } String stub = documentationProvider.generateDocumentationContentStub(pair.second); CaretModel caretModel = editor.getCaretModel(); if (stub != null) { int insertionOffset = commentStartOffset + commentBodyRelativeOffset; // if (CodeStyleSettingsManager.getSettings(project).JD_ADD_BLANK_AFTER_DESCRIPTION) { // buffer.setLength(0); // if (linePrefix != null) { // buffer.append(linePrefix); // } // buffer.append("\n"); // buffer.append(stub); // stub = buffer.toString(); // } document.insertString(insertionOffset, stub); docManager.commitDocument(document); pair = documentationProvider.parseContext(anchor); } if (caretOffsetToSet >= 0) { caretModel.moveToOffset(caretOffsetToSet); } if (pair == null || pair.second == null) { return; } int start = Math.min(calcStartReformatOffset(pair.first), calcStartReformatOffset(pair.second)); int end = pair.second.getTextRange().getEndOffset(); CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); codeStyleManager.reformatText(anchor.getContainingFile(), start, end); int caretOffset = caretModel.getOffset(); if (caretOffset > 0 && caretOffset <= document.getTextLength()) { char c = document.getCharsSequence().charAt(caretOffset - 1); if (!StringUtil.isWhiteSpace(c)) { document.insertString(caretOffset, " "); caretModel.moveToOffset(caretOffset + 1); } } }
public static void verifyCaretAndSelectionState( Editor editor, CaretAndSelectionState caretState, String message) { boolean hasChecks = false; for (int i = 0; i < caretState.carets.size(); i++) { EditorTestUtil.CaretInfo expected = caretState.carets.get(i); if (expected.position != null || expected.selection != null) { hasChecks = true; break; } } if (!hasChecks) { return; // nothing to check, so we skip caret/selection assertions } String messageSuffix = message == null ? "" : (message + ": "); CaretModel caretModel = editor.getCaretModel(); List<Caret> allCarets = new ArrayList<Caret>(caretModel.getAllCarets()); assertEquals( messageSuffix + " Unexpected number of carets", caretState.carets.size(), allCarets.size()); for (int i = 0; i < caretState.carets.size(); i++) { String caretDescription = caretState.carets.size() == 1 ? "" : "caret " + (i + 1) + "/" + caretState.carets.size() + " "; Caret currentCaret = allCarets.get(i); int actualCaretLine = editor.getDocument().getLineNumber(currentCaret.getOffset()); int actualCaretColumn = currentCaret.getOffset() - editor.getDocument().getLineStartOffset(actualCaretLine); LogicalPosition actualCaretPosition = new LogicalPosition(actualCaretLine, actualCaretColumn); int selectionStart = currentCaret.getSelectionStart(); int selectionEnd = currentCaret.getSelectionEnd(); LogicalPosition actualSelectionStart = editor.offsetToLogicalPosition(selectionStart); LogicalPosition actualSelectionEnd = editor.offsetToLogicalPosition(selectionEnd); CaretInfo expected = caretState.carets.get(i); if (expected.position != null) { assertEquals( messageSuffix + caretDescription + "unexpected caret position", expected.position, actualCaretPosition); } if (expected.selection != null) { LogicalPosition expectedSelectionStart = editor.offsetToLogicalPosition(expected.selection.getStartOffset()); LogicalPosition expectedSelectionEnd = editor.offsetToLogicalPosition(expected.selection.getEndOffset()); assertEquals( messageSuffix + caretDescription + "unexpected selection start", expectedSelectionStart, actualSelectionStart); assertEquals( messageSuffix + caretDescription + "unexpected selection end", expectedSelectionEnd, actualSelectionEnd); } else { assertFalse( messageSuffix + caretDescription + "should has no selection, but was: (" + actualSelectionStart + ", " + actualSelectionEnd + ")", currentCaret.hasSelection()); } } }
public void testEndProcessing() throws IOException { String text = "class Test {\n" + " public String s = \"this is a long string literal that is expected to be soft-wrapped into multiple visual lines\"; \n" + "}"; init(30, text); myEditor.getCaretModel().moveToOffset(text.indexOf("\n") + 1); List<? extends SoftWrap> softWraps = new ArrayList<SoftWrap>(getSoftWrapModel().getRegisteredSoftWraps()); assertTrue(!softWraps.isEmpty()); CaretModel caretModel = myEditor.getCaretModel(); int expectedVisualLine = caretModel.getVisualPosition().line; while (!softWraps.isEmpty()) { SoftWrap softWrap = softWraps.get(0); int caretOffsetBefore = caretModel.getOffset(); end(); // Expecting the caret to be moved at the last non-white space symbol on the current visual // line. int caretOffset = caretModel.getOffset(); assertTrue(caretOffset > caretOffsetBefore); assertFalse(caretOffset > softWrap.getStart()); if (caretOffset < softWrap.getStart()) { // There is a possible case that there are white space symbols between caret position // applied on 'end' processing and // soft wrap. Let's check that and emulate one more 'end' typing in order to move caret // right before soft wrap. for (int i = caretOffset; i < softWrap.getStart(); i++) { char c = text.charAt(i); assertTrue(c == ' ' || c == '\t'); } caretOffsetBefore = caretOffset; end(); caretOffset = caretModel.getOffset(); assertTrue(caretOffset > caretOffsetBefore); } assertEquals(softWrap.getStart(), caretOffset); assertEquals( new VisualPosition( expectedVisualLine, myEditor.offsetToVisualPosition(softWrap.getStart() - 1).column + 1), caretModel.getVisualPosition()); softWraps.remove(0); expectedVisualLine++; } // Check that caret is placed on a last non-white space symbol on current logical line. end(); int lastNonWhiteSpaceSymbolOffset = text.indexOf("\";") + 2; assertEquals(lastNonWhiteSpaceSymbolOffset, caretModel.getOffset()); assertEquals( myEditor.offsetToVisualPosition(lastNonWhiteSpaceSymbolOffset), caretModel.getVisualPosition()); assertEquals(expectedVisualLine, caretModel.getVisualPosition().line); // Check that caret is place to the very end of the logical line. end(); int lastSymbolOffset = myEditor.getDocument().getLineEndOffset(caretModel.getLogicalPosition().line); assertEquals(lastSymbolOffset, caretModel.getOffset()); assertEquals(myEditor.offsetToVisualPosition(lastSymbolOffset), caretModel.getVisualPosition()); assertEquals(expectedVisualLine, caretModel.getVisualPosition().line); }
public final void move(Editor editor, final PsiFile file) { myMover.beforeMove(editor, myInfo, myIsDown); final Document document = editor.getDocument(); final int start = StatementUpDownMover.getLineStartSafeOffset(document, myInfo.toMove.startLine); final int end = StatementUpDownMover.getLineStartSafeOffset(document, myInfo.toMove.endLine); myInfo.range1 = document.createRangeMarker(start, end); String textToInsert = document.getCharsSequence().subSequence(start, end).toString(); if (!StringUtil.endsWithChar(textToInsert, '\n')) textToInsert += '\n'; final int start2 = document.getLineStartOffset(myInfo.toMove2.startLine); final int end2 = StatementUpDownMover.getLineStartSafeOffset(document, myInfo.toMove2.endLine); String textToInsert2 = document.getCharsSequence().subSequence(start2, end2).toString(); if (!StringUtil.endsWithChar(textToInsert2, '\n')) textToInsert2 += '\n'; myInfo.range2 = document.createRangeMarker(start2, end2); if (myInfo.range1.getStartOffset() < myInfo.range2.getStartOffset()) { myInfo.range1.setGreedyToLeft(true); myInfo.range1.setGreedyToRight(false); myInfo.range2.setGreedyToLeft(true); myInfo.range2.setGreedyToRight(true); } else { myInfo.range1.setGreedyToLeft(true); myInfo.range1.setGreedyToRight(true); myInfo.range2.setGreedyToLeft(true); myInfo.range2.setGreedyToRight(false); } final CaretModel caretModel = editor.getCaretModel(); final int caretRelativePos = caretModel.getOffset() - start; final SelectionModel selectionModel = editor.getSelectionModel(); final int selectionStart = selectionModel.getSelectionStart(); final int selectionEnd = selectionModel.getSelectionEnd(); final boolean hasSelection = selectionModel.hasSelection(); // to prevent flicker caretModel.moveToOffset(0); // There is a possible case that the user performs, say, method move. It's also possible that // one (or both) of moved methods // are folded. We want to preserve their states then. The problem is that folding processing is // based on PSI element pointers // and the pointers behave as following during move up/down: // method1() {} // method2() {} // Pointer for the fold region from method1 points to 'method2()' now and vice versa (check // range markers processing on // document change for further information). I.e. information about fold regions statuses holds // the data swapped for // 'method1' and 'method2'. Hence, we want to apply correct 'collapsed' status. FoldRegion topRegion = null; FoldRegion bottomRegion = null; for (FoldRegion foldRegion : editor.getFoldingModel().getAllFoldRegions()) { if (!foldRegion.isValid() || (!contains(myInfo.range1, foldRegion) && !contains(myInfo.range2, foldRegion))) { continue; } if (contains(myInfo.range1, foldRegion) && !contains(topRegion, foldRegion)) { topRegion = foldRegion; } else if (contains(myInfo.range2, foldRegion) && !contains(bottomRegion, foldRegion)) { bottomRegion = foldRegion; } } document.insertString(myInfo.range1.getStartOffset(), textToInsert2); document.deleteString( myInfo.range1.getStartOffset() + textToInsert2.length(), myInfo.range1.getEndOffset()); document.insertString(myInfo.range2.getStartOffset(), textToInsert); int s = myInfo.range2.getStartOffset() + textToInsert.length(); int e = myInfo.range2.getEndOffset(); if (e > s) { document.deleteString(s, e); } final Project project = file.getProject(); PsiDocumentManager.getInstance(project).commitAllDocuments(); // Swap fold regions status if necessary. if (topRegion != null && bottomRegion != null) { final FoldRegion finalTopRegion = topRegion; final FoldRegion finalBottomRegion = bottomRegion; editor .getFoldingModel() .runBatchFoldingOperation( new Runnable() { @Override public void run() { boolean topExpanded = finalTopRegion.isExpanded(); finalTopRegion.setExpanded(finalBottomRegion.isExpanded()); finalBottomRegion.setExpanded(topExpanded); } }); } CodeFoldingManager.getInstance(project).allowFoldingOnCaretLine(editor); if (hasSelection) { restoreSelection(editor, selectionStart, selectionEnd, start, myInfo.range2.getStartOffset()); } caretModel.moveToOffset(myInfo.range2.getStartOffset() + caretRelativePos); if (myInfo.indentTarget) { indentLinesIn(editor, file, document, project, myInfo.range2); } if (myInfo.indentSource) { indentLinesIn(editor, file, document, project, myInfo.range1); } myMover.afterMove(editor, file, myInfo, myIsDown); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); }