Skip to content

Commit 2aa6780

Browse files
authored
Merge pull request #883 from messani/master
Add source position tracking for grid tables
2 parents c436465 + aab5543 commit 2aa6780

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

src/Markdig.Tests/TestSourcePosition.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,29 @@ public void TestPipeTable3()
834834
", "pipetables");
835835
}
836836

837+
[Test]
838+
public void TestGridTable()
839+
{
840+
Check("0\n\n+-+-+\n|A|B|\n+=+=+\n|C|D|\n+-+-+", @"
841+
paragraph ( 0, 0) 0-0
842+
literal ( 0, 0) 0-0
843+
table ( 2, 0) 3-31
844+
tablerow ( 3, 0) 9-13
845+
tablecell ( 3, 0) 9-11
846+
paragraph ( 3, 1) 10-10
847+
literal ( 3, 1) 10-10
848+
tablecell ( 3, 2) 11-13
849+
paragraph ( 3, 3) 12-12
850+
literal ( 3, 3) 12-12
851+
tablerow ( 5, 0) 21-25
852+
tablecell ( 5, 0) 21-23
853+
paragraph ( 5, 1) 22-22
854+
literal ( 5, 1) 22-22
855+
tablecell ( 5, 2) 23-25
856+
paragraph ( 5, 3) 24-24
857+
literal ( 5, 3) 24-24", "gridtables");
858+
}
859+
837860
[Test]
838861
public void TestIndentedCode()
839862
{

src/Markdig/Extensions/Tables/GridTableParser.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Copyright (c) Alexandre Mutel. All rights reserved.
2-
// This file is licensed under the BSD-Clause 2 license.
2+
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

55
using Markdig.Helpers;
66
using Markdig.Parsers;
77
using Markdig.Syntax;
8+
using System.Linq;
89

910
namespace Markdig.Extensions.Tables;
1011

@@ -60,7 +61,12 @@ public override BlockState TryOpen(BlockProcessor processor)
6061
}
6162
// Store the line (if we need later to build a ParagraphBlock because the GridTable was in fact invalid)
6263
tableState.AddLine(ref processor.Line);
63-
var table = new Table(this);
64+
var table = new Table(this)
65+
{
66+
Line = processor.LineIndex,
67+
Column = processor.Column,
68+
Span = { Start = lineStart }
69+
};
6470
table.SetData(typeof(GridTableState), tableState);
6571

6672
// Calculate the total width of all columns
@@ -94,10 +100,12 @@ public override BlockState TryContinue(BlockProcessor processor, Block block)
94100
tableState.AddLine(ref processor.Line);
95101
if (processor.CurrentChar == '+')
96102
{
103+
gridTable.UpdateSpanEnd(processor.Line.End);
97104
return HandleNewRow(processor, tableState, gridTable);
98105
}
99106
if (processor.CurrentChar == '|')
100107
{
108+
gridTable.UpdateSpanEnd(processor.Line.End);
101109
return HandleContents(processor, tableState, gridTable);
102110
}
103111
TerminateCurrentRow(processor, tableState, gridTable, true);
@@ -182,8 +190,18 @@ private static void TerminateCurrentRow(BlockProcessor processor, GridTableState
182190
var columnSlice = columns[i];
183191
if (columnSlice.CurrentCell != null)
184192
{
185-
currentRow ??= new TableRow();
186-
193+
if (currentRow == null)
194+
{
195+
TableCell firstCell = columns.First(c => c.CurrentCell != null).CurrentCell!;
196+
TableCell lastCell = columns.Last(c => c.CurrentCell != null).CurrentCell!;
197+
198+
currentRow ??= new TableRow()
199+
{
200+
Span = new SourceSpan(firstCell.Span.Start, lastCell.Span.End),
201+
Line = firstCell.Line
202+
};
203+
}
204+
187205
// If this cell does not already belong to a row
188206
if (columnSlice.CurrentCell.Parent is null)
189207
{
@@ -271,7 +289,10 @@ private BlockState HandleContents(BlockProcessor processor, GridTableState table
271289
columnSlice.CurrentCell = new TableCell(this)
272290
{
273291
ColumnSpan = columnSlice.CurrentColumnSpan,
274-
ColumnIndex = i
292+
ColumnIndex = i,
293+
Column = columnSlice.Start,
294+
Line = processor.LineIndex,
295+
Span = new SourceSpan(line.Start + columnSlice.Start, line.Start + columnSlice.End)
275296
};
276297

277298
columnSlice.BlockProcessor ??= processor.CreateChild();
@@ -281,7 +302,8 @@ private BlockState HandleContents(BlockProcessor processor, GridTableState table
281302
}
282303
// Process the content of the cell
283304
columnSlice.BlockProcessor!.LineIndex = processor.LineIndex;
284-
columnSlice.BlockProcessor.ProcessLine(sliceForCell);
305+
306+
columnSlice.BlockProcessor.ProcessLinePart(sliceForCell, sliceForCell.Start - line.Start);
285307
}
286308

287309
// Go to next column

src/Markdig/Parsers/BlockProcessor.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,17 +493,41 @@ public void ProcessLine(StringSlice newLine)
493493

494494
ContinueProcessingLine = true;
495495

496-
ResetLine(newLine);
496+
ResetLine(newLine, 0);
497497

498+
Process();
499+
500+
LineIndex++;
501+
}
502+
503+
/// <summary>
504+
/// Processes part of a line.
505+
/// </summary>
506+
/// <param name="line">The line.</param>
507+
/// <param name="column">The column.</param>
508+
public void ProcessLinePart(StringSlice line, int column)
509+
{
510+
CurrentLineStartPosition = line.Start - column;
511+
512+
ContinueProcessingLine = true;
513+
514+
ResetLine(line, column);
515+
516+
Process();
517+
}
518+
519+
/// <summary>
520+
/// Process current string slice.
521+
/// </summary>
522+
private void Process()
523+
{
498524
TryContinueBlocks();
499525

500526
// If the line was not entirely processed by pending blocks, try to process it with any new block
501527
TryOpenBlocks();
502528

503529
// Close blocks that are no longer opened
504530
CloseAll(false);
505-
506-
LineIndex++;
507531
}
508532

509533
internal bool IsOpen(Block block)
@@ -956,18 +980,17 @@ private void ProcessNewBlocks(BlockState result, bool allowClosing)
956980
ContinueProcessingLine = !result.IsDiscard();
957981
}
958982

959-
private void ResetLine(StringSlice newLine)
983+
private void ResetLine(StringSlice newLine, int column)
960984
{
961985
Line = newLine;
962-
Column = 0;
986+
Column = column;
963987
ColumnBeforeIndent = 0;
964988
StartBeforeIndent = Start;
965-
originalLineStart = newLine.Start;
989+
originalLineStart = newLine.Start - column;
966990
TriviaStart = newLine.Start;
967991
}
968992

969993

970-
971994
[MemberNotNull(nameof(Document), nameof(Parsers))]
972995
internal void Setup(MarkdownDocument document, BlockParserList parsers, MarkdownParserContext? context, bool trackTrivia)
973996
{

0 commit comments

Comments
 (0)