Skip to content
Open
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
149 changes: 141 additions & 8 deletions languageserver/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
TextDocumentItem,
Trace,
Tracer,
CodeLensRequest,
Position,
CompletionItem
} from "vscode-languageserver-protocol";

import { execSync, spawn } from "child_process";
Expand Down Expand Up @@ -342,7 +345,7 @@ describe("diagnostics", () => {
notification: notification,
});

await connection.sendNotification(
connection.sendNotification(
DidOpenTextDocumentNotification.type,
{
textDocument: TextDocumentItem.create(
Expand Down Expand Up @@ -798,7 +801,6 @@ describe("contracts", () => {
});

describe("codelenses", () => {
const codelensRequest = "textDocument/codeLens";

test("contract codelensss", async () => {
await withConnection(async (connection) => {
Expand All @@ -811,11 +813,11 @@ describe("codelenses", () => {
code.toString()
);

await connection.sendNotification(DidOpenTextDocumentNotification.type, {
connection.sendNotification(DidOpenTextDocumentNotification.type, {
textDocument: document,
});

let codelens = await connection.sendRequest(codelensRequest, {
let codelens = await connection.sendRequest(CodeLensRequest.type, {
textDocument: document,
});

Expand All @@ -838,11 +840,11 @@ describe("codelenses", () => {
code.toString()
);

await connection.sendNotification(DidOpenTextDocumentNotification.type, {
connection.sendNotification(DidOpenTextDocumentNotification.type, {
textDocument: document,
});

let codelens = await connection.sendRequest(codelensRequest, {
let codelens = await connection.sendRequest(CodeLensRequest.type, {
textDocument: document,
});

Expand All @@ -865,11 +867,11 @@ describe("codelenses", () => {
code.toString()
);

await connection.sendNotification(DidOpenTextDocumentNotification.type, {
connection.sendNotification(DidOpenTextDocumentNotification.type, {
textDocument: document,
});

let codelens = await connection.sendRequest(codelensRequest, {
let codelens = await connection.sendRequest(CodeLensRequest.type, {
textDocument: document,
});

Expand All @@ -881,3 +883,134 @@ describe("codelenses", () => {
}, true);
});
});

function sortCompletionItems(items: CompletionItem[]) {
items.sort((a, b) => {
let aLabel = a?.sortText ?? a.label;
let bLabel = b?.sortText ?? b.label;
return aLabel.localeCompare(bLabel);
});
}

describe("completion", () => {
async function testCode(code: string, position: Position, expectedCompletions: CompletionItem[]) {
return withConnection(async (connection) => {

const uri = await createTestDocument(connection, code);

const result= await connection.sendRequest("textDocument/completion", {
textDocument: { uri: uri },
position,
});

sortCompletionItems(result as CompletionItem[]);
sortCompletionItems(expectedCompletions);

expect(result).toEqual(expectedCompletions);
});
}

test("no char", async () =>
testCode(
`
access(all) struct S {
access(all) let foo: Int

access(all) init(foo: Int) {
self.foo = foo
}
}

access(all) fun main() {
let s = S(foo: 42)
s.
}
`,
{ line: 11, character: 14 },
[
{
label: 'foo',
kind: 5,
commitCharacters: [ '.' ],
data: { uri: 'file:///test.cdc', id: 'foo' }
},
{
label: 'isInstance',
kind: 3,
insertText: 'isInstance(${1:type})',
insertTextFormat: 2,
command: { title: '', command: 'editor.action.triggerParameterHints' },
data: { uri: 'file:///test.cdc', id: 'isInstance' }
},
{
label: 'getType',
kind: 3,
insertText: 'getType()',
insertTextFormat: 2,
command: { title: '', command: 'editor.action.triggerParameterHints' },
data: { uri: 'file:///test.cdc', id: 'getType' }
},
{
label: 'forEachAttachment',
kind: 3,
insertText: 'forEachAttachment(${1:f})',
insertTextFormat: 2,
command: { title: '', command: 'editor.action.triggerParameterHints' },
data: { uri: 'file:///test.cdc', id: 'forEachAttachment' }
}
]
)
)

test("one char", async () =>
testCode(
`
access(all) struct S {
access(all) let foo: Int

access(all) init(foo: Int) {
self.foo = foo
}
}

access(all) fun main() {
let s = S(foo: 42)
s.f
}
`,
{ line: 11, character: 15 },
[
{
label: 'foo',
kind: 5,
commitCharacters: [ '.' ],
data: { uri: 'file:///test.cdc', id: 'foo' }
},
{
label: 'isInstance',
kind: 3,
insertText: 'isInstance(${1:type})',
insertTextFormat: 2,
command: { title: '', command: 'editor.action.triggerParameterHints' },
data: { uri: 'file:///test.cdc', id: 'isInstance' }
},
{
label: 'getType',
kind: 3,
insertText: 'getType()',
insertTextFormat: 2,
command: { title: '', command: 'editor.action.triggerParameterHints' },
data: { uri: 'file:///test.cdc', id: 'getType' }
},
{
label: 'forEachAttachment',
kind: 3,
insertText: 'forEachAttachment(${1:f})',
insertTextFormat: 2,
command: { title: '', command: 'editor.action.triggerParameterHints' },
data: { uri: 'file:///test.cdc', id: 'forEachAttachment' }
}
]
)
)
})
Loading