1
- using System . Collections . Generic ;
2
- using System . Linq ;
1
+ using System . Linq ;
3
2
4
3
using MiKoSolutions . SemanticParsers . TypeScript . Yaml ;
5
4
@@ -9,69 +8,78 @@ public static class GapFiller
9
8
{
10
9
public static void Fill ( File file , CharacterPositionFinder finder )
11
10
{
12
- var children = file . Children ;
13
- for ( var index = 0 ; index < children . Count ; index ++ )
11
+ for ( var index = 0 ; index < file . Children . Count ; index ++ )
14
12
{
15
- AdjustNode ( children , index , finder ) ;
13
+ AdjustNode ( file , index , finder ) ;
16
14
}
17
15
}
18
16
19
- private static void AdjustNode ( IList < ContainerOrTerminalNode > parentChildren , int indexInParentChildren , CharacterPositionFinder finder )
17
+ private static void AdjustNode ( IParent parent , int indexInParentChildren , CharacterPositionFinder finder )
20
18
{
19
+ var parentChildren = parent . Children ;
20
+
21
21
var child = parentChildren [ indexInParentChildren ] ;
22
22
23
23
if ( parentChildren . Count == 1 )
24
24
{
25
- AdjustSingleChild ( child , finder ) ;
25
+ AdjustSingleChild ( parent , finder ) ;
26
26
}
27
27
else
28
28
{
29
29
// first child, so adjust end-position to next sibling
30
30
if ( indexInParentChildren == 0 && parentChildren . Count > 0 )
31
31
{
32
- AdjustFirstChild ( child , parentChildren , indexInParentChildren , finder ) ;
32
+ AdjustFirstChild ( parent , indexInParentChildren , finder ) ;
33
33
}
34
34
35
35
// child between first and last one, adjust gaps to previous sibling
36
36
if ( indexInParentChildren > 0 && indexInParentChildren < parentChildren . Count - 1 )
37
37
{
38
- AdjustMiddleChild ( child , parentChildren , indexInParentChildren , finder ) ;
38
+ AdjustMiddleChild ( parent , indexInParentChildren , finder ) ;
39
39
}
40
40
41
41
// last child, adjust start-position and end-position (on same line)
42
42
if ( indexInParentChildren == parentChildren . Count - 1 )
43
43
{
44
- AdjustLastChild ( child , parentChildren , indexInParentChildren , finder ) ;
44
+ AdjustLastChild ( parent , indexInParentChildren , finder ) ;
45
45
}
46
46
}
47
47
48
48
if ( child is Container c )
49
49
{
50
- AdjustContainerChild ( c , finder ) ;
50
+ AdjustContainerChild ( parent , c , finder ) ;
51
51
}
52
52
else if ( child is TerminalNode t )
53
53
{
54
- AdjustTerminalNodeChild ( t , finder ) ;
54
+ AdjustTerminalNodeChild ( parent , t , finder ) ;
55
55
}
56
56
}
57
57
58
- private static void AdjustSingleChild ( ContainerOrTerminalNode child , CharacterPositionFinder finder )
58
+ private static void AdjustSingleChild ( IParent parent , CharacterPositionFinder finder )
59
59
{
60
60
// TODO: RKN find out how to adjust
61
61
}
62
62
63
- private static void AdjustFirstChild ( ContainerOrTerminalNode child , IList < ContainerOrTerminalNode > parentChildren , int indexInParentChildren , CharacterPositionFinder finder )
63
+ private static void AdjustFirstChild ( IParent parent , int indexInParentChildren , CharacterPositionFinder finder )
64
64
{
65
+ var parentChildren = parent . Children ;
66
+ var child = parentChildren [ indexInParentChildren ] ;
65
67
var nextSibling = parentChildren [ indexInParentChildren + 1 ] ;
66
68
67
- var startPos = new LineInfo ( child . LocationSpan . Start . LineNumber , 1 ) ;
69
+ // same line, so start immediately after
70
+ var startPos = parent is Container c && c . LocationSpan . Start . LineNumber == child . LocationSpan . Start . LineNumber
71
+ ? finder . GetLineInfo ( c . HeaderSpan . End + 1 )
72
+ : new LineInfo ( child . LocationSpan . Start . LineNumber , 1 ) ;
73
+
68
74
var endPos = FindNewEndPos ( child , nextSibling , finder ) ;
69
75
70
76
child . LocationSpan = new LocationSpan ( startPos , endPos ) ;
71
77
}
72
78
73
- private static void AdjustMiddleChild ( ContainerOrTerminalNode child , IList < ContainerOrTerminalNode > parentChildren , int indexInParentChildren , CharacterPositionFinder finder )
79
+ private static void AdjustMiddleChild ( IParent parent , int indexInParentChildren , CharacterPositionFinder finder )
74
80
{
81
+ var parentChildren = parent . Children ;
82
+ var child = parentChildren [ indexInParentChildren ] ;
75
83
var previousSibling = parentChildren [ indexInParentChildren - 1 ] ;
76
84
var nextSibling = parentChildren [ indexInParentChildren + 1 ] ;
77
85
@@ -82,8 +90,10 @@ private static void AdjustMiddleChild(ContainerOrTerminalNode child, IList<Conta
82
90
child . LocationSpan = new LocationSpan ( startPos , endPos ) ;
83
91
}
84
92
85
- private static void AdjustLastChild ( ContainerOrTerminalNode child , IList < ContainerOrTerminalNode > parentChildren , int indexInParentChildren , CharacterPositionFinder finder )
93
+ private static void AdjustLastChild ( IParent parent , int indexInParentChildren , CharacterPositionFinder finder )
86
94
{
95
+ var parentChildren = parent . Children ;
96
+ var child = parentChildren [ indexInParentChildren ] ;
87
97
var previousSibling = parentChildren [ indexInParentChildren - 1 ] ;
88
98
89
99
var indexAfter = finder . GetCharacterPosition ( previousSibling . LocationSpan . End ) + 1 ;
@@ -93,7 +103,7 @@ private static void AdjustLastChild(ContainerOrTerminalNode child, IList<Contain
93
103
child . LocationSpan = new LocationSpan ( startPos , endPos ) ;
94
104
}
95
105
96
- private static void AdjustContainerChild ( Container child , CharacterPositionFinder finder )
106
+ private static void AdjustContainerChild ( IParent parent , Container child , CharacterPositionFinder finder )
97
107
{
98
108
AdjustChildren ( child , finder ) ;
99
109
@@ -118,12 +128,20 @@ private static void AdjustContainerChild(Container child, CharacterPositionFinde
118
128
var firstChild = child . Children . First ( ) ;
119
129
var lastChild = child . Children . Last ( ) ;
120
130
121
- var headerStartLine = firstChild . LocationSpan . Start . LineNumber - 1 ;
131
+ var headerOffset = child . LocationSpan . Start . LineNumber == firstChild . LocationSpan . Start . LineNumber
132
+ ? 0 // it's on the same line
133
+ : - 1 ;
134
+
135
+ var footerOffset = child . LocationSpan . Start . LineNumber == firstChild . LocationSpan . Start . LineNumber
136
+ ? 0 // it's on the same line
137
+ : 1 ;
138
+
139
+ var headerStartLine = firstChild . LocationSpan . Start . LineNumber + headerOffset ;
122
140
123
141
var headerStart = child . HeaderSpan . Start ;
124
142
var headerEnd = finder . GetCharacterPosition ( headerStartLine , finder . GetLineLength ( headerStartLine ) ) ;
125
143
126
- var footerStartLine = lastChild . LocationSpan . End . LineNumber + 1 ;
144
+ var footerStartLine = lastChild . LocationSpan . End . LineNumber + footerOffset ;
127
145
var footerStart = finder . GetCharacterPosition ( footerStartLine , 1 ) ;
128
146
var footerEnd = finder . GetCharacterPosition ( child . LocationSpan . End ) ;
129
147
@@ -134,7 +152,7 @@ private static void AdjustContainerChild(Container child, CharacterPositionFinde
134
152
}
135
153
}
136
154
137
- private static void AdjustTerminalNodeChild ( TerminalNode child , CharacterPositionFinder finder )
155
+ private static void AdjustTerminalNodeChild ( IParent parent , TerminalNode child , CharacterPositionFinder finder )
138
156
{
139
157
var start = finder . GetCharacterPosition ( child . LocationSpan . Start ) ;
140
158
var end = finder . GetCharacterPosition ( child . LocationSpan . End ) ;
@@ -143,11 +161,9 @@ private static void AdjustTerminalNodeChild(TerminalNode child, CharacterPositio
143
161
144
162
private static void AdjustChildren ( Container container , CharacterPositionFinder finder )
145
163
{
146
- var children = container . Children ;
147
-
148
- for ( var index = 0 ; index < children . Count ; index ++ )
164
+ for ( var index = 0 ; index < container . Children . Count ; index ++ )
149
165
{
150
- AdjustNode ( children , index , finder ) ;
166
+ AdjustNode ( container , index , finder ) ;
151
167
}
152
168
}
153
169
0 commit comments