Java Code Examples for com.intellij.openapi.editor.SelectionModel#hasSelection()
The following examples show how to use
com.intellij.openapi.editor.SelectionModel#hasSelection() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EscapeHandler.java From consulo with Apache License 2.0 | 6 votes |
@Override public void execute(Editor editor, DataContext dataContext) { TemplateState templateState = TemplateManagerImpl.getTemplateState(editor); if (templateState != null && !templateState.isFinished()) { SelectionModel selectionModel = editor.getSelectionModel(); LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor); // the idea behind lookup checking is that if there is a preselected value in lookup // then user might want just to close lookup but not finish a template. // E.g. user wants to move to the next template segment by Tab without completion invocation. // If there is no selected value in completion that user definitely wants to finish template boolean lookupIsEmpty = lookup == null || lookup.getCurrentItem() == null; if (!selectionModel.hasSelection() && lookupIsEmpty) { CommandProcessor.getInstance().setCurrentCommandName(CodeInsightBundle.message("finish.template.command")); templateState.gotoEnd(true); return; } } if (myOriginalHandler.isEnabled(editor, dataContext)) { myOriginalHandler.execute(editor, dataContext); } }
Example 2
Source File: LivePreviewController.java From consulo with Apache License 2.0 | 6 votes |
private void performReplaceAll(Editor e) { Project project = mySearchResults.getProject(); if (!ReadonlyStatusHandler.ensureDocumentWritable(project, e.getDocument())) { return; } if (mySearchResults.getFindModel() != null) { final FindModel copy = new FindModel(); copy.copyFrom(mySearchResults.getFindModel()); final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel(); final int offset; if (!selectionModel.hasSelection() || copy.isGlobal()) { copy.setGlobal(true); offset = 0; } else { offset = selectionModel.getBlockSelectionStarts()[0]; } FindUtil.replace(project, e, offset, copy, this); } }
Example 3
Source File: FoldLinesLikeThis.java From consulo with Apache License 2.0 | 6 votes |
@Nullable private static String getSingleLineSelection(@Nonnull Editor editor) { final SelectionModel model = editor.getSelectionModel(); final Document document = editor.getDocument(); if (!model.hasSelection()) { final int offset = editor.getCaretModel().getOffset(); if (offset <= document.getTextLength()) { final int lineNumber = document.getLineNumber(offset); final String line = document.getText().substring(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber)).trim(); if (StringUtil.isNotEmpty(line)) { return line; } } return null; } final int start = model.getSelectionStart(); final int end = model.getSelectionEnd(); if (document.getLineNumber(start) == document.getLineNumber(end)) { final String selection = document.getText().substring(start, end).trim(); if (StringUtil.isNotEmpty(selection)) { return selection; } } return null; }
Example 4
Source File: ExtractRuleAction.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void actionPerformed(@NotNull AnActionEvent e) { PsiElement el = MyActionUtils.getSelectedPsiElement(e); if ( el==null ) return; final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE); if ( psiFile==null ) return; Editor editor = e.getData(PlatformDataKeys.EDITOR); if ( editor==null ) return; SelectionModel selectionModel = editor.getSelectionModel(); if ( !selectionModel.hasSelection() ) { List<PsiElement> expressions = findExtractableRules(el); IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PsiElement>() { @Override public void pass(PsiElement element) { selectionModel.setSelection(element.getTextOffset(), element.getTextRange().getEndOffset()); extractSelection(psiFile, editor, selectionModel); } }, PsiElement::getText); } else { extractSelection(psiFile, editor, selectionModel); } }
Example 5
Source File: EscapeHandler.java From consulo with Apache License 2.0 | 6 votes |
@Override public void execute(Editor editor, DataContext dataContext) { final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { final TemplateState state = TemplateManagerImpl.getTemplateState(editor); if (state != null && editor.getUserData(InplaceRefactoring.INPLACE_RENAMER) != null) { final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor); if (lookup != null) { selectionModel.removeSelection(); lookup.hide(); return; } } } myOriginalHandler.execute(editor, dataContext); }
Example 6
Source File: HungryBackspaceAction.java From consulo with Apache License 2.0 | 6 votes |
@RequiredWriteAction @Override public void executeWriteAction(@Nonnull Editor editor, Caret caret, DataContext dataContext) { final Document document = editor.getDocument(); final int caretOffset = editor.getCaretModel().getOffset(); if (caretOffset < 1) { return; } final SelectionModel selectionModel = editor.getSelectionModel(); final CharSequence text = document.getCharsSequence(); final char c = text.charAt(caretOffset - 1); if (!selectionModel.hasSelection() && StringUtil.isWhiteSpace(c)) { int startOffset = CharArrayUtil.shiftBackward(text, caretOffset - 2, "\t \n") + 1; document.deleteString(startOffset, caretOffset); } else { final EditorActionHandler handler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE); handler.execute(editor, caret, dataContext); } }
Example 7
Source File: ConvertIndentsActionBase.java From consulo with Apache License 2.0 | 6 votes |
@RequiredWriteAction @Override public void executeWriteAction(final Editor editor, @Nullable Caret caret, DataContext dataContext) { final SelectionModel selectionModel = editor.getSelectionModel(); int changedLines = 0; if (selectionModel.hasSelection()) { changedLines = performAction(editor, new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd())); } else { changedLines += performAction(editor, new TextRange(0, editor.getDocument().getTextLength())); } if (changedLines == 0) { HintManager.getInstance().showInformationHint(editor, "All lines already have requested indentation"); } else { HintManager.getInstance().showInformationHint(editor, "Changed indentation in " + changedLines + (changedLines == 1 ? " line" : " lines")); } }
Example 8
Source File: SwapSelectionBoundariesAction.java From consulo with Apache License 2.0 | 6 votes |
@Override public void execute(Editor editor, DataContext dataContext) { if (!(editor instanceof EditorEx)) { return; } final SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { return; } EditorEx editorEx = (EditorEx)editor; final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); final CaretModel caretModel = editor.getCaretModel(); boolean moveToEnd = caretModel.getOffset() == start; editorEx.setStickySelection(false); editorEx.setStickySelection(true); if (moveToEnd) { caretModel.moveToOffset(end); } else { caretModel.moveToOffset(start); } }
Example 9
Source File: KillRingSaveAction.java From consulo with Apache License 2.0 | 6 votes |
@Override public void execute(final Editor editor, final DataContext dataContext) { SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { return; } final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); if (start >= end) { return; } KillRingUtil.copyToKillRing(editor, start, end, false); if (myRemove) { ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(editor.getDocument(),editor.getProject()) { @Override public void run() { editor.getDocument().deleteString(start, end); } }); } }
Example 10
Source File: XQuickEvaluateHandler.java From consulo with Apache License 2.0 | 6 votes |
@Nullable private static ExpressionInfo getExpressionInfo(final XDebuggerEvaluator evaluator, final Project project, final ValueHintType type, final Editor editor, final int offset) { SelectionModel selectionModel = editor.getSelectionModel(); int selectionStart = selectionModel.getSelectionStart(); int selectionEnd = selectionModel.getSelectionEnd(); if ((type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT) && selectionModel.hasSelection() && selectionStart <= offset && offset <= selectionEnd) { return new ExpressionInfo(new TextRange(selectionStart, selectionEnd)); } return evaluator.getExpressionInfoAtOffset(project, editor.getDocument(), offset, type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT); }
Example 11
Source File: RearrangeCodeAction.java From consulo with Apache License 2.0 | 5 votes |
@RequiredUIAccess @Override public void actionPerformed(@Nonnull AnActionEvent e) { final Project project = e.getProject(); if (project == null) { return; } final Editor editor = e.getData(CommonDataKeys.EDITOR); if (editor == null) { return; } PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); Document document = editor.getDocument(); documentManager.commitDocument(document); final PsiFile file = documentManager.getPsiFile(document); if (file == null) { return; } SelectionModel model = editor.getSelectionModel(); if (model.hasSelection()) { new RearrangeCodeProcessor(file, model).run(); } else { new RearrangeCodeProcessor(file).run(); } }
Example 12
Source File: LogView.java From logviewer with Apache License 2.0 | 5 votes |
/** * Returns the selected text if there is any selection. If not return all based on parameter * * @param defaultToAll If no selection, then this decides whether to return all text */ String getSelectedText(boolean defaultToAll) { ConsoleView console = this.getConsole(); Editor myEditor = console != null ? (Editor) CommonDataKeys.EDITOR.getData((DataProvider) console) : null; if (myEditor != null) { Document document = myEditor.getDocument(); final SelectionModel selectionModel = myEditor.getSelectionModel(); if (selectionModel.hasSelection()) { return selectionModel.getSelectedText(); } else if (defaultToAll) { return document.getText(); } } return null; }
Example 13
Source File: AbstractLayoutCodeProcessor.java From consulo with Apache License 2.0 | 5 votes |
static List<TextRange> getSelectedRanges(@Nonnull SelectionModel selectionModel) { final List<TextRange> ranges = new SmartList<>(); if (selectionModel.hasSelection()) { TextRange range = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); ranges.add(range); } return ranges; }
Example 14
Source File: VcsSelectionUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nullable private static VcsSelection getSelectionFromEditor(VcsContext context) { Editor editor = context.getEditor(); if (editor == null) return null; SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { return null; } return new VcsSelection(editor.getDocument(), selectionModel); }
Example 15
Source File: AbstractRegionToKillRingTest.java From consulo with Apache License 2.0 | 5 votes |
/** * Checks current editor and returns tuple of <code>(selected text; text over than selected)</code>. * * @return tuple of <code>(selected text; text over than selected)</code>. */ @Nonnull protected static Pair<String, String> parse() { SelectionModel selectionModel = myEditor.getSelectionModel(); if (!selectionModel.hasSelection()) { return new Pair<String, String>(null, myEditor.getDocument().getText()); } CharSequence text = myEditor.getDocument().getCharsSequence(); String selectedText = text.subSequence(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()).toString(); StringBuilder nonSelectedText = new StringBuilder(); nonSelectedText.append(text.subSequence(0, selectionModel.getSelectionStart())) .append(text.subSequence(selectionModel.getSelectionEnd(), text.length())); return new Pair<String, String>(selectedText, nonSelectedText.toString()); }
Example 16
Source File: VariableInplaceRenamer.java From consulo with Apache License 2.0 | 5 votes |
@Override protected void beforeTemplateStart() { super.beforeTemplateStart(); myLanguage = myScope.getLanguage(); if (shouldCreateSnapshot()) { final ResolveSnapshotProvider resolveSnapshotProvider = INSTANCE.forLanguage(myLanguage); mySnapshot = resolveSnapshotProvider != null ? resolveSnapshotProvider.createSnapshot(myScope) : null; } final SelectionModel selectionModel = myEditor.getSelectionModel(); mySelectedRange = selectionModel.hasSelection() ? new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()) : null; }
Example 17
Source File: NameSuggestionsField.java From consulo with Apache License 2.0 | 5 votes |
public NameSuggestionsField(final String[] suggestedNames, final Project project, final FileType fileType, @Nullable final Editor editor) { this(suggestedNames, project, fileType); if (editor == null) return; // later here because EditorTextField creates Editor during addNotify() final Runnable selectionRunnable = new Runnable() { @Override public void run() { final int offset = editor.getCaretModel().getOffset(); List<TextRange> ranges = new ArrayList<TextRange>(); SelectWordUtil.addWordSelection(editor.getSettings().isCamelWords(), editor.getDocument().getCharsSequence(), offset, ranges); Editor myEditor = getEditor(); if (myEditor == null) return; for (TextRange wordRange : ranges) { String word = editor.getDocument().getText(wordRange); if (!word.equals(getEnteredName())) continue; final SelectionModel selectionModel = editor.getSelectionModel(); myEditor.getSelectionModel().removeSelection(); final int wordRangeStartOffset = wordRange.getStartOffset(); int myOffset = offset - wordRangeStartOffset; myEditor.getCaretModel().moveToOffset(myOffset); TextRange selected = new TextRange(Math.max(0, selectionModel.getSelectionStart() - wordRangeStartOffset), Math.max(0, selectionModel.getSelectionEnd() - wordRangeStartOffset)); selected = selected.intersection(new TextRange(0, myEditor.getDocument().getTextLength())); if (selectionModel.hasSelection() && selected != null && !selected.isEmpty()) { myEditor.getSelectionModel().setSelection(selected.getStartOffset(), selected.getEndOffset()); } else if (shouldSelectAll()) { myEditor.getSelectionModel().setSelection(0, myEditor.getDocument().getTextLength()); } break; } } }; SwingUtilities.invokeLater(selectionRunnable); }
Example 18
Source File: EditorDocOps.java From KodeBeagle with Apache License 2.0 | 5 votes |
public final Pair<Integer, Integer> getLineOffSets(final Editor projectEditor, final int distance) { Document document = projectEditor.getDocument(); SelectionModel selectionModel = projectEditor.getSelectionModel(); int head = 0; int tail = document.getLineCount() - 1; if (selectionModel.hasSelection()) { head = document.getLineNumber(selectionModel.getSelectionStart()); tail = document.getLineNumber(selectionModel.getSelectionEnd()); /*Selection model gives one more line if line is selected completely. By Checking if complete line is slected and decreasing tail*/ if ((document.getLineStartOffset(tail) == selectionModel.getSelectionEnd())) { tail--; } } else { int currentLine = document.getLineNumber(projectEditor.getCaretModel().getOffset()); if (currentLine - distance >= 0) { head = currentLine - distance; } if (currentLine + distance <= document.getLineCount() - 1) { tail = currentLine + distance; } } int start = document.getLineStartOffset(head); int end = document.getLineEndOffset(tail); Pair<Integer, Integer> pair = new Pair<>(start, end); return pair; }
Example 19
Source File: HaxeIntroduceHandler.java From intellij-haxe with Apache License 2.0 | 4 votes |
protected void performAction(HaxeIntroduceOperation 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(); if (selectionModel.hasSelection()) { element1 = file.findElementAt(selectionModel.getSelectionStart()); element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1); } 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); } } 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); } final Project project = operation.getProject(); if (element1 == null || element2 == null) { showCannotPerformError(project, editor); return; } element1 = HaxeRefactoringUtil.getSelectedExpression(project, file, element1, element2); if (!isValidForExtraction(element1)) { showCannotPerformError(project, editor); return; } if (!checkIntroduceContext(file, editor, element1)) { return; } operation.setElement(element1); performActionOnElement(operation); }
Example 20
Source File: CustomTemplateCallback.java From consulo with Apache License 2.0 | 4 votes |
public static int getOffset(@Nonnull Editor editor) { SelectionModel selectionModel = editor.getSelectionModel(); return selectionModel.hasSelection() ? selectionModel.getSelectionStart() : Math.max(editor.getCaretModel().getOffset() - 1, 0); }