Skip to content

Commit

Permalink
feat: fine-grained control over inlay hints for parameter names
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Apr 9, 2024
1 parent 19015c3 commit 291fd8b
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import {
isSdsArgument,
isSdsBlockLambdaResult,
isSdsLambda,
isSdsLiteral,
isSdsParameter,
isSdsPlaceholder,
isSdsReference,
isSdsYield,
SdsArgument,
SdsBlockLambdaResult,
SdsPlaceholder,
SdsYield,
} from '../generated/ast.js';
import { Argument } from '../helpers/nodeProperties.js';
import { SafeDsNodeMapper } from '../helpers/safe-ds-node-mapper.js';
Expand Down Expand Up @@ -43,47 +41,60 @@ export class SafeDsInlayHintProvider extends AbstractInlayHintProvider {
return;
}

if (this.settingsProvider.shouldShowAssigneeTypeInlayHints()) {
this.computeAssigneeTypeInlayHint(node, cstNode, acceptor);
}

if (this.settingsProvider.shouldShowLambdaParameterTypeInlayHints()) {
this.computeLambdaParameterTypeInlayHint(node, cstNode, acceptor);
}

if (this.settingsProvider.shouldShowParameterNameInlayHints()) {
this.computeParameterNameInlayHint(node, cstNode, acceptor);
}
this.computeAssigneeTypeInlayHint(node, cstNode, acceptor);
this.computeLambdaParameterTypeInlayHint(node, cstNode, acceptor);
this.computeParameterNameInlayHint(node, cstNode, acceptor);
}

private computeAssigneeTypeInlayHint(
node: AstNode | SdsBlockLambdaResult | SdsPlaceholder | SdsYield,
cstNode: CstNode,
acceptor: InlayHintAcceptor,
) {
if (isSdsBlockLambdaResult(node) || isSdsPlaceholder(node) || isSdsYield(node)) {
private computeAssigneeTypeInlayHint(node: AstNode, cstNode: CstNode, acceptor: InlayHintAcceptor) {
if (
this.settingsProvider.shouldShowAssigneeTypeInlayHints() &&
(isSdsBlockLambdaResult(node) || isSdsPlaceholder(node) || isSdsYield(node))
) {
this.computeTypeInlayHint(node, cstNode, acceptor);
}
}

private computeLambdaParameterTypeInlayHint(node: AstNode, cstNode: CstNode, acceptor: InlayHintAcceptor) {
if (isSdsParameter(node) && AstUtils.hasContainerOfType(node, isSdsLambda) && !node.type) {
if (
this.settingsProvider.shouldShowLambdaParameterTypeInlayHints() &&
isSdsParameter(node) &&
AstUtils.hasContainerOfType(node, isSdsLambda) &&
!node.type
) {
this.computeTypeInlayHint(node, cstNode, acceptor);
}
}

private computeParameterNameInlayHint(node: AstNode | SdsArgument, cstNode: CstNode, acceptor: InlayHintAcceptor) {
if (isSdsArgument(node) && Argument.isPositional(node)) {
const parameter = this.nodeMapper.argumentToParameter(node);
if (parameter) {
acceptor({
position: cstNode.range.start,
label: `${parameter.name} = `,
kind: InlayHintKind.Parameter,
tooltip: createMarkupContent(this.documentationProvider.getDocumentation(parameter)),
});
}
private computeParameterNameInlayHint(node: AstNode, cstNode: CstNode, acceptor: InlayHintAcceptor) {
if (!isSdsArgument(node) || Argument.isNamed(node)) {
return;
}

const shouldShowParameterNameInlayHints = this.settingsProvider.shouldShowParameterNameInlayHints();
if (shouldShowParameterNameInlayHints === 'none') {
return;
}

const parameter = this.nodeMapper.argumentToParameter(node);
if (!parameter) {
return;
}

const value = node.value;
if (
(shouldShowParameterNameInlayHints === 'onlyLiterals' && !isSdsLiteral(value)) ||
(shouldShowParameterNameInlayHints === 'exceptReferences' && isSdsReference(value))
) {
return;
}

acceptor({
position: cstNode.range.start,
label: `${parameter.name} = `,
kind: InlayHintKind.Parameter,
tooltip: createMarkupContent(this.documentationProvider.getDocumentation(parameter)),
});
}

private computeTypeInlayHint(node: AstNode, cstNode: CstNode, acceptor: InlayHintAcceptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class SafeDsSettingsProvider {
return this.cachedSettings.inlayHints?.lambdaParameterTypes?.enabled ?? true;
}

shouldShowParameterNameInlayHints(): boolean {
return this.cachedSettings.inlayHints?.parameterNames?.enabled ?? true;
shouldShowParameterNameInlayHints(): InlayHintsSettings['parameterNames']['enabled'] {
return this.cachedSettings.inlayHints?.parameterNames?.enabled ?? 'onlyLiterals';
}

/* c8 ignore start */
Expand Down Expand Up @@ -98,7 +98,7 @@ interface InlayHintsSettings {
enabled: boolean;
};
parameterNames: {
enabled: boolean;
enabled: 'none' | 'onlyLiterals' | 'exceptReferences' | 'all';
};
}

Expand Down
Loading

0 comments on commit 291fd8b

Please sign in to comment.