Esempio n. 1
0
        private async Task CheckItemsAsync(
            CodeFixContext context, SyntaxToken nameToken, bool isGeneric,
            CompletionList completionList, WordSimilarityChecker similarityChecker)
        {
            var document          = context.Document;
            var cancellationToken = context.CancellationToken;

            var onlyConsiderGenerics = isGeneric;
            var results = new MultiDictionary <double, string>();

            foreach (var item in completionList.ItemsList)
            {
                if (onlyConsiderGenerics && !IsGeneric(item))
                {
                    continue;
                }

                var candidateText = item.FilterText;
                if (!similarityChecker.AreSimilar(candidateText, out var matchCost))
                {
                    continue;
                }

                var insertionText = await GetInsertionTextAsync(document, item, cancellationToken : cancellationToken).ConfigureAwait(false);

                results.Add(matchCost, insertionText);
            }

            var nameText    = nameToken.ValueText;
            var codeActions = results.OrderBy(kvp => kvp.Key)
                              .SelectMany(kvp => kvp.Value.Order())
                              .Where(t => t != nameText)
                              .Take(3)
                              .Select(n => CreateCodeAction(nameToken, nameText, n, document))
                              .ToImmutableArrayOrEmpty <CodeAction>();

            if (codeActions.Length > 1)
            {
                // Wrap the spell checking actions into a single top level suggestion
                // so as to not clutter the list.
                context.RegisterCodeFix(
                    CodeAction.CreateWithPriority(
                        CodeActionPriority.Low,
                        string.Format(FeaturesResources.Fix_typo_0, nameText),
                        codeActions,
                        isInlinable: true),
                    context.Diagnostics);
            }
            else
            {
                context.RegisterFixes(codeActions, context.Diagnostics);
            }
        }
        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 onlyConsiderGenerics = IsGeneric(nameNode);
            var results = new MultiDictionary <double, string>();

            using (var similarityChecker = new WordSimilarityChecker(nameText, substringsAreSimilar: true))
            {
                foreach (var item in completionList.Items)
                {
                    if (onlyConsiderGenerics && !IsGeneric(item))
                    {
                        continue;
                    }

                    var    candidateText = item.FilterText;
                    double matchCost;
                    if (!similarityChecker.AreSimilar(candidateText, out matchCost))
                    {
                        continue;
                    }

                    var insertionText = await GetInsertionTextAsync(document, item, cancellationToken : cancellationToken).ConfigureAwait(false);

                    results.Add(matchCost, insertionText);
                }
            }

            var matches = results.OrderBy(kvp => kvp.Key)
                          .SelectMany(kvp => kvp.Value.Order())
                          .Where(t => t != nameText)
                          .Take(3)
                          .Select(n => CreateCodeAction(nameNode, nameText, n, document));

            context.RegisterFixes(matches, context.Diagnostics);
        }
        public async Task RunTestsAsync(IEnumerable <ITest> selectedTests, TestExecutionOptions options, CancellationToken cancellationToken)
        {
            this.cancellationToken = cancellationToken;
            GroupTestsByProject(selectedTests);

            ClearTasks();
            ShowUnitTestsPad();
            ShowOutputPad();

            ResetTestResults();
            saveAllFilesCommand.SaveAllFiles();

            // Run the build, if necessary:
            var projectsToBuild = testsByProject.Keys.Where(p => p.IsBuildNeededBeforeTestRun).Select(p => p.Project).ToList();

            if (projectsToBuild.Count > 0)
            {
                using (cancellationToken.Register(buildService.CancelBuild)) {
                    var buildOptions = new BuildOptions(BuildTarget.Build);
                    buildOptions.BuildDetection = BuildOptions.BuildOnExecute;
                    var buildResults = await buildService.BuildAsync(projectsToBuild, buildOptions);

                    if (buildResults.Result != BuildResultCode.Success)
                    {
                        return;
                    }
                }
            }

            cancellationToken.ThrowIfCancellationRequested();
            using (IProgressMonitor progressMonitor = statusBarService.CreateProgressMonitor(cancellationToken)) {
                int projectsLeftToRun = testsByProject.Count;
                foreach (IGrouping <ITestProject, ITest> g in testsByProject.OrderBy(g => g.Key.DisplayName))
                {
                    currentProjectBeingTested = g.Key;
                    progressMonitor.TaskName  = GetProgressMonitorLabel(currentProjectBeingTested);
                    progressMonitor.Progress  = GetProgress(projectsLeftToRun);
                    using (testProgressMonitor = progressMonitor.CreateSubTask(1.0 / testsByProject.Count)) {
                        using (ITestRunner testRunner = currentProjectBeingTested.CreateTestRunner(options)) {
                            testRunner.TestFinished += testRunner_TestFinished;
                            var writer = new MessageViewCategoryTextWriter(testService.UnitTestMessageView);
                            await testRunner.RunAsync(g, testProgressMonitor, writer, testProgressMonitor.CancellationToken);
                        }
                    }
                    projectsLeftToRun--;
                    progressMonitor.CancellationToken.ThrowIfCancellationRequested();
                }
            }

            ShowErrorList();
        }
Esempio n. 4
0
        private async Task CreateSpellCheckCodeIssueAsync(CodeFixContext context, TSimpleName nameNode, string nameText, CancellationToken cancellationToken)
        {
            var document       = context.Document;
            var service        = CompletionService.GetService(document);
            var completionList = await service.GetCompletionsAsync(
                document, nameNode.SpanStart, cancellationToken : cancellationToken).ConfigureAwait(false);

            if (completionList == null)
            {
                return;
            }

            var onlyConsiderGenerics = IsGeneric(nameNode);
            var results = new MultiDictionary <double, string>();

            using (var similarityChecker = new WordSimilarityChecker(nameText, substringsAreSimilar: true))
            {
                foreach (var item in completionList.Items)
                {
                    if (onlyConsiderGenerics && !IsGeneric(item))
                    {
                        continue;
                    }

                    var    candidateText = item.FilterText;
                    double matchCost;
                    if (!similarityChecker.AreSimilar(candidateText, out matchCost))
                    {
                        continue;
                    }

                    var insertionText = await GetInsertionTextAsync(document, item, cancellationToken : cancellationToken).ConfigureAwait(false);

                    results.Add(matchCost, insertionText);
                }
            }

            var matches = results.OrderBy(kvp => kvp.Key)
                          .SelectMany(kvp => kvp.Value.Order())
                          .Where(t => t != nameText)
                          .Take(3)
                          .Select(n => CreateCodeAction(nameNode, nameText, n, document));

            context.RegisterFixes(matches, context.Diagnostics);
        }
Esempio n. 5
0
        private async Task CreateSpellCheckCodeIssueAsync(CodeFixContext context, TSimpleName nameNode, string nameText, CancellationToken cancellationToken)
        {
            var document       = context.Document;
            var completionList = await CompletionService.GetCompletionListAsync(
                document, nameNode.SpanStart, CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo(), cancellationToken : cancellationToken).ConfigureAwait(false);

            if (completionList == null)
            {
                return;
            }

            var completionRules      = CompletionService.GetCompletionRules(document);
            var onlyConsiderGenerics = IsGeneric(nameNode);
            var results = new MultiDictionary <double, string>();

            int closeMatchThreshold = EditDistance.GetCloseMatchThreshold(nameText);

            foreach (var item in completionList.Items)
            {
                if (onlyConsiderGenerics && !IsGeneric(item))
                {
                    continue;
                }

                var    candidateText = item.FilterText;
                double matchCost;
                if (!EditDistance.IsCloseMatch(nameText, candidateText, closeMatchThreshold, out matchCost))
                {
                    continue;
                }

                var insertionText = completionRules.GetTextChange(item).NewText;
                results.Add(matchCost, insertionText);
            }

            var matches = results.OrderBy(kvp => kvp.Key)
                          .SelectMany(kvp => kvp.Value.Order())
                          .Where(t => t != nameText)
                          .Take(3)
                          .Select(n => CreateCodeAction(nameNode, nameText, n, document));

            context.RegisterFixes(matches, context.Diagnostics);
        }