use of org.eclipse.lsp4j.SignatureInformation in project xtext-core by eclipse.
the class SignatureHelpServiceImpl method getSignatureHelp.
private SignatureHelp getSignatureHelp(OperationCall call, String operationName, int offset) {
// If the offset exceeds either the opening or the closing characters'
// offset we should not
// provide signature help at all.
List<Integer> separatorIndices = new ArrayList<>();
for (INode node : NodeModelUtils.getNode(call).getChildren()) {
String text = node.getText();
if (SignatureHelpServiceImpl.OPENING_CHAR.equals(text) && node.getOffset() >= offset) {
return ISignatureHelpService.EMPTY;
} else {
if (SignatureHelpServiceImpl.CLOSING_CHAR.equals(text) && node.getOffset() < offset) {
return ISignatureHelpService.EMPTY;
} else {
if (SignatureHelpServiceImpl.SEPARATOR_CHAR.equals(text)) {
separatorIndices.add(node.getOffset());
}
}
}
}
// Here we will distinguish between three different AST states:
// 1. Valid state: when the number of parameter separators equals with
// the number of (parameters - 1)
// and each separator is surrounded by two parameters.
// 2. Broken recoverable state: the number of parameters equals with the
// number of separators and the
// last separator offset is greater than the last parameter (offset +
// length) and each separator is
// surrounded by two parameters. Expect the last separator.
// 3. Broken inconsistent state: non of the above cases.
int paramCount = call.getParams().size();
int separatorCount = separatorIndices.size();
if (separatorCount + 1 == paramCount || separatorCount == paramCount) {
List<INode> paramNodes = NodeModelUtils.findNodesForFeature(call, TestLanguagePackage.Literals.OPERATION__PARAMS);
// Parameter count could be greater than separator count.
for (int i = 0; i < separatorCount; i++) {
INode paramNode = paramNodes.get(i);
// should not be provided.
if (paramNode.getOffset() + paramNode.getLength() > separatorIndices.get(i).intValue()) {
return ISignatureHelpService.EMPTY;
}
}
} else {
return ISignatureHelpService.EMPTY;
}
final int currentParameter;
if (paramCount != 0) {
if (separatorIndices.contains(offset)) {
currentParameter = separatorIndices.indexOf(offset) + 2;
} else {
// Here we can execute a binary search for sure, because the
// nodes where visited in order.
currentParameter = -Collections.binarySearch(separatorIndices, offset);
}
} else {
currentParameter = 0;
}
Iterable<Operation> visibleOperations = Iterables.filter(getVisibleOperationsWithName(call, operationName), it -> currentParameter <= it.getParams().size());
int paramOffset = separatorIndices.contains(offset) ? 2 : 1;
final Integer activeParamIndex;
if (paramCount == 0) {
Iterable<Integer> paramSize = Iterables.transform(visibleOperations, it -> it.getParams().size());
// parameter on use-side.
if (!Iterables.any(paramSize, it -> it.intValue() == 0) && Iterables.any(visibleOperations, it -> !it.getParams().isEmpty())) {
activeParamIndex = 0;
} else {
activeParamIndex = null;
}
} else {
activeParamIndex = currentParameter - paramOffset;
}
SignatureHelp signatureHelp = new SignatureHelp();
signatureHelp.setActiveParameter(activeParamIndex);
signatureHelp.setActiveSignature(0);
signatureHelp.setSignatures(IterableExtensions.sortWith(IterableExtensions.toList(Iterables.transform(visibleOperations, (Operation operation) -> {
SignatureInformation signatureInformation = new SignatureInformation(getLabel(operation));
signatureInformation.setParameters(Lists.transform(operation.getParams(), (Parameter param) -> new ParameterInformation(param.getName() + ": " + getLabel(param.getType()))));
return signatureInformation;
})), SignatureHelpServiceImpl.SIGNATURE_INFO_ORDERING));
return signatureHelp;
}
use of org.eclipse.lsp4j.SignatureInformation in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method signatureHelp.
/**
* Displays a function's parameters, including which one is currently
* active. Called automatically by VSCode any time that the user types "(",
* so be sure to check that a function call is actually happening at the
* current position.
*/
@Override
public CompletableFuture<SignatureHelp> signatureHelp(TextDocumentPositionParams position) {
String textDocumentUri = position.getTextDocument().getUri();
if (!textDocumentUri.endsWith(AS_EXTENSION) && !textDocumentUri.endsWith(MXML_EXTENSION)) {
//we couldn't find a node at the specified location
return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
}
IASNode offsetNode = null;
IMXMLTagData offsetTag = getOffsetMXMLTag(position);
if (offsetTag != null) {
IMXMLTagAttributeData attributeData = getMXMLTagAttributeWithValueAtOffset(offsetTag, currentOffset);
if (attributeData != null) {
//some attributes can have ActionScript completion, such as
//events and properties with data binding
IClassDefinition tagDefinition = (IClassDefinition) currentProject.resolveXMLNameToDefinition(offsetTag.getXMLName(), offsetTag.getMXMLDialect());
IDefinition attributeDefinition = currentProject.resolveSpecifier(tagDefinition, attributeData.getShortName());
if (attributeDefinition instanceof IEventDefinition) {
IMXMLInstanceNode mxmlNode = (IMXMLInstanceNode) getOffsetNode(position);
IMXMLEventSpecifierNode eventNode = mxmlNode.getEventSpecifierNode(attributeData.getShortName());
for (IASNode asNode : eventNode.getASNodes()) {
IASNode containingNode = getContainingNodeIncludingStart(asNode, currentOffset);
if (containingNode != null) {
offsetNode = containingNode;
}
}
if (offsetNode == null) {
offsetNode = eventNode;
}
}
}
}
if (offsetNode == null) {
offsetNode = getOffsetNode(position);
}
if (offsetNode == null) {
//we couldn't find a node at the specified location
return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
}
IFunctionCallNode functionCallNode = getAncestorFunctionCallNode(offsetNode);
IFunctionDefinition functionDefinition = null;
if (functionCallNode != null) {
IExpressionNode nameNode = functionCallNode.getNameNode();
IDefinition definition = nameNode.resolve(currentUnit.getProject());
if (definition instanceof IFunctionDefinition) {
functionDefinition = (IFunctionDefinition) definition;
} else if (definition instanceof IClassDefinition) {
IClassDefinition classDefinition = (IClassDefinition) definition;
functionDefinition = classDefinition.getConstructor();
} else if (nameNode instanceof IIdentifierNode) {
//special case for super()
IIdentifierNode identifierNode = (IIdentifierNode) nameNode;
if (identifierNode.getName().equals(IASKeywordConstants.SUPER)) {
ITypeDefinition typeDefinition = nameNode.resolveType(currentProject);
if (typeDefinition instanceof IClassDefinition) {
IClassDefinition classDefinition = (IClassDefinition) typeDefinition;
functionDefinition = classDefinitionToConstructor(classDefinition);
}
}
}
}
if (functionDefinition != null) {
SignatureHelp result = new SignatureHelp();
List<SignatureInformation> signatures = new ArrayList<>();
SignatureInformation signatureInfo = new SignatureInformation();
signatureInfo.setLabel(getSignatureLabel(functionDefinition));
List<ParameterInformation> parameters = new ArrayList<>();
for (IParameterDefinition param : functionDefinition.getParameters()) {
ParameterInformation paramInfo = new ParameterInformation();
paramInfo.setLabel(param.getBaseName());
parameters.add(paramInfo);
}
signatureInfo.setParameters(parameters);
signatures.add(signatureInfo);
result.setSignatures(signatures);
result.setActiveSignature(0);
int index = getFunctionCallNodeArgumentIndex(functionCallNode, offsetNode);
IParameterDefinition[] params = functionDefinition.getParameters();
int paramCount = params.length;
if (paramCount > 0 && index >= paramCount) {
if (index >= paramCount) {
IParameterDefinition lastParam = params[paramCount - 1];
if (lastParam.isRest()) {
//functions with rest parameters may accept any
//number of arguments, so continue to make the rest
//parameter active
index = paramCount - 1;
} else {
//if there's no rest parameter, and we're beyond the
//final parameter, none should be active
index = -1;
}
}
}
if (index != -1) {
result.setActiveParameter(index);
}
return CompletableFuture.completedFuture(result);
}
return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
}
use of org.eclipse.lsp4j.SignatureInformation in project eclipse.jdt.ls by eclipse.
the class SignatureHelpHandler method signatureHelp.
public SignatureHelp signatureHelp(TextDocumentPositionParams position, IProgressMonitor monitor) {
SignatureHelp help = new SignatureHelp();
if (!preferenceManager.getPreferences(null).isSignatureHelpEnabled()) {
return help;
}
try {
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(position.getTextDocument().getUri());
final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), position.getPosition().getLine(), position.getPosition().getCharacter());
int[] contextInfomation = getContextInfomation(unit.getBuffer(), offset);
if (contextInfomation[0] == -1) {
return help;
}
SignatureHelpRequestor collector = new SignatureHelpRequestor(unit, contextInfomation[0] + 1);
if (offset > -1 && !monitor.isCanceled()) {
unit.codeComplete(contextInfomation[0] + 1, collector, monitor);
help = collector.getSignatureHelp(monitor);
if (help != null) {
int currentParameter = contextInfomation[1];
List<SignatureInformation> infos = help.getSignatures();
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).getParameters().size() >= currentParameter + 1) {
help.setActiveSignature(i);
help.setActiveParameter(currentParameter);
break;
}
}
}
}
} catch (CoreException ex) {
JavaLanguageServerPlugin.logException("Find signatureHelp failure ", ex);
}
return help;
}
use of org.eclipse.lsp4j.SignatureInformation in project eclipse.jdt.ls by eclipse.
the class SignatureHelpHandler method isSameParameters.
private boolean isSameParameters(IMethod m, SignatureHelp help, SignatureHelpRequestor collector, IJavaProject javaProject, IProgressMonitor monitor) throws JavaModelException {
if (m == null || help == null || javaProject == null) {
return false;
}
List<SignatureInformation> infos = help.getSignatures();
for (int i = 0; i < infos.size(); i++) {
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
IMethod method = JDTUtils.resolveMethod(proposal, javaProject, monitor);
if (m.getElementName().equals(method.getElementName()) && JDTUtils.isSameParameters(method, m)) {
return true;
}
}
return false;
}
use of org.eclipse.lsp4j.SignatureInformation in project eclipse.jdt.ls by eclipse.
the class SignatureHelpHandler method signatureHelp.
public SignatureHelp signatureHelp(SignatureHelpParams position, IProgressMonitor monitor) {
SignatureHelp help = new SignatureHelp();
if (!preferenceManager.getPreferences(null).isSignatureHelpEnabled()) {
return help;
}
try {
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(position.getTextDocument().getUri());
final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), position.getPosition().getLine(), position.getPosition().getCharacter());
int[] contextInfomation = getContextInfomation(unit.getBuffer(), offset);
ASTNode node = getNode(unit, contextInfomation, monitor);
if (node == null) {
return help;
}
IMethod method = getMethod(node);
String name = method != null ? method.getElementName() : getMethodName(node, unit, contextInfomation);
SignatureHelpRequestor collector = new SignatureHelpRequestor(unit, name, contextInfomation[0] + 1);
if (offset > -1 && !monitor.isCanceled()) {
unit.codeComplete(contextInfomation[0] + 1, collector, monitor);
help = collector.getSignatureHelp(monitor);
if (help.getSignatures().isEmpty()) {
int pos = offset;
if (method != null) {
int start;
if (node instanceof MethodInvocation) {
start = ((MethodInvocation) node).getName().getStartPosition();
} else if (node instanceof ClassInstanceCreation) {
start = ((ClassInstanceCreation) node).getType().getStartPosition();
} else {
start = node.getStartPosition();
}
pos = start + method.getElementName().length();
}
unit.codeComplete(pos, collector, monitor);
help = collector.getSignatureHelp(monitor);
addConstructorProposals(help, node, method, collector, pos);
}
if (!monitor.isCanceled() && help != null) {
SignatureHelp help2 = null;
SignatureHelpRequestor collector2 = null;
if (contextInfomation[0] + 1 != offset) {
collector2 = new SignatureHelpRequestor(unit, offset, name, true);
unit.codeComplete(offset, collector2, monitor);
help2 = collector2.getSignatureHelp(monitor);
}
int currentParameter = contextInfomation[1];
int size = currentParameter + 1;
List<SignatureInformation> infos = help.getSignatures();
int activeParameter = currentParameter < 0 ? 0 : currentParameter;
if (node != null) {
IJavaProject javaProject = unit.getJavaProject();
if (help2 != null) {
if (method != null) {
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).getParameters().size() >= size) {
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
if (!monitor.isCanceled() && JDTUtils.isSameParameters(m, method)) {
help.setActiveSignature(i);
help.setActiveParameter(activeParameter);
return help;
}
}
}
}
if (!monitor.isCanceled() && help.getActiveSignature() == null) {
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).getParameters().size() >= size) {
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
if (!monitor.isCanceled() && isSameParameters(m, help2, collector2, javaProject, monitor)) {
help.setActiveSignature(i);
help.setActiveParameter(activeParameter);
return help;
}
for (CompletionProposal typeProposal : collector2.getTypeProposals()) {
if (isSameParameters(m, method, typeProposal)) {
help.setActiveSignature(i);
help.setActiveParameter(activeParameter);
return help;
}
}
}
}
}
}
if (!monitor.isCanceled() && help.getActiveSignature() == null) {
if (method != null) {
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).getParameters().size() >= size) {
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
if (!monitor.isCanceled() && JDTUtils.isSameParameters(method, m)) {
help.setActiveSignature(i);
help.setActiveParameter(activeParameter);
return help;
}
}
}
}
}
if (!monitor.isCanceled() && help.getActiveSignature() == null) {
for (int i = 0; i < infos.size(); i++) {
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
if (Flags.isVarargs(proposal.getFlags())) {
help.setActiveSignature(i);
char[][] infoTypes = Signature.getParameterTypes(SignatureUtil.fix83600(proposal.getSignature()));
if (infoTypes.length <= activeParameter) {
help.setActiveParameter(infoTypes.length - 1);
} else {
help.setActiveParameter(activeParameter);
}
return help;
}
}
}
if (!monitor.isCanceled() && help.getActiveSignature() == null && node instanceof Block) {
String methodName = getMethodName(node, unit, contextInfomation);
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).getParameters().size() >= activeParameter) {
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
if (!monitor.isCanceled() && m != null && m.getElementName().equals(methodName)) {
help.setActiveSignature(i);
help.setActiveParameter(activeParameter);
return help;
}
}
}
}
if (method != null && !monitor.isCanceled() && help.getActiveSignature() == null) {
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).getParameters().size() >= size) {
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
if (!monitor.isCanceled() && isSameParameters(m, method, null)) {
help.setActiveSignature(i);
help.setActiveParameter(activeParameter);
return help;
}
}
}
}
}
}
}
} catch (CoreException ex) {
JavaLanguageServerPlugin.logException("Find signatureHelp failure ", ex);
}
return help;
}
Aggregations