Skip to content

Commit 8fadc40

Browse files
committed
Add action for requesting signature help
1 parent 5b807c3 commit 8fadc40

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

bundles/org.eclipse.cdt.lsp.clangd/plugin.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
id="org.eclipse.cdt.lsp.clangd.editor.commands.command.getSymbolInfo"
8080
name="Receive Symbol Info">
8181
</command>
82+
<command
83+
id="org.eclipse.cdt.lsp.clangd.editor.commands.command.getSignatureHelp"
84+
name="Receive Signature Help">
85+
</command>
8286
</extension>
8387
<extension
8488
point="org.eclipse.ui.bindings">
@@ -125,6 +129,18 @@
125129
</with>
126130
</activeWhen>
127131
</handler>
132+
<handler
133+
class="org.eclipse.cdt.lsp.internal.clangd.editor.handlers.ReceiveSignatureHelpCommandHandler"
134+
commandId="org.eclipse.cdt.lsp.clangd.editor.commands.command.getSignatureHelp">
135+
<activeWhen>
136+
<with
137+
variable="activeEditor">
138+
<test
139+
property="org.eclipse.cdt.lsp.editor.active">
140+
</test>
141+
</with>
142+
</activeWhen>
143+
</handler>
128144
</extension>
129145
<extension
130146
point="org.eclipse.ui.menus">
@@ -155,6 +171,14 @@
155171
checkEnabled="true">
156172
</visibleWhen>
157173
</command>
174+
<command
175+
commandId="org.eclipse.cdt.lsp.clangd.editor.commands.command.getSignatureHelp"
176+
label="Receive Signature Help"
177+
style="push">
178+
<visibleWhen
179+
checkEnabled="true">
180+
</visibleWhen>
181+
</command>
158182
</menuContribution>
159183
</extension>
160184

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)