com.intellij.openapi.editor.VisualPosition Java Examples

The following examples show how to use com.intellij.openapi.editor.VisualPosition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Selector.java    From translator with MIT License 6 votes vote down vote up
public VisualPosition toVisualPosition() {
    int line = 0;
    int column = 0;

    if(editor != null){
        VisualPosition start = selectionModel.getSelectionStartPosition();
        VisualPosition end = selectionModel.getSelectionEndPosition();

        if (end != null && start != null) {
            line = end.getLine() + LINE_INTERVAL;
            column = start.getColumn() + ((end.column - start.getColumn())/2);
        }
    }

    return new VisualPosition(line, column);
}
 
Example #2
Source File: DuplicateLinesAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredWriteAction
@Override
public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) {
  if (editor.getSelectionModel().hasSelection()) {
    int selStart = editor.getSelectionModel().getSelectionStart();
    int selEnd = editor.getSelectionModel().getSelectionEnd();
    VisualPosition rangeStart = editor.offsetToVisualPosition(Math.min(selStart, selEnd));
    VisualPosition rangeEnd = editor.offsetToVisualPosition(Math.max(selStart, selEnd));
    final Pair<Integer,Integer> copiedRange =
      DuplicateAction.duplicateLinesRange(editor, editor.getDocument(), rangeStart, rangeEnd);
    if (copiedRange != null) {
      editor.getSelectionModel().setSelection(copiedRange.first, copiedRange.second);
    }
  }
  else {
    VisualPosition caretPos = editor.getCaretModel().getVisualPosition();
    DuplicateAction.duplicateLinesRange(editor, editor.getDocument(), caretPos, caretPos);
  }
}
 
Example #3
Source File: TypeInfoUtil.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String getTypeInfo(Module module, VisualPosition blockStart, VisualPosition blockEnd, VirtualFile projectFile) {
    final String canonicalPath = projectFile.getCanonicalPath();
    if (canonicalPath == null){
        return null;
    }
    final String workDir = ExecUtil.guessWorkDir(module);
    if (ToolKey.GHC_MODI_KEY.getPath(module.getProject()) != null) {
        GhcModi ghcModi = module.getComponent(GhcModi.class);
        if (ghcModi != null) {
            return GhcModi.getFutureType(module.getProject(), ghcModi.type(canonicalPath,
                    blockStart, blockEnd));

        } else {
            return null;
        }
    } else {
        return GhcMod.type(module, workDir, canonicalPath, blockStart, blockEnd);
    }
}
 
Example #4
Source File: GhcModi.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
public Future<String> type(final @NotNull String canonicalPath,
                           @NotNull final VisualPosition startPosition,
                           @NotNull final VisualPosition  stopPosition) {
    return handleGhcModiCall(new GhcModiCallable<String>(){
        @Override
        public String call() throws GhcModiError {
            final String command = "type " + canonicalPath + ' ' + startPosition.line + ' ' + startPosition.column;
            final String stdout = simpleExec(command);
            try {
                return stdout == null ? null : GhcModUtil.handleTypeInfo(startPosition, stopPosition, stdout);
            } catch (GhcModUtil.TypeInfoParseException e) {
                NotificationUtil.displayToolsNotification(
                  NotificationType.ERROR, module.getProject(), "Type Info Error",
                  "There was an error when executing the ghc-modi `type` command:\n\n" + stdout);
                return null;
            }
        }
    });
}
 
Example #5
Source File: NamedElementDuplicateHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredWriteAction
@Override
public void executeWriteAction(Editor editor, DataContext dataContext) {
  Project project = editor.getProject();
  if (project != null && !editor.getSelectionModel().hasSelection()) {
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (file != null) {
      VisualPosition caret = editor.getCaretModel().getVisualPosition();
      Pair<LogicalPosition, LogicalPosition> lines = EditorUtil.calcSurroundingRange(editor, caret, caret);
      TextRange toDuplicate = new TextRange(editor.logicalPositionToOffset(lines.first), editor.logicalPositionToOffset(lines.second));

      PsiElement name = findNameIdentifier(editor, file, toDuplicate);
      if (name != null && !name.getTextRange().containsOffset(editor.getCaretModel().getOffset())) {
        editor.getCaretModel().moveToOffset(name.getTextOffset());
      }
    }
  }

  myOriginal.execute(editor, dataContext);
}
 
Example #6
Source File: HaskellDocumentationProvider.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    int startOffset = element.getTextRange().getStartOffset();
    int endOffset = element.getTextRange().getEndOffset();
    Module module = ModuleUtilCore.findModuleForPsiElement(element);
    FileDocumentManager fileDocumentManager= FileDocumentManager.getInstance();
    VirtualFile projectFile = element.getContainingFile().getVirtualFile();
    Document cachedDocument = fileDocumentManager.getCachedDocument(projectFile);
    if (cachedDocument == null) {
        return null;
    }
    int startLineNumber = cachedDocument.getLineNumber(startOffset);
    int endLineNumber = cachedDocument.getLineNumber(endOffset);
    int startColumn = startOffset - cachedDocument.getLineStartOffset(startLineNumber);
    int endColumn = endOffset - cachedDocument.getLineStartOffset(endLineNumber);

    // and also correct them for (0,0) vs (1,1) leftmost coordinate (intellij -> ghc)
    VisualPosition startPosition = TypeInfoUtil.correctFor0BasedVS1Based(new VisualPosition(startLineNumber, startColumn));
    VisualPosition endPosition = TypeInfoUtil.correctFor0BasedVS1Based(new VisualPosition(endLineNumber, endColumn));
    return TypeInfoUtil.getTypeInfo(module,startPosition,endPosition, projectFile);
}
 
Example #7
Source File: PopupFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static Point getVisibleBestPopupLocation(@Nonnull 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());
    }
  }

  final int lineHeight = editor.getLineHeight();
  Point p = editor.visualPositionToXY(visualPosition);
  p.y += lineHeight;

  final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
  return !visibleArea.contains(p) && !visibleArea.contains(p.x, p.y - lineHeight) ? null : p;
}
 
Example #8
Source File: AfterLineEndInlayImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public VisualPosition getVisualPosition() {
  int offset = getOffset();
  int logicalLine = myEditor.getDocument().getLineNumber(offset);
  int lineEndOffset = myEditor.getDocument().getLineEndOffset(logicalLine);
  VisualPosition position = myEditor.offsetToVisualPosition(lineEndOffset, true, true);
  if (myEditor.getFoldingModel().isOffsetCollapsed(lineEndOffset)) return position;
  List<Inlay> inlays = myEditor.getInlayModel().getAfterLineEndElementsForLogicalLine(logicalLine);
  int order = inlays.indexOf(this);
  return new VisualPosition(position.line, position.column + 1 + order);
}
 
Example #9
Source File: GhcMod.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String type(@NotNull Module module, @NotNull String workDir, @NotNull String canonicalPath,
                          VisualPosition startPosition, @NotNull VisualPosition stopPosition) {
    final String stdout = simpleExec(module, workDir, getFlags(module.getProject()), "type" , canonicalPath,
            String.valueOf(startPosition.line), String.valueOf(startPosition.column));
    if (stdout == null) return null;
    try {
        return GhcModUtil.handleTypeInfo(startPosition, stopPosition, stdout);
    } catch (GhcModUtil.TypeInfoParseException e) {
          NotificationUtil.displayToolsNotification(
                  NotificationType.ERROR, module.getProject(), "Type Info Error",
                  "There was an error when executing the `ghc-mod type` command:\n\n" + stdout);
          return null;
    }
}
 
Example #10
Source File: HintManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void showQuestionHint(@Nonnull final Editor editor,
                             final int offset1,
                             final int offset2,
                             @Nonnull final LightweightHint hint,
                             @Nonnull final QuestionAction action,
                             @PositionFlags short constraint) {
  final VisualPosition pos1 = editor.offsetToVisualPosition(offset1);
  final VisualPosition pos2 = editor.offsetToVisualPosition(offset2);
  final Point p = getHintPosition(hint, editor, pos1, pos2, constraint);
  showQuestionHint(editor, p, offset1, offset2, hint, action, constraint);
}
 
Example #11
Source File: HintManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void showErrorHint(@Nonnull Editor editor, @Nonnull String hintText, int offset1, int offset2, short constraint, int flags, int timeout) {
  JComponent label = HintUtil.createErrorLabel(hintText);
  LightweightHint hint = new LightweightHint(label);
  final VisualPosition pos1 = editor.offsetToVisualPosition(offset1);
  final VisualPosition pos2 = editor.offsetToVisualPosition(offset2);
  final Point p = getHintPosition(hint, editor, pos1, pos2, constraint);
  showEditorHint(hint, editor, p, flags, timeout, false);
}
 
Example #12
Source File: GhcModUtilTest.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public void testCanHandleTypeWithSpacesOutput() {
    String ghcModTypeInfo =
            "24 48 24 59 \"(Player -> Bool) -> [Player] -> [Player]\"\n" +
            "24 48 24 109 \"[Player] -> [Player]\"\n" +
            "24 1 25 52 \"Player -> [Player] -> [Player]\"";
    String typeInfo = GhcModUtil.unsafeHandleTypeInfo(new VisualPosition (24, 49), new VisualPosition (24, 49), ghcModTypeInfo);
    Assert.assertEquals("(Player -> Bool) -> [Player] -> [Player]", typeInfo);
}
 
Example #13
Source File: GhcModUtilTest.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public void testCanHandleMultipleLines() {
    String ghcModTypeInfo =
            "24 48 24 59 \"(Player -> Bool) -> [Player] -> [Player]\"\n" +
            "24 48 24 109 \"[Player] -> [Player]\"\n" +
            "24 1 25 52 \"Player -> [Player] -> [Player]\"";
    String typeInfo = GhcModUtil.unsafeHandleTypeInfo(new VisualPosition (24, 60),new VisualPosition (24, 60), ghcModTypeInfo);
    Assert.assertEquals("[Player] -> [Player]", typeInfo);
}
 
Example #14
Source File: GhcModUtil.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public static String handleTypeInfo(VisualPosition selectionStartPosition,
                                    VisualPosition selectionStopPosition,
                                    @NotNull String stdout) throws TypeInfoParseException {
    try {
        return unsafeHandleTypeInfo(selectionStartPosition, selectionStopPosition, stdout);
    } catch (InputMismatchException e) {
        throw new TypeInfoParseException(stdout, e);
    }
}
 
Example #15
Source File: GhcModUtil.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a String to display to the user as the type info.
 * If we are unable to parse the type info from ghc-mod, a
 * java.util.InputMismatchException might be thrown from the Scanner.
 */
@SuppressWarnings("ObjectAllocationInLoop") // Should only be 3-5 loops.
public static String unsafeHandleTypeInfo(VisualPosition selectionStartPosition,
                                          VisualPosition selectionStopPosition,
                                          @NotNull String stdout) {
    Scanner typeInfosScanner = new Scanner(stdout);
    String lineSeparator = System.getProperty("line.separator");
    typeInfosScanner.useDelimiter(lineSeparator);
    while (typeInfosScanner.hasNext()){
        Scanner typeInfoScanner = new Scanner(typeInfosScanner.next());
        typeInfoScanner.useDelimiter("\"");
        String rowAndColInfo = typeInfoScanner.next();
        Scanner rowAndColScanner = new Scanner(rowAndColInfo);
        int startRow = rowAndColScanner.nextInt();
        int startCol = rowAndColScanner.nextInt();
        int endRow   = rowAndColScanner.nextInt();
        int endCol   = rowAndColScanner.nextInt();
        String typeOnRowAndCol = typeInfoScanner.next();
        if (! (new VisualPosition(startRow, startCol).after(selectionStartPosition))
                && ! selectionStopPosition.after(new VisualPosition(endRow, endCol))){
            typeInfosScanner.close();
            typeInfoScanner.close();
            rowAndColScanner.close();
            return typeOnRowAndCol;

        }
        typeInfoScanner.close();
        rowAndColScanner.close();
    }
    typeInfosScanner.close();
    return "No enclosing type found";
}
 
Example #16
Source File: GhcModUtilTest.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public void testCanUseSelection() {
    String ghcModTypeInfo =
            "24 48 24 59 \"(Player -> Bool) -> [Player] -> [Player]\"\n" +
            "24 48 24 109 \"[Player] -> [Player]\"\n" +
            "24 1 25 52 \"Player -> [Player] -> [Player]\"";
    String typeInfo = GhcModUtil.unsafeHandleTypeInfo(
            new VisualPosition(24, 5),
            new VisualPosition (24, 60), ghcModTypeInfo);
    Assert.assertEquals("Player -> [Player] -> [Player]", typeInfo);
}
 
Example #17
Source File: DesktopEditorAnalyzeStatusPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void doUpdateTrafficLightVisibility() {
  if (trafficLightVisible) {
    if (showToolbar && myEditor.myView != null) {
      VisualPosition pos = myEditor.getCaretModel().getPrimaryCaret().getVisualPosition();
      Point point = myEditor.visualPositionToXY(pos);
      point = SwingUtilities.convertPoint(myEditor.getContentComponent(), point, myEditor.getScrollPane());

      JComponent stComponent = statusToolbar.getComponent();
      if (stComponent.isVisible()) {
        Rectangle bounds = SwingUtilities.convertRectangle(stComponent, stComponent.getBounds(), myEditor.getScrollPane());

        if (!bounds.isEmpty() && bounds.contains(point)) {
          cachedToolbarBounds = bounds;
          stComponent.setVisible(false);
          setSmallIconVisible(true);
        }
      }
      else if (!cachedToolbarBounds.contains(point)) {
        stComponent.setVisible(true);
        setSmallIconVisible(false);
      }
    }
    else {
      statusToolbar.getComponent().setVisible(false);
      setSmallIconVisible(true);
    }
  }
  else {
    statusToolbar.getComponent().setVisible(false);
    setSmallIconVisible(false);
  }
}
 
Example #18
Source File: EditorView.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public VisualPosition offsetToVisualPosition(int offset, boolean leanTowardsLargerOffsets, boolean beforeSoftWrap) {
  assertIsDispatchThread();
  assertNotInBulkMode();
  myEditor.getSoftWrapModel().prepareToMapping();
  return myMapper.offsetToVisualPosition(offset, leanTowardsLargerOffsets, beforeSoftWrap);
}
 
Example #19
Source File: EditorView.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public VisualPosition logicalToVisualPosition(@Nonnull LogicalPosition pos, boolean beforeSoftWrap) {
  assertIsDispatchThread();
  assertNotInBulkMode();
  myEditor.getSoftWrapModel().prepareToMapping();
  return myMapper.logicalToVisualPosition(pos, beforeSoftWrap);
}
 
Example #20
Source File: EditorView.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public LogicalPosition visualToLogicalPosition(@Nonnull VisualPosition pos) {
  assertIsDispatchThread();
  assertNotInBulkMode();
  myEditor.getSoftWrapModel().prepareToMapping();
  return myMapper.visualToLogicalPosition(pos);
}
 
Example #21
Source File: EditorView.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public VisualPosition xyToVisualPosition(@Nonnull Point2D p) {
  assertIsDispatchThread();
  assertNotInBulkMode();
  myEditor.getSoftWrapModel().prepareToMapping();
  return myMapper.xyToVisualPosition(p);
}
 
Example #22
Source File: EditorView.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public Point2D visualPositionToXY(@Nonnull VisualPosition pos) {
  assertIsDispatchThread();
  assertNotInBulkMode();
  myEditor.getSoftWrapModel().prepareToMapping();
  return myMapper.visualPositionToXY(pos);
}
 
Example #23
Source File: InlineInlayImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public VisualPosition getVisualPosition() {
  int offset = getOffset();
  VisualPosition pos = myEditor.offsetToVisualPosition(offset);
  List<Inlay> inlays = myEditor.getInlayModel().getInlineElementsInRange(offset, offset);
  int order = inlays.indexOf(this);
  return new VisualPosition(pos.line, pos.column + order, true);
}
 
Example #24
Source File: CompositeSoftWrapPainter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public int paint(@Nonnull Graphics g, @Nonnull SoftWrapDrawingType drawingType, int x, int y, int lineHeight) {
  initDelegateIfNecessary();
  if (!myEditor.getSettings().isAllSoftWrapsShown()) {
    int visualLine = y / lineHeight;
    LogicalPosition position = myEditor.visualToLogicalPosition(new VisualPosition(visualLine, 0));
    if (position.line != myEditor.getCaretModel().getLogicalPosition().line) {
      return myDelegate.getDrawingHorizontalOffset(g, drawingType, x, y, lineHeight);
    }
  }
  return myDelegate.paint(g, drawingType, x, y, lineHeight);
}
 
Example #25
Source File: ParameterHintsPassFactory.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean delayRemoval(Inlay inlay, TIntObjectHashMap<Caret> caretMap) {
  int offset = inlay.getOffset();
  Caret caret = caretMap.get(offset);
  if (caret == null) return false;
  char afterCaret = myEditor.getDocument().getImmutableCharSequence().charAt(offset);
  if (afterCaret != ',' && afterCaret != ')') return false;
  VisualPosition afterInlayPosition = myEditor.offsetToVisualPosition(offset, true, false);
  // check whether caret is to the right of inlay
  if (!caret.getVisualPosition().equals(afterInlayPosition)) return false;
  return true;
}
 
Example #26
Source File: IntentionHintComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean canPlaceBulbOnTheSameLine(Editor editor) {
  if (ApplicationManager.getApplication().isUnitTestMode() || editor.isOneLineMode()) return false;
  if (Registry.is("always.show.intention.above.current.line", false)) return false;
  final int offset = editor.getCaretModel().getOffset();
  final VisualPosition pos = editor.offsetToVisualPosition(offset);
  int line = pos.line;

  final int firstNonSpaceColumnOnTheLine = EditorActionUtil.findFirstNonSpaceColumnOnTheLine(editor, line);
  if (firstNonSpaceColumnOnTheLine == -1) return false;
  final Point point = editor.visualPositionToXY(new VisualPosition(line, firstNonSpaceColumnOnTheLine));
  return point.x > AllIcons.Actions.RealIntentionBulb.getIconWidth() + (editor.isOneLineMode() ? SMALL_BORDER_SIZE : NORMAL_BORDER_SIZE) * 2;
}
 
Example #27
Source File: SelectionEndColumnMacro.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected String expand(Editor editor) {
  VisualPosition selectionEndPosition = editor.getSelectionModel().getSelectionEndPosition();
  if (selectionEndPosition == null) {
    return null;
  }
  return String.valueOf(editor.visualToLogicalPosition(selectionEndPosition).column + 1);
}
 
Example #28
Source File: SelectionEndLineMacro.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected String expand(Editor editor) {
  VisualPosition selectionEndPosition = editor.getSelectionModel().getSelectionEndPosition();
  if (selectionEndPosition == null) {
    return null;
  }
  return String.valueOf(editor.visualToLogicalPosition(selectionEndPosition).line + 1);
}
 
Example #29
Source File: SelectionStartLineMacro.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected String expand(Editor editor) {
  VisualPosition selectionStartPosition = editor.getSelectionModel().getSelectionStartPosition();
  if (selectionStartPosition == null) {
    return null;
  }
  return String.valueOf(editor.visualToLogicalPosition(selectionStartPosition).line + 1);
}
 
Example #30
Source File: SelectionStartColumnMacro.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected String expand(Editor editor) {
  VisualPosition selectionStartPosition = editor.getSelectionModel().getSelectionStartPosition();
  if (selectionStartPosition == null) {
    return null;
  }
  return String.valueOf(editor.visualToLogicalPosition(selectionStartPosition).column + 1);
}