Esempio n. 1
0
        private async Task CreateSpellCheckCodeIssueAsync(CodeFixContext context, TSimpleName nameNode, string nameText, CancellationToken cancellationToken)
        {
            var document = context.Document;
            var service  = CompletionService.GetService(document);

            // Disable snippets from ever appearing in the completion items. It's
            // very unlikely the user would ever mispell a snippet, then use spell-
            // checking to fix it, then try to invoke the snippet.
            var originalOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            var options = originalOptions.WithChangedOption(CompletionOptions.SnippetsBehavior, document.Project.Language, SnippetsRule.NeverInclude);

            var completionList = await service.GetCompletionsAsync(
                document, nameNode.SpanStart, options : options, cancellationToken : cancellationToken).ConfigureAwait(false);

            if (completionList == null)
            {
                return;
            }

            var similarityChecker = WordSimilarityChecker.Allocate(nameText, substringsAreSimilar: true);

            try
            {
                await CheckItemsAsync(
                    context, nameNode, nameText,
                    completionList, similarityChecker).ConfigureAwait(false);
            }
            finally
            {
                similarityChecker.Free();
            }
        }
Esempio n. 2
0
        private SearchQuery(string name, SearchKind kind)
        {
            Name = name ?? throw new ArgumentNullException(nameof(name));
            Kind = kind;

            switch (kind)
            {
            case SearchKind.Exact:
                _predicate = s => StringComparer.Ordinal.Equals(name, s);
                break;

            case SearchKind.ExactIgnoreCase:
                _predicate = s => CaseInsensitiveComparison.Comparer.Equals(name, s);
                break;

            case SearchKind.Fuzzy:
                // Create a single WordSimilarityChecker and capture a delegate reference to
                // its 'AreSimilar' method. That way we only create the WordSimilarityChecker
                // once and it can cache all the information it needs while it does the AreSimilar
                // check against all the possible candidates.
                _wordSimilarityChecker = WordSimilarityChecker.Allocate(name, substringsAreSimilar: false);
                _predicate             = _wordSimilarityChecker.AreSimilar;
                break;

            default:
                throw ExceptionUtilities.UnexpectedValue(kind);
            }
        }
Esempio n. 3
0
 public TextChunk(string text, bool allowFuzzingMatching)
 {
     this.Text              = text;
     this.CharacterSpans    = StringBreaker.BreakIntoCharacterParts(text);
     this.SimilarityChecker = allowFuzzingMatching
         ? WordSimilarityChecker.Allocate(text, substringsAreSimilar: false)
         : null;
 }
            public TextChunk(string text, bool allowFuzzingMatching)
            {
                this.Text              = text;
                this.PatternHumps      = StringBreaker.GetCharacterParts(text);
                this.SimilarityChecker = allowFuzzingMatching
                    ? WordSimilarityChecker.Allocate(text, substringsAreSimilar: false)
                    : null;

                IsLowercase = !ContainsUpperCaseLetter(text);
            }
            public TextChunk(string text, bool allowFuzzingMatching)
            {
                this.Text    = text;
                PatternHumps = TemporaryArray <TextSpan> .Empty;
                StringBreaker.AddCharacterParts(text, ref PatternHumps);

                this.SimilarityChecker = allowFuzzingMatching
                    ? WordSimilarityChecker.Allocate(text, substringsAreSimilar: false)
                    : null;

                IsLowercase = !ContainsUpperCaseLetter(text);
            }
Esempio n. 6
0
            public async Task <ImmutableArray <SymbolResult <ISymbol> > > FindDeclarationsAsync(
                string name,
                TSimpleNameSyntax nameNode,
                SymbolFilter filter
                )
            {
                if (name != null && string.IsNullOrWhiteSpace(name))
                {
                    return(ImmutableArray <SymbolResult <ISymbol> > .Empty);
                }

                using var query = Exact
                    ? SearchQuery.Create(name, ignoreCase: true)
                    : SearchQuery.CreateFuzzy(name);
                var symbols = await FindDeclarationsAsync(filter, query).ConfigureAwait(false);

                if (Exact)
                {
                    // We did an exact, case insensitive, search.  Case sensitive matches should
                    // be preferred though over insensitive ones.
                    return(symbols.SelectAsArray(
                               s =>
                               SymbolResult.Create(s.Name, nameNode, s, weight: s.Name == name ? 0 : 1)
                               ));
                }

                // TODO(cyrusn): It's a shame we have to compute this twice.  However, there's no
                // great way to store the original value we compute because it happens deep in the
                // compiler bowels when we call FindDeclarations.
                var similarityChecker = WordSimilarityChecker.Allocate(
                    name,
                    substringsAreSimilar: false
                    );

                var result = symbols.SelectAsArray(
                    s =>
                {
                    var areSimilar = similarityChecker.AreSimilar(s.Name, out var matchCost);

                    Debug.Assert(areSimilar);
                    return(SymbolResult.Create(s.Name, nameNode, s, matchCost));
                }
                    );

                similarityChecker.Free();

                return(result);
            }
        private async Task CreateSpellCheckCodeIssueAsync(
            CodeFixContext context, SyntaxToken nameToken, bool isGeneric, CancellationToken cancellationToken)
        {
            var document = context.Document;
            var service  = CompletionService.GetService(document);

            // Disable snippets and unimported types from ever appearing in the completion items.
            // -    It's very unlikely the user would ever misspell a snippet, then use spell-checking to fix it,
            //      then try to invoke the snippet.
            // -    We believe spell-check should only compare what you have typed to what symbol would be offered here.
            var options = CompletionOptions.Default with
            {
                HideAdvancedMembers = context.Options.HideAdvancedMembers,
                SnippetsBehavior    = SnippetsRule.NeverInclude,
                ShowItemsFromUnimportedNamespaces = false,
                TargetTypedCompletionFilter       = false,
                ExpandedCompletionBehavior        = ExpandedCompletionMode.NonExpandedItemsOnly
            };

            var passThroughOptions = document.Project.Solution.Options;

            var completionList = await service.GetCompletionsAsync(
                document, nameToken.SpanStart, options, passThroughOptions, cancellationToken : cancellationToken).ConfigureAwait(false);

            if (completionList.Items.IsEmpty)
            {
                return;
            }

            var nameText          = nameToken.ValueText;
            var similarityChecker = WordSimilarityChecker.Allocate(nameText, substringsAreSimilar: true);

            try
            {
                await CheckItemsAsync(
                    context, nameToken, isGeneric,
                    completionList, similarityChecker).ConfigureAwait(false);
            }
            finally
            {
                similarityChecker.Free();
            }
        }
Esempio n. 8
0
        private async Task CreateSpellCheckCodeIssueAsync(
            CodeFixContext context, SyntaxToken nameToken, bool isGeneric, CancellationToken cancellationToken)
        {
            var document = context.Document;
            var service  = CompletionService.GetService(document);

            // Disable snippets and unimported types from ever appearing in the completion items.
            // -    It's very unlikely the user would ever misspell a snippet, then use spell-checking to fix it,
            //      then try to invoke the snippet.
            // -    We believe spell-check should only compare what you have typed to what symbol would be offered here.
            var originalOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            var options = originalOptions
                          .WithChangedOption(CompletionOptions.SnippetsBehavior, document.Project.Language, SnippetsRule.NeverInclude)
                          .WithChangedOption(CompletionOptions.ShowItemsFromUnimportedNamespaces, document.Project.Language, false)
                          .WithChangedOption(CompletionServiceOptions.IsExpandedCompletion, false);

            var completionList = await service.GetCompletionsAsync(
                document, nameToken.SpanStart, options : options, cancellationToken : cancellationToken).ConfigureAwait(false);

            if (completionList == null)
            {
                return;
            }

            var nameText          = nameToken.ValueText;
            var similarityChecker = WordSimilarityChecker.Allocate(nameText, substringsAreSimilar: true);

            try
            {
                await CheckItemsAsync(
                    context, nameToken, isGeneric,
                    completionList, similarityChecker).ConfigureAwait(false);
            }
            finally
            {
                similarityChecker.Free();
            }
        }