Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use record instead of class for LSP types. #258

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading