Skip to content

Commit 0933a49

Browse files
author
nicosammito
committed
feat: improve function suggestion logic and enhance related tests for better type compatibility
1 parent 08e928d commit 0933a49

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

src/suggestion/getNodeSuggestions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const getNodeSuggestions = (
1515

1616
if (type && functions) {
1717
function getGenericsCount(input: string): number {
18-
const match = input.match(/<([^>]+)>/);
18+
const match = input.trim().match(/^<([^>]+)>/);
1919
if (!match) return 0;
2020
return match[1].split(',').map(s => s.trim()).filter(Boolean).length;
2121
}
@@ -30,7 +30,8 @@ export const getNodeSuggestions = (
3030
`;
3131
}).join("\n")}
3232
${functions?.map((_, i) => `const check${i}: TargetType = {} as F${i};`).join("\n")}
33-
`;
33+
`;
34+
3435

3536
const fileName = "index.ts";
3637
const host = createCompilerHost(fileName, sourceCode);
@@ -54,7 +55,7 @@ export const getNodeSuggestions = (
5455
}
5556

5657

57-
return functionToSuggest?.map(f=> {
58+
return functionToSuggest?.map(f => {
5859
return {
5960
__typename: "NodeFunction",
6061
id: `gid://sagittarius/NodeFunction/1`,

test/nodeSuggestion.test.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ describe("getNodeSuggestions", () => {
1212

1313
expect(map).toEqual(expect.arrayContaining([
1414
'std::list::first',
15-
'std::list::max',
1615
'std::list::at',
1716
'std::list::last',
18-
'std::list::sum',
1917
'std::list::pop',
2018
'std::list::find_last',
21-
'std::list::min',
2219
'std::list::join',
2320
'std::list::find',
2421
'std::number::as_text',
@@ -30,24 +27,59 @@ describe("getNodeSuggestions", () => {
3027
'std::text::reverse',
3128
'std::text::replace_last',
3229
'std::text::lowercase',
33-
'std::text::to_ascii',
3430
'std::text::encode',
3531
'std::text::remove',
3632
'std::text::swapcase',
3733
'std::text::append',
3834
'std::text::insert',
3935
'std::text::prepend',
40-
'std::text::as_bytes',
4136
'std::text::hex',
4237
'std::text::replace',
4338
'std::text::from_ascii',
4439
'std::text::octal',
45-
'std::text::chars',
4640
'std::text::capitalize',
47-
'std::text::split',
4841
'std::text::replace_first',
4942
'std::text::uppercase',
5043
'std::text::at',
44+
'std::control::return',
45+
'std::control::value'
46+
]))
47+
48+
});
49+
50+
it("should suggest functions with compatible return types and prioritize exact matches for boolean", () => {
51+
52+
// We are looking for suggestions for a 'number' type
53+
const suggestions = getNodeSuggestions("BOOLEAN", FUNCTION_SIGNATURES, DATA_TYPES);
54+
55+
const map = suggestions.map(s => s.functionDefinition?.identifier)
56+
57+
expect(map).toEqual(expect.arrayContaining([
58+
'std::list::first',
59+
'std::list::at',
60+
'std::list::last',
61+
'std::list::pop',
62+
'std::list::find_last',
63+
'std::list::is_empty',
64+
'std::list::find',
65+
'std::number::is_zero',
66+
'std::number::has_digits',
67+
'std::number::is_greater',
68+
'std::number::is_equal',
69+
'std::number::is_positive',
70+
'std::number::is_less',
71+
'std::object::contains_key',
72+
'std::object::get',
73+
'std::object::set',
74+
'std::boolean::from_number',
75+
'std::boolean::is_equal',
76+
'std::boolean::from_text',
77+
'std::boolean::negate',
78+
'std::text::is_equal',
79+
'std::text::ends_with',
80+
'std::text::start_with',
81+
'std::text::contains',
82+
'std::control::return',
5183
'std::control::value'
5284
]))
5385

0 commit comments

Comments
 (0)