Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show default values of parameters in signature help #1251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import type {
} from 'vscode-languageserver';
import { createMarkupContent } from '../documentation/safe-ds-comment-provider.js';
import { isSdsAbstractCall, SdsCallable, SdsParameter } from '../generated/ast.js';
import { getParameters, Parameter } from '../helpers/nodeProperties.js';
import { getParameters } from '../helpers/nodeProperties.js';
import { type SafeDsNodeMapper } from '../helpers/safe-ds-node-mapper.js';
import type { SafeDsServices } from '../safe-ds-module.js';
import { type SafeDsTypeComputer } from '../typing/safe-ds-type-computer.js';
import { CallableType, NamedType } from '../typing/model.js';
import { SignatureHelpProvider } from 'langium/lsp';

export class SafeDsSignatureHelpProvider implements SignatureHelpProvider {
Expand Down Expand Up @@ -88,17 +87,11 @@ export class SafeDsSignatureHelpProvider implements SignatureHelpProvider {
}

private getLabel(callable: SdsCallable): string {
const type = this.typeComputer.computeType(callable);

if (type instanceof NamedType) {
return `${type.declaration.name}(${getParameters(callable)
.map((it) => this.getParameterLabel(it))
.join(', ')})`;
} else if (type instanceof CallableType && isNamed(callable)) {
return `${callable.name}${type}`;
} else {
return type.toString();
}
const name = isNamed(callable) ? callable.name : 'λ';
const parameters = `(${getParameters(callable)
.map((it) => this.getParameterLabel(it))
.join(', ')})`;
return `${name}${parameters}`;
}

private getParameterInformation = (parameter: SdsParameter) => {
Expand All @@ -108,9 +101,10 @@ export class SafeDsSignatureHelpProvider implements SignatureHelpProvider {
};

private getParameterLabel = (parameter: SdsParameter) => {
const optionality = Parameter.isOptional(parameter) ? '?' : '';
const type = this.typeComputer.computeType(parameter);
return `${parameter.name}${optionality}: ${type}`;
const defaultValueCstNode = parameter.defaultValue?.$cstNode;
const defaultValue = defaultValueCstNode ? ` = ${defaultValueCstNode.text}` : '';
return `${parameter.name}: ${type}${defaultValue}`;
};

/* c8 ignore start */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('SafeDsSignatureHelpProvider', async () => {
`,
expectedSignature: [
{
label: 'A(p: Int) -> ()',
label: 'A(p: Int)',
documentation: {
kind: 'markdown',
value: 'Lorem ipsum.',
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('SafeDsSignatureHelpProvider', async () => {
`,
expectedSignature: [
{
label: 'f(p: Int) -> ()',
label: 'f(p: Int)',
documentation: {
kind: 'markdown',
value: 'Lorem ipsum.',
Expand All @@ -166,7 +166,7 @@ describe('SafeDsSignatureHelpProvider', async () => {
`,
expectedSignature: [
{
label: '(p: Int) -> ()',
label: 'λ(p: Int)',
documentation: undefined,
parameters: [
{
Expand All @@ -188,11 +188,11 @@ describe('SafeDsSignatureHelpProvider', async () => {
`,
expectedSignature: [
{
label: 'f(p?: Int) -> ()',
label: 'f(p: Int = 0)',
documentation: undefined,
parameters: [
{
label: 'p?: Int',
label: 'p: Int = 0',
},
],
},
Expand Down