-
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.
feat: Add first signature help derivation
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {ParseTree, TerminalNode} from "antlr4ts/tree"; | ||
import {Position} from "vscode-languageserver"; | ||
import {TokenStream} from "antlr4ts/TokenStream"; | ||
import {Token} from "antlr4ts"; | ||
|
||
export const getPositionOfTokenBefore = (tokens: TokenStream, position: Position): Position | undefined => { | ||
let lastToken: Token|undefined = undefined | ||
for (let i = 0; i < tokens.size; i++) { | ||
const token = tokens.get(i) // lookup seems to be O(1) | ||
if (token.line >= position.line + 1 && token.charPositionInLine >= position.character) { | ||
break | ||
} | ||
|
||
lastToken = token; | ||
} | ||
|
||
return lastToken ? Position.create(lastToken.line-1, lastToken.charPositionInLine) : undefined | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
ParameterInformation, | ||
Position, | ||
SignatureHelp, | ||
SignatureInformation, | ||
TextDocumentIdentifier | ||
} from "vscode-languageserver"; | ||
import {DocumentManagerInterface} from "./document_manager"; | ||
import {getDefinitionSymbolFromPosition} from "./go_to_definition"; | ||
import {getPositionOfTokenBefore} from "./parseTree/get_terminal_before_position"; | ||
|
||
export const getSignatureHelp = async (identifier: TextDocumentIdentifier, position: Position, documentManager: DocumentManagerInterface): Promise<SignatureHelp | undefined> => { | ||
const parseResult = await documentManager.getParseResult(identifier); | ||
if (!parseResult) { | ||
return undefined; | ||
} | ||
|
||
const positionOfTokenBefore = getPositionOfTokenBefore(parseResult.parser.inputStream, position); | ||
if (!positionOfTokenBefore) { | ||
return undefined; | ||
} | ||
|
||
const definitionSymbol = await getDefinitionSymbolFromPosition(identifier, positionOfTokenBefore, documentManager); | ||
if (!definitionSymbol) { | ||
return undefined; | ||
} | ||
|
||
const parameters: ParameterInformation[] = (definitionSymbol.symbol.parameters ?? []).map(parameter => ({ label: parameter?.text ?? "" })) | ||
const signature: SignatureInformation = { label: definitionSymbol.symbol.node.text, parameters, activeParameter: 0 } | ||
return { signatures: [signature], activeSignature: 0, activeParameter: 0 } | ||
} |
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,36 @@ | ||
import {assert, expect} from "chai"; | ||
|
||
import {Location, Position, Range} from "vscode-languageserver"; | ||
import {MockDocumentManager} from "./mocks/mock_document_manager"; | ||
import {getReferences} from "../src/references"; | ||
import {getSignatureHelp} from "../src/signature_help"; | ||
|
||
describe('references', function () { | ||
const assertSignatureDefinitionFound = async (code: string, signatureInvoked: Position, parameters: string[], activeParameter: number) => { | ||
const uri = 'main.pv'; | ||
|
||
const documentManager = new MockDocumentManager(); | ||
documentManager.parse(uri, code); | ||
const signatureHelp = await getSignatureHelp({uri}, signatureInvoked, documentManager); | ||
|
||
assert.isDefined(signatureHelp); | ||
if (!signatureHelp) { | ||
return; | ||
} | ||
|
||
expect(signatureHelp.signatures.length).to.equal(1) | ||
const signature = signatureHelp.signatures[0] | ||
|
||
expect(signature.parameters.length).to.equal(parameters.length); | ||
expect(signature.parameters.map(parameter => parameter.label)).deep.equal(parameters); | ||
|
||
expect(signature.activeParameter).to.equal(activeParameter); | ||
}; | ||
|
||
it("finds table signature definition", async () => { | ||
const code = `table Ids(nat).\nprocess \nget Ids(`; | ||
const signatureInvoked = {line: 2, character: 8}; | ||
|
||
await assertSignatureDefinitionFound(code, signatureInvoked, ['nat'], 0); | ||
}); | ||
}); |