Skip to content

Commit

Permalink
Use record instead of class for LSP types. (#258)
Browse files Browse the repository at this point in the history
Fixes #253
  • Loading branch information
karthiknadig committed Jul 20, 2023
1 parent a484777 commit 0512a11
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions generator/plugins/dotnet/custom/OrType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

public class OrType<T, U> : IOrType
public record OrType<T, U> : IOrType
{
public object? Value { get; }
public OrType(T t)
Expand Down Expand Up @@ -32,7 +32,7 @@ public override string ToString()
}
}

public class OrType<T, U, V> : IOrType
public record OrType<T, U, V> : IOrType
{
public object? Value { get; }

Expand Down Expand Up @@ -79,7 +79,7 @@ public override string ToString()
}


public class OrType<T, U, V, W> : IOrType
public record OrType<T, U, V, W> : IOrType
{
public object? Value { get; }

Expand Down
6 changes: 2 additions & 4 deletions generator/plugins/dotnet/dotnet_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,10 @@ def generate_property(

if prop_def.type.kind == "stringLiteral":
lines.append(
f'public {type_name}{optional} {name} {{ get; set; }} = "{prop_def.type.value}";'
f'public {type_name}{optional} {name} {{ get; init; }} = "{prop_def.type.value}";'
)
elif prop_def.type.kind == "base" and prop_def.type.name == "null":
lines.append(f"public {type_name}{optional} {name} {{ get; set; }} = null;")
else:
lines.append(f"public {type_name}{optional} {name} {{ get; set; }}")
lines.append(f"public {type_name}{optional} {name} {{ get; init; }}")

usings.append("DataMember")
if converter:
Expand Down
6 changes: 5 additions & 1 deletion generator/plugins/dotnet/dotnet_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,23 @@ def class_wrapper(
inner: List[str],
derived: Optional[str] = None,
class_attributes: Optional[List[str]] = None,
is_record=True,
) -> List[str]:
if hasattr(type_def, "name"):
name = get_special_case_class_name(type_def.name)
else:
raise ValueError(f"Unknown type: {type_def}")

rec_or_cls = "record" if is_record else "class"
lines = (
get_doc(type_def.documentation)
+ generate_extras(type_def)
+ (class_attributes if class_attributes else [])
+ [
"[DataContract]",
f"public class {name}: {derived}" if derived else f"public class {name}",
f"public {rec_or_cls} {name}: {derived}"
if derived
else f"public {rec_or_cls} {name}",
"{",
]
)
Expand Down
3 changes: 3 additions & 0 deletions generator/plugins/dotnet/dotnet_special_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def generate_special_class(
["public LSPObject(Dictionary<string, object?> value):base(value){}"],
"Dictionary<string, object?>",
["[JsonConverter(typeof(CustomObjectConverter<LSPObject>))]"],
is_record=False,
),
)
if type_def.name == "InitializedParams":
Expand All @@ -58,6 +59,7 @@ def generate_special_class(
],
"Dictionary<string, object?>",
["[JsonConverter(typeof(CustomObjectConverter<InitializedParams>))]"],
is_record=False,
),
)
if type_def.name == "LSPAny":
Expand All @@ -82,6 +84,7 @@ def generate_special_class(
type_def,
["public LSPArray(List<object> value):base(value){}"],
"List<object>",
is_record=False,
),
)

Expand Down

0 comments on commit 0512a11

Please sign in to comment.