@NotNull private LineRange getLineRange(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, @NotNull ExCommand cmd) { final LineRange range = cmd.getLineRange(editor, caret, context); final LineRange normalizedRange; // Something like "30,20sort" gets converted to "20,30sort" if (range.getEndLine() < range.getStartLine()) { normalizedRange = new LineRange(range.getEndLine(), range.getStartLine()); } else { normalizedRange = range; } // If we don't have a range, we either have "sort", a selection, or a block if (normalizedRange.getEndLine() - normalizedRange.getStartLine() == 0) { // If we have a selection. final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); final int startLine = editor.offsetToLogicalPosition(start).line; final int endLine = editor.offsetToLogicalPosition(end).line; return new LineRange(startLine, endLine); } // If we have a generic selection, i.e. "sort" entire document else { return new LineRange(0, editor.getDocument().getLineCount() - 1); } } return normalizedRange; }
protected static void performAction(GoIntroduceOperation operation) { SelectionModel selectionModel = operation.getEditor().getSelectionModel(); boolean hasSelection = selectionModel.hasSelection(); GoExpression expression = hasSelection ? findExpressionInSelection(operation.getFile(), selectionModel.getSelectionStart(), selectionModel.getSelectionEnd())
@NotNull protected PsiElement findElementAtCaretOrInSelection() { SelectionModel selectionModel = myFixture.getEditor().getSelectionModel(); if (selectionModel.hasSelection()) { PsiElement left = myFixture.getFile().findElementAt(selectionModel.getSelectionStart()); PsiElement right = myFixture.getFile().findElementAt(selectionModel.getSelectionEnd() - 1); assertNotNull(left); assertNotNull(right); return ObjectUtils.assertNotNull(PsiTreeUtil.findCommonParent(left, right)); } else { return ObjectUtils.assertNotNull(myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset())); } }
if(editor.getSelectionModel().hasSelection()) { psiElement = psiFile.findElementAt(editor.getSelectionModel().getSelectionStart()); } else {
@Override public List<PsiElement> getTargets() { if (!this.myEditor.getSelectionModel().hasSelection()) { return Collections.emptyList(); } Caret primaryCaret = this.myEditor.getCaretModel().getPrimaryCaret(); PsiElement myFocusedElement = this.myFile.getViewProvider().findElementAt(primaryCaret.getOffset()); if (myFocusedElement == null) { myFocusedElement = this.myFile.getLastChild(); } myFocusedElement = CsvHelper.getParentFieldElement(myFocusedElement); if (myFocusedElement == null) { return Collections.emptyList(); } return Collections.singletonList(myFocusedElement); }
@Nullable private static PsiElement findTargetElement(Editor editor, PsiFile file) { int offset = editor.getCaretModel().getOffset(); PsiElement endElement = file.findElementAt(offset); SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection() && selectionModel.getSelectionStart() < offset) { PsiElement startElement = file.findElementAt(selectionModel.getSelectionStart()); if (startElement != null && startElement != endElement && startElement.getTextRange().getEndOffset() == offset) { return startElement; } } return endElement; }
/** * Checks if there is a symbol at the caret and if there is no selection. Then it checks whether the symbol * has already a reference to a local construct like Module. If not, then we check if there is a surrounding * Module/Block/With that can be used for the introduction. * @param project current project * @param editor editor * @param file file we are editing * @return true if we can introduce a symbol in a surround Module/Block/With */ @Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { if (editor.getSelectionModel().hasSelection()) { return false; } PsiElement element = findElementAtCaret(file, editor); if (element != null) { PsiReference reference = element.getReference(); if (reference != null) { PsiElement resolve = reference.resolve(); if (resolve instanceof Symbol) { LocalizationConstruct.MScope elementConstruct = ((Symbol) resolve).getLocalizationConstruct(); if (elementConstruct != LocalizationConstruct.MScope.NULL_SCOPE) { return false; } } } return !PsiTreeUtil.treeWalkUp(new FindLocalisationProcessor(), element, file, ResolveState.initial()); } return false; }
private DraftInput createComment() { DraftInput comment = new DraftInput(); comment.message = getText(); comment.path = PathUtils.ensureSlashSeparators(filePath); comment.side = commentSide; SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { comment.range = handleRangeComment(selectionModel); comment.line = comment.range.endLine; // end line as per specification } else { comment.line = editor.getDocument().getLineNumber(editor.getCaretModel().getOffset()) + 1; } if (commentToEdit != null) { // preserve: the selection might not exist anymore but we should not loose it comment.range = commentToEdit.range; comment.line = commentToEdit.line; comment.inReplyTo = commentToEdit.inReplyTo; } return comment; }
if (!selectionModel.hasSelection()) { List<BnfExpression> expressions = ContainerUtil.newArrayList(); while (parentExpression != null) {
/** Only show if user has selected region and is in a lexer or parser rule */ @Override public void update(AnActionEvent e) { Presentation presentation = e.getPresentation(); VirtualFile grammarFile = MyActionUtils.getGrammarFileFromEvent(e); if ( grammarFile==null ) { presentation.setEnabled(false); return; } Editor editor = e.getData(PlatformDataKeys.EDITOR); if ( editor==null ) { presentation.setEnabled(false); return; } ParserRuleRefNode parserRule = MyActionUtils.getParserRuleSurroundingRef(e); LexerRuleRefNode lexerRule = MyActionUtils.getLexerRuleSurroundingRef(e); if ( parserRule==null && lexerRule==null ) { presentation.setEnabled(false); return; } SelectionModel selectionModel = editor.getSelectionModel(); if ( !selectionModel.hasSelection() ) { presentation.setEnabled(false); } // TODO: disable if selection spans rules }
private static void searchSelection(Editor editor, Project project) { final SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { selectionModel.selectWordAtCaret(false);
if (editor != null) { final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { final String selectedText = selectionModel.getSelectedText(); if (selectedText != null) {