本文整理汇总了Java中com.intellij.openapi.editor.Editor.getCaretModel方法的典型用法代码示例。如果您正苦于以下问题:Java Editor.getCaretModel方法的具体用法?Java Editor.getCaretModel怎么用?Java Editor.getCaretModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.editor.Editor
的用法示例。
在下文中一共展示了Editor.getCaretModel方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: actionPerformed
import com.intellij.openapi.editor.Editor; //导入方法依赖的package包/类
/**
* Inserts the string generated by {@link #generateString()} at the caret(s) in the editor.
*
* @param event the performed action
*/
@Override
public final void actionPerformed(final AnActionEvent event) {
final Editor editor = event.getData(CommonDataKeys.EDITOR);
if (editor == null) {
return;
}
final Project project = event.getData(CommonDataKeys.PROJECT);
final Document document = editor.getDocument();
final CaretModel caretModel = editor.getCaretModel();
final Runnable replaceCaretSelections = () -> caretModel.getAllCarets().forEach(caret -> {
final int start = caret.getSelectionStart();
final int end = caret.getSelectionEnd();
final String string = generateString();
final int newEnd = start + string.length();
document.replaceString(start, end, string);
caret.setSelection(start, newEnd);
});
WriteCommandAction.runWriteCommandAction(project, replaceCaretSelections);
}
示例2: handleInsert
import com.intellij.openapi.editor.Editor; //导入方法依赖的package包/类
@Override
public void handleInsert(final InsertionContext context) {
if (element.getParent() instanceof FieldReference) {
final Editor editor = context.getEditor();
final CaretModel caretModel = editor.getCaretModel();
final Document document = editor.getDocument();
document.insertString(caretModel.getOffset(), "()");
caretModel.moveCaretRelatively(1, 0, false, false, false);
}
}
示例3: getCurrentWords
import com.intellij.openapi.editor.Editor; //导入方法依赖的package包/类
private 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])) {
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;
}
return new String(chars, start, end - start);
}
示例4: getCurrentWords
import com.intellij.openapi.editor.Editor; //导入方法依赖的package包/类
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;
}
示例5: actionPerformed
import com.intellij.openapi.editor.Editor; //导入方法依赖的package包/类
public void actionPerformed(AnActionEvent event) {
SourcetrailOptions options = SourcetrailOptions.getInstance();
String MESSAGE_SPLIT_STRING = ">>";
VirtualFile vFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
String fileName = vFile != null ? vFile.getPath() : null;
LogicalPosition logicalPosition = new LogicalPosition(0,0);
final Editor editor = event.getData(CommonDataKeys.EDITOR);
if (editor != null)
{
CaretModel caretModel = editor.getCaretModel();
if ( caretModel != null)
{
logicalPosition = caretModel.getLogicalPosition();
}
}
String text = "setActiveToken" + MESSAGE_SPLIT_STRING +
fileName + MESSAGE_SPLIT_STRING
+ (logicalPosition.line + 1) + MESSAGE_SPLIT_STRING + logicalPosition.column + "<EOM>";
try
{
Socket socket = new Socket(options.getIp(), options.getSourcetrailPort());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(text);
writer.flush();
socket.close();
}
catch(Exception e)
{
String errorMsg =
"No connection to a Sourcetrail instance\n\n Make sure Sourcetrail is running and the right address is used("
+ options.getIp() + ":" + options.getSourcetrailPort() + ")";
Messages.showMessageDialog(errorMsg, "SourcetrailPluginError", Messages.getErrorIcon());
e.printStackTrace();
}
StatusBar statusbar = WindowManager.getInstance().getStatusBar(PlatformDataKeys.PROJECT.getData(event.getDataContext()));
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder("Location sent to Sourcetrail", MessageType.INFO, null)
.setFadeoutTime(3000)
.createBalloon()
.show(RelativePoint.getCenterOf(statusbar.getComponent()), Balloon.Position.atRight);
}