@Override public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) { CommandProcessor.getInstance().setCurrentCommandGroupId(EditorActionUtil.DELETE_COMMAND_GROUP); CopyPasteManager.getInstance().stopKillRings(); boolean camelMode = editor.getSettings().isCamelWords(); if (myNegateCamelMode) { camelMode = !camelMode; } if (editor.getSelectionModel().hasSelection()) { EditHelpers.deleteSelectedText(editor); return; } deleteToWordEnd(editor, camelMode); }
private static int getWordEndOffset(Editor editor, int offset, boolean camelMode) { Document document = editor.getDocument(); CharSequence text = document.getCharsSequence(); if (offset >= document.getTextLength() - 1) return offset; int newOffset = offset + 1; int lineNumber = editor.getCaretModel().getLogicalPosition().line; int maxOffset = document.getLineEndOffset(lineNumber); if (newOffset > maxOffset) { return offset; } for (; newOffset < maxOffset; newOffset++) { if (EditorActionUtil.isWordEnd(text, newOffset, camelMode) || EditorActionUtil.isWordStart(text, newOffset, camelMode)) { break; } } return newOffset; }
int getWordAtCaretEnd() { Document document = myEditor.getDocument(); int offset = getOffset(); if (offset >= document.getTextLength() - 1 || document.getLineCount() == 0) return offset; int newOffset = offset + 1; int lineNumber = getLogicalPosition().line; int maxOffset = document.getLineEndOffset(lineNumber); if (newOffset > maxOffset) { if (lineNumber + 1 >= document.getLineCount()) return offset; maxOffset = document.getLineEndOffset(lineNumber + 1); } boolean camel = myEditor.getSettings().isCamelWords(); for (; newOffset < maxOffset; newOffset++) { if (EditorActionUtil.isWordOrLexemeEnd(myEditor, newOffset, camel)) break; } return newOffset; }
/** * Creates a selection in editor per each search result. Existing carets and selections in editor are discarded. * * @param caretShiftFromSelectionStart if non-negative, defines caret position relative to selection start, for each created selection. * if negative, carets will be positioned at selection ends */ public static void selectSearchResultsInEditor(@NotNull Editor editor, @NotNull Iterator<FindResult> resultIterator, int caretShiftFromSelectionStart) { if (!editor.getCaretModel().supportsMultipleCarets()) { return; } ArrayList<CaretState> caretStates = new ArrayList<CaretState>(); while (resultIterator.hasNext()) { FindResult findResult = resultIterator.next(); int caretOffset = getCaretPosition(findResult, caretShiftFromSelectionStart); int selectionStartOffset = findResult.getStartOffset(); int selectionEndOffset = findResult.getEndOffset(); EditorActionUtil.makePositionVisible(editor, caretOffset); EditorActionUtil.makePositionVisible(editor, selectionStartOffset); EditorActionUtil.makePositionVisible(editor, selectionEndOffset); caretStates.add(new CaretState(editor.offsetToLogicalPosition(caretOffset), editor.offsetToLogicalPosition(selectionStartOffset), editor.offsetToLogicalPosition(selectionEndOffset))); } if (caretStates.isEmpty()) { return; } editor.getCaretModel().setCaretsAndSelections(caretStates); }
/** * Attempts to add a new caret to editor, with selection corresponding to given search result. * * @param caretShiftFromSelectionStart if non-negative, defines caret position relative to selection start, for each created selection. * if negative, caret will be positioned at selection end * @return <code>true</code> if caret was added successfully, <code>false</code> if it cannot be done, e.g. because a caret already * exists at target position */ public static boolean selectSearchResultInEditor(@NotNull Editor editor, @NotNull FindResult result, int caretShiftFromSelectionStart) { if (!editor.getCaretModel().supportsMultipleCarets()) { return false; } int caretOffset = getCaretPosition(result, caretShiftFromSelectionStart); EditorActionUtil.makePositionVisible(editor, caretOffset); Caret newCaret = editor.getCaretModel().addCaret(editor.offsetToVisualPosition(caretOffset)); if (newCaret == null) { return false; } else { int selectionStartOffset = result.getStartOffset(); int selectionEndOffset = result.getEndOffset(); EditorActionUtil.makePositionVisible(editor, selectionStartOffset); EditorActionUtil.makePositionVisible(editor, selectionEndOffset); newCaret.setSelection(selectionStartOffset, selectionEndOffset); return true; } }
int getWordAtCaretEnd() { Document document = myEditor.getDocument(); int offset = myEditor.getCaretModel().getOffset(); CharSequence text = document.getCharsSequence(); if (offset >= document.getTextLength() - 1 || document.getLineCount() == 0) return offset; int newOffset = offset + 1; int lineNumber = myEditor.getCaretModel().getLogicalPosition().line; int maxOffset = document.getLineEndOffset(lineNumber); if (newOffset > maxOffset) { if (lineNumber + 1 >= document.getLineCount()) return offset; maxOffset = document.getLineEndOffset(lineNumber + 1); } boolean camel = myEditor.getSettings().isCamelWords(); for (; newOffset < maxOffset; newOffset++) { if (EditorActionUtil.isWordEnd(text, newOffset, camel)) break; } return newOffset; }
private int getWordAtOffsetStart(int offset) { Document document = myEditor.getDocument(); if (offset == 0) { return 0; } int lineNumber = myEditor.offsetToLogicalPosition(offset).line; CharSequence text = document.getCharsSequence(); int newOffset = offset - 1; int minOffset = lineNumber > 0 ? document.getLineEndOffset(lineNumber - 1) : 0; boolean camel = myEditor.getSettings().isCamelWords(); for (; newOffset > minOffset; newOffset--) { if (EditorActionUtil.isWordStart(text, newOffset, camel)) { break; } } return newOffset; }
private int getWordAtOffsetEnd(int offset) { Document document = myEditor.getDocument(); CharSequence text = document.getCharsSequence(); if (offset >= document.getTextLength() - 1 || document.getLineCount() == 0) { return offset; } int newOffset = offset + 1; int lineNumber = myEditor.offsetToLogicalPosition(offset).line; int maxOffset = document.getLineEndOffset(lineNumber); if (newOffset > maxOffset) { if (lineNumber + 1 >= document.getLineCount()) { return offset; } maxOffset = document.getLineEndOffset(lineNumber + 1); } boolean camel = myEditor.getSettings().isCamelWords(); for (; newOffset < maxOffset; newOffset++) { if (EditorActionUtil.isWordEnd(text, newOffset, camel)) { break; } } return newOffset; }
/** * Creates a selection in editor per each search result. Existing carets and selections in editor are discarded. * * @param caretShiftFromSelectionStart if non-negative, defines caret position relative to selection start, for each created selection. * if negative, carets will be positioned at selection ends */ public static void selectSearchResultsInEditor(@Nonnull Editor editor, @Nonnull Iterator<FindResult> resultIterator, int caretShiftFromSelectionStart) { if (!editor.getCaretModel().supportsMultipleCarets()) { return; } ArrayList<CaretState> caretStates = new ArrayList<>(); while (resultIterator.hasNext()) { FindResult findResult = resultIterator.next(); int caretOffset = getCaretPosition(findResult, caretShiftFromSelectionStart); int selectionStartOffset = findResult.getStartOffset(); int selectionEndOffset = findResult.getEndOffset(); EditorActionUtil.makePositionVisible(editor, caretOffset); EditorActionUtil.makePositionVisible(editor, selectionStartOffset); EditorActionUtil.makePositionVisible(editor, selectionEndOffset); caretStates.add(new CaretState(editor.offsetToLogicalPosition(caretOffset), editor.offsetToLogicalPosition(selectionStartOffset), editor.offsetToLogicalPosition(selectionEndOffset))); } if (caretStates.isEmpty()) { return; } editor.getCaretModel().setCaretsAndSelections(caretStates); }
/** * Attempts to add a new caret to editor, with selection corresponding to given search result. * * @param caretShiftFromSelectionStart if non-negative, defines caret position relative to selection start, for each created selection. * if negative, caret will be positioned at selection end * @return <code>true</code> if caret was added successfully, <code>false</code> if it cannot be done, e.g. because a caret already * exists at target position */ public static boolean selectSearchResultInEditor(@Nonnull Editor editor, @Nonnull FindResult result, int caretShiftFromSelectionStart) { if (!editor.getCaretModel().supportsMultipleCarets()) { return false; } int caretOffset = getCaretPosition(result, caretShiftFromSelectionStart); EditorActionUtil.makePositionVisible(editor, caretOffset); Caret newCaret = editor.getCaretModel().addCaret(editor.offsetToVisualPosition(caretOffset)); if (newCaret == null) { return false; } else { int selectionStartOffset = result.getStartOffset(); int selectionEndOffset = result.getEndOffset(); EditorActionUtil.makePositionVisible(editor, selectionStartOffset); EditorActionUtil.makePositionVisible(editor, selectionEndOffset); newCaret.setSelection(selectionStartOffset, selectionEndOffset); return true; } }
private static int getWordAtCaretStart( Project project, String filePath ) { Editor[] editor = new Editor[1]; editor[0] = getActiveEditor( project ); if( !(editor[0] instanceof EditorImpl) ) { return -1; } EditorImpl editorImpl = (EditorImpl)editor[0]; if( !editorImpl.getVirtualFile().getPath().equals( filePath ) ) { return -1; } CaretModel cm = editorImpl.getCaretModel(); Document document = editorImpl.getDocument(); int offset = cm.getOffset(); if( offset == 0 ) { return 0; } int lineNumber = cm.getLogicalPosition().line; int newOffset = offset - 1; int minOffset = lineNumber > 0 ? document.getLineEndOffset( lineNumber - 1 ) : 0; for( ; newOffset > minOffset; newOffset-- ) { if( EditorActionUtil.isWordOrLexemeStart( editorImpl, newOffset, false ) ) { break; } } return newOffset; }
@Override public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) { CommandProcessor.getInstance().setCurrentCommandGroupId(EditorActionUtil.DELETE_COMMAND_GROUP); CopyPasteManager.getInstance().stopKillRings(); if (editor.getSelectionModel().hasSelection()) { EditHelpers.deleteSelectedText(editor); return; } deleteSpaces(editor); }
@Override public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) { CommandProcessor.getInstance().setCurrentCommandGroupId(EditorActionUtil.DELETE_COMMAND_GROUP); CopyPasteManager.getInstance().stopKillRings(); if (editor.getSelectionModel().hasSelection()) { EditorModificationUtil.deleteSelectedText(editor); return; } backspaceToWordStart(editor); }
public static boolean isWordTypeStart(int wordType, @NotNull Editor editor, int offset, boolean isCamel) { switch (wordType) { case WORD_SPACE_DELIMITED: return isWhitespaceEnd(editor.getDocument().getCharsSequence(), offset, isCamel); case WORD_IDE: return EditorActionUtil.isWordOrLexemeStart(editor, offset, isCamel); case WORD_MIA: return isWordStart(editor.getDocument().getCharsSequence(), offset, isCamel); case WORD_IDENTIFIER: return isIdentifierStart(editor.getDocument().getCharsSequence(), offset, isCamel); } return false; }
public static boolean isWordTypeEnd(int wordType, @NotNull Editor editor, int offset, boolean isCamel) { switch (wordType) { case WORD_SPACE_DELIMITED: return isWhitespaceStart(editor.getDocument().getCharsSequence(), offset); case WORD_IDE: return EditorActionUtil.isWordOrLexemeEnd(editor, offset, isCamel); case WORD_MIA: return isWordEnd(editor.getDocument().getCharsSequence(), offset, isCamel); case WORD_IDENTIFIER: return isIdentifierEnd(editor.getDocument().getCharsSequence(), offset, isCamel); } return false; }
private static void setupSelection(@NotNull Editor editor, boolean isWithSelection, int selectionStart, @NotNull LogicalPosition blockSelectionStart) { SelectionModel selectionModel = editor.getSelectionModel(); CaretModel caretModel = editor.getCaretModel(); if (isWithSelection) { if (editor.isColumnMode() && !caretModel.supportsMultipleCarets()) { selectionModel.setBlockSelection(blockSelectionStart, caretModel.getLogicalPosition()); } else { selectionModel.setSelection(selectionStart, caretModel.getVisualPosition(), caretModel.getOffset()); } } else { selectionModel.removeSelection(); } EditorActionUtil.selectNonexpandableFold(editor); }
private void doDeleteTest() { doTest(() -> { final Editor editor = myFixture.getEditor(); final Document document = editor.getDocument(); final int selectionStart = editor.getSelectionModel().getSelectionStart(); final int selectionEnd = editor.getSelectionModel().getSelectionEnd(); CommandProcessor.getInstance().setCurrentCommandGroupId(EditorActionUtil.DELETE_COMMAND_GROUP); document.deleteString(selectionStart, selectionEnd); editor.getCaretModel().moveToOffset(selectionStart); }, true, ChangeSignatureDetectorAction.CHANGE_SIGNATURE); }
@Override public void invokePopup(final EditorMouseEvent event) { if (myEditorPopupActions.isEmpty()) return; ActionGroup group = new DefaultActionGroup(myEditorPopupActions); EditorPopupHandler handler = EditorActionUtil.createEditorPopupHandler(group); handler.invokePopup(event); }
@Override public void moveToOffset(final int offset, final boolean locateBeforeSoftWrap) { assertIsDispatchThread(); validateCallContext(); if (mySkipChangeRequests) { return; } myEditor.getCaretModel().doWithCaretMerging(new Runnable() { @Override public void run() { final LogicalPosition logicalPosition = myEditor.offsetToLogicalPosition(offset); CaretEvent event = moveToLogicalPosition(logicalPosition, locateBeforeSoftWrap, null, false); final LogicalPosition positionByOffsetAfterMove = myEditor.offsetToLogicalPosition(myOffset); if (!positionByOffsetAfterMove.equals(logicalPosition)) { StringBuilder debugBuffer = new StringBuilder(); moveToLogicalPosition(logicalPosition, locateBeforeSoftWrap, debugBuffer, true); int textStart = Math.max(0, Math.min(offset, myOffset) - 1); final DocumentEx document = myEditor.getDocument(); int textEnd = Math.min(document.getTextLength() - 1, Math.max(offset, myOffset) + 1); CharSequence text = document.getCharsSequence().subSequence(textStart, textEnd); int inverseOffset = myEditor.logicalPositionToOffset(logicalPosition); LogMessageEx.error( LOG, "caret moved to wrong offset. Please submit a dedicated ticket and attach current editor's text to it.", "Requested: offset=" + offset + ", logical position='" + logicalPosition + "' but actual: offset=" + myOffset + ", logical position='" + myLogicalCaret + "' (" + positionByOffsetAfterMove + "). " + myEditor.dumpState() + "\ninterested text [" + textStart + ";" + textEnd + "): '" + text + "'\n debug trace: " + debugBuffer + "\nLogical position -> offset ('" + logicalPosition + "'->'" + inverseOffset + "')" ); } if (event != null) { myEditor.getCaretModel().fireCaretPositionChanged(event); EditorActionUtil.selectNonexpandableFold(myEditor); } } }); }
int getWordAtCaretStart() { Document document = myEditor.getDocument(); int offset = getOffset(); if (offset == 0) return 0; int lineNumber = getLogicalPosition().line; int newOffset = offset - 1; int minOffset = lineNumber > 0 ? document.getLineEndOffset(lineNumber - 1) : 0; boolean camel = myEditor.getSettings().isCamelWords(); for (; newOffset > minOffset; newOffset--) { if (EditorActionUtil.isWordOrLexemeStart(myEditor, newOffset, camel)) break; } return newOffset; }
@Nullable private static TextRange getCamelSelectionRange(CharSequence editorText, int cursorOffset, CharCondition isWordPartCondition) { if (cursorOffset < 0 || cursorOffset >= editorText.length()) { return null; } if (cursorOffset > 0 && !isWordPartCondition.value(editorText.charAt(cursorOffset)) && isWordPartCondition.value(editorText.charAt(cursorOffset - 1))) { cursorOffset--; } if (isWordPartCondition.value(editorText.charAt(cursorOffset))) { int start = cursorOffset; int end = cursorOffset + 1; final int textLen = editorText.length(); while (start > 0 && isWordPartCondition.value(editorText.charAt(start - 1)) && !EditorActionUtil.isHumpBound(editorText, start, true)) { start--; } while (end < textLen && isWordPartCondition.value(editorText.charAt(end)) && !EditorActionUtil.isHumpBound(editorText, end, false)) { end++; } if (start + 1 < end) { return new TextRange(start, end); } } return null; }
protected String getNewIndent( @NotNull final PsiFile file, @NotNull final Document document, @NotNull final CharSequence oldIndent) { CharSequence nonEmptyIndent = oldIndent; final CharSequence editorCharSequence = document.getCharsSequence(); final int nLines = document.getLineCount(); for (int line = 0; line < nLines && nonEmptyIndent.length() == 0; ++line) { final int lineStart = document.getLineStartOffset(line); final int indentEnd = EditorActionUtil.findFirstNonSpaceOffsetOnTheLine(document, line); if (lineStart < indentEnd) { nonEmptyIndent = editorCharSequence.subSequence(lineStart, indentEnd); } } final boolean usesSpacesForIndentation = nonEmptyIndent.length() > 0 && nonEmptyIndent.charAt(nonEmptyIndent.length() - 1) == ' '; final boolean firstIndent = nonEmptyIndent.length() == 0; final CodeStyleSettings currentSettings = CodeStyleSettingsManager.getSettings(file.getProject()); final CommonCodeStyleSettings.IndentOptions indentOptions = currentSettings.getIndentOptions(file.getFileType()); if (firstIndent && indentOptions.USE_TAB_CHARACTER || !firstIndent && !usesSpacesForIndentation) { int nTabsToIndent = indentOptions.INDENT_SIZE / indentOptions.TAB_SIZE; if (indentOptions.INDENT_SIZE % indentOptions.TAB_SIZE != 0) { ++nTabsToIndent; } return oldIndent + StringUtil.repeatSymbol('\t', nTabsToIndent); } return oldIndent + StringUtil.repeatSymbol(' ', indentOptions.INDENT_SIZE); }
private static boolean canPlaceBulbOnTheSameLine(Editor editor) { if (ApplicationManager.getApplication().isUnitTestMode() || editor.isOneLineMode()) return false; final int offset = editor.getCaretModel().getOffset(); final VisualPosition pos = editor.offsetToVisualPosition(offset); int line = pos.line; final int firstNonSpaceColumnOnTheLine = EditorActionUtil.findFirstNonSpaceColumnOnTheLine(editor, line); if (firstNonSpaceColumnOnTheLine == -1) return false; final Point point = editor.visualPositionToXY(new VisualPosition(line, firstNonSpaceColumnOnTheLine)); return point.x > AllIcons.Actions.RealIntentionBulb.getIconWidth() + (editor.isOneLineMode() ? SMALL_BORDER_SIZE : NORMAL_BORDER_SIZE) * 2; }
@Override public void beforeDocumentChange(DocumentEvent e) { if (myDeaf) return; if (DumbService.isDumb(myProject)) return; if (myInitialText == null) { final Document document = e.getDocument(); final PsiDocumentManager documentManager = myPsiDocumentManager; if (!documentManager.isUncommited(document)) { final CommandProcessor processor = CommandProcessor.getInstance(); final String currentCommandName = processor.getCurrentCommandName(); if (!Comparing.strEqual(TYPING_COMMAND_NAME, currentCommandName) && !Comparing.strEqual(PASTE_COMMAND_NAME, currentCommandName) && !Comparing.strEqual("Cut", currentCommandName) && !Comparing.strEqual(LanguageChangeSignatureDetector.MOVE_PARAMETER, currentCommandName) && !Comparing.equal(EditorActionUtil.DELETE_COMMAND_GROUP, processor.getCurrentCommandGroupId())) { return; } final PsiFile file = documentManager.getPsiFile(document); if (file != null) { final PsiElement element = file.findElementAt(e.getOffset()); if (element != null) { final ChangeInfo info = createInitialChangeInfo(element); if (info != null) { final PsiElement method = info.getMethod(); final TextRange textRange = method.getTextRange(); if (document.getTextLength() <= textRange.getEndOffset()) return; if (method instanceof PsiNameIdentifierOwner) { myInitialName = ((PsiNameIdentifierOwner)method).getName(); } myInitialText = document.getText(textRange); myInitialChangeInfo = info; } } } } } }
private void doDeleteTest() { doTest(new Runnable() { @Override public void run() { final Editor editor = myFixture.getEditor(); final Document document = editor.getDocument(); final int selectionStart = editor.getSelectionModel().getSelectionStart(); final int selectionEnd = editor.getSelectionModel().getSelectionEnd(); CommandProcessor.getInstance().setCurrentCommandGroupId(EditorActionUtil.DELETE_COMMAND_GROUP); document.deleteString(selectionStart, selectionEnd); editor.getCaretModel().moveToOffset(selectionStart); } }, true, ChangeSignatureDetectorAction.CHANGE_SIGNATURE); }
@Override public void moveToOffset(int offset, boolean locateBeforeSoftWrap) { assertIsDispatchThread(); validateCallContext(); if (mySkipChangeRequests) { return; } final LogicalPosition logicalPosition = myEditor.offsetToLogicalPosition(offset); CaretEvent event = moveToLogicalPosition(logicalPosition, locateBeforeSoftWrap, null, false); final LogicalPosition positionByOffsetAfterMove = myEditor.offsetToLogicalPosition(myOffset); if (!myIgnoreWrongMoves && !positionByOffsetAfterMove.equals(logicalPosition)) { StringBuilder debugBuffer = new StringBuilder(); moveToLogicalPosition(logicalPosition, locateBeforeSoftWrap, debugBuffer, true); int textStart = Math.max(0, Math.min(offset, myOffset) - 1); final DocumentEx document = myEditor.getDocument(); int textEnd = Math.min(document.getTextLength() - 1, Math.max(offset, myOffset) + 1); CharSequence text = document.getCharsSequence().subSequence(textStart, textEnd); StringBuilder positionToOffsetTrace = new StringBuilder(); int inverseOffset = myEditor.logicalPositionToOffset(logicalPosition, positionToOffsetTrace); LogMessageEx.error( LOG, "caret moved to wrong offset. Please submit a dedicated ticket and attach current editor's text to it.", String.format( "Requested: offset=%d, logical position='%s' but actual: offset=%d, logical position='%s' (%s). %s%n" + "interested text [%d;%d): '%s'%n debug trace: %s%nLogical position -> offset ('%s'->'%d') trace: %s", offset, logicalPosition, myOffset, myLogicalCaret, positionByOffsetAfterMove, myEditor.dumpState(), textStart, textEnd, text, debugBuffer, logicalPosition, inverseOffset, positionToOffsetTrace )); } if (event != null) { myCaretListeners.getMulticaster().caretPositionChanged(event); EditorActionUtil.selectNonexpandableFold(myEditor); } }
int getWordAtCaretStart() { Document document = myEditor.getDocument(); int offset = myEditor.getCaretModel().getOffset(); if (offset == 0) return 0; int lineNumber = myEditor.getCaretModel().getLogicalPosition().line; CharSequence text = document.getCharsSequence(); int newOffset = offset - 1; int minOffset = lineNumber > 0 ? document.getLineEndOffset(lineNumber - 1) : 0; boolean camel = myEditor.getSettings().isCamelWords(); for (; newOffset > minOffset; newOffset--) { if (EditorActionUtil.isWordStart(text, newOffset, camel)) break; } return newOffset; }
@Nullable private static TextRange getCamelSelectionRange(CharSequence editorText, int cursorOffset) { if (cursorOffset < 0 || cursorOffset >= editorText.length()) { return null; } if (cursorOffset > 0 && !Character.isJavaIdentifierPart(editorText.charAt(cursorOffset)) && Character.isJavaIdentifierPart(editorText.charAt(cursorOffset - 1))) { cursorOffset--; } if (Character.isJavaIdentifierPart(editorText.charAt(cursorOffset))) { int start = cursorOffset; int end = cursorOffset + 1; final int textLen = editorText.length(); while (start > 0 && Character.isJavaIdentifierPart(editorText.charAt(start - 1)) && !EditorActionUtil.isHumpBound(editorText, start, true)) { start--; } while (end < textLen && Character.isJavaIdentifierPart(editorText.charAt(end)) && !EditorActionUtil.isHumpBound(editorText, end, false)) { end++; } if (start + 1 < end) { return new TextRange(start, end); } } return null; }
@Nullable public static PsiReference findReference(Editor editor) { PsiReference result = findReference(editor, editor.getCaretModel().getOffset()); if (result == null) { final Integer offset = editor.getUserData(EditorActionUtil.EXPECTED_CARET_OFFSET); if (offset != null) { result = findReference(editor, offset); } } return result; }
@Nullable public static PsiElement findTargetElement(Editor editor, int flags) { ApplicationManager.getApplication().assertIsDispatchThread(); final PsiElement result = getInstance().findTargetElement(editor, flags, editor.getCaretModel().getOffset()); if (result != null) { return result; } final Integer offset = editor.getUserData(EditorActionUtil.EXPECTED_CARET_OFFSET); if (offset != null) { return getInstance().findTargetElement(editor, flags, offset); } return result; }
private static boolean canPlaceBulbOnTheSameLine(Editor editor) { if (ApplicationManager.getApplication().isUnitTestMode() || editor.isOneLineMode()) return false; final int offset = editor.getCaretModel().getOffset(); final VisualPosition pos = editor.offsetToVisualPosition(offset); int line = pos.line; final int firstNonSpaceColumnOnTheLine = EditorActionUtil.findFirstNonSpaceColumnOnTheLine(editor, line); if (firstNonSpaceColumnOnTheLine == -1) return false; final Point point = editor.visualPositionToXY(new VisualPosition(line, firstNonSpaceColumnOnTheLine)); return point.x > (AllIcons.Actions.RealIntentionBulb.getIconWidth() + (editor.isOneLineMode() ? SMALL_BORDER_SIZE : NORMAL_BORDER_SIZE) * 2); }
@Override public void moveToOffset(final int offset, final boolean locateBeforeSoftWrap) { assertIsDispatchThread(); validateCallContext(); if (mySkipChangeRequests) { return; } myEditor.getCaretModel().doWithCaretMerging(() -> { final LogicalPosition logicalPosition = myEditor.offsetToLogicalPosition(offset); CaretEvent event = moveToLogicalPosition(logicalPosition, locateBeforeSoftWrap, null, false); final LogicalPosition positionByOffsetAfterMove = myEditor.offsetToLogicalPosition(getOffset()); if (!positionByOffsetAfterMove.equals(logicalPosition)) { StringBuilder debugBuffer = new StringBuilder(); moveToLogicalPosition(logicalPosition, locateBeforeSoftWrap, debugBuffer, true); int actualOffset = getOffset(); int textStart = Math.max(0, Math.min(offset, actualOffset) - 1); final DocumentEx document = myEditor.getDocument(); int textEnd = Math.min(document.getTextLength() - 1, Math.max(offset, actualOffset) + 1); CharSequence text = document.getCharsSequence().subSequence(textStart, textEnd); int inverseOffset = myEditor.logicalPositionToOffset(logicalPosition); LogMessageEx.error( LOG, "caret moved to wrong offset. Please submit a dedicated ticket and attach current editor's text to it.", "Requested: offset=" + offset + ", logical position='" + logicalPosition + "' but actual: offset=" + actualOffset + ", logical position='" + myLogicalCaret + "' (" + positionByOffsetAfterMove + "). " + myEditor.dumpState() + "\ninterested text [" + textStart + ";" + textEnd + "): '" + text + "'\n debug trace: " + debugBuffer + "\nLogical position -> offset ('" + logicalPosition + "'->'" + inverseOffset + "')" ); } if (event != null) { myEditor.getCaretModel().fireCaretPositionChanged(event); EditorActionUtil.selectNonexpandableFold(myEditor); } }); }
@Nullable private static TextRange getWordOrLexemeSelectionRange(@Nullable Editor editor, @Nonnull CharSequence editorText, int cursorOffset, @Nonnull CharCondition isWordPartCondition) { int length = editorText.length(); if (length == 0) return null; if (cursorOffset == length || cursorOffset > 0 && !isWordPartCondition.value(editorText.charAt(cursorOffset)) && isWordPartCondition.value(editorText.charAt(cursorOffset - 1))) { cursorOffset--; } if (isWordPartCondition.value(editorText.charAt(cursorOffset))) { int start = cursorOffset; int end = cursorOffset; while (start > 0 && isWordPartCondition.value(editorText.charAt(start - 1)) && (editor == null || !EditorActionUtil.isLexemeBoundary(editor, start))) { start--; } while (end < length && isWordPartCondition.value(editorText.charAt(end)) && (end == start || editor == null || !EditorActionUtil.isLexemeBoundary(editor, end))) { end++; } return new TextRange(start, end); } return null; }