Skip to content

Commit

Permalink
feat: emulate vscode's "editor.action.triggerSuggest" command
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <fbricon@gmail.com>
  • Loading branch information
fbricon authored and angelozerr committed Jan 11, 2024
1 parent a846eeb commit bfce498
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.lsp4ij.commands.editor;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.redhat.devtools.lsp4ij.commands.LSPCommand;
import com.redhat.devtools.lsp4ij.commands.LSPCommandAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Emulates Visual Studio Code's "editor.action.triggerSuggest" command, to trigger code completion after selecting a completion item.
*/
public class TriggerSuggestAction extends LSPCommandAction {

private static final String CODE_COMPLETION_ACTION = "CodeCompletion";

@Override
protected void commandPerformed(@NotNull LSPCommand command, @NotNull AnActionEvent e) {
Project project = e.getProject();
if (project == null) {
return;
}
@Nullable TextEditor activeEditor = findActiveEditor(project);
if (activeEditor == null) {
return;
}
AnAction codeCompletionAction = ActionManager.getInstance().getAction(CODE_COMPLETION_ACTION);
if (codeCompletionAction != null ) {
DataContext dataContext = createDataContext(activeEditor, project);
ActionUtil.invokeAction(codeCompletionAction, dataContext, ActionPlaces.UNKNOWN, null, null);
}
}

private static DataContext createDataContext(TextEditor activeEditor, Project project) {
SimpleDataContext.Builder contextBuilder = SimpleDataContext.builder();
contextBuilder.add(CommonDataKeys.PROJECT, project)
.add(CommonDataKeys.EDITOR, activeEditor.getEditor());
return contextBuilder.build();
}

public static @Nullable TextEditor findActiveEditor(Project project) {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
FileEditor editor = fileEditorManager.getSelectedEditor();
return editor instanceof TextEditor? (TextEditor) editor: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public void handleInsert(@NotNull InsertionContext context) {
EditorModificationUtil.moveCaretRelatively(editor, -template.getTemplateText().length());
TemplateManager.getInstance(context.getProject()).startTemplate(context.getEditor(), template);
}
// Execute custom command of the completion item if needed
Command command = item.getCommand();
if (command != null) {
executeCustomCommand(command, LSPIJUtils.toUri(context.getDocument()));
}
}

/**
Expand Down Expand Up @@ -267,11 +272,7 @@ protected void apply(Document document, char trigger, int stateMask, int offset)
LSPIJUtils.applyEdits(editor, document, Collections.singletonList(textEdit));
}

// Execute custom command of the completion item if needed
Command command = item.getCommand();
if (command != null) {
executeCustomCommand(command, LSPIJUtils.toUri(document));
}

} catch (RuntimeException ex) {
LOGGER.warn(ex.getLocalizedMessage(), ex);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@
id="lsp.console.explorer.copy.command"
class="com.redhat.devtools.lsp4ij.console.explorer.actions.CopyStartServerCommandAction"
icon="AllIcons.Actions.Copy"/>


<!-- Common LSP Client actions -->
<!-- Reuse VS Code action ids while waiting for standardization of LSP commands https://github.com/microsoft/language-server-protocol/issues/788 -->
<action id="editor.action.triggerSuggest"
class="com.redhat.devtools.lsp4ij.commands.editor.TriggerSuggestAction"
/>
</actions>

<!-- LSP application listeners -->
Expand Down

0 comments on commit bfce498

Please sign in to comment.