Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(signature-help): for struct init inside function call #259

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
56 changes: 22 additions & 34 deletions server/src/e2e/suite/signatureHelp.test.ts
Original file line number Diff line number Diff line change
@@ -5,53 +5,41 @@ import type {TestCase} from "./TestParser"

suite("Signatures Test Suite", () => {
const testSuite = new (class extends BaseTestSuite {
public async getSignature(input: string): Promise<vscode.SignatureHelp> {
await this.editor.edit(builder => {
const fullRange = new vscode.Range(
this.document.lineAt(0).range.start,
this.document.lineAt(this.document.lineCount - 1).range.end,
)
builder.delete(fullRange)
builder.insert(this.document.positionAt(0), input)
})

const editorText = this.document.getText()
const caretIndex = editorText.indexOf("<caret>")
public async getSignatureHelp(input: string): Promise<vscode.SignatureHelp | undefined> {
const textWithoutCaret = input.replace("<caret>", "")
await this.replaceDocumentText(textWithoutCaret)

const caretIndex = input.indexOf("<caret>")
if (caretIndex === -1) {
throw new Error("No <caret> marker found in input")
}

const caretPosition = this.document.positionAt(caretIndex)

await this.editor.edit(builder => {
const caretRange = this.document.getWordRangeAtPosition(caretPosition, /<caret>/)
if (caretRange) {
builder.delete(caretRange)
}
})

this.editor.selection = new vscode.Selection(caretPosition, caretPosition)
this.editor.revealRange(new vscode.Range(caretPosition, caretPosition))
const position = this.document.positionAt(caretIndex)
this.editor.selection = new vscode.Selection(position, position)
this.editor.revealRange(new vscode.Range(position, position))

return vscode.commands.executeCommand<vscode.SignatureHelp>(
"vscode.executeSignatureHelpProvider",
this.document.uri,
caretPosition,
position,
)
}

protected runTest(testFile: string, testCase: TestCase): void {
test(`Signature: ${testCase.name}`, async () => {
const signature = await this.getSignature(testCase.input)
const items = signature.signatures.map(item => {
const label = item.label
if (item.activeParameter !== undefined) {
const activeParamLabel = item.parameters[item.activeParameter]?.label ?? ""
return `${activeParamLabel.toString()}\n${label}`
}
return ""
})
const signature = await this.getSignatureHelp(testCase.input)
const items =
signature === undefined
? ["no signature help"]
: signature.signatures.map(item => {
const label = item.label
if (item.activeParameter !== undefined) {
const activeParamLabel =
item.parameters[item.activeParameter]?.label ?? ""
return `${activeParamLabel.toString()}\n${label}`
}
return ""
})
const expected = testCase.expected.split("\n").filter((line: string) => line !== "")
if (BaseTestSuite.UPDATE_SNAPSHOTS) {
this.updates.push({
@@ -75,5 +63,5 @@ suite("Signatures Test Suite", () => {
teardown(async () => testSuite.teardown())
suiteTeardown(() => testSuite.suiteTeardown())

testSuite.runTestsFromDirectory("signatures")
testSuite.runTestsFromDirectory("signatureHelp")
})
225 changes: 225 additions & 0 deletions server/src/e2e/suite/testcases/signatureHelp/struct.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
========================================================================
Struct Key
========================================================================
primitive Int;
primitive String;

struct Person {
age: Int;
name: String;
}

contract Foo {
init() {
const userA = Person{age:<caret>,}
}
}
------------------------------------------------------------------------
age: Int
Person{ age: Int, name: String }

========================================================================
Struct Key (second value)
========================================================================
primitive Int;
primitive String;

struct Person {
age: Int;
name: String;
}

contract Foo {
init() {
const userA = Person{age:42, name: <caret>}
}
}
------------------------------------------------------------------------
name: String
Person{ age: Int, name: String }

========================================================================
Struct Multiline
========================================================================
primitive Int;
primitive String;

struct Person {
age: Int;
name: String;
}

contract Foo {
init() {
const userA = Person{
age:<caret>,
name: "A"
}
}
}
------------------------------------------------------------------------
age: Int
Person{ age: Int, name: String }

========================================================================
Struct init inside function call, on name
========================================================================
primitive Int;
primitive Bool;
primitive String;

struct Config {
name: String;
version: Int;
active: Bool;
}

contract Foo {
fun setup(config: Config) {}

init() {
self.setup(Con<caret>fig{name: "A", version: 2, active: 1 });
}
}
------------------------------------------------------------------------
config: Config
fun setup(config: Config)

========================================================================
Struct init inside function call, on open brace
========================================================================
primitive Int;
primitive Bool;
primitive String;

struct Config {
name: String;
version: Int;
active: Bool;
}

contract Foo {
fun setup(config: Config) {}

init() {
self.setup(Config<caret>{name: "A", version: 2, active: 1 });
}
}
------------------------------------------------------------------------
no signature help

========================================================================
Struct init inside function call, on field
========================================================================
primitive Int;
primitive Bool;
primitive String;

struct Config {
name: String;
version: Int;
active: Bool;
}

contract Foo {
fun setup(config: Config) {}

init() {
self.setup(Config{<caret>name: "A", version: 2, active: 1 });
}
}
------------------------------------------------------------------------
name: String
Config{ name: String, version: Int, active: Bool }

========================================================================
Struct init inside function call, on last field
========================================================================
primitive Int;
primitive Bool;
primitive String;

struct Config {
name: String;
version: Int;
active: Bool;
}

contract Foo {
fun setup(config: Config) {}

init() {
self.setup(Config{name: "A", version: 2, <caret>active: 1 });
}
}
------------------------------------------------------------------------
active: Bool
Config{ name: String, version: Int, active: Bool }

========================================================================
Nested struct init inside function call, on first field
========================================================================
primitive Int;
primitive Bool;
primitive String;

struct Config {
name: String;
version: Int;
data: Data;
}

struct Data {
active: Bool;
}

contract Foo {
fun setup(config: Config) {}

init() {
self.setup(Config{
name: "A",
version: 2,
data: Data{
<caret>active: 10,
},
});
}
}
------------------------------------------------------------------------
active: Bool
Data{ active: Bool }

========================================================================
Nested struct init inside function call, after last field
========================================================================
primitive Int;
primitive Bool;
primitive String;

struct Config {
name: String;
version: Int;
data: Data;
}

struct Data {
active: Bool;
}

contract Foo {
fun setup(config: Config) {}

init() {
self.setup(Config{
name: "A",
version: 2,
data: Data{
active: 10,
}<caret>,
});
}
}
------------------------------------------------------------------------
data: Data
Config{ name: String, version: Int, data: Data }
62 changes: 0 additions & 62 deletions server/src/e2e/suite/testcases/signatures/struct.test

This file was deleted.

Loading