Exemple #1
0
        public static async Task <ImmutableArray <Diagnostic> > AnalyzeSpellingAsync(
            Project project,
            SpellingData spellingData,
            SpellingFixerOptions options        = null,
            CancellationToken cancellationToken = default)
        {
            ISpellingService service = MefWorkspaceServices.Default.GetService <ISpellingService>(project.Language);

            if (service == null)
            {
                return(ImmutableArray <Diagnostic> .Empty);
            }

            ImmutableArray <Diagnostic> .Builder diagnostics = ImmutableArray.CreateBuilder <Diagnostic>();

            foreach (Document document in project.Documents)
            {
                if (!document.SupportsSyntaxTree)
                {
                    continue;
                }

                ImmutableArray <Diagnostic> diagnostics2 = await AnalyzeSpellingAsync(
                    service,
                    document,
                    spellingData,
                    options,
                    cancellationToken)
                                                           .ConfigureAwait(false);

                diagnostics.AddRange(diagnostics2);
            }

            return(diagnostics.ToImmutableArray());
        }
Exemple #2
0
        public Spellchecker(
            SpellingData data,
            Regex wordRegex             = null,
            SpellcheckerOptions options = null)
        {
            Data      = data;
            WordRegex = wordRegex ?? _wordRegex;
            Options   = options ?? SpellcheckerOptions.Default;

            _splitRegex = new Regex("-|" + _splitCasePattern, RegexOptions.IgnorePatternWhitespace);
        }
Exemple #3
0
        public SpellingFixer(
            Solution solution,
            SpellingData spellingData,
            IFormatProvider formatProvider = null,
            SpellingFixerOptions options   = null)
        {
            Workspace = solution.Workspace;

            SpellingData   = spellingData;
            FormatProvider = formatProvider;
            Options        = options ?? SpellingFixerOptions.Default;
        }
Exemple #4
0
        public SpellingAnalysisContext(
            Action <Diagnostic> reportDiagnostic,
            SpellingData spellingData,
            SpellingFixerOptions options,
            CancellationToken cancellationToken)
        {
            SpellingData      = spellingData;
            Options           = options;
            CancellationToken = cancellationToken;

            _reportDiagnostic = reportDiagnostic;

            _spellchecker = new Spellchecker(spellingData, options: new SpellcheckerOptions(options.SplitMode, options.MinWordLength, options.MaxWordLength));
        }
Exemple #5
0
        public static async Task <ImmutableArray <Diagnostic> > AnalyzeSpellingAsync(
            ISpellingService service,
            Document document,
            SpellingData spellingData,
            SpellingFixerOptions options        = null,
            CancellationToken cancellationToken = default)
        {
            SyntaxTree tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

            if (tree == null)
            {
                return(ImmutableArray <Diagnostic> .Empty);
            }

            if (!options.IncludeGeneratedCode &&
                GeneratedCodeUtility.IsGeneratedCode(tree, f => service.SyntaxFacts.IsComment(f), cancellationToken))
            {
                return(ImmutableArray <Diagnostic> .Empty);
            }

            SyntaxNode root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            return(service.AnalyzeSpelling(root, spellingData, options, cancellationToken));
        }
        public static ImmutableArray <string> SwapLetters(string value, SpellingData spellingData)
        {
            int length = value.Length;

            if (length < 4)
            {
                return(ImmutableArray <string> .Empty);
            }

            ImmutableArray <string> .Builder fixes = null;

            char[] arr = value.ToCharArray();

            Array.Sort(arr, (x, y) => x.CompareTo(y));

            var key = new string(arr);

            if (!spellingData.CharMap.TryGetValue(key, out ImmutableHashSet <string> values))
            {
                return(ImmutableArray <string> .Empty);
            }

            int maxCharDiff = (length <= 6) ? 2 : 3;

            foreach (string value2 in values)
            {
                if (length == value2.Length)
                {
                    int charDiff       = 0;
                    int diffStartIndex = -1;
                    int diffEndIndex   = -1;

                    for (int i = 0; i < length; i++)
                    {
                        if (value[i] != value2[i])
                        {
                            if (diffStartIndex == -1)
                            {
                                diffStartIndex = i;
                            }
                            else
                            {
                                diffEndIndex = i;
                            }

                            charDiff++;
                        }

                        if (i > 1 &&
                            charDiff > maxCharDiff)
                        {
                            return(ImmutableArray <string> .Empty);
                        }
                    }

                    if (diffEndIndex - diffStartIndex <= 2)
                    {
                        (fixes ??= ImmutableArray.CreateBuilder <string>()).Add(value2);
                    }
                }
            }

            return(fixes?.ToImmutableArray() ?? ImmutableArray <string> .Empty);
        }
        public static ImmutableArray <string> Fuzzy(
            string value,
            SpellingData spellingData,
            CancellationToken cancellationToken = default)
        {
            int length = value.Length;

            if (length <= 3)
            {
                return(ImmutableArray <string> .Empty);
            }

            ImmutableHashSet <string> .Builder matches = null;
            WordCharMap map         = spellingData.CharIndexMap;
            WordCharMap reversedMap = spellingData.ReversedCharIndexMap;
            var         intersects  = new ImmutableHashSet <string> [length];

            int i = 0;

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (!map.TryGetValue(value, i, out ImmutableHashSet <string> values))
                {
                    break;
                }

                ImmutableHashSet <string> intersect = (i == 0)
                    ? values
                    : intersects[i - 1].Intersect(values);

                if (i == length - 2)
                {
                    TryAddMatches(intersect, length, ref matches);
                }
                else if (i == length - 1)
                {
                    TryAddMatches(intersect, length + 1, ref matches);

                    break;
                }

                if (intersect.Count == 0)
                {
                    break;
                }

                intersects[i] = intersect;
                i++;
            }

            int j = length - 1;

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (!reversedMap.TryGetValue(value[j], length - j - 1, out ImmutableHashSet <string> values))
                {
                    break;
                }

                ImmutableHashSet <string> intersect = (j == length - 1)
                    ? values
                    : values.Intersect(intersects[j + 1]);

                if (j == 1)
                {
                    TryAddMatches(intersect, length, ref matches);
                }
                else if (j == 0)
                {
                    TryAddMatches(intersect, length + 1, ref matches);
                    break;
                }

                if (intersect.Count == 0)
                {
                    break;
                }

                int diff = j - i;

                if (diff <= 0)
                {
                    if (TryAddMatches(intersects[j - 1].Intersect(intersect), length + 1, ref matches))
                    {
                        break;
                    }
                }
                else if (diff == 1 &&
                         j > 1)
                {
                    if (TryAddMatches(intersects[j - 2].Intersect(intersect), length - 1, length, ref matches))
                    {
                        break;
                    }
                }

                intersects[j] = intersect;
                j--;
            }

            if (matches == null)
            {
                return(ImmutableArray <string> .Empty);
            }

            return(matches
                   .OrderBy(f => f)
                   .ToImmutableArray());
        }
Exemple #8
0
 public abstract ImmutableArray <Diagnostic> AnalyzeSpelling(
     SyntaxNode node,
     SpellingData spellingData,
     SpellingFixerOptions options,
     CancellationToken cancellationToken);
Exemple #9
0
 public abstract DiagnosticAnalyzer CreateAnalyzer(
     SpellingData spellingData,
     SpellingFixerOptions options);