Search in sources :

Example 1 with Inlay

use of com.intellij.openapi.editor.Inlay in project intellij-community by JetBrains.

the class JavaMethodOverloadSwitchHandler method executeWriteAction.

@Override
public void executeWriteAction(Editor editor, @Nullable Caret caret, DataContext dataContext) {
    if (caret == null)
        caret = editor.getCaretModel().getPrimaryCaret();
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return;
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    PsiElement exprList = getExpressionList(editor, caret.getOffset(), project);
    if (!(exprList instanceof PsiExpressionList))
        return;
    int lbraceOffset = exprList.getTextRange().getStartOffset();
    ParameterInfoController controller = ParameterInfoController.findControllerAtOffset(editor, lbraceOffset);
    if (controller == null)
        return;
    Object[] objects = controller.getObjects();
    Object highlighted = controller.getHighlighted();
    if (objects == null || objects.length <= 1 || highlighted == null)
        return;
    int currentIndex = ContainerUtil.indexOf(Arrays.asList(objects), highlighted);
    if (currentIndex < 0)
        return;
    PsiMethod currentMethod = (PsiMethod) ((CandidateInfo) objects[currentIndex]).getElement();
    Map<String, String> enteredParameters = exprList.getUserData(ENTERED_PARAMETERS);
    if (enteredParameters == null) {
        exprList.putUserData(ENTERED_PARAMETERS, enteredParameters = new HashMap<>());
    }
    PsiExpression[] enteredExpressions = ((PsiExpressionList) exprList).getExpressions();
    assert enteredExpressions.length == 0 || enteredExpressions.length == currentMethod.getParameterList().getParametersCount();
    for (int i = 0; i < enteredExpressions.length; i++) {
        PsiExpression expression = enteredExpressions[i];
        String value = expression.getText().trim();
        if (!value.isEmpty()) {
            String key = getParameterKey(currentMethod, i);
            enteredParameters.put(key, value);
        }
    }
    PsiMethod targetMethod = (PsiMethod) ((CandidateInfo) objects[(currentIndex + (mySwitchUp ? -1 : 1) + objects.length) % objects.length]).getElement();
    PsiParameterList parameterList = targetMethod.getParameterList();
    int parametersCount = parameterList.getParametersCount();
    // avoid caret impact on hints location
    caret.moveToOffset(lbraceOffset);
    int offset = lbraceOffset + 1;
    int endOffset = exprList.getTextRange().getEndOffset() - 1;
    List<Inlay> oldInlays = editor.getInlayModel().getInlineElementsInRange(offset, endOffset);
    for (Inlay inlay : oldInlays) {
        Disposer.dispose(inlay);
    }
    editor.getDocument().deleteString(offset, endOffset);
    int targetCaretPosition = -1;
    List<Inlay> addedHints = new ArrayList<>(parametersCount);
    for (int i = 0; i < parametersCount; i++) {
        String key = getParameterKey(targetMethod, i);
        String value = enteredParameters.getOrDefault(key, "");
        if (value.isEmpty() && targetCaretPosition == -1)
            targetCaretPosition = offset;
        if (i < parametersCount - 1)
            value += ", ";
        editor.getDocument().insertString(offset, value);
        String name = parameterList.getParameters()[i].getName();
        if (name != null) {
            addedHints.add(ParameterHintsPresentationManager.getInstance().addHint(editor, offset, name + ":", false, true));
        }
        offset += value.length();
    }
    if (targetCaretPosition == -1)
        targetCaretPosition = offset;
    caret.moveToLogicalPosition(editor.offsetToLogicalPosition(targetCaretPosition).leanForward(true));
    Disposer.register(controller, () -> {
        for (Inlay hint : addedHints) {
            if (hint != null)
                ParameterHintsPresentationManager.getInstance().unpin(hint);
        }
    });
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    CompletionMemory.registerChosenMethod(targetMethod, (PsiCall) exprList.getParent());
    // update popup immediately (otherwise, it will be updated only after delay)
    controller.updateComponent();
}
Also used : Inlay(com.intellij.openapi.editor.Inlay) ParameterInfoController(com.intellij.codeInsight.hint.ParameterInfoController) Project(com.intellij.openapi.project.Project)

Example 2 with Inlay

use of com.intellij.openapi.editor.Inlay in project intellij-community by JetBrains.

the class EditorInlayTest method testInlayDoesntGetInsideSurrogatePair.

public void testInlayDoesntGetInsideSurrogatePair() throws Exception {
    // Cannot set up text with singular surrogate characters directly
    initText("");
    runWriteCommand(() -> myEditor.getDocument().setText(HIGH_SURROGATE + LOW_SURROGATE + LOW_SURROGATE));
    Inlay inlay = addInlay(2);
    assertNotNull(inlay);
    assertTrue(inlay.isValid());
    runWriteCommand(() -> ((DocumentEx) myEditor.getDocument()).moveText(2, 3, 1));
    assertFalse(inlay.isValid() && DocumentUtil.isInsideSurrogatePair(myEditor.getDocument(), inlay.getOffset()));
}
Also used : Inlay(com.intellij.openapi.editor.Inlay)

Example 3 with Inlay

use of com.intellij.openapi.editor.Inlay in project intellij-community by JetBrains.

the class XDebuggerInlayUtil method clearInlays.

public static void clearInlays(@NotNull Project project) {
    UIUtil.invokeLaterIfNeeded(() -> {
        FileEditor[] editors = FileEditorManager.getInstance(project).getAllEditors();
        for (FileEditor editor : editors) {
            if (editor instanceof TextEditor) {
                Editor e = ((TextEditor) editor).getEditor();
                List<Inlay> existing = e.getInlayModel().getInlineElementsInRange(0, e.getDocument().getTextLength());
                for (Inlay inlay : existing) {
                    if (inlay.getRenderer() instanceof MyRenderer) {
                        Disposer.dispose(inlay);
                    }
                }
            }
        }
    });
}
Also used : FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Inlay(com.intellij.openapi.editor.Inlay) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Example 4 with Inlay

use of com.intellij.openapi.editor.Inlay in project intellij-community by JetBrains.

the class JavaMethodCallElement method showParameterHints.

public static void showParameterHints(InsertionContext context, PsiMethod method, PsiCallExpression methodCall) {
    PsiParameterList parameterList = method.getParameterList();
    int parametersCount = parameterList.getParametersCount();
    if (methodCall == null || parametersCount == 0 || context.getCompletionChar() == Lookup.COMPLETE_STATEMENT_SELECT_CHAR || Registry.is("java.completion.argument.live.template") || !Registry.is("java.completion.argument.hints")) {
        return;
    }
    Editor editor = context.getEditor();
    CaretModel caretModel = editor.getCaretModel();
    int offset = caretModel.getOffset();
    // avoid caret impact on hints location
    caretModel.moveToOffset(offset - 1);
    editor.getDocument().insertString(offset, StringUtil.repeat(", ", parametersCount - 1));
    List<Inlay> addedHints = new ArrayList<>(parametersCount);
    for (PsiParameter parameter : parameterList.getParameters()) {
        String name = parameter.getName();
        if (name != null) {
            addedHints.add(ParameterHintsPresentationManager.getInstance().addHint(editor, offset, name + ":", false, true));
        }
        offset += 2;
    }
    int braceOffset = caretModel.getOffset();
    caretModel.moveToLogicalPosition(editor.offsetToLogicalPosition(braceOffset + 1).leanForward(true));
    Project project = context.getProject();
    MethodParameterInfoHandler handler = new MethodParameterInfoHandler();
    ShowParameterInfoContext infoContext = new ShowParameterInfoContext(editor, project, context.getFile(), braceOffset, braceOffset);
    handler.findElementForParameterInfo(infoContext);
    Disposer.register(new ParameterInfoController(project, editor, braceOffset, infoContext.getItemsToShow(), null, methodCall.getArgumentList(), handler, false, false), () -> {
        for (Inlay inlay : addedHints) {
            if (inlay != null)
                ParameterHintsPresentationManager.getInstance().unpin(inlay);
        }
    });
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) Inlay(com.intellij.openapi.editor.Inlay) ArrayList(java.util.ArrayList) ParameterInfoController(com.intellij.codeInsight.hint.ParameterInfoController) Project(com.intellij.openapi.project.Project) MethodParameterInfoHandler(com.intellij.codeInsight.hint.api.impls.MethodParameterInfoHandler) Editor(com.intellij.openapi.editor.Editor) ShowParameterInfoContext(com.intellij.codeInsight.hint.ShowParameterInfoContext)

Example 5 with Inlay

use of com.intellij.openapi.editor.Inlay in project intellij-community by JetBrains.

the class ParameterHintsPresentationManager method addHint.

public Inlay addHint(@NotNull Editor editor, int offset, @NotNull String hintText, boolean useAnimation, boolean pinned) {
    MyRenderer renderer = new MyRenderer(editor, hintText, useAnimation);
    Inlay inlay = editor.getInlayModel().addInlineElement(offset, renderer);
    if (inlay != null) {
        if (pinned)
            inlay.putUserData(PINNED, Boolean.TRUE);
        if (useAnimation)
            scheduleRendererUpdate(editor, inlay);
    }
    return inlay;
}
Also used : Inlay(com.intellij.openapi.editor.Inlay)

Aggregations

Inlay (com.intellij.openapi.editor.Inlay)9 Editor (com.intellij.openapi.editor.Editor)3 ParameterInfoController (com.intellij.codeInsight.hint.ParameterInfoController)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2 Project (com.intellij.openapi.project.Project)2 ShowParameterInfoContext (com.intellij.codeInsight.hint.ShowParameterInfoContext)1 MethodParameterInfoHandler (com.intellij.codeInsight.hint.api.impls.MethodParameterInfoHandler)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 EditorCustomElementRenderer (com.intellij.openapi.editor.EditorCustomElementRenderer)1 VisualPosition (com.intellij.openapi.editor.VisualPosition)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1