Skip to content

Commit 001949e

Browse files
committed
Fix cross-platform issue with line endings on Linux
1 parent 5642e56 commit 001949e

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/StructId.Analyzer/EntityFrameworkGenerator.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ protected override string SelectTemplate(StructIdModel model)
9191
if (builtInEFTypes.Contains(model.ValueTypeFullName))
9292
return ThisAssembly.Resources.Templates.EntityFramework.Text;
9393

94-
if (model.ValueTypeAllInterfaces.Contains("System.IParsable<T>") &&
95-
model.ValueTypeAllInterfaces.Contains("System.IFormattable"))
94+
if (UsesStringProvider(model))
9695
return ThisAssembly.Resources.Templates.EntityFrameworkParsable.Text;
9796

9897
return ThisAssembly.Resources.Templates.EntityFramework.Text;
@@ -108,8 +107,7 @@ void GenerateValueSelector(SourceProductionContext context, ((ImmutableArray<Str
108107
var model = new SelectorModel(
109108
structIds.Select(x => new EFStructIdModel(x.TypeFullName,
110109
!builtInEFTypes.Contains(x.ValueTypeFullName)
111-
? x.ValueTypeAllInterfaces.Contains("System.IParsable<T>") &&
112-
x.ValueTypeAllInterfaces.Contains("System.IFormattable")
110+
? UsesStringProvider(x)
113111
? "string" : x.ValueTypeFullName
114112
: x.ValueTypeFullName)),
115113
customConverters.Select(x => new ConverterModel(x.TModel, x.TProvider, x.TConverter)),
@@ -127,6 +125,19 @@ record EFStructIdModel(string TSelf, string TValueType)
127125
public string TValue => builtInTypesMap.TryGetValue(TValueType, out var value) ? value : TValueType;
128126
}
129127

128+
static bool UsesStringProvider(StructIdModel model) =>
129+
HasInterface(model, "System.IParsable") &&
130+
HasInterface(model, "System.IFormattable");
131+
132+
static bool HasInterface(StructIdModel model, string interfaceType) =>
133+
model.ValueTypeAllInterfaces.AsImmutableArray().Any(i => StripGenericArgs(i) == interfaceType);
134+
135+
static string StripGenericArgs(string name)
136+
{
137+
var idx = name.IndexOf('<');
138+
return idx >= 0 ? name.Substring(0, idx) : name;
139+
}
140+
130141
record ConverterModel(string TModel, string TProvider, string TConverter);
131142

132143
record TemplatizedModel(string TModel, string TConverter, string Code);

src/StructId.CodeFix/TemplateCodeFix.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,20 @@ protected override Task<Document> GetChangedDocumentAsync(CancellationToken canc
4949
var modifiers = declaration.Modifiers;
5050

5151
if (!modifiers.Any(SyntaxKind.FileKeyword))
52-
modifiers = modifiers.Insert(0, Token(SyntaxKind.FileKeyword));
52+
{
53+
var file = SpacedToken(SyntaxKind.FileKeyword);
54+
if (modifiers.Count > 0)
55+
{
56+
var firstModifier = modifiers[0];
57+
file = file.WithLeadingTrivia(firstModifier.LeadingTrivia);
58+
modifiers = modifiers.Replace(firstModifier, firstModifier.WithLeadingTrivia(TriviaList()));
59+
}
60+
61+
modifiers = modifiers.Insert(0, file);
62+
}
5363

5464
if (!modifiers.Any(SyntaxKind.PartialKeyword))
55-
modifiers = modifiers.Insert(1, Token(SyntaxKind.PartialKeyword));
65+
modifiers = modifiers.Insert(1, SpacedToken(SyntaxKind.PartialKeyword));
5666

5767
// Remove accessibility modifiers which are replaced by 'file' visibility
5868
if (modifiers.FirstOrDefault(x => x.IsKind(SyntaxKind.PublicKeyword)) is { } @public)
@@ -63,16 +73,19 @@ protected override Task<Document> GetChangedDocumentAsync(CancellationToken canc
6373
modifiers = modifiers.Remove(@private);
6474

6575
if (declaration.Identifier.Text != "TSelf")
66-
declaration = declaration.WithIdentifier(Identifier("TSelf"));
76+
declaration = declaration.WithIdentifier(Identifier(
77+
declaration.Identifier.LeadingTrivia,
78+
"TSelf",
79+
declaration.Identifier.TrailingTrivia));
6780

6881
if (!declaration.IsKind(SyntaxKind.RecordStructDeclaration))
6982
{
7083
declaration = RecordDeclaration(
7184
SyntaxKind.RecordStructDeclaration,
7285
declaration.AttributeLists,
7386
modifiers,
74-
Token(SyntaxKind.RecordKeyword),
75-
Token(SyntaxKind.StructKeyword),
87+
SpacedToken(SyntaxKind.RecordKeyword),
88+
SpacedToken(SyntaxKind.StructKeyword),
7689
declaration.Identifier,
7790
declaration.TypeParameterList,
7891
declaration.ParameterList,
@@ -81,7 +94,8 @@ protected override Task<Document> GetChangedDocumentAsync(CancellationToken canc
8194
declaration.OpenBraceToken,
8295
declaration.Members,
8396
declaration.CloseBraceToken,
84-
declaration.SemicolonToken);
97+
declaration.SemicolonToken)
98+
.WithTriviaFrom(original);
8599
}
86100
else if (modifiers != declaration.Modifiers)
87101
{
@@ -90,5 +104,8 @@ protected override Task<Document> GetChangedDocumentAsync(CancellationToken canc
90104

91105
return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(original, declaration)));
92106
}
107+
108+
static SyntaxToken SpacedToken(SyntaxKind kind)
109+
=> Token(TriviaList(), kind, TriviaList(Space));
93110
}
94-
}
111+
}

0 commit comments

Comments
 (0)