Skip to content

Commit 4a4d437

Browse files
committed
fix(completion): fix completion for types of fields
Fixes #369
1 parent 800c7c5 commit 4a4d437

File tree

6 files changed

+9291
-7568
lines changed

6 files changed

+9291
-7568
lines changed

server/src/e2e/suite/testcases/completion/fields.test

+48
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,51 @@ trait Foo {
289289
2 name(): String
290290
14 call Use as function argument
291291
14 not Negate expression
292+
293+
========================================================================
294+
Contract field type completion
295+
========================================================================
296+
primitive Int;
297+
primitive Address;
298+
primitive String;
299+
300+
contract Foo {
301+
name: <caret>
302+
303+
fun name(): String {
304+
return self.name;
305+
}
306+
307+
fun bar() {
308+
return self.<caret>;
309+
}
310+
}
311+
------------------------------------------------------------------------
312+
13 map<K, V>
313+
9 Address
314+
9 Int
315+
9 String
316+
317+
========================================================================
318+
Trait field type completion
319+
========================================================================
320+
primitive Int;
321+
primitive Address;
322+
primitive String;
323+
324+
trait Foo {
325+
name: <caret>
326+
327+
fun name(): String {
328+
return self.name;
329+
}
330+
331+
fun bar() {
332+
return self.<caret>;
333+
}
334+
}
335+
------------------------------------------------------------------------
336+
13 map<K, V>
337+
9 Address
338+
9 Int
339+
9 String

tree-sitter-tact/grammar.js

+15-19
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,12 @@ module.exports = grammar({
399399

400400
message_value: ($) => seq("(", $._expression, ")"),
401401

402-
struct_body: ($) => seq("{", semicolonSepWithTrailing($.field), "}"),
402+
struct_body: ($) => seq("{", repeat($.field), "}"),
403403

404404
/* Fields (of messages, structs, contracts, traits) */
405405

406-
field: ($) => seq(field("name", $.identifier), $._field_after_id),
406+
field: ($) =>
407+
seq(field("name", $.identifier), $._field_after_id, optional(";")),
407408

408409
// Like _constant, but without a semicolon at the end
409410
storage_constant: ($) =>
@@ -420,12 +421,14 @@ module.exports = grammar({
420421
seq(field("name", $.identifier), $._field_after_id),
421422

422423
_field_after_id: ($) =>
423-
seq(
424-
":",
425-
field("type", $._type),
426-
field("_completion_anchor", optional($.identifier)),
427-
field("tlb", optional($.tlb_serialization)),
428-
optional(seq("=", field("value", $._expression))),
424+
prec.left(
425+
seq(
426+
":",
427+
field("type", $._type),
428+
field("_completion_anchor", optional($.identifier)),
429+
field("tlb", optional($.tlb_serialization)),
430+
optional(seq("=", field("value", $._expression))),
431+
),
429432
),
430433

431434
/* Contracts, Traits */
@@ -465,13 +468,13 @@ module.exports = grammar({
465468
"{",
466469
repeat(
467470
choice(
468-
seq($._body_item_without_semicolon, ";"),
471+
seq($.storage_constant, optional(";")),
472+
seq($.storage_variable, optional(";")),
469473
$.init_function,
470474
$._receiver_function,
471475
alias($._function_definition, $.storage_function),
472476
),
473477
),
474-
optional($._body_item_without_semicolon),
475478
"}",
476479
),
477480

@@ -480,22 +483,15 @@ module.exports = grammar({
480483
"{",
481484
repeat(
482485
choice(
483-
seq($._body_item_without_semicolon, ";"),
486+
seq($.storage_constant, optional(";")),
487+
seq($.storage_variable, optional(";")),
484488
$._receiver_function,
485489
alias($._function_definition, $.storage_function),
486490
),
487491
),
488-
optional($._body_item_without_semicolon),
489492
"}",
490493
),
491494

492-
_body_item_without_semicolon: ($) =>
493-
choice(
494-
$.storage_constant,
495-
$.storage_variable,
496-
alias($._function_declaration, $.storage_function),
497-
),
498-
499495
init_function: ($) =>
500496
seq(
501497
"init",

0 commit comments

Comments
 (0)