-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #24 ### Summary of Changes Implement a signature help provider.
- Loading branch information
1 parent
a9eb3bb
commit ed33676
Showing
30 changed files
with
466 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/safe-ds-lang/src/language/lsp/safe-ds-signature-help-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { | ||
type AstNode, | ||
type DocumentationProvider, | ||
findLeafNodeAtOffset, | ||
findNodesForKeyword, | ||
getContainerOfType, | ||
isNamed, | ||
type LangiumDocument, | ||
type MaybePromise, | ||
type SignatureHelpProvider, | ||
} from 'langium'; | ||
import type { | ||
CancellationToken, | ||
SignatureHelp, | ||
SignatureHelpOptions, | ||
SignatureHelpParams, | ||
} from 'vscode-languageserver'; | ||
import { createMarkupContent } from '../documentation/safe-ds-comment-provider.js'; | ||
import { isSdsAbstractCall, SdsCallable, SdsParameter } from '../generated/ast.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'; | ||
|
||
export class SafeDsSignatureHelpProvider implements SignatureHelpProvider { | ||
private readonly documentationProvider: DocumentationProvider; | ||
private readonly nodeMapper: SafeDsNodeMapper; | ||
private readonly typeComputer: SafeDsTypeComputer; | ||
|
||
constructor(services: SafeDsServices) { | ||
this.documentationProvider = services.documentation.DocumentationProvider; | ||
this.nodeMapper = services.helpers.NodeMapper; | ||
this.typeComputer = services.types.TypeComputer; | ||
} | ||
|
||
provideSignatureHelp( | ||
document: LangiumDocument, | ||
params: SignatureHelpParams, | ||
_cancelToken?: CancellationToken, | ||
): MaybePromise<SignatureHelp | undefined> { | ||
const rootCstNode = document.parseResult.value.$cstNode; | ||
/* c8 ignore start */ | ||
if (!rootCstNode) { | ||
return undefined; | ||
} | ||
/* c8 ignore stop */ | ||
|
||
const offset = document.textDocument.offsetAt(params.position); | ||
const sourceCstNode = findLeafNodeAtOffset(rootCstNode, offset); | ||
/* c8 ignore start */ | ||
if (!sourceCstNode) { | ||
return undefined; | ||
} | ||
/* c8 ignore stop */ | ||
|
||
return this.getSignature(sourceCstNode.astNode, offset); | ||
} | ||
|
||
/** | ||
* Returns the signature help for the given node at the given offset. | ||
*/ | ||
private getSignature(node: AstNode, offset: number): MaybePromise<SignatureHelp | undefined> { | ||
const containingAbstractCall = getContainerOfType(node, isSdsAbstractCall); | ||
if (!containingAbstractCall) { | ||
return undefined; | ||
} | ||
|
||
const callable = this.nodeMapper.callToCallable(containingAbstractCall); | ||
if (!callable) { | ||
return undefined; | ||
} | ||
|
||
const commas = findNodesForKeyword(containingAbstractCall.argumentList.$cstNode, ','); | ||
const activeParameter = commas.findLastIndex((comma) => comma.offset < offset) + 1; | ||
|
||
return { | ||
signatures: [ | ||
{ | ||
label: this.getLabel(callable), | ||
parameters: getParameters(callable).map(this.getParameterInformation), | ||
documentation: createMarkupContent(this.documentationProvider.getDocumentation(callable)), | ||
}, | ||
], | ||
activeSignature: 0, | ||
activeParameter, | ||
}; | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
|
||
private getParameterInformation = (parameter: SdsParameter) => { | ||
return { | ||
label: this.getParameterLabel(parameter), | ||
}; | ||
}; | ||
|
||
private getParameterLabel = (parameter: SdsParameter) => { | ||
const type = this.typeComputer.computeType(parameter); | ||
return `${parameter.name}: ${type}`; | ||
}; | ||
|
||
/* c8 ignore start */ | ||
get signatureHelpOptions(): SignatureHelpOptions { | ||
return { | ||
triggerCharacters: ['('], | ||
retriggerCharacters: [','], | ||
}; | ||
} | ||
|
||
/* c8 ignore stop */ | ||
} |
Oops, something went wrong.