提交 983fcca1 编写于 作者: C CyrusNajmabadi

Move helpers to common layer.

上级 1d1a5c96
......@@ -172,7 +172,9 @@ public static MemberDeclarationSyntax LastOperator(SyntaxList<MemberDeclarationS
Func<SyntaxList<TDeclaration>, TDeclaration> before = null)
where TDeclaration : SyntaxNode
{
var index = GetInsertionIndex(declarationList, declaration, options, availableIndices, after, before);
var index = GetInsertionIndex(
declarationList, declaration, options, availableIndices,
CSharpDeclarationComparer.Instance, after, before);
if (availableIndices != null)
{
availableIndices.Insert(index, true);
......@@ -191,114 +193,6 @@ public static MemberDeclarationSyntax LastOperator(SyntaxList<MemberDeclarationS
return declaration.ChildTokens().Where(t => t.IsKind(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken) && t.IsMissing).Any();
}
public static int GetInsertionIndex<TDeclaration>(
SyntaxList<TDeclaration> declarationList,
TDeclaration declaration,
CodeGenerationOptions options,
IList<bool> availableIndices,
Func<SyntaxList<TDeclaration>, TDeclaration> after = null,
Func<SyntaxList<TDeclaration>, TDeclaration> before = null)
where TDeclaration : SyntaxNode
{
Contract.ThrowIfTrue(availableIndices != null && availableIndices.Count != declarationList.Count + 1);
if (options != null)
{
// Try to strictly obey the after option by inserting immediately after the member containing the location
if (options.AfterThisLocation != null)
{
var afterMember = declarationList.LastOrDefault(m => m.SpanStart <= options.AfterThisLocation.SourceSpan.Start);
if (afterMember != null)
{
var index = declarationList.IndexOf(afterMember);
index = GetPreferredIndex(index + 1, availableIndices, forward: true);
if (index != -1)
{
return index;
}
}
}
// Try to strictly obey the before option by inserting immediately before the member containing the location
if (options.BeforeThisLocation != null)
{
var beforeMember = declarationList.FirstOrDefault(m => m.Span.End >= options.BeforeThisLocation.SourceSpan.End);
if (beforeMember != null)
{
var index = declarationList.IndexOf(beforeMember);
index = GetPreferredIndex(index, availableIndices, forward: false);
if (index != -1)
{
return index;
}
}
}
if (options.AutoInsertionLocation)
{
if (declarationList.IsEmpty())
{
return 0;
}
else if (declarationList.IsSorted(CSharpDeclarationComparer.Instance))
{
var result = Array.BinarySearch(declarationList.ToArray(), declaration, CSharpDeclarationComparer.Instance);
var index = GetPreferredIndex(result < 0 ? ~result : result, availableIndices, forward: true);
if (index != -1)
{
return index;
}
}
if (after != null)
{
var member = after(declarationList);
if (member != null)
{
var index = declarationList.IndexOf(member);
if (index >= 0)
{
index = GetPreferredIndex(index + 1, availableIndices, forward: true);
if (index != -1)
{
return index;
}
}
}
}
if (before != null)
{
var member = before(declarationList);
if (member != null)
{
var index = declarationList.IndexOf(member);
if (index >= 0)
{
index = GetPreferredIndex(index, availableIndices, forward: false);
if (index != -1)
{
return index;
}
}
}
}
}
}
// Otherwise, add the declaration to the end.
{
var index = GetPreferredIndex(declarationList.Count, availableIndices, forward: false);
if (index != -1)
{
return index;
}
}
return declarationList.Count;
}
public static SyntaxNode GetContextNode(
Location location, CancellationToken cancellationToken)
{
......
......@@ -228,5 +228,114 @@ public static T GetReuseableSyntaxNodeForAttribute<T>(AttributeData attribute, C
attribute.ApplicationSyntaxReference.GetSyntax() as T :
null;
}
public static int GetInsertionIndex<TDeclaration>(
SyntaxList<TDeclaration> declarationList,
TDeclaration declaration,
CodeGenerationOptions options,
IList<bool> availableIndices,
IComparer<TDeclaration> comparer,
Func<SyntaxList<TDeclaration>, TDeclaration> after = null,
Func<SyntaxList<TDeclaration>, TDeclaration> before = null)
where TDeclaration : SyntaxNode
{
Contract.ThrowIfTrue(availableIndices != null && availableIndices.Count != declarationList.Count + 1);
if (options != null)
{
// Try to strictly obey the after option by inserting immediately after the member containing the location
if (options.AfterThisLocation != null)
{
var afterMember = declarationList.LastOrDefault(m => m.SpanStart <= options.AfterThisLocation.SourceSpan.Start);
if (afterMember != null)
{
var index = declarationList.IndexOf(afterMember);
index = GetPreferredIndex(index + 1, availableIndices, forward: true);
if (index != -1)
{
return index;
}
}
}
// Try to strictly obey the before option by inserting immediately before the member containing the location
if (options.BeforeThisLocation != null)
{
var beforeMember = declarationList.FirstOrDefault(m => m.Span.End >= options.BeforeThisLocation.SourceSpan.End);
if (beforeMember != null)
{
var index = declarationList.IndexOf(beforeMember);
index = GetPreferredIndex(index, availableIndices, forward: false);
if (index != -1)
{
return index;
}
}
}
if (options.AutoInsertionLocation)
{
if (declarationList.IsEmpty())
{
return 0;
}
else if (declarationList.IsSorted(comparer))
{
var result = Array.BinarySearch(declarationList.ToArray(), declaration, comparer);
var index = GetPreferredIndex(result < 0 ? ~result : result, availableIndices, forward: true);
if (index != -1)
{
return index;
}
}
if (after != null)
{
var member = after(declarationList);
if (member != null)
{
var index = declarationList.IndexOf(member);
if (index >= 0)
{
index = GetPreferredIndex(index + 1, availableIndices, forward: true);
if (index != -1)
{
return index;
}
}
}
}
if (before != null)
{
var member = before(declarationList);
if (member != null)
{
var index = declarationList.IndexOf(member);
if (index >= 0)
{
index = GetPreferredIndex(index, availableIndices, forward: false);
if (index != -1)
{
return index;
}
}
}
}
}
}
// Otherwise, add the declaration to the end.
{
var index = GetPreferredIndex(declarationList.Count, availableIndices, forward: false);
if (index != -1)
{
return index;
}
}
return declarationList.Count;
}
}
}
......@@ -149,7 +149,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
before = BeforeDeclaration(declarationList, options, before)
Dim index = GetInsertionIndex(
declarationList, declaration, options, availableIndices, after, before)
declarationList, declaration, options, availableIndices,
VisualBasicDeclarationComparer.Instance, after, before)
If availableIndices IsNot Nothing Then
availableIndices.Insert(index, True)
......@@ -158,84 +159,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Return declarationList.Insert(index, declaration)
End Function
Private Function GetInsertionIndex(Of TDeclaration As SyntaxNode)(
declarationList As SyntaxList(Of TDeclaration),
declaration As TDeclaration,
options As CodeGenerationOptions,
availableIndices As IList(Of Boolean),
after As Func(Of SyntaxList(Of TDeclaration), TDeclaration),
before As Func(Of SyntaxList(Of TDeclaration), TDeclaration)) As Integer
If options IsNot Nothing Then
' Try to use AfterThisLocation
If options.AfterThisLocation IsNot Nothing Then
Dim afterMember = declarationList.LastOrDefault(Function(m) m.SpanStart <= options.AfterThisLocation.SourceSpan.Start)
If afterMember IsNot Nothing Then
Dim index = declarationList.IndexOf(afterMember)
index = GetPreferredIndex(index + 1, availableIndices, forward:=True)
If index <> -1 Then
Return index
End If
End If
End If
' Try to use BeforeThisLocation
If options.BeforeThisLocation IsNot Nothing Then
Dim beforeMember = declarationList.FirstOrDefault(Function(m) m.Span.End >= options.BeforeThisLocation.SourceSpan.End)
If beforeMember IsNot Nothing Then
Dim index = declarationList.IndexOf(beforeMember)
index = GetPreferredIndex(index, availableIndices, forward:=False)
If index <> -1 Then
Return index
End If
End If
End If
If options.AutoInsertionLocation Then
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
End If
If after IsNot Nothing Then
Dim member = after(declarationList)
If member IsNot Nothing Then
Dim index = declarationList.IndexOf(member)
index = GetPreferredIndex(index + 1, availableIndices, forward:=True)
If index <> -1 Then
Return index
End If
End If
End If
If before IsNot Nothing Then
Dim member = before(declarationList)
If member IsNot Nothing Then
Dim index = declarationList.IndexOf(member)
index = GetPreferredIndex(index, availableIndices, forward:=False)
If index <> -1 Then
Return index
End If
End If
End If
End If
End If
' Otherwise, add the method to the end.
Dim index1 = GetPreferredIndex(declarationList.Count, availableIndices, forward:=False)
If index1 <> -1 Then
Return index1
End If
Return declarationList.Count
End Function
Public Function GetDestination(destination As SyntaxNode) As CodeGenerationDestination
If destination IsNot Nothing Then
Select Case destination.Kind
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册