Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add type to smartFilter #1262

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
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
26 changes: 21 additions & 5 deletions packages/ansible-language-server/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { TextDocument } from "vscode-languageserver-textdocument";
import * as path from "path";
import { readFileSync } from "fs";
import { WorkspaceManager } from "../src/services/workspaceManager";
import { createConnection, TextDocuments } from "vscode-languageserver/node";
import {
CompletionItem,
createConnection,
TextDocuments,
} from "vscode-languageserver/node";
import { ValidationManager } from "../src/services/validationManager";
import { ExtensionSettings } from "../src/interfaces/extensionSettings";

Expand Down Expand Up @@ -93,7 +97,10 @@ export function isWindows(): boolean {
* @param triggerCharacter - string against which fuzzy search is to be done
* @returns list after sorting and filtering
*/
export function smartFilter(completionList, triggerCharacter) {
export function smartFilter(
completionList: CompletionItem[],
triggerCharacter: string,
): CompletionItem[] {
if (!completionList) {
return [];
}
Expand All @@ -108,7 +115,12 @@ export function smartFilter(completionList, triggerCharacter) {
});

let filteredCompletionList = triggerCharacter
? searcher.search(triggerCharacter).slice(0, 5)
? searcher
.search(triggerCharacter)
.slice(0, 5)
.map((completion) => {
return completion.item;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssbarnea if it's a single item, we can just return it like

.map(completion => completion.item);

})
: completionList.slice(0, 5);

if (filteredCompletionList.length === 0) {
Expand All @@ -118,9 +130,13 @@ export function smartFilter(completionList, triggerCharacter) {
keys: ["label"],
threshold: 0.2,
});

filteredCompletionList = triggerCharacter
? newSearcher.search(triggerCharacter).slice(0, 5)
? newSearcher
.search(triggerCharacter)
.slice(0, 5)
.map((completion) => {
return completion.item;
})
: completionList.slice(0, 5);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,8 @@ function testPlayKeywords(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
} else {
expect(filteredCompletion[0].item.label).to.be.equal(completion);
expect(filteredCompletion[0].item.textEdit.newText).to.be.equal(
completion,
);
}
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
}
});
});
Expand Down Expand Up @@ -95,15 +88,8 @@ function testRoleKeywords(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
} else {
expect(filteredCompletion[0].item.label).to.be.equal(completion);
expect(filteredCompletion[0].item.textEdit.newText).to.be.equal(
completion,
);
}
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
}
});
});
Expand Down Expand Up @@ -140,15 +126,8 @@ function testBlockKeywords(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
} else {
expect(filteredCompletion[0].item.label).to.be.equal(completion);
expect(filteredCompletion[0].item.textEdit.newText).to.be.equal(
completion,
);
}
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
}
});
});
Expand Down Expand Up @@ -185,15 +164,8 @@ function testTaskKeywords(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
} else {
expect(filteredCompletion[0].item.label).to.be.equal(completion);
expect(filteredCompletion[0].item.textEdit.newText).to.be.equal(
completion,
);
}
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
}
});
});
Expand Down Expand Up @@ -266,15 +238,8 @@ function testModuleNames(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).to.contain(completion);
expect(filteredCompletion[0].textEdit.newText).to.contain(completion);
} else {
expect(filteredCompletion[0].item.label).to.contain(completion);
expect(filteredCompletion[0].item.textEdit.newText).to.contain(
completion,
);
}
expect(filteredCompletion[0].label).to.contain(completion);
expect(filteredCompletion[0].textEdit.newText).to.contain(completion);
}
});
});
Expand Down Expand Up @@ -335,15 +300,8 @@ function testModuleOptions(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
} else {
expect(filteredCompletion[0].item.label).to.be.equal(completion);
expect(filteredCompletion[0].item.textEdit.newText).be.equal(
completion,
);
}
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
}
});
});
Expand Down Expand Up @@ -394,11 +352,7 @@ function testModuleOptionsValues(
actualCompletion,
triggerCharacter,
).map((completion) => {
if (!completion.item) {
return completion.label;
} else {
return completion.item.label;
}
return completion.label;
});

if (!completion) {
Expand All @@ -411,11 +365,7 @@ function testModuleOptionsValues(
actualCompletion,
triggerCharacter,
).map((completion) => {
if (!completion.item) {
return completion.textEdit.newText;
} else {
return completion.item.textEdit.newText;
}
return completion.textEdit.newText;
});

if (!completion) {
Expand Down Expand Up @@ -479,15 +429,8 @@ function testModuleNamesWithoutFQCN(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
} else {
expect(filteredCompletion[0].item.label).to.be.equal(completion);
expect(filteredCompletion[0].item.textEdit.newText).to.be.equal(
completion,
);
}
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
}
});
});
Expand Down Expand Up @@ -530,15 +473,8 @@ function testPlaybookAdjacentCollection(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
} else {
expect(filteredCompletion[0].item.label).to.be.equal(completion);
expect(filteredCompletion[0].item.textEdit.newText).be.equal(
completion,
);
}
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
}
});
});
Expand Down Expand Up @@ -575,15 +511,8 @@ function testNonPlaybookAdjacentCollection(
if (!completion) {
expect(filteredCompletion.length).be.equal(0);
} else {
if (!filteredCompletion[0].item) {
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
} else {
expect(filteredCompletion[0].item.label).to.be.equal(completion);
expect(filteredCompletion[0].item.textEdit.newText).be.equal(
completion,
);
}
expect(filteredCompletion[0].label).be.equal(completion);
expect(filteredCompletion[0].textEdit.newText).be.equal(completion);
}
});
});
Expand Down Expand Up @@ -628,11 +557,7 @@ function testHostValues(
actualCompletion,
triggerCharacter,
).map((completion) => {
if (!completion.item) {
return completion.label;
} else {
return completion.item.label;
}
return completion.label;
});

if (!completion) {
Expand Down Expand Up @@ -689,11 +614,7 @@ function testVarsCompletionInsideJinja(
actualCompletion,
triggerCharacter,
).map((completion) => {
if (!completion.item) {
return completion.label;
} else {
return completion.item.label;
}
return completion.label;
});

if (!completion) {
Expand Down
Loading