Skip to content

Commit 6b5e183

Browse files
author
Kapil Borle
committed
Add method to get extents of equal tokens
1 parent dd0b9ea commit 6b5e183

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Rules/AlignAssignmentStatement.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,98 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
6464
{
6565
yield break;
6666
}
67+
68+
// it is probably much easier have a hashtable writer that formats the hashtable and writes it
69+
// but it my make handling comments hard. So we need to use this approach.
70+
71+
// check if each key is on a separate line
72+
// align only if each key=val pair is on a separate line
73+
// if each pair on a separate line
74+
// find all the assignment operators
75+
// if all the assignment operators are aligned (check the column number of each alignment operator)
76+
// skip
77+
// else
78+
// find the distance between the assignment operaters its left expression
79+
// find the longest left expression
80+
// make sure all the assignment operators are in the same column as that of the longest left hand.
81+
// else
82+
83+
var alignments = new List<int>();
84+
foreach (var astItem in hashtableAsts)
85+
{
86+
var hashtableAst = (HashtableAst)astItem;
87+
if (!HasKeysOnSeparateLines(hashtableAst))
88+
{
89+
continue;
90+
}
91+
92+
var nodeTuples = GetExtents(tokenOps, hashtableAst);
93+
if (nodeTuples == null)
94+
{
95+
continue;
96+
}
97+
}
98+
}
99+
100+
private static IList<Tuple<IScriptExtent, IScriptExtent>> GetExtents(
101+
TokenOperations tokenOps,
102+
HashtableAst hashtableAst)
103+
{
104+
var nodeTuples = new List<Tuple<IScriptExtent, IScriptExtent>>();
105+
foreach (var kvp in hashtableAst.KeyValuePairs)
106+
{
107+
var keyStartPos = kvp.Item1.Extent.StartScriptPosition;
108+
var tokenNode = tokenOps.GetTokenNodes(
109+
token => token.Extent.StartScriptPosition == keyStartPos).FirstOrDefault();
110+
if (tokenNode == null)
111+
{
112+
return null;
113+
}
114+
115+
var leftToken = tokenNode.Value;
116+
var tempNode = tokenNode.Next;
117+
while (tempNode != null
118+
&& tempNode.Value.Kind != TokenKind.EndOfInput
119+
&& tempNode.Value.Kind != TokenKind.Equals)
120+
{
121+
tempNode = tempNode.Next;
122+
}
123+
124+
if (tempNode == null || tempNode.Value.Kind == TokenKind.EndOfInput)
125+
{
126+
return null;
127+
}
128+
129+
var equalToken = tempNode.Value;
130+
if (kvp.Item1.Extent.EndLineNumber != equalToken.Extent.StartLineNumber)
131+
{
132+
return null;
133+
}
134+
135+
nodeTuples.Add(new Tuple<IScriptExtent, IScriptExtent>(
136+
equalToken.Extent,
137+
kvp.Item1.Extent));
138+
}
139+
140+
return nodeTuples;
141+
}
142+
143+
private bool HasKeysOnSeparateLines(HashtableAst hashtableAst)
144+
{
145+
var lines = new HashSet<int>();
146+
foreach (var kvp in hashtableAst.KeyValuePairs)
147+
{
148+
if (lines.Contains(kvp.Item1.Extent.StartLineNumber))
149+
{
150+
return false;
151+
}
152+
else
153+
{
154+
lines.Add(kvp.Item1.Extent.StartLineNumber);
155+
}
156+
}
157+
158+
return true;
67159
}
68160

69161
/// <summary>

0 commit comments

Comments
 (0)