本文整理汇总了Java中com.intellij.openapi.editor.Caret.getOffset方法的典型用法代码示例。如果您正苦于以下问题:Java Caret.getOffset方法的具体用法?Java Caret.getOffset怎么用?Java Caret.getOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.editor.Caret
的用法示例。
在下文中一共展示了Caret.getOffset方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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());
}
示例2: getPsiFileInEditor
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Nullable
public static PsiFile getPsiFileInEditor(@NotNull Caret caret, @NotNull final Project project) {
Editor editor = caret.getEditor();
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (file == null) return null;
PsiUtilCore.ensureValid(file);
final Language language = getLanguageInEditor(caret, project);
if (language == null) return file;
if (language == file.getLanguage()) return file;
int caretOffset = caret.getOffset();
int mostProbablyCorrectLanguageOffset = caretOffset == caret.getSelectionEnd() ? caret.getSelectionStart() : caretOffset;
return getPsiFileAtOffset(file, mostProbablyCorrectLanguageOffset);
}
示例3: 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;
}
示例4: doExecute
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Override
protected void doExecute(Editor editor, @Nullable Caret caret, DataContext dataContext) {
int offset = caret != null ? caret.getOffset() : editor.getCaretModel().getOffset();
PsiElement exprList = getExpressionList(editor, offset, dataContext);
if (exprList != null) {
int listOffset = exprList.getTextRange().getStartOffset();
ParameterInfoController.prevOrNextParameter(editor, listOffset, myIsNextParameterHandler);
}
}
示例5: 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);
}
示例6: getLanguageFromCaret
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
@Nullable
static Language getLanguageFromCaret(@NotNull Project project,
@Nullable Editor editor,
@Nullable PsiFile psiFile) {
if (editor == null || psiFile == null) return null;
Caret caret = editor.getCaretModel().getPrimaryCaret();
int offset = caret.getOffset();
PsiElement element = InjectedLanguageManager.getInstance(project).findInjectedElementAt(psiFile, offset);
PsiFile file = element != null ? element.getContainingFile() : psiFile;
return file.getLanguage();
}
示例7: 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);
}
示例8: 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);
}
示例9: calcSelectionEnd
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
private static int calcSelectionEnd(Caret caret) {
return caret.hasSelection() ? caret.getSelectionEnd() : caret.getOffset();
}
示例10: calcStartOffset
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
public static int calcStartOffset(Caret caret) {
return caret.hasSelection() ? caret.getSelectionStart() : caret.getOffset();
}
示例11: execute
import com.intellij.openapi.editor.Caret; //导入方法依赖的package包/类
public void execute(Editor editor, DataContext dataContext) {
final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
if (file == null) return;
final Caret caret = editor.getCaretModel().getCurrentCaret();
final EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter();
final CharSequence text = editor.getDocument().getCharsSequence();
int offset = caret.getOffset();
FileType fileType = getFileType(file, offset);
HighlighterIterator iterator = highlighter.createIterator(offset);
if (iterator.atEnd()) {
offset--;
}
else if (!BraceMatchingUtil.isLBraceToken(iterator, text, fileType)) {
offset--;
if (offset >= 0) {
final HighlighterIterator i = highlighter.createIterator(offset);
if (!BraceMatchingUtil.isRBraceToken(i, text, getFileType(file, i.getStart()))) offset++;
}
}
if (offset < 0) return;
iterator = highlighter.createIterator(offset);
fileType = getFileType(file, iterator.getStart());
while (!BraceMatchingUtil.isLBraceToken(iterator, text, fileType) &&
!BraceMatchingUtil.isRBraceToken(iterator, text, fileType)) {
if (iterator.getStart() == 0) return;
iterator.retreat();
offset = iterator.getStart();
}
if (BraceMatchingUtil.matchBrace(text, fileType, iterator, true)) {
moveCaret(editor, caret, iterator.getEnd());
return;
}
iterator = highlighter.createIterator(offset);
if (BraceMatchingUtil.matchBrace(text, fileType, iterator, false)) {
moveCaret(editor, caret, iterator.getStart());
}
}
示例12: 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);
}