Skip to content

Commit

Permalink
feat: show default values of parameters in signature help (#1251)
Browse files Browse the repository at this point in the history
Closes #1147

### Summary of Changes

Show default values of parameters in the signature help. Previously, it
only showed whether a parameter was optional or not.
  • Loading branch information
lars-reimann authored Nov 1, 2024
1 parent 5913a54 commit ff8fa13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
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

0 comments on commit ff8fa13

Please sign in to comment.