-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from sveinungf/dev/src-gen-cellstyle
Source generator - Attribute for named style
- Loading branch information
Showing
95 changed files
with
2,703 additions
and
966 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...h.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ClassWithEmptyCellStyle.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
Diagnostics: [ | ||
{ | ||
Id: SPCH1006, | ||
Title: Invalid attribute argument, | ||
Severity: Error, | ||
WarningLevel: 0, | ||
Location: : (6,5)-(6,18), | ||
MessageFormat: '{0}' is an invalid argument for attribute '{1}', | ||
Message: '' is an invalid argument for attribute 'SpreadCheetah.SourceGeneration.CellStyleAttribute', | ||
Category: SpreadCheetah.SourceGenerator | ||
} | ||
] | ||
} |
134 changes: 134 additions & 0 deletions
134
...apshots/G.CellStyle_ClassWithMultipleAttributes#MyNamespace.MyGenRowContext.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//HintName: MyNamespace.MyGenRowContext.g.cs | ||
// <auto-generated /> | ||
#nullable enable | ||
using SpreadCheetah; | ||
using SpreadCheetah.SourceGeneration; | ||
using SpreadCheetah.Styling; | ||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MyNamespace | ||
{ | ||
public partial class MyGenRowContext | ||
{ | ||
private static MyGenRowContext? _default; | ||
public static MyGenRowContext Default => _default ??= new MyGenRowContext(); | ||
|
||
public MyGenRowContext() | ||
{ | ||
} | ||
|
||
private WorksheetRowTypeInfo<MyNamespace.ClassWithCellStyle>? _ClassWithCellStyle; | ||
public WorksheetRowTypeInfo<MyNamespace.ClassWithCellStyle> ClassWithCellStyle => _ClassWithCellStyle | ||
??= WorksheetRowMetadataServices.CreateObjectInfo<MyNamespace.ClassWithCellStyle>( | ||
AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null, CreateWorksheetRowDependencyInfo0); | ||
|
||
private static WorksheetRowDependencyInfo CreateWorksheetRowDependencyInfo0(Spreadsheet spreadsheet) | ||
{ | ||
var styleIds = new[] | ||
{ | ||
spreadsheet.GetStyleId("Italic"), | ||
spreadsheet.GetStyleId("Bold"), | ||
}; | ||
return new WorksheetRowDependencyInfo(styleIds); | ||
} | ||
|
||
private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) | ||
{ | ||
var cells = ArrayPool<StyledCell>.Shared.Rent(4); | ||
try | ||
{ | ||
cells[0] = new StyledCell("FirstName", styleId); | ||
cells[1] = new StyledCell("LastName", styleId); | ||
cells[2] = new StyledCell("YearOfBirth", styleId); | ||
cells[3] = new StyledCell("Initials", styleId); | ||
await spreadsheet.AddRowAsync(cells.AsMemory(0, 4), token).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
ArrayPool<StyledCell>.Shared.Return(cells, true); | ||
} | ||
} | ||
|
||
private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellStyle? obj, CancellationToken token) | ||
{ | ||
if (spreadsheet is null) | ||
throw new ArgumentNullException(nameof(spreadsheet)); | ||
if (obj is null) | ||
return spreadsheet.AddRowAsync(ReadOnlyMemory<StyledCell>.Empty, token); | ||
return AddAsRowInternalAsync(spreadsheet, obj, token); | ||
} | ||
|
||
private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, | ||
IEnumerable<MyNamespace.ClassWithCellStyle?> objs, | ||
CancellationToken token) | ||
{ | ||
if (spreadsheet is null) | ||
throw new ArgumentNullException(nameof(spreadsheet)); | ||
if (objs is null) | ||
throw new ArgumentNullException(nameof(objs)); | ||
return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); | ||
} | ||
|
||
private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, | ||
MyNamespace.ClassWithCellStyle obj, | ||
CancellationToken token) | ||
{ | ||
var cells = ArrayPool<StyledCell>.Shared.Rent(4); | ||
try | ||
{ | ||
var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.ClassWithCellStyle); | ||
var styleIds = worksheetRowDependencyInfo.StyleIds; | ||
await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
ArrayPool<StyledCell>.Shared.Return(cells, true); | ||
} | ||
} | ||
|
||
private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, | ||
IEnumerable<MyNamespace.ClassWithCellStyle?> objs, | ||
CancellationToken token) | ||
{ | ||
var cells = ArrayPool<StyledCell>.Shared.Rent(4); | ||
try | ||
{ | ||
var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.ClassWithCellStyle); | ||
var styleIds = worksheetRowDependencyInfo.StyleIds; | ||
foreach (var obj in objs) | ||
{ | ||
await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); | ||
} | ||
} | ||
finally | ||
{ | ||
ArrayPool<StyledCell>.Shared.Return(cells, true); | ||
} | ||
} | ||
|
||
private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, | ||
MyNamespace.ClassWithCellStyle? obj, | ||
StyledCell[] cells, IReadOnlyList<StyleId> styleIds, CancellationToken token) | ||
{ | ||
if (obj is null) | ||
return spreadsheet.AddRowAsync(ReadOnlyMemory<StyledCell>.Empty, token); | ||
|
||
cells[0] = new StyledCell(new DataCell(obj.FirstName), styleIds[0]); | ||
cells[1] = new StyledCell(new DataCell(obj.LastName), styleIds[1]); | ||
cells[2] = new StyledCell(new DataCell(obj.YearOfBirth), null); | ||
cells[3] = new StyledCell(new DataCell(obj.Initials), styleIds[1]); | ||
return spreadsheet.AddRowAsync(cells.AsMemory(0, 4), token); | ||
} | ||
|
||
private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) | ||
{ | ||
return value is null || value.Length <= truncateLength | ||
? new DataCell(value) | ||
: new DataCell(value.AsMemory(0, truncateLength)); | ||
} | ||
} | ||
} |
Oops, something went wrong.