|
| 1 | +package org.eclipse.cdt.lsp.internal.clangd.editor.handlers; |
| 2 | + |
| 3 | +import java.net.URI; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.eclipse.core.commands.AbstractHandler; |
| 7 | +import org.eclipse.core.commands.ExecutionEvent; |
| 8 | +import org.eclipse.core.commands.ExecutionException; |
| 9 | +import org.eclipse.core.runtime.Adapters; |
| 10 | +import org.eclipse.jface.text.BadLocationException; |
| 11 | +import org.eclipse.jface.text.IDocument; |
| 12 | +import org.eclipse.jface.text.ITextSelection; |
| 13 | +import org.eclipse.jface.viewers.ISelection; |
| 14 | +import org.eclipse.lsp4e.LSPEclipseUtils; |
| 15 | +import org.eclipse.lsp4e.LanguageServers; |
| 16 | +import org.eclipse.lsp4j.SignatureHelp; |
| 17 | +import org.eclipse.lsp4j.SignatureHelpParams; |
| 18 | +import org.eclipse.ui.IEditorInput; |
| 19 | +import org.eclipse.ui.IEditorPart; |
| 20 | +import org.eclipse.ui.IFileEditorInput; |
| 21 | +import org.eclipse.ui.IURIEditorInput; |
| 22 | +import org.eclipse.ui.handlers.HandlerUtil; |
| 23 | +import org.eclipse.ui.texteditor.ITextEditor; |
| 24 | + |
| 25 | +public class ReceiveSignatureHelpCommandHandler extends AbstractHandler { |
| 26 | + |
| 27 | + @Override |
| 28 | + public Object execute(ExecutionEvent event) throws ExecutionException { |
| 29 | + return execute(HandlerUtil.getActiveEditor(event), HandlerUtil.getCurrentSelection(event)); |
| 30 | + } |
| 31 | + |
| 32 | + @SuppressWarnings("restriction") |
| 33 | + private Object execute(IEditorPart activeEditor, ISelection currentSelection) { |
| 34 | + // Try to adapt to ITextEditor (e.g. to support editors embedded in |
| 35 | + // MultiPageEditorParts), otherwise use activeEditor. |
| 36 | + IEditorPart innerEditor = Optional.ofNullable((IEditorPart) Adapters.adapt(activeEditor, ITextEditor.class)) |
| 37 | + .orElse(activeEditor); |
| 38 | + |
| 39 | + if (!(currentSelection instanceof ITextSelection)) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + ITextSelection textSelection = (ITextSelection) currentSelection; |
| 44 | + |
| 45 | + getUri(innerEditor).ifPresent(fileUri -> { |
| 46 | + IDocument document = LSPEclipseUtils.getDocument(innerEditor.getEditorInput()); |
| 47 | + |
| 48 | + SignatureHelpParams signatureHelpParams = new SignatureHelpParams(); |
| 49 | + try { |
| 50 | + signatureHelpParams.setTextDocument(LSPEclipseUtils.toTextDocumentIdentifier(document)); |
| 51 | + signatureHelpParams.setPosition(LSPEclipseUtils.toPosition(textSelection.getOffset(), document)); |
| 52 | + } catch (BadLocationException e) { |
| 53 | + // TODO handle this |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + Optional<SignatureHelp> result = LanguageServers.forDocument(document) |
| 59 | + .computeFirst(server -> server.getTextDocumentService().signatureHelp(signatureHelpParams)) |
| 60 | + .get(); |
| 61 | + if (result.isPresent()) { |
| 62 | + // TODO return AST |
| 63 | + System.out.println(result.get()); |
| 64 | + } |
| 65 | + } catch (InterruptedException | java.util.concurrent.ExecutionException e) { |
| 66 | + // TODO Auto-generated catch block |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns the URI of the given editor depending on the type of its |
| 76 | + * {@link IEditorPart#getEditorInput()}. |
| 77 | + * |
| 78 | + * @return the URI or an empty {@link Optional} if the URI couldn't be |
| 79 | + * determined. |
| 80 | + */ |
| 81 | + private static Optional<URI> getUri(IEditorPart editor) { |
| 82 | + IEditorInput editorInput = editor.getEditorInput(); |
| 83 | + |
| 84 | + if (editorInput instanceof IFileEditorInput) { |
| 85 | + return Optional.of(((IFileEditorInput) editor.getEditorInput()).getFile().getLocationURI()); |
| 86 | + } else if (editorInput instanceof IURIEditorInput) { |
| 87 | + return Optional.of(((IURIEditorInput) editorInput).getURI()); |
| 88 | + } else { |
| 89 | + return Optional.empty(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments