当前位置: 首页>>代码示例>>Java>>正文


Java CaretModel类代码示例

本文整理汇总了Java中com.intellij.openapi.editor.CaretModel的典型用法代码示例。如果您正苦于以下问题:Java CaretModel类的具体用法?Java CaretModel怎么用?Java CaretModel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


CaretModel类属于com.intellij.openapi.editor包,在下文中一共展示了CaretModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getVisibleBestPopupLocation

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
@Nullable
private static Point getVisibleBestPopupLocation(@NotNull Editor editor) {
  VisualPosition visualPosition = editor.getUserData(ANCHOR_POPUP_POSITION);

  if (visualPosition == null) {
    CaretModel caretModel = editor.getCaretModel();
    if (caretModel.isUpToDate()) {
      visualPosition = caretModel.getVisualPosition();
    }
    else {
      visualPosition = editor.offsetToVisualPosition(caretModel.getOffset());
    }
  }

  Point p = editor.visualPositionToXY(new VisualPosition(visualPosition.line + 1, visualPosition.column));

  final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
  return visibleArea.contains(p) ? p : null;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:20,代码来源:PopupFactoryImpl.java

示例2: actionPerformed

import com.intellij.openapi.editor.CaretModel; //导入依赖的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);
}
 
开发者ID:FWDekker,项目名称:intellij-randomness,代码行数:29,代码来源:DataInsertAction.java

示例3: findMatchingElement

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
@Nullable
PsiElement findMatchingElement(PsiFile file,
                               Editor editor) {
  final CaretModel caretModel = editor.getCaretModel();
  final int position = caretModel.getOffset();
  PsiElement element = file.findElementAt(position);
  while (element != null) {
     if (predicate.satisfiedBy(element)) {
      return element;
    } else {
      element = element.getParent();
      if (isStopElement(element)) {
        break;
      }
    }
  }
  return null;
}
 
开发者ID:internetisalie,项目名称:lua-for-idea,代码行数:19,代码来源:Intention.java

示例4: navigate

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
/**
 * Tries to navigate caret at the given editor to the target position inserting missing white spaces if necessary.
 * 
 * @param position  target caret position
 * @param editor    target editor
 * @param project   target project
 */
@SuppressWarnings("MethodMayBeStatic")
public void navigate(@NotNull LogicalPosition position, @NotNull Editor editor, @NotNull final Project project) {
  final Document document = editor.getDocument();
  final CaretModel caretModel = editor.getCaretModel();
  final int endLineOffset = document.getLineEndOffset(position.line);
  final LogicalPosition endLinePosition = editor.offsetToLogicalPosition(endLineOffset);
  if (endLinePosition.column < position.column && !editor.getSettings().isVirtualSpace() && !editor.isViewer()) {
    final String toInsert = StringUtil.repeat(" ", position.column - endLinePosition.column);
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      @Override
      public void run() {
        document.insertString(endLineOffset, toInsert);
        PsiDocumentManager.getInstance(project).commitDocument(document);
      }
    });
    
  }
  caretModel.moveToLogicalPosition(position);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:27,代码来源:JavadocHelper.java

示例5: getSelectedMethod

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
private static PsiMethod getSelectedMethod(Editor editor, PsiFile file, DataContext dataContext) {
  final PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
  PsiMethod selectedMethod = null;
  if (element instanceof PsiMethod) {
    selectedMethod = (PsiMethod)element;
  }
  else if (element instanceof PsiParameter && ((PsiParameter)element).getDeclarationScope() instanceof PsiMethod){
    selectedMethod = (PsiMethod)((PsiParameter)element).getDeclarationScope();
  }
  else {
    final CaretModel caretModel = editor.getCaretModel();
    final int position = caretModel.getOffset();
    final PsiElement elementAt = file.findElementAt(position);
    final PsiMethodCallExpression methodCallExpression =
     PsiTreeUtil.getParentOfType(elementAt, PsiMethodCallExpression.class);
    if (methodCallExpression != null) {
      selectedMethod = methodCallExpression.resolveMethod();
    } else {
      final PsiParameterList parameterList = PsiTreeUtil.getParentOfType(elementAt, PsiParameterList.class);
      if (parameterList != null && parameterList.getParent() instanceof PsiMethod) {
        selectedMethod = (PsiMethod)parameterList.getParent();
      }
    }
  }
  return selectedMethod;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:27,代码来源:IntroduceParameterObjectHandler.java

示例6: testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
public void testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange() throws IOException {
  // Inspired by IDEA-61275

  String text =
    "/**\n" +
    " * This is a test comment\n" +
    " */\n" +
    "public <caret>class Test {\n" +
    "}";
  init(text, TestFileType.JAVA);

  CaretModel caretModel = myEditor.getCaretModel();
  int caretOffset = caretModel.getOffset();
  
  assertEquals(caretOffset, caretModel.getOffset());

  updateFoldRegions();
  toggleFoldRegionState(getFoldRegion(0), false);
  type('a');
  updateFoldRegions();

  assertEquals(caretOffset + 1, caretModel.getOffset());
  assertEquals(1, myEditor.getFoldingModel().getAllFoldRegions().length);
  FoldRegion foldRegion = getFoldRegion(0);
  assertFalse(foldRegion.isExpanded());
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:27,代码来源:FoldingProcessingOnDocumentModificationTest.java

示例7: testAddParamChangeReturnType

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
public void testAddParamChangeReturnType() {
  doTest(() -> {
    myFixture.type("int param");
    CaretModel model = myFixture.getEditor().getCaretModel();
    PsiElement element = myFixture.getElementAtCaret();
    PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class, false);
    assertTrue(method != null);
    PsiTypeElement returnTypeElement = method.getReturnTypeElement();
    assertTrue(returnTypeElement != null);
    model.moveToOffset(returnTypeElement.getTextRange().getEndOffset());
    int i = returnTypeElement.getTextLength();
    while (i-- > 0) {
      myFixture.type('\b');
    }
    myFixture.type("boolean");
  }, true, ChangeSignatureDetectorAction.CHANGE_SIGNATURE);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:18,代码来源:ChangeSignatureGestureTest.java

示例8: executeWriteAction

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
@Override
public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) {
  CaretModel caretModel = editor.getCaretModel();
  int caretOffset = caretModel.getOffset();
  if (caretOffset <= 0) {
    return;
  }

  boolean camel = editor.getSettings().isCamelWords();
  for (int i = caretOffset - 1; i >= 0; i--) {
    if (EditorActionUtil.isWordOrLexemeStart(editor, i, camel)) {
      KillRingUtil.cut(editor, i, caretOffset);
      return;
    }
  }

  KillRingUtil.cut(editor, 0, caretOffset);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:19,代码来源:KillToWordStartAction.java

示例9: doEnter

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
  if (isModified) {
    return false;
  }
  final CaretModel caretModel = editor.getCaretModel();
  PsiElement atCaret = psiElement.getContainingFile().findElementAt(caretModel.getOffset());
  if (atCaret instanceof PsiWhiteSpace) {
    atCaret = atCaret.getPrevSibling();
  }
  final PsiElement comment = PsiTreeUtil.getParentOfType(atCaret, PsiComment.class, false);
  if (comment != null) {
    SmartEnterUtil.plainEnter(editor);
    editor.getDocument().insertString(caretModel.getOffset(), "# ");
    caretModel.moveToOffset(caretModel.getOffset() + 2);
    return true;
  }
  return false;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:19,代码来源:PyCommentBreakerEnterProcessor.java

示例10: getCaretShift

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
private static int getCaretShift(PsiElement startToMove, PsiElement endToMove, CaretModel caretModel, boolean selectionStartAtCaret) {
  int shift;
  if (selectionStartAtCaret) {
    shift = caretModel.getOffset() - startToMove.getTextRange().getStartOffset();
  }
  else {
    shift = caretModel.getOffset();
    if (startToMove != endToMove) {
      shift += startToMove.getTextLength();

      PsiElement tmp = startToMove.getNextSibling();
      while (tmp != endToMove && tmp != null) {
        if (!(tmp instanceof PsiWhiteSpace))
          shift += tmp.getTextLength();
        tmp = tmp.getNextSibling();
      }
    }

    shift -= endToMove.getTextOffset();
  }
  return shift;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:23,代码来源:PyStatementMover.java

示例11: isTestClass

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
private static boolean isTestClass(PsiFile file, Editor editor) {
  PsiElement element1 = null;
  final SelectionModel selectionModel = editor.getSelectionModel();
  if (selectionModel.hasSelection()) {
    element1 = file.findElementAt(selectionModel.getSelectionStart());
  }
  else {
    final CaretModel caretModel = editor.getCaretModel();
    final Document document = editor.getDocument();
    int lineNumber = document.getLineNumber(caretModel.getOffset());
    if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
      element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
    }
  }
  if (element1 != null) {
    final PyClass clazz = PyUtil.getContainingClassOrSelf(element1);
    if (clazz != null && PythonUnitTestUtil.isTestCaseClass(clazz)) return true;
  }
  return false;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:21,代码来源:PyIntroduceFieldHandler.java

示例12: invoke

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  PsiElement element1 = null;
  PsiElement element2 = null;
  final SelectionModel selectionModel = editor.getSelectionModel();
  if (selectionModel.hasSelection()) {
    element1 = file.findElementAt(selectionModel.getSelectionStart());
    element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
  }
  else {
    final CaretModel caretModel = editor.getCaretModel();
    final Document document = editor.getDocument();
    int lineNumber = document.getLineNumber(caretModel.getOffset());
    if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
      element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
      element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
    }
  }
  if (element1 == null || element2 == null) {
    CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.introduce.selection.error"), getTitle(),
                                        "members.pull.up");
    return;
  }
  doRefactor(project, element1, element2, editor, file, dataContext);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:25,代码来源:PyClassRefactoringHandler.java

示例13: doCreateAndNavigate

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
PsiElement[] doCreateAndNavigate(String newName, PsiDirectory directory, String rootTagName, boolean chooseTagName, boolean navigate)
  throws Exception {
  final XmlFile file = AndroidResourceUtil
    .createFileResource(newName, directory, rootTagName, myResourceType.getName(), myValuesResourceFile);

  if (navigate) {
    doNavigate(file);
  }
  if (chooseTagName) {
    XmlDocument document = file.getDocument();
    if (document != null) {
      XmlTag rootTag = document.getRootTag();
      if (rootTag != null) {
        final Project project = file.getProject();
        final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
        if (editor != null) {
          CaretModel caretModel = editor.getCaretModel();
          caretModel.moveToOffset(rootTag.getTextOffset() + 1);
          XmlTagInplaceRenamer.rename(editor, rootTag);
        }
      }
    }
  }
  return new PsiElement[]{file};
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:26,代码来源:CreateTypedResourceFileAction.java

示例14: satisfiedBy

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
@Override
public boolean satisfiedBy(PsiElement element, @Nullable Editor editor) {
  if (editor == null) {
    return false;
  }
  final SelectionModel selectionModel = editor.getSelectionModel();
  final Document document = editor.getDocument();
  if (selectionModel.hasSelection()) {
    final int start = selectionModel.getSelectionStart();
    final int end = selectionModel.getSelectionEnd();
    if (start < 0 || end < 0 || start > end) {
      // shouldn't happen but http://ea.jetbrains.com/browser/ea_problems/50192
      return false;
    }
    final String text = document.getCharsSequence().subSequence(start, end).toString();
    return indexOfUnicodeEscape(text, 1) >= 0;
  }
  else {
    final CaretModel caretModel = editor.getCaretModel();
    final int lineNumber = document.getLineNumber(caretModel.getOffset());
    final String line = document.getText(new TextRange(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber)));
    final int column = caretModel.getLogicalPosition().column;
    final int index = indexOfUnicodeEscape(line, column);
    return index >= 0 && column >= index;
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:27,代码来源:UnicodeUnescapeIntention.java

示例15: updateRawHtmlText

import com.intellij.openapi.editor.CaretModel; //导入依赖的package包/类
protected void updateRawHtmlText(final String htmlTxt) {
    final DocumentEx myDocument = myTextViewer.getDocument();

    if (project.isDisposed()) return;

    ApplicationManager.getApplication().runWriteAction(new Runnable() {
        @Override
        public void run() {
            if (project.isDisposed()) return;

            CommandProcessor.getInstance().executeCommand(project, new Runnable() {
                @Override
                public void run() {
                    if (project.isDisposed()) return;

                    myDocument.replaceString(0, myDocument.getTextLength(), htmlTxt);
                    final CaretModel caretModel = myTextViewer.getCaretModel();
                    if (caretModel.getOffset() >= myDocument.getTextLength()) {
                        caretModel.moveToOffset(myDocument.getTextLength());
                    }
                }
            }, null, null, UndoConfirmationPolicy.DEFAULT, myDocument);
        }
    });
}
 
开发者ID:vsch,项目名称:idea-multimarkdown,代码行数:26,代码来源:MultiMarkdownPreviewEditor.java


注:本文中的com.intellij.openapi.editor.CaretModel类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。