本文整理汇总了Java中com.intellij.openapi.editor.Caret.hasSelection方法的典型用法代码示例。如果您正苦于以下问题:Java Caret.hasSelection方法的具体用法?Java Caret.hasSelection怎么用?Java Caret.hasSelection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.editor.Caret
的用法示例。
在下文中一共展示了Caret.hasSelection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: actionPerformed
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Override
public void actionPerformed(final AnActionEvent e) {
Editor editor = e.getData(PlatformDataKeys.EDITOR);
if (editor != null) {
Document document = editor.getDocument();
CharSequence chars = document.getCharsSequence();
Plugin plugin = Plugin.getInstance();
for (Caret caret : editor.getCaretModel().getAllCarets()) {
if (caret.hasSelection()) {
boolean startWord = EditHelpers.isWordStart(chars, caret.getSelectionStart(),false);
boolean endWord = EditHelpers.isWordEnd(chars, caret.getSelectionEnd(),false);
if (myIsRemoveWord) {
plugin.removeHighlightWord(chars.subSequence(caret.getSelectionStart(), caret.getSelectionEnd()));
} else {
plugin.addHighlightWord(chars.subSequence(caret.getSelectionStart(), caret.getSelectionEnd()), startWord, endWord, null);
}
}
}
}
}
示例2: isSomeChangeSelected
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
private boolean isSomeChangeSelected(@NotNull ThreeSide side) {
EditorEx editor = getEditor(side);
List<Caret> carets = editor.getCaretModel().getAllCarets();
if (carets.size() != 1) return true;
Caret caret = carets.get(0);
if (caret.hasSelection()) return true;
int line = editor.getDocument().getLineNumber(editor.getExpectedCaretOffset());
List<TextMergeChange> changes = getAllChanges();
for (TextMergeChange change : changes) {
if (!isEnabled(change)) continue;
int line1 = change.getStartLine(side);
int line2 = change.getEndLine(side);
if (DiffUtil.isSelectedByLine(line, line1, line2)) return true;
}
return false;
}
示例3: getLanguageInEditor
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Nullable
public static Language getLanguageInEditor(@NotNull Caret caret, @NotNull final Project project) {
Editor editor = caret.getEditor();
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (file == null) return null;
int caretOffset = caret.getOffset();
int mostProbablyCorrectLanguageOffset = caretOffset == caret.getSelectionEnd() ? caret.getSelectionStart() : caretOffset;
PsiElement elt = getElementAtOffset(file, mostProbablyCorrectLanguageOffset);
Language lang = findLanguageFromElement(elt);
if (caret.hasSelection()) {
final Language rangeLanguage = evaluateLanguageInRange(caret.getSelectionStart(), caret.getSelectionEnd(), file);
if (rangeLanguage == null) return file.getLanguage();
lang = rangeLanguage;
}
return narrowLanguage(lang, file.getLanguage());
}
示例4: getFoldRegionsForSelection
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
/**
* Returns fold regions inside selection, or all regions in editor, if selection doesn't exist or doesn't contain fold regions.
*/
protected List<FoldRegion> getFoldRegionsForSelection(@NotNull Editor editor, @Nullable Caret caret) {
FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
if (caret == null) {
caret = editor.getCaretModel().getPrimaryCaret();
}
if (caret.hasSelection()) {
List<FoldRegion> result = new ArrayList<FoldRegion>();
for (FoldRegion region : allRegions) {
if (region.getStartOffset() >= caret.getSelectionStart() && region.getEndOffset() <= caret.getSelectionEnd()) {
result.add(region);
}
}
if (!result.isEmpty()) {
return result;
}
}
return Arrays.asList(allRegions);
}
示例5: getInjectedEditorForInjectedFile
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@NotNull
public static Editor getInjectedEditorForInjectedFile(@NotNull Editor hostEditor, @NotNull Caret hostCaret, @Nullable final PsiFile injectedFile) {
if (injectedFile == null || hostEditor instanceof EditorWindow || hostEditor.isDisposed()) return hostEditor;
Project project = hostEditor.getProject();
if (project == null) project = injectedFile.getProject();
Document document = PsiDocumentManager.getInstance(project).getDocument(injectedFile);
if (!(document instanceof DocumentWindowImpl)) return hostEditor;
DocumentWindowImpl documentWindow = (DocumentWindowImpl)document;
if (hostCaret.hasSelection()) {
int selstart = hostCaret.getSelectionStart();
if (selstart != -1) {
int selend = Math.max(selstart, hostCaret.getSelectionEnd());
if (!documentWindow.containsRange(selstart, selend)) {
// selection spreads out the injected editor range
return hostEditor;
}
}
}
if (!documentWindow.isValid()) {
return hostEditor; // since the moment we got hold of injectedFile and this moment call, document may have been dirtied
}
return EditorWindowImpl.create(documentWindow, (EditorImpl)hostEditor, injectedFile);
}
示例6: isSomeChangeSelected
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
protected boolean isSomeChangeSelected(@NotNull Side side) {
if (myDiffChanges.isEmpty()) return false;
EditorEx editor = getEditor(side);
List<Caret> carets = editor.getCaretModel().getAllCarets();
if (carets.size() != 1) return true;
Caret caret = carets.get(0);
if (caret.hasSelection()) return true;
int line = editor.getDocument().getLineNumber(editor.getExpectedCaretOffset());
for (SimpleDiffChange change : myDiffChanges) {
if (change.isSelectedByLine(line, side)) return true;
}
return false;
}
示例7: isSomeChangeSelected
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
protected static boolean isSomeChangeSelected(@NotNull Editor editor, @NotNull LineStatusTracker tracker) {
List<Caret> carets = editor.getCaretModel().getAllCarets();
if (carets.size() != 1) return true;
Caret caret = carets.get(0);
if (caret.hasSelection()) return true;
if (caret.getOffset() == editor.getDocument().getTextLength() &&
tracker.getRangeForLine(editor.getDocument().getLineCount()) != null) {
return true;
}
return tracker.getRangeForLine(caret.getLogicalPosition().line) != null;
}
示例8: getLanguageAtCurrentPositionInEditor
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
private static Language getLanguageAtCurrentPositionInEditor(Caret caret, final PsiFile psiFile) {
int caretOffset = caret.getOffset();
int mostProbablyCorrectLanguageOffset = caretOffset == caret.getSelectionStart() ||
caretOffset == caret.getSelectionEnd()
? caret.getSelectionStart()
: caretOffset;
if (caret.hasSelection()) {
return getLanguageAtOffset(psiFile, mostProbablyCorrectLanguageOffset, caret.getSelectionEnd());
}
return PsiUtilCore.getLanguageAtOffset(psiFile, mostProbablyCorrectLanguageOffset);
}
示例9: analyzeContext
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Override
protected void analyzeContext(final Editor editor, @Nullable final Caret caret, @NotNull final LineSelectionManager manager) {
int previousCaretLine = -1;
boolean haveMultipleCaretsPerLine = false;
boolean haveMultiLineSelection = false;
int caretCount = editor.getCaretModel().getCaretCount();
boolean haveMultipleCarets = caretCount > 1;
for (Caret caret1 : editor.getCaretModel().getAllCarets()) {
int caretLine = caret1.getLogicalPosition().line;
if (!haveMultiLineSelection && caret1.hasSelection()) {
EditorCaret editorCaret = manager.getEditorCaret(caret1);
if (editorCaret.hasLines()) {
haveMultiLineSelection = true;
}
}
if (!haveMultipleCaretsPerLine && previousCaretLine != -1) {
if (caretLine == previousCaretLine) {
haveMultipleCaretsPerLine = true;
}
}
previousCaretLine = caretLine;
if (haveMultiLineSelection && haveMultipleCaretsPerLine) break;
}
myLineMode = !haveMultipleCaretsPerLine;
mySingleLine = !haveMultiLineSelection;
mySingleMatch = haveMultipleCarets;
myMoveFirstMatch = !mySingleMatch;
myPattern = null;
myStartSearchCarets = null;
myPatternCaret = null;
myCaretToEndGroup = false;
if (mySingleMatch) {
// this is search forward/backward
caretsChanged(editor);
}
}
示例10: executeWriteAction
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Override
public void executeWriteAction(Editor editor, @Nullable Caret caret, DataContext dataContext) {
CommandProcessor.getInstance().setCurrentCommandGroupId(EditorActionUtil.DELETE_COMMAND_GROUP);
if (caret == null) {
caret = editor.getCaretModel().getCurrentCaret();
}
if (caret.hasSelection()) {
EditHelpers.delete(editor, caret, caret.getSelectionStart(), caret.getSelectionEnd(), myClearOnly);
return;
}
final Document doc = editor.getDocument();
int caretOffset = caret.getOffset();
final int lineNumber = doc.getLineNumber(caretOffset);
int lineEndOffset = doc.getLineEndOffset(lineNumber);
int start;
int end;
if (myToLineStart) {
int lineStartOffset = doc.getLineStartOffset(lineNumber);
int indent = EditHelpers.countWhiteSpace(doc.getCharsSequence(), lineStartOffset, lineEndOffset);
start = lineStartOffset + indent;
end = caretOffset;
if (caretOffset <= lineStartOffset + indent) {
if (editor.getCaretModel().getCaretCount() > 1) return;
start = lineStartOffset;
end = caretOffset;
}
} else {
start = caretOffset;
end = lineEndOffset;
if (lineEndOffset < doc.getTextLength() && CharArrayUtil.isEmptyOrSpaces(doc.getCharsSequence(), caretOffset, lineEndOffset)) {
end++;
}
if (end > lineEndOffset) {
end = lineEndOffset;
}
if (end > lineEndOffset || end <= start) {
return;
}
}
EditHelpers.delete(editor, caret, start, end, myClearOnly);
}
示例11: executeWriteAction
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Override
public void executeWriteAction(Editor editor, @Nullable Caret caret, DataContext dataContext) {
if (caret == null) {
caret = editor.getCaretModel().getCurrentCaret();
}
if (!myIgnoreSelection && caret.hasSelection()) {
delete(editor, caret, caret.getSelectionStart(), caret.getSelectionEnd());
return;
}
final Document doc = editor.getDocument();
int caretOffset = caret.getOffset();
if ((myToLineStart && (caretOffset == 0)) || (!myToLineStart && (caretOffset >= doc.getTextLength()))) {
return;
}
final int lineNumber = doc.getLineNumber(caretOffset);
int lineEndOffset = doc.getLineEndOffset(lineNumber);
int lineStartOffset = doc.getLineStartOffset(lineNumber);
int start;
int end;
if (myToLineStart) {
start = lineStartOffset;
end = caretOffset;
}
else {
if (caretOffset >= lineEndOffset) {
start = lineEndOffset;
end = lineEndOffset + 1;
}
else {
start = caretOffset;
end = lineEndOffset;
if (lineEndOffset < doc.getTextLength() && CharArrayUtil.isEmptyOrSpaces(doc.getCharsSequence(), caretOffset, lineEndOffset)) {
end++;
}
}
}
delete(editor, caret, start, end);
}
示例12: calcSelectionEnd
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
private static int calcSelectionEnd(Caret caret) {
return caret.hasSelection() ? caret.getSelectionEnd() : caret.getOffset();
}
示例13: calcStartOffset
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
public static int calcStartOffset(Caret caret) {
return caret.hasSelection() ? caret.getSelectionStart() : caret.getOffset();
}
示例14: doExecute
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Override
public void doExecute(Editor editor, @Nullable Caret c, DataContext dataContext) {
Caret caret = c == null ? editor.getCaretModel().getPrimaryCaret() : c;
TextRange wordSelectionRange = getSelectionRange(editor, caret);
boolean notFoundPreviously = getAndResetNotFoundStatus(editor);
boolean wholeWordSearch = isWholeWordSearch(editor);
if (caret.hasSelection()) {
Project project = editor.getProject();
String selectedText = caret.getSelectedText();
if (project == null || selectedText == null) {
return;
}
FindManager findManager = FindManager.getInstance(project);
FindModel model = getFindModel(selectedText, wholeWordSearch);
findManager.setSelectNextOccurrenceWasPerformed();
findManager.setFindNextModel(model);
int searchStartOffset = notFoundPreviously ? 0 : caret.getSelectionEnd();
FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), searchStartOffset, model);
if (findResult.isStringFound()) {
boolean caretAdded = FindUtil.selectSearchResultInEditor(editor, findResult, caret.getOffset() - caret.getSelectionStart());
if (!caretAdded) {
// this means that the found occurence is already selected
if (notFoundPreviously) {
setNotFoundStatus(editor); // to make sure we won't show hint anymore if there are no more occurrences
}
}
}
else {
setNotFoundStatus(editor);
showHint(editor);
}
}
else {
if (wordSelectionRange == null) {
return;
}
setSelection(editor, caret, wordSelectionRange);
setWholeWordSearch(editor, true);
}
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
示例15: doExecute
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Override
public void doExecute(final Editor editor, @Nullable Caret c, DataContext dataContext) {
Caret caret = c == null ? editor.getCaretModel().getPrimaryCaret() : c;
boolean wholeWordsSearch = false;
if (!caret.hasSelection()) {
TextRange wordSelectionRange = getSelectionRange(editor, caret);
if (wordSelectionRange != null) {
setSelection(editor, caret, wordSelectionRange);
wholeWordsSearch = true;
}
}
String selectedText = caret.getSelectedText();
Project project = editor.getProject();
if (project == null || selectedText == null) {
return;
}
int caretShiftFromSelectionStart = caret.getOffset() - caret.getSelectionStart();
final FindManager findManager = FindManager.getInstance(project);
final FindModel model = getFindModel(selectedText, wholeWordsSearch);
FindUtil.selectSearchResultsInEditor(editor, new Iterator<FindResult>() {
FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), 0, model);
@Override
public boolean hasNext() {
return findResult.isStringFound();
}
@Override
public FindResult next() {
FindResult result = findResult;
findResult = findManager.findString(editor.getDocument().getCharsSequence(), findResult.getEndOffset(), model);
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}, caretShiftFromSelectionStart);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}