diff --git a/src/EditorFeatures/CSharpTest/Squiggles/ErrorSquiggleProducerTests.cs b/src/EditorFeatures/CSharpTest/Squiggles/ErrorSquiggleProducerTests.cs index 2d344ebba8a514367cbc1d95e71933e093ae9d81..2a7e66a372d7c396f98ed9f9fdd7053619797e2a 100644 --- a/src/EditorFeatures/CSharpTest/Squiggles/ErrorSquiggleProducerTests.cs +++ b/src/EditorFeatures/CSharpTest/Squiggles/ErrorSquiggleProducerTests.cs @@ -74,15 +74,20 @@ public void SuggestionTagsForUnnecessaryCode() @" - using System.Collections; // Unused using. - class Program - { - void Test() - { - System.Int32 x = 2; // Simplify type name. - x += 1; - } - } +// System is used - rest are unused. +using System.Collections; +using System; +using System.Diagnostics; +using System.Collections.Generic; + +class Program +{ + void Test() + { + Int32 x = 2; // Int32 can be simplified. + x += 1; + } +} "; @@ -94,18 +99,30 @@ void Test() ImmutableArray.Create( new CSharpSimplifyTypeNamesDiagnosticAnalyzer(), new CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer())); - var spans = GetErrorSpans(workspace, analyzerMap.ToImmutable()); - spans = spans.OrderBy(s => s.Span.Span.Start); + var spans = + GetErrorSpans(workspace, analyzerMap.ToImmutable()) + .OrderBy(s => s.Span.Span.Start).ToImmutableArray(); - Assert.Equal(2, spans.Count()); - var first = spans.First(); - var second = spans.Last(); + Assert.Equal(3, spans.Length); + var first = spans[0]; + var second = spans[1]; + var third = spans[2]; Assert.Equal(PredefinedErrorTypeNames.Suggestion, first.Tag.ErrorType); Assert.Equal(CSharpFeaturesResources.RemoveUnnecessaryUsingsDiagnosticTitle, first.Tag.ToolTipContent); + Assert.Equal(40, first.Span.Start); + Assert.Equal(25, first.Span.Length); + Assert.Equal(PredefinedErrorTypeNames.Suggestion, second.Tag.ErrorType); - Assert.Equal(WorkspacesResources.NameCanBeSimplified, second.Tag.ToolTipContent); + Assert.Equal(CSharpFeaturesResources.RemoveUnnecessaryUsingsDiagnosticTitle, second.Tag.ToolTipContent); + Assert.Equal(82, second.Span.Start); + Assert.Equal(60, second.Span.Length); + + Assert.Equal(PredefinedErrorTypeNames.Suggestion, third.Tag.ErrorType); + Assert.Equal(WorkspacesResources.NameCanBeSimplified, third.Tag.ToolTipContent); + Assert.Equal(196, third.Span.Start); + Assert.Equal(5, third.Span.Length); } } diff --git a/src/EditorFeatures/VisualBasicTest/Squiggles/ErrorSquiggleProducerTests.vb b/src/EditorFeatures/VisualBasicTest/Squiggles/ErrorSquiggleProducerTests.vb index f0d0c1ebd1f6cd4453b31571768b214fc91f5957..a8227db8cd21dce014e7ec354e1ad76a27508296 100644 --- a/src/EditorFeatures/VisualBasicTest/Squiggles/ErrorSquiggleProducerTests.vb +++ b/src/EditorFeatures/VisualBasicTest/Squiggles/ErrorSquiggleProducerTests.vb @@ -78,23 +78,32 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.Squiggles New VisualBasicRemoveUnnecessaryImportsDiagnosticAnalyzer())) Dim spans = ProduceSquiggles(analyzerMap.ToImmutable(), -"Imports System.Collections ' Unused import. +" +' System.Diagnostics is used - rest are unused. +Imports System.Diagnostics +Imports System.Collections +Imports System.Collections.Generic +Imports System.Linq + Class C1 Sub Foo() - Dim x as System.Int32 = 2 ' Simplify type name. - x = x + 1 + Process.Start(GetType(Int32).ToString()) 'Int32 can be simplified. End Sub -End Class") - spans = spans.OrderBy(Function(s) s.Span.Span.Start) +End Class").OrderBy(Function(s) s.Span.Span.Start).ToImmutableArray() - Assert.Equal(2, spans.Count()) - Dim first = spans.First() - Dim second = spans.Last() + Assert.Equal(2, spans.Length) + Dim first = spans(0) + Dim second = spans(1) Assert.Equal(PredefinedErrorTypeNames.Suggestion, first.Tag.ErrorType) Assert.Equal(VBFeaturesResources.RemoveUnnecessaryImportsDiagnosticTitle, first.Tag.ToolTipContent) + Assert.Equal(79, first.Span.Start) + Assert.Equal(83, first.Span.Length) + Assert.Equal(PredefinedErrorTypeNames.Suggestion, second.Tag.ErrorType) Assert.Equal(WorkspacesResources.NameCanBeSimplified, second.Tag.ToolTipContent) + Assert.Equal(221, second.Span.Start) + Assert.Equal(5, second.Span.Length) End Sub End Class End Namespace diff --git a/src/Workspaces/Core/Portable/Shared/Extensions/CommonSyntaxNodeExtensions.cs b/src/Workspaces/Core/Portable/Shared/Extensions/CommonSyntaxNodeExtensions.cs index be2c50665d728e02a53a3f8c952b70426cb9c61b..efbf81d792562795e22cc166ff8b53a62bb1f506 100644 --- a/src/Workspaces/Core/Portable/Shared/Extensions/CommonSyntaxNodeExtensions.cs +++ b/src/Workspaces/Core/Portable/Shared/Extensions/CommonSyntaxNodeExtensions.cs @@ -266,7 +266,9 @@ public static TextSpan GetContainedSpan(this IEnumerable nodes) { SyntaxNode lastNode = null; TextSpan? textSpan = null; - foreach (var node in nodes) + + // Sort the nodes in source location order. + foreach (var node in nodes.OrderBy(n => n.SpanStart)) { if (lastNode == null) {