提交 40eb6953 编写于 作者: C CyrusNajmabadi

Remove dead code.

上级 3f4339f8
......@@ -472,23 +472,23 @@ public void TryMatchSingleWordPattern_CultureAwareSingleWordPreferCaseSensitiveE
}
}
private static IList<string> PartListToSubstrings(string identifier, StringBreaks parts)
private static ImmutableArray<string> PartListToSubstrings(string identifier, ArrayBuilder<TextSpan> parts)
{
var result = new List<string>();
for (int i = 0, n = parts.GetCount(); i < n; i++)
var result = ArrayBuilder<string>.GetInstance();
foreach (var span in parts)
{
var span = parts[i];
result.Add(identifier.Substring(span.Start, span.Length));
}
return result;
parts.Free();
return result.ToImmutableAndFree();
}
private static IList<string> BreakIntoCharacterParts(string identifier)
=> PartListToSubstrings(identifier, StringBreaker.BreakIntoCharacterParts(identifier));
private static ImmutableArray<string> BreakIntoCharacterParts(string identifier)
=> PartListToSubstrings(identifier, StringBreaker.GetCharacterParts(identifier));
private static IList<string> BreakIntoWordParts(string identifier)
=> PartListToSubstrings(identifier, StringBreaker.BreakIntoWordParts(identifier));
private static ImmutableArray<string> BreakIntoWordParts(string identifier)
=> PartListToSubstrings(identifier, StringBreaker.GetWordParts(identifier));
private static PatternMatch? TestNonFuzzyMatch(string candidate, string pattern)
{
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles
Namespace Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.UnitTests
......@@ -20,7 +21,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.UnitTests
End Function
Private Sub TestNameCreation(namingStyle As MutableNamingStyle, expectedName As String, ParamArray words As String())
Assert.Equal(expectedName, namingStyle.NamingStyle.CreateName(words))
Assert.Equal(expectedName, namingStyle.NamingStyle.CreateName(words.ToImmutableArray()))
End Sub
Private Sub TestNameCompliance(namingStyle As MutableNamingStyle, candidateName As String)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Words = System.Collections.Generic.IEnumerable<string>;
using Microsoft.CodeAnalysis.Text;
using Words = System.Collections.Immutable.ImmutableArray<string>;
namespace Microsoft.CodeAnalysis.CSharp.Completion.Providers
{
......@@ -13,56 +13,55 @@ internal partial class DeclarationNameCompletionProvider
{
internal class NameGenerator
{
internal static ImmutableArray<IEnumerable<string>> GetBaseNames(ITypeSymbol type)
internal static ImmutableArray<Words> GetBaseNames(ITypeSymbol type)
{
var baseName = TryRemoveInterfacePrefix(type);
using (var breaks = StringBreaker.BreakIntoWordParts(baseName))
{
return GetInterleavedPatterns(breaks, baseName);
}
var parts = StringBreaker.GetWordParts(baseName);
var result = GetInterleavedPatterns(parts, baseName);
parts.Free();
return result;
}
private static ImmutableArray<IEnumerable<string>> GetInterleavedPatterns(StringBreaks breaks, string baseName)
private static ImmutableArray<Words> GetInterleavedPatterns(ArrayBuilder<TextSpan> breaks, string baseName)
{
var result = ArrayBuilder<IEnumerable<string>>.GetInstance();
var breakCount = breaks.GetCount();
var result = ArrayBuilder<Words>.GetInstance();
var breakCount = breaks.Count;
result.Add(GetWords(0, breakCount, breaks, baseName));
for (int length = breakCount - 1; length > 0; length--)
for (int i = breakCount - 1; i > 0; i--)
{
// going forward
result.Add(GetLongestForwardSubsequence(length, breaks, baseName));
result.Add(GetLongestForwardSubsequence(i, breaks, baseName));
// going backward
result.Add(GetLongestBackwardSubsequence(length, breaks, baseName));
result.Add(GetLongestBackwardSubsequence(i, breaks, baseName));
}
return result.ToImmutable();
}
private static Words GetLongestBackwardSubsequence(int length, StringBreaks breaks, string baseName)
private static Words GetLongestBackwardSubsequence(int length, ArrayBuilder<TextSpan> breaks, string baseName)
{
var breakCount = breaks.GetCount();
var breakCount = breaks.Count;
var start = breakCount - length;
return GetWords(start, breakCount, breaks, baseName);
}
private static Words GetLongestForwardSubsequence(int length, StringBreaks breaks, string baseName)
private static Words GetLongestForwardSubsequence(int length, ArrayBuilder<TextSpan> breaks, string baseName)
{
var end = length;
return GetWords(0, end, breaks, baseName);
return GetWords(0, length, breaks, baseName);
}
private static Words GetWords(int start, int end, StringBreaks breaks, string baseName)
private static Words GetWords(int start, int end, ArrayBuilder<TextSpan> breaks, string baseName)
{
var result = ImmutableArray.Create<string>();
var result = ArrayBuilder<string>.GetInstance();
for (; start < end; start++)
{
var @break = breaks[start];
result = result.Add(baseName.Substring(@break.Start, @break.Length));
result.Add(baseName.Substring(@break.Start, @break.Length));
}
return result;
return result.ToImmutableAndFree();
}
private static string TryRemoveInterfacePrefix(ITypeSymbol type)
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -155,8 +156,8 @@ private ITypeSymbol UnwrapType(ITypeSymbol type, Compilation compilation)
return type;
}
private async Task<IEnumerable<(string, SymbolKind)>> GetRecommendedNamesAsync(
IEnumerable<IEnumerable<string>> baseNames,
private async Task<ImmutableArray<(string, SymbolKind)>> GetRecommendedNamesAsync(
ImmutableArray<ImmutableArray<string>> baseNames,
NameDeclarationInfo declarationInfo,
CSharpSyntaxContext context,
Document document,
......@@ -186,7 +187,7 @@ private ITypeSymbol UnwrapType(ITypeSymbol type, Compilation compilation)
}
}
return result.Select(kvp => (kvp.Key, kvp.Value));
return result.Select(kvp => (kvp.Key, kvp.Value)).ToImmutableArray();
}
CompletionItem CreateCompletionItem(string name, Glyph glyph, string sortText)
......
......@@ -11,10 +11,12 @@
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Semantics;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.InitializeParameter
......@@ -128,7 +130,7 @@ internal abstract partial class AbstractInitializeMemberFromParameterCodeRefacto
}
private IFieldSymbol CreateField(
IParameterSymbol parameter, ImmutableArray<NamingRule> rules, List<string> parameterNameParts)
IParameterSymbol parameter, ImmutableArray<NamingRule> rules, ImmutableArray<string> parameterNameParts)
{
foreach (var rule in rules)
{
......@@ -149,7 +151,7 @@ internal abstract partial class AbstractInitializeMemberFromParameterCodeRefacto
throw ExceptionUtilities.Unreachable;
}
private static string GenerateUniqueName(IParameterSymbol parameter, List<string> parameterNameParts, NamingRule rule)
private static string GenerateUniqueName(IParameterSymbol parameter, ImmutableArray<string> parameterNameParts, NamingRule rule)
{
// Determine an appropriate name to call the new field.
var containingType = parameter.ContainingType;
......@@ -163,7 +165,7 @@ private static string GenerateUniqueName(IParameterSymbol parameter, List<string
}
private IPropertySymbol CreateProperty(
IParameterSymbol parameter, ImmutableArray<NamingRule> rules, List<string> parameterNameParts)
IParameterSymbol parameter, ImmutableArray<NamingRule> rules, ImmutableArray<string> parameterNameParts)
{
foreach (var rule in rules)
{
......@@ -489,24 +491,23 @@ private IOperation TryFindFieldOrPropertyAssignmentStatement(IParameterSymbol pa
/// Get the individual words in the parameter name. This way we can generate
/// appropriate field/property names based on the user's preference.
/// </summary>
private List<string> GetParameterWordParts(IParameterSymbol parameter)
private ImmutableArray<string> GetParameterWordParts(IParameterSymbol parameter)
{
using (var breaks = StringBreaker.BreakIntoWordParts(parameter.Name))
{
return CreateWords(breaks, parameter.Name);
}
var parts = StringBreaker.GetWordParts(parameter.Name);
var result = CreateWords(parts, parameter.Name);
parts.Free();
return result;
}
private List<string> CreateWords(StringBreaks wordBreaks, string name)
private ImmutableArray<string> CreateWords(ArrayBuilder<TextSpan> parts, string name)
{
var result = new List<string>(wordBreaks.GetCount());
for (int i = 0, n = wordBreaks.GetCount(); i < n; i++)
var result = ArrayBuilder<string>.GetInstance(parts.Count);
foreach (var part in parts)
{
var br = wordBreaks[i];
result.Add(name.Substring(br.Start, br.Length));
result.Add(name.Substring(part.Start, part.Length));
}
return result;
return result.ToImmutableAndFree();
}
}
}
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
......@@ -59,7 +60,7 @@ internal partial struct NamingStyle
newName, newPrefix, newSuffix, newWordSeparator, newCapitalizationScheme);
}
public string CreateName(IEnumerable<string> words)
public string CreateName(ImmutableArray<string> words)
{
var wordsWithCasing = ApplyCapitalization(words);
var combinedWordsWithCasing = string.Join(WordSeparator, wordsWithCasing);
......
......@@ -34,7 +34,7 @@ private struct TextChunk : IDisposable
public TextChunk(string text, bool allowFuzzingMatching)
{
this.Text = text;
this.PatternHumps = StringBreaks.CreateFallbackList(text, word: false);
this.PatternHumps = StringBreaker.GetCharacterParts(text);
this.SimilarityChecker = allowFuzzingMatching
? WordSimilarityChecker.Allocate(text, substringsAreSimilar: false)
: null;
......
......@@ -211,7 +211,7 @@ private static bool ContainsUpperCaseLetter(string pattern)
matchedSpan: GetMatchedSpan(caseInsensitiveIndex, patternChunk.Text.Length));
}
candidateHumpsOpt = StringBreaks.CreateFallbackList(candidate, word: true);
candidateHumpsOpt = StringBreaker.GetWordParts(candidate);
for (int i = 0, n = candidateHumpsOpt.Count; i < n; i++)
{
var hump = TextSpan.FromBounds(candidateHumpsOpt[i].Start, candidateLength);
......@@ -229,7 +229,7 @@ private static bool ContainsUpperCaseLetter(string pattern)
// See if we can find a camel case match.
if (candidateHumpsOpt == null)
{
candidateHumpsOpt = StringBreaks.CreateFallbackList(candidate, word: true);
candidateHumpsOpt = StringBreaker.GetWordParts(candidate);
}
// Didn't have an exact/prefix match, or a high enough quality substring match.
......
......@@ -286,8 +286,8 @@ public static IMethodSymbol CreateEqualsMethod(this Compilation compilation, Imm
public static string GetLocalName(this INamedTypeSymbol containingType)
{
var parts = StringBreaker.BreakIntoWordParts(containingType.Name);
for (var i = parts.GetCount() - 1; i >= 0; i--)
var parts = StringBreaker.GetWordParts(containingType.Name);
for (var i = parts.Count - 1; i >= 0; i--)
{
var p = parts[i];
if (char.IsLetter(containingType.Name[p.Start]))
......
......@@ -9,72 +9,20 @@
namespace Microsoft.CodeAnalysis.Shared.Utilities
{
/// <summary>
/// Values returned from <see cref="StringBreaker"/> routines.
/// Optimized for short strings with a handful of spans.
/// Each span is encoded in two bitfields 'gap' and 'length' and these
/// bitfields are stored in a 32-bit bitmap.
/// Falls back to a <see cref="List{T}"/> if the encoding won't work.
/// </summary>
internal partial struct StringBreaks : IDisposable
internal static class StringBreaker
{
private readonly ArrayBuilder<TextSpan> _spans;
private readonly EncodedSpans _encodedSpans;
// These two values may be adjusted. The remaining constants are
// derived from them. The values are chosen to minimize the number
// of fallbacks during normal typing. With 5 total bits per span, we
// can encode up to 6 spans, each as long as 15 chars with 0 or 1 char
// gap. This is sufficient for the vast majority of framework symbols.
private const int BitsForGap = 1;
private const int BitsForLength = 4;
private const int BitsPerEncodedSpan = BitsForGap + BitsForLength;
private const int MaxShortSpans = 32 / BitsPerEncodedSpan;
private const int MaxGap = (1 << BitsForGap) - 1;
private const int MaxLength = (1 << BitsForLength) - 1;
public static StringBreaks CreateSpans(string text, bool word)
{
Debug.Assert(text != null);
return TryEncodeSpans(text, word, out var encodedSpans)
? new StringBreaks(encodedSpans)
: new StringBreaks(CreateFallbackList(text, word));
}
private static bool TryEncodeSpans(string text, bool word, out EncodedSpans encodedSpans)
{
encodedSpans = default(EncodedSpans);
for (int start = 0, b = 0; start < text.Length;)
{
var span = StringBreaker.GenerateSpan(text, start, word);
if (span.IsEmpty)
{
// All done
break;
}
int gap = span.Start - start;
Debug.Assert(gap >= 0, "Bad generator.");
if (b >= MaxShortSpans ||
span.Length > MaxLength ||
gap > MaxGap)
{
// Too many spans, or span cannot be encoded.
return false;
}
encodedSpans[b++] = Encode(gap, span.Length);
start = span.End;
}
/// <summary>
/// Breaks an identifier string into constituent parts.
/// </summary>
public static ArrayBuilder<TextSpan> GetWordParts(string identifier)
=> GetParts(identifier, word: true);
return true;
}
public static ArrayBuilder<TextSpan> GetCharacterParts(string identifier)
=> GetParts(identifier, word: false);
internal static ArrayBuilder<TextSpan> CreateFallbackList(string text, bool word)
public static ArrayBuilder<TextSpan> GetParts(string text, bool word)
{
var list = ArrayBuilder<TextSpan>.GetInstance();
var parts = ArrayBuilder<TextSpan>.GetInstance();
for (int start = 0; start < text.Length;)
{
var span = StringBreaker.GenerateSpan(text, start, word);
......@@ -86,108 +34,13 @@ internal static ArrayBuilder<TextSpan> CreateFallbackList(string text, bool word
Debug.Assert(span.Start >= start, "Bad generator.");
list.Add(span);
parts.Add(span);
start = span.End;
}
return list;
}
private StringBreaks(EncodedSpans encodedSpans)
{
_encodedSpans = encodedSpans;
_spans = null;
}
private StringBreaks(ArrayBuilder<TextSpan> spans)
{
_encodedSpans = default(EncodedSpans);
_spans = spans;
}
public void Dispose()
{
_spans?.Free();
}
public int GetCount()
{
if (_spans != null)
{
return _spans.Count;
}
int i;
for (i = 0; i < MaxShortSpans; i++)
{
if (_encodedSpans[i] == 0)
{
break;
}
}
return i;
}
public TextSpan this[int index]
{
get
{
if (index < 0)
{
throw new IndexOutOfRangeException(nameof(index));
}
if (_spans != null)
{
return _spans[index];
}
for (int i = 0, start = 0; i < MaxShortSpans; i++)
{
byte b = _encodedSpans[i];
if (b == 0)
{
break;
}
start += DecodeGap(b);
int length = DecodeLength(b);
if (i == index)
{
return new TextSpan(start, length);
}
start += length;
}
throw new IndexOutOfRangeException(nameof(index));
}
}
private static byte Encode(int gap, int length)
{
Debug.Assert(gap >= 0 && gap <= MaxGap);
Debug.Assert(length >= 0 && length <= MaxLength);
return unchecked((byte)((gap << BitsForLength) | length));
return parts;
}
private static int DecodeLength(byte b) => b & MaxLength;
private static int DecodeGap(byte b) => b >> BitsForLength;
}
internal static class StringBreaker
{
/// <summary>
/// Breaks an identifier string into constituent parts.
/// </summary>
public static StringBreaks BreakIntoWordParts(string identifier)
=> StringBreaks.CreateSpans(identifier, word: true);
public static StringBreaks BreakIntoCharacterParts(string identifier)
=> StringBreaks.CreateSpans(identifier, word: false);
public static TextSpan GenerateSpan(string identifier, int wordStart, bool word)
{
int length = identifier.Length;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
namespace Microsoft.CodeAnalysis.Shared.Utilities
{
internal partial struct StringBreaks
{
private struct EncodedSpans
{
private const uint Mask = (1u << BitsPerEncodedSpan) - 1u;
private uint _value;
public byte this[int index]
{
get
{
Debug.Assert(index >= 0 && index < MaxShortSpans);
return (byte)((_value >> (index * BitsPerEncodedSpan)) & Mask);
}
set
{
Debug.Assert(index >= 0 && index < MaxShortSpans);
int shift = index * BitsPerEncodedSpan;
_value = (_value & ~(Mask << shift)) | ((uint)value << shift);
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册