Skip to content

Commit 0512a11

Browse files
authored
Use record instead of class for LSP types. (#258)
Fixes #253
1 parent a484777 commit 0512a11

4 files changed

Lines changed: 13 additions & 8 deletions

File tree

generator/plugins/dotnet/custom/OrType.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
public class OrType<T, U> : IOrType
3+
public record OrType<T, U> : IOrType
44
{
55
public object? Value { get; }
66
public OrType(T t)
@@ -32,7 +32,7 @@ public override string ToString()
3232
}
3333
}
3434

35-
public class OrType<T, U, V> : IOrType
35+
public record OrType<T, U, V> : IOrType
3636
{
3737
public object? Value { get; }
3838

@@ -79,7 +79,7 @@ public override string ToString()
7979
}
8080

8181

82-
public class OrType<T, U, V, W> : IOrType
82+
public record OrType<T, U, V, W> : IOrType
8383
{
8484
public object? Value { get; }
8585

generator/plugins/dotnet/dotnet_classes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,10 @@ def generate_property(
219219

220220
if prop_def.type.kind == "stringLiteral":
221221
lines.append(
222-
f'public {type_name}{optional} {name} {{ get; set; }} = "{prop_def.type.value}";'
222+
f'public {type_name}{optional} {name} {{ get; init; }} = "{prop_def.type.value}";'
223223
)
224-
elif prop_def.type.kind == "base" and prop_def.type.name == "null":
225-
lines.append(f"public {type_name}{optional} {name} {{ get; set; }} = null;")
226224
else:
227-
lines.append(f"public {type_name}{optional} {name} {{ get; set; }}")
225+
lines.append(f"public {type_name}{optional} {name} {{ get; init; }}")
228226

229227
usings.append("DataMember")
230228
if converter:

generator/plugins/dotnet/dotnet_helpers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,23 @@ def class_wrapper(
108108
inner: List[str],
109109
derived: Optional[str] = None,
110110
class_attributes: Optional[List[str]] = None,
111+
is_record=True,
111112
) -> List[str]:
112113
if hasattr(type_def, "name"):
113114
name = get_special_case_class_name(type_def.name)
114115
else:
115116
raise ValueError(f"Unknown type: {type_def}")
116117

118+
rec_or_cls = "record" if is_record else "class"
117119
lines = (
118120
get_doc(type_def.documentation)
119121
+ generate_extras(type_def)
120122
+ (class_attributes if class_attributes else [])
121123
+ [
122124
"[DataContract]",
123-
f"public class {name}: {derived}" if derived else f"public class {name}",
125+
f"public {rec_or_cls} {name}: {derived}"
126+
if derived
127+
else f"public {rec_or_cls} {name}",
124128
"{",
125129
]
126130
)

generator/plugins/dotnet/dotnet_special_classes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def generate_special_class(
4545
["public LSPObject(Dictionary<string, object?> value):base(value){}"],
4646
"Dictionary<string, object?>",
4747
["[JsonConverter(typeof(CustomObjectConverter<LSPObject>))]"],
48+
is_record=False,
4849
),
4950
)
5051
if type_def.name == "InitializedParams":
@@ -58,6 +59,7 @@ def generate_special_class(
5859
],
5960
"Dictionary<string, object?>",
6061
["[JsonConverter(typeof(CustomObjectConverter<InitializedParams>))]"],
62+
is_record=False,
6163
),
6264
)
6365
if type_def.name == "LSPAny":
@@ -82,6 +84,7 @@ def generate_special_class(
8284
type_def,
8385
["public LSPArray(List<object> value):base(value){}"],
8486
"List<object>",
87+
is_record=False,
8588
),
8689
)
8790

0 commit comments

Comments
 (0)