Skip to content

Commit 23d2fce

Browse files
committed
Add code fixer for tab order
Handle scenarios without docking, e.g. all controls that have misaligned `TabIndex` vs z-order aren't docked. There are three distinct scenarios when it comes to re-ordering statements: 1. Move down by one, or swap neighbouring statements - in this case we need to swap the leading trivia before we switch. 2. Move down by more than one position - we need to swap leading trivia with the statement directly below the statement we are moving, and update the trivia for the moved statement with the leading trivia of the statement above which we're inserting. 3. Move up - we need to swap the leading trivia for the moved statement with the leading trivia of the statement above which we're inserting, and update that statement's trivia with the leading trivia of the statement directly below it.
1 parent 35493de commit 23d2fce

13 files changed

+1245
-200
lines changed

src/WindowsForms.Analyzers/ControlTabOrderAnalyzer.cs

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
66
using System.Diagnostics;
7+
using System.Linq;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.CSharp.Syntax;
910
using Microsoft.CodeAnalysis.Diagnostics;
@@ -100,6 +101,8 @@ private void CodeBlockAction(OperationBlockAnalysisContext context)
100101
return;
101102
}
102103

104+
Dictionary<string, List<Location>> containerProperties = BuildContainerAddLocations(calculatedContext.ContainerAddLocations);
105+
103106
// _controlsAddIndex dictionary, which looks something like this:
104107
//
105108
// [this.Controls.Add] : new List { button3, this.button1 }
@@ -111,42 +114,99 @@ private void CodeBlockAction(OperationBlockAnalysisContext context)
111114
// [this.button1:1]
112115
// [label2:0]
113116
Dictionary<string, int> flatControlsAddIndex = new();
114-
foreach (string key in calculatedContext.ControlsAddIndex.Keys)
117+
Dictionary<string, string> containersByControl = new();
118+
foreach (string containerName in calculatedContext.ControlsAddIndex.Keys)
115119
{
116-
for (int i = 0; i < calculatedContext.ControlsAddIndex[key].Count; i++)
120+
for (int i = 0; i < calculatedContext.ControlsAddIndex[containerName].Count; i++)
117121
{
118-
string controlName = calculatedContext.ControlsAddIndex[key][i];
122+
string controlName = calculatedContext.ControlsAddIndex[containerName][i];
119123
flatControlsAddIndex[controlName] = i;
124+
125+
containersByControl[controlName] = containerName;
120126
}
121127
}
122128

123129
// Verify explicit TabIndex is the same as the "add order"
124-
foreach (string key in calculatedContext.ControlsTabIndex.Keys)
130+
foreach (string controlName in calculatedContext.ControlsTabIndex.Keys)
125131
{
126-
if (!flatControlsAddIndex.ContainsKey(key))
132+
if (!flatControlsAddIndex.ContainsKey(controlName))
127133
{
128134
// TODO: assert, diagnostics, etc.
129135
continue;
130136
}
131137

132-
int tabIndex = calculatedContext.ControlsTabIndex[key];
133-
int addIndex = flatControlsAddIndex[key];
138+
int tabIndex = calculatedContext.ControlsTabIndex[controlName];
139+
int addIndex = flatControlsAddIndex[controlName];
134140

135141
if (tabIndex == addIndex)
136142
{
137143
continue;
138144
}
139145

146+
string containerName = containersByControl[controlName];
147+
Dictionary<string, string?> properties = new();
148+
properties["ZOrder"] = addIndex.ToString();
149+
properties["TabIndex"] = tabIndex.ToString();
150+
140151
var diagnostic = Diagnostic.Create(
141152
descriptor: InconsistentTabIndexRuleIdDescriptor,
142-
location: calculatedContext.ControlsAddIndexLocations[key],
143-
properties: new Dictionary<string, string?> { { "ZOrder", addIndex.ToString() }, { "TabIndex", tabIndex.ToString() } }.ToImmutableDictionary(),
144-
key, addIndex, tabIndex);
153+
location: calculatedContext.ControlsAddIndexLocations[controlName],
154+
additionalLocations: containerProperties[containerName],
155+
properties.ToImmutableDictionary(),
156+
controlName, addIndex, tabIndex);
145157
context.ReportDiagnostic(diagnostic);
146158
}
147159
}
148160
}
149161

162+
private static Dictionary<string, List<Location>> BuildContainerAddLocations(in Dictionary<string, List<Location>> containerAddLocations)
163+
{
164+
Dictionary<string, List<Location>> containerProperties = new();
165+
166+
// Check that 'container.Controls.Add(...)' statements are consequitive.
167+
// If not - the code has been manually modified, we won't be able to provide an auto-fix.
168+
foreach (string containerName in containerAddLocations.Keys)
169+
{
170+
containerProperties[containerName] = new();
171+
172+
Location startLine = Location.None;
173+
Location endLine = Location.None;
174+
List<int> lines = new();
175+
foreach (Location location in containerAddLocations[containerName])
176+
{
177+
if (startLine == Location.None || startLine.GetLineSpan().StartLinePosition.Line > location.GetLineSpan().StartLinePosition.Line)
178+
{
179+
startLine = location;
180+
}
181+
182+
if (endLine.GetLineSpan().StartLinePosition.Line < location.GetLineSpan().StartLinePosition.Line)
183+
{
184+
endLine = location;
185+
}
186+
187+
lines.Add(location.GetLineSpan().StartLinePosition.Line);
188+
}
189+
190+
Debug.Assert(startLine != Location.None);
191+
Debug.Assert(endLine != Location.None);
192+
193+
if (startLine == endLine)
194+
{
195+
// A single control with an invalid TabIndex
196+
}
197+
else if (Enumerable.Range(startLine.GetLineSpan().StartLinePosition.Line, endLine.GetLineSpan().StartLinePosition.Line - startLine.GetLineSpan().StartLinePosition.Line).Except(lines).Any())
198+
{
199+
// 'container.Controls.Add(...)' statements aren't consequitive.
200+
}
201+
else
202+
{
203+
containerProperties[containerName] = containerAddLocations[containerName];
204+
}
205+
}
206+
207+
return containerProperties;
208+
}
209+
150210
private void ParseControlAddStatements(InvocationExpressionSyntax expressionSyntax, CalculatedAnalysisContext calculatedContext)
151211
{
152212
if (!expressionSyntax.Expression.ToString().EndsWith(".Controls.Add"))
@@ -169,7 +229,7 @@ private void ParseControlAddStatements(InvocationExpressionSyntax expressionSynt
169229

170230
// this is something like "this.Controls.Add" or "panel1.Controls.Add", but good enough for our intents and purposes
171231
ExpressionSyntax? syntax = expressionSyntax.Expression;
172-
string container = syntax.ToString();
232+
string containerName = syntax.ToString();
173233

174234
// Transform "Controls.Add" statements into a map. E.g.:
175235
//
@@ -182,13 +242,20 @@ private void ParseControlAddStatements(InvocationExpressionSyntax expressionSynt
182242
// [this.Controls.Add] : new List { button3, this.button1 }
183243
// [panel1.Controls.Add] : new List { label2 }
184244

185-
if (!calculatedContext.ControlsAddIndex.ContainsKey(container))
245+
if (!calculatedContext.ControlsAddIndex.ContainsKey(containerName))
246+
{
247+
calculatedContext.ControlsAddIndex[containerName] = new List<string>();
248+
}
249+
250+
calculatedContext.ControlsAddIndex[containerName].Add(controlName);
251+
calculatedContext.ControlsAddIndexLocations[controlName] = syntax.Parent!.Parent!.GetLocation(); // e.g.: 'this.Controls.Add(button3);'
252+
253+
if (!calculatedContext.ContainerAddLocations.ContainsKey(containerName))
186254
{
187-
calculatedContext.ControlsAddIndex[container] = new List<string>();
255+
calculatedContext.ContainerAddLocations[containerName] = new();
188256
}
189257

190-
calculatedContext.ControlsAddIndex[container].Add(controlName);
191-
calculatedContext.ControlsAddIndexLocations[controlName] = syntax.Parent!.Parent!.GetLocation();
258+
calculatedContext.ContainerAddLocations[containerName].Add(calculatedContext.ControlsAddIndexLocations[controlName]);
192259
}
193260

194261
private void ParseTabIndexAssignments(AssignmentExpressionSyntax expressionSyntax, OperationBlockAnalysisContext context, CalculatedAnalysisContext calculatedContext)
@@ -246,6 +313,8 @@ private sealed class CalculatedAnalysisContext
246313
// Contains the list of fields and local controls in order those are added to parent controls.
247314
public Dictionary<string, List<string>> ControlsAddIndex { get; } = new();
248315
public Dictionary<string, Location> ControlsAddIndexLocations { get; } = new();
316+
317+
public Dictionary<string, List<Location>> ContainerAddLocations { get; } = new();
249318
}
250319
}
251320
}

src/WindowsForms.CodeFixes/CodeFixResources.Designer.cs

Lines changed: 25 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
4-
Microsoft ResX Schema
5-
6-
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
11-
associated with the data types.
12-
13-
Example:
14-
15-
... ado.net/XML headers & schema ...
16-
<resheader name="resmimetype">text/microsoft-resx</resheader>
17-
<resheader name="version">2.0</resheader>
18-
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19-
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20-
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21-
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22-
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23-
<value>[base64 mime encoded serialized .NET Framework object]</value>
24-
</data>
25-
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26-
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27-
<comment>This is a comment</comment>
28-
</data>
29-
30-
There are any number of "resheader" rows that contain simple
31-
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
37-
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
41-
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
45-
read any of the formats listed below.
46-
47-
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
49-
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50-
: and then encoded with base64 encoding.
51-
52-
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
54-
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55-
: and then encoded with base64 encoding.
563

57-
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
59-
: using a System.ComponentModel.TypeConverter
60-
: and then encoded with base64 encoding.
61-
-->
624
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
635
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
646
<xsd:element name="root" msdata:IsDataSet="true">
@@ -117,8 +59,8 @@
11759
<resheader name="writer">
11860
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
11961
</resheader>
120-
<data name="CodeFixTitle" xml:space="preserve">
121-
<value>Make uppercase</value>
122-
<comment>The title of the code fix.</comment>
62+
<data name="ControlTabOrderAnalyzerCodeFixTitle" xml:space="preserve">
63+
<value>Match Z-order to TabIndex</value>
64+
<comment>The title of the code fix. {Locked="TabIndex"}.</comment>
12365
</data>
124-
</root>
66+
</root>

0 commit comments

Comments
 (0)