use of com.intellij.openapi.editor.CaretModel in project qi4j-sdk by Qi4j.
the class AbstractIntention method findMatchingElement.
@Nullable
private PsiElement findMatchingElement(@NotNull PsiFile file, @NotNull Editor editor) {
CaretModel caretModel = editor.getCaretModel();
int position = caretModel.getOffset();
PsiElement element = file.findElementAt(position);
return findMatchingElement(element);
}
use of com.intellij.openapi.editor.CaretModel in project ECTranslation by Skykai521.
the class ECTranslation method getCurrentWords.
public String getCurrentWords(Editor editor) {
Document document = editor.getDocument();
CaretModel caretModel = editor.getCaretModel();
int caretOffset = caretModel.getOffset();
int lineNum = document.getLineNumber(caretOffset);
int lineStartOffset = document.getLineStartOffset(lineNum);
int lineEndOffset = document.getLineEndOffset(lineNum);
String lineContent = document.getText(new TextRange(lineStartOffset, lineEndOffset));
char[] chars = lineContent.toCharArray();
int start = 0, end = 0, cursor = caretOffset - lineStartOffset;
if (!Character.isLetter(chars[cursor])) {
Logger.warn("Caret not in a word");
return null;
}
for (int ptr = cursor; ptr >= 0; ptr--) {
if (!Character.isLetter(chars[ptr])) {
start = ptr + 1;
break;
}
}
int lastLetter = 0;
for (int ptr = cursor; ptr < lineEndOffset - lineStartOffset; ptr++) {
lastLetter = ptr;
if (!Character.isLetter(chars[ptr])) {
end = ptr;
break;
}
}
if (end == 0) {
end = lastLetter + 1;
}
String ret = new String(chars, start, end - start);
Logger.info("Selected words: " + ret);
return ret;
}
Aggregations