提交 f05ef0ed 编写于 作者: C CyrusNajmabadi

Update VB side.

上级 ad6935df
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Option Strict Off
Imports Microsoft.CodeAnalysis.CodeActions
Imports Microsoft.CodeAnalysis.CodeFixes
Imports Microsoft.CodeAnalysis.Diagnostics
......@@ -11,7 +10,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.Diagnostics.Genera
Inherits AbstractVisualBasicDiagnosticProviderBasedUserDiagnosticTest
Friend Overrides Function CreateDiagnosticProviderAndFixer(workspace As Workspace) As Tuple(Of DiagnosticAnalyzer, CodeFixProvider)
Return New Tuple(Of DiagnosticAnalyzer, CodeFixProvider)(Nothing, New GenerateVariableCodeFixProvider())
Return New Tuple(Of DiagnosticAnalyzer, CodeFixProvider)(
Nothing, New GenerateVariableCodeFixProvider())
End Function
Protected Overrides Function MassageActions(actions As IList(Of CodeAction)) As IList(Of CodeAction)
......
......@@ -243,7 +243,9 @@ public static MemberDeclarationSyntax LastOperator(SyntaxList<MemberDeclarationS
}
var desiredIndex = TryGetDesiredIndexIfGrouped(
declarationList, declaration, availableIndices);
declarationList, declaration, availableIndices,
CSharpDeclarationComparer.WithoutNamesInstance,
CSharpDeclarationComparer.WithNamesInstance);
if (desiredIndex.HasValue)
{
return desiredIndex.Value;
......@@ -298,104 +300,6 @@ public static MemberDeclarationSyntax LastOperator(SyntaxList<MemberDeclarationS
return declarationList.Count;
}
private static int? TryGetDesiredIndexIfGrouped<TDeclarationSyntax>(
SyntaxList<TDeclarationSyntax> declarationList,
TDeclarationSyntax declaration,
IList<bool> availableIndices)
where TDeclarationSyntax : SyntaxNode
{
var result = TryGetDesiredIndexIfGroupedWorker(declarationList, declaration, availableIndices);
if (result == null)
{
return null;
}
result = GetPreferredIndex(result.Value, availableIndices, forward: true);
if (result == -1)
{
return null;
}
return result;
}
private static int? TryGetDesiredIndexIfGroupedWorker<TDeclarationSyntax>(
SyntaxList<TDeclarationSyntax> declarationList,
TDeclarationSyntax declaration,
IList<bool> availableIndices)
where TDeclarationSyntax : SyntaxNode
{
var comparerWithoutNameCheck = new CSharpDeclarationComparer(includeName: false);
if (!declarationList.IsSorted(comparerWithoutNameCheck))
{
// Existing declarations weren't grouped. Don't try to find a location
// to this declaration into.
return null;
}
// The list was grouped (by type, staticness, accessibility). Try to find a location
// to put the new declaration into.
var result = Array.BinarySearch(declarationList.ToArray(), declaration, comparerWithoutNameCheck);
var desiredGroupIndex = result < 0 ? ~result : result;
Debug.Assert(desiredGroupIndex >= 0);
Debug.Assert(desiredGroupIndex <= declarationList.Count);
// Now, walk forward until we hit the last member of this group.
while (desiredGroupIndex < declarationList.Count)
{
// Stop walking forward if we hit an unavailable index.
if (availableIndices != null && !availableIndices[desiredGroupIndex])
{
break;
}
if (0 != comparerWithoutNameCheck.Compare(declaration, declarationList[desiredGroupIndex]))
{
// Found the index of an item not of our group.
break;
}
desiredGroupIndex++;
}
// Now, walk backward until we find the last member with the same name
// as us. We want to keep overloads together, so we'll place ourselves
// after that member.
var comparerWithNameCheck = new CSharpDeclarationComparer(includeName: true);
var currentIndex = desiredGroupIndex;
while (currentIndex > 0)
{
var previousIndex = currentIndex - 1;
// Stop walking backward if we hit an unavailable index.
if (availableIndices != null && !availableIndices[previousIndex])
{
break;
}
if (0 != comparerWithoutNameCheck.Compare(declaration, declarationList[previousIndex]))
{
// Hit the previous group of items.
break;
}
// Still in the same group. If we find something with the same name
// then place ourselves after it.
if (0 == comparerWithNameCheck.Compare(declaration, declarationList[previousIndex]))
{
// Found something with the same name. Generate after this item.
return currentIndex;
}
currentIndex--;
}
// Couldn't find anything with our name. Just place us at the end of this group.
return desiredGroupIndex;
}
public static SyntaxNode GetContextNode(
Location location, CancellationToken cancellationToken)
{
......
......@@ -32,7 +32,7 @@ public override CodeGenerationDestination GetDestination(SyntaxNode node)
}
protected override IComparer<SyntaxNode> GetMemberComparer()
=> new CSharpDeclarationComparer(includeName: false);
=> CSharpDeclarationComparer.WithoutNamesInstance;
protected override AbstractImportsAdder CreateImportsAdder(
Document document)
......
......@@ -55,9 +55,12 @@ internal class CSharpDeclarationComparer : IComparer<SyntaxNode>
{ SyntaxKind.FalseKeyword, 21 },
};
public static readonly CSharpDeclarationComparer WithNamesInstance = new CSharpDeclarationComparer(includeName: true);
public static readonly CSharpDeclarationComparer WithoutNamesInstance = new CSharpDeclarationComparer(includeName: true);
private readonly bool _includeName;
public CSharpDeclarationComparer(bool includeName)
private CSharpDeclarationComparer(bool includeName)
{
_includeName = includeName;
}
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Editing;
......@@ -228,5 +229,106 @@ public static T GetReuseableSyntaxNodeForAttribute<T>(AttributeData attribute, C
attribute.ApplicationSyntaxReference.GetSyntax() as T :
null;
}
public static int? TryGetDesiredIndexIfGrouped<TDeclarationSyntax>(
SyntaxList<TDeclarationSyntax> declarationList,
TDeclarationSyntax declaration,
IList<bool> availableIndices,
IComparer<TDeclarationSyntax> comparerWithoutNameCheck,
IComparer<TDeclarationSyntax> comparerWithNameCheck)
where TDeclarationSyntax : SyntaxNode
{
var result = TryGetDesiredIndexIfGroupedWorker(
declarationList, declaration, availableIndices,
comparerWithoutNameCheck, comparerWithNameCheck);
if (result == null)
{
return null;
}
result = GetPreferredIndex(result.Value, availableIndices, forward: true);
if (result == -1)
{
return null;
}
return result;
}
private static int? TryGetDesiredIndexIfGroupedWorker<TDeclarationSyntax>(
SyntaxList<TDeclarationSyntax> declarationList,
TDeclarationSyntax declaration,
IList<bool> availableIndices,
IComparer<TDeclarationSyntax> comparerWithoutNameCheck,
IComparer<TDeclarationSyntax> comparerWithNameCheck)
where TDeclarationSyntax : SyntaxNode
{
if (!declarationList.IsSorted(comparerWithoutNameCheck))
{
// Existing declarations weren't grouped. Don't try to find a location
// to this declaration into.
return null;
}
// The list was grouped (by type, staticness, accessibility). Try to find a location
// to put the new declaration into.
var result = Array.BinarySearch(declarationList.ToArray(), declaration, comparerWithoutNameCheck);
var desiredGroupIndex = result < 0 ? ~result : result;
Debug.Assert(desiredGroupIndex >= 0);
Debug.Assert(desiredGroupIndex <= declarationList.Count);
// Now, walk forward until we hit the last member of this group.
while (desiredGroupIndex < declarationList.Count)
{
// Stop walking forward if we hit an unavailable index.
if (availableIndices != null && !availableIndices[desiredGroupIndex])
{
break;
}
if (0 != comparerWithoutNameCheck.Compare(declaration, declarationList[desiredGroupIndex]))
{
// Found the index of an item not of our group.
break;
}
desiredGroupIndex++;
}
// Now, walk backward until we find the last member with the same name
// as us. We want to keep overloads together, so we'll place ourselves
// after that member.
var currentIndex = desiredGroupIndex;
while (currentIndex > 0)
{
var previousIndex = currentIndex - 1;
// Stop walking backward if we hit an unavailable index.
if (availableIndices != null && !availableIndices[previousIndex])
{
break;
}
if (0 != comparerWithoutNameCheck.Compare(declaration, declarationList[previousIndex]))
{
// Hit the previous group of items.
break;
}
// Still in the same group. If we find something with the same name
// then place ourselves after it.
if (0 == comparerWithNameCheck.Compare(declaration, declarationList[previousIndex]))
{
// Found something with the same name. Generate after this item.
return currentIndex;
}
currentIndex--;
}
// Couldn't find anything with our name. Just place us at the end of this group.
return desiredGroupIndex;
}
}
}
\ No newline at end of file
......@@ -195,12 +195,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Dim declarations = declarationList.ToArray()
If (declarations.Length = 0) Then
Return 0
ElseIf declarations.IsSorted(VisualBasicDeclarationComparer.Instance) Then
Dim result = Array.BinarySearch(Of TDeclaration)(declarations, declaration, VisualBasicDeclarationComparer.Instance)
Dim index = GetPreferredIndex(If(result < 0, Not result, result), availableIndices, forward:=True)
If index <> -1 Then
Return index
End If
Dim desiredIndex = TryGetDesiredIndexIfGrouped(
declarationList, declaration, availableIndices,
VisualBasicDeclarationComparer.WithoutNamesInstance,
VisualBasicDeclarationComparer.WithNamesInstance)
If desiredIndex.HasValue Then
Return desiredIndex.Value
End If
If after IsNot Nothing Then
......
......@@ -28,7 +28,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End Function
Protected Overrides Function GetMemberComparer() As IComparer(Of SyntaxNode)
Return VisualBasicDeclarationComparer.Instance
Return VisualBasicDeclarationComparer.WithoutNamesInstance
End Function
Protected Overrides Function GetAvailableInsertionIndices(destination As SyntaxNode, cancellationToken As CancellationToken) As IList(Of Boolean)
......
......@@ -7,8 +7,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Friend Class VisualBasicDeclarationComparer
Implements IComparer(Of SyntaxNode)
Public Shared ReadOnly Instance As IComparer(Of SyntaxNode) = New VisualBasicDeclarationComparer()
Private Shared ReadOnly s_kindPrecedenceMap As Dictionary(Of SyntaxKind, Integer) = New Dictionary(Of SyntaxKind, Integer)(SyntaxFacts.EqualityComparer) From
{
{SyntaxKind.FieldDeclaration, 0},
......@@ -61,7 +59,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
{SyntaxKind.CTypeKeyword, 24}
}
Private Sub New()
Public Shared ReadOnly WithNamesInstance As New VisualBasicDeclarationComparer(includeName:=True)
Public Shared ReadOnly WithoutNamesInstance As New VisualBasicDeclarationComparer(includeName:=False)
Private ReadOnly _includeName As Boolean
Private Sub New(includeName As Boolean)
_includeName = includeName
End Sub
Public Function Compare(x As SyntaxNode, y As SyntaxNode) As Integer Implements IComparer(Of SyntaxNode).Compare
......@@ -152,28 +156,31 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Return node
End Function
Private Shared Function Compare(x As DelegateStatementSyntax, y As DelegateStatementSyntax) As Integer
Private Function Compare(x As DelegateStatementSyntax, y As DelegateStatementSyntax) As Integer
Dim result = 0
If EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) AndAlso
EqualIdentifierName(x.Identifier, y.Identifier, result) Then
If EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) Then
EqualTypeParameterCount(x.TypeParameterList, y.TypeParameterList, result)
If _includeName Then
EqualIdentifierName(x.Identifier, y.Identifier, result)
End If
End If
Return result
End Function
Private Shared Function Compare(x As FieldDeclarationSyntax, y As FieldDeclarationSyntax) As Integer
Private Function Compare(x As FieldDeclarationSyntax, y As FieldDeclarationSyntax) As Integer
Dim result = 0
If EqualConstness(x.Modifiers, y.Modifiers, result) AndAlso
EqualSharedness(x.Modifiers, y.Modifiers, result) AndAlso
EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) Then
If _includeName Then
EqualIdentifierName(
x.Declarators.FirstOrDefault().Names.FirstOrDefault().Identifier,
y.Declarators.FirstOrDefault().Names.FirstOrDefault().Identifier,
result)
End If
End If
Return result
End Function
......@@ -189,37 +196,41 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Return result
End Function
Private Shared Function Compare(x As MethodStatementSyntax, y As MethodStatementSyntax) As Integer
Private Function Compare(x As MethodStatementSyntax, y As MethodStatementSyntax) As Integer
Dim result = 0
If EqualSharedness(x.Modifiers, y.Modifiers, result) AndAlso
EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) AndAlso
EqualIdentifierName(x.Identifier, y.Identifier, result) AndAlso
EqualTypeParameterCount(x.TypeParameterList, y.TypeParameterList, result) Then
EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) Then
EqualParameterLists(x.ParameterList, y.ParameterList, result)
If _includeName Then
EqualIdentifierName(x.Identifier, y.Identifier, result)
End If
End If
Return result
End Function
Private Shared Function Compare(x As EventStatementSyntax, y As EventStatementSyntax) As Integer
Private Function Compare(x As EventStatementSyntax, y As EventStatementSyntax) As Integer
Dim result = 0
If EqualSharedness(x.Modifiers, y.Modifiers, result) AndAlso
EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) Then
If _includeName Then
EqualIdentifierName(x.Identifier, y.Identifier, result)
End If
End If
Return result
End Function
Private Shared Function Compare(x As PropertyStatementSyntax, y As PropertyStatementSyntax) As Integer
Private Function Compare(x As PropertyStatementSyntax, y As PropertyStatementSyntax) As Integer
Dim result = 0
If EqualSharedness(x.Modifiers, y.Modifiers, result) AndAlso
EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) Then
If _includeName Then
EqualIdentifierName(x.Identifier, y.Identifier, result)
End If
End If
Return result
End Function
......@@ -233,22 +244,25 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Return result
End Function
Private Shared Function Compare(x As EnumStatementSyntax, y As EnumStatementSyntax) As Integer
Private Function Compare(x As EnumStatementSyntax, y As EnumStatementSyntax) As Integer
Dim result = 0
If EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) Then
If _includeName Then
EqualIdentifierName(x.Identifier, y.Identifier, result)
End If
End If
Return result
End Function
Private Shared Function Compare(x As TypeStatementSyntax, y As TypeStatementSyntax) As Integer
Private Function Compare(x As TypeStatementSyntax, y As TypeStatementSyntax) As Integer
Dim result = 0
If EqualSharedness(x.Modifiers, y.Modifiers, result) AndAlso
EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) AndAlso
EqualIdentifierName(x.Identifier, y.Identifier, result) Then
EqualAccessibility(x, x.Modifiers, y, y.Modifiers, result) Then
EqualTypeParameterCount(x.TypeParameterList, y.TypeParameterList, result)
If _includeName Then
EqualIdentifierName(x.Identifier, y.Identifier, result)
End If
End If
Return result
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册