ballerina-lang
ballerina-lang copied to clipboard
[Improvement]: Preserve user defined order of fields when filling mapping constructor for required fields
Description
Consider the following type def:
type LogRecord record {|
string direction;
string x\-request\-id;
string http\-method;
string resource\-path;
string...;
|};
When you type {<cursor>} and use the Fill Required Fields option it generates the following:
LogRecord rec = {x\-request\-id: "",
http\-method: "",
resource\-path: "",
direction: ""};
Note how the user defined order of fields is not preserved in this generated set of fields within the mapping constructor. It would be better if it preserved the user-defined order.
Describe your problem(s)
No response
Describe your solution(s)
No response
Related area
-> Editor
Related issue(s) (optional)
No response
Suggested label(s) (optional)
No response
Suggested assignee(s) (optional)
No response
IMO, the priority of this issue should be bumped up, as the existing implementation negatively affects the UX in some scenarios, making the code look unseemly. For instance, consider the following examples generated by the CA:
Case 1: Coordinate information
In such scenarios, the content is more readable if we preserve the order, as we are accustomed to reading the line first, and then the offset, similar to how we read the x-coordinate followed by the y-coordinate.
type LineRange record {|
int line;
int offset;
|};
function fn1() {
LineRange loc = {offset: 0, line: 0};
}
Case 2: Identification information
It's a common practice to have the id as the first field, and since the user has provided this information, the CA should preserve it.
type Employee record {|
string id;
string name;
string address;
|};
function fn2() {
Employee employee = {address: "", name: "", id: ""};
}