protected void VerifyCommitCharacters(string initialMarkup, string textTypedSoFar, char[] validChars, char[] invalidChars = null)
        {
            Assert.NotNull(validChars);
            invalidChars = invalidChars ?? new [] { 'x' };

            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(initialMarkup))
            {
                var hostDocument = workspace.DocumentWithCursor;
                var documentId   = workspace.GetDocumentId(hostDocument);
                var document     = workspace.CurrentSolution.GetDocument(documentId);
                var position     = hostDocument.CursorPosition.Value;

                var completionList = GetCompletionList(document, position, CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo());
                var item           = completionList.Items.First(i => i.DisplayText.StartsWith(textTypedSoFar));

                var completionService = document.Project.LanguageServices.GetService <ICompletionService>();
                var completionRules   = completionService.GetCompletionRules();

                foreach (var ch in validChars)
                {
                    Assert.True(completionRules.IsCommitCharacter(item, ch, textTypedSoFar), $"Expected '{ch}' to be a commit character");
                }

                foreach (var ch in invalidChars)
                {
                    Assert.False(completionRules.IsCommitCharacter(item, ch, textTypedSoFar), $"Expected '{ch}' NOT to be a commit character");
                }
            }
        }
        private void VerifyProviderCommitCheckResults(Document document, int position, string itemToCommit, string expectedCodeAfterCommit, char?commitCharOpt, string textTypedSoFar)
        {
            var textBuffer   = WorkspaceFixture.Workspace.Documents.Single().TextBuffer;
            var textSnapshot = textBuffer.CurrentSnapshot.AsText();

            var items     = GetCompletionList(document, position, CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo()).Items;
            var firstItem = items.First(i => CompareItems(i.DisplayText, itemToCommit));

            var completionRules = GetCompletionRules(document);
            var commitChar      = commitCharOpt ?? '\t';

            var text = document.GetTextAsync().Result;

            if (commitChar == '\t' || completionRules.IsCommitCharacter(firstItem, commitChar, textTypedSoFar))
            {
                var textChange = completionRules.GetTextChange(firstItem, commitChar, textTypedSoFar);

                // Adjust TextChange to include commit character, so long as it isn't TAB.
                if (commitChar != '\t')
                {
                    textChange = new TextChange(textChange.Span, textChange.NewText.TrimEnd(commitChar) + commitChar);
                }

                text = text.WithChanges(textChange);
            }
            else
            {
                // nothing was commited, but we should insert the commit character.
                var textChange = new TextChange(new TextSpan(firstItem.FilterSpan.End, 0), commitChar.ToString());
                text = text.WithChanges(textChange);
            }

            Assert.Equal(expectedCodeAfterCommit, text.ToString());
        }
        private CompletionItemGroup GetCompletionGroup(string markup)
        {
            var    provider = CreateProvider();
            string code;
            int    cursorPosition;

            MarkupTestFile.GetPosition(markup, out code, out cursorPosition);
            var document    = CreateDocument(code);
            var triggerInfo = CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo();

            return(provider.GetGroupAsync(document, cursorPosition, triggerInfo, CancellationToken.None).Result);
        }
        public async Task <CompletionResult> GetCompletionData(int position, char?triggerChar, bool useSignatureHelp)
        {
            IList <ICompletionDataEx> completionData   = null;
            IOverloadProviderEx       overloadProvider = null;
            bool?isCompletion = null;

            var document = _roslynHost.GetDocument(_documentId);

            if (useSignatureHelp || triggerChar != null)
            {
                var signatureHelpProvider = _roslynHost.GetService <ISignatureHelpProvider>();
                var isSignatureHelp       = useSignatureHelp || signatureHelpProvider.IsTriggerCharacter(triggerChar.Value);
                if (isSignatureHelp)
                {
                    var signatureHelp = await signatureHelpProvider.GetItemsAsync(
                        document,
                        position,
                        new SignatureHelpTriggerInfo(
                            useSignatureHelp
                                ? SignatureHelpTriggerReason.InvokeSignatureHelpCommand
                                : SignatureHelpTriggerReason.TypeCharCommand, triggerChar))
                                        .ConfigureAwait(false);

                    if (signatureHelp != null)
                    {
                        overloadProvider = new RoslynOverloadProvider(signatureHelp);
                    }
                }
                else
                {
                    isCompletion = await CompletionService.IsCompletionTriggerCharacterAsync(document, position - 1).ConfigureAwait(false);
                }
            }

            if (overloadProvider == null && isCompletion != false)
            {
                var data = await CompletionService.GetCompletionListAsync(
                    document,
                    position,
                    triggerChar != null
                    ?CompletionTriggerInfo.CreateTypeCharTriggerInfo(triggerChar.Value)
                    : CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo()
                    ).ConfigureAwait(false);

                completionData = data?.Items.Select(item => new RoslynCompletionData(item, _snippetService.SnippetManager)).ToArray <ICompletionDataEx>()
                                 ?? Array.Empty <ICompletionDataEx>();
            }

            return(new CompletionResult(completionData, overloadProvider));
        }
        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>();

            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 = 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);
        }
        private void VerifyCustomCommitProviderCheckResults(Document document, string codeBeforeCommit, int position, string itemToCommit, string expectedCodeAfterCommit, char? commitChar)
        {
            var textBuffer = WorkspaceFixture.Workspace.Documents.Single().TextBuffer;

            var items = GetCompletionList(document, position, CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo()).Items;
            var firstItem = items.First(i => CompareItems(i.DisplayText, itemToCommit));

            var customCommitCompletionProvider = CompletionProvider as ICustomCommitCompletionProvider;
            if (customCommitCompletionProvider != null)
            {
                var completionRules = GetCompletionRules(document);
                var textView = WorkspaceFixture.Workspace.Documents.Single().GetTextView();
                VerifyCustomCommitWorker(customCommitCompletionProvider, firstItem, completionRules, textView, textBuffer, codeBeforeCommit, expectedCodeAfterCommit, commitChar);
            }
            else
            {
                throw new Exception();
            }
        }
예제 #7
0
        public IList <CompletionItem> GetCompletion(int position, string text = null)
        {
            CompletionTriggerInfo triggerInfo;

            if (!string.IsNullOrEmpty(text))
            {
                triggerInfo = CompletionTriggerInfo.CreateTypeCharTriggerInfo(char.Parse(text));
            }
            else
            {
                triggerInfo = CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo();
            }

            var groups = _completionService.GetGroups(GetCurrentDocument(), position,
                                                      triggerInfo,
                                                      _completionService.GetDefaultCompletionProviders(),
                                                      CancellationToken.None);

            return((groups ?? Enumerable.Empty <CompletionItemGroup>()).SelectMany(t => t.Items).OrderByDescending(t => t.SortText).ToArray());
        }
예제 #8
0
        public async Task <CompletionResult> GetCompletionData(int position, char?triggerChar)
        {
            IList <ICompletionDataEx> completionData   = null;
            IOverloadProvider         overloadProvider = null;
            bool?isCompletion = null;

            if (triggerChar != null)
            {
                var isSignatureHelp = await _interactiveManager.IsSignatureHelpTriggerCharacter(position - 1).ConfigureAwait(false);

                if (isSignatureHelp)
                {
                    var signatureHelp = await _interactiveManager.GetSignatureHelp(
                        new SignatureHelpTriggerInfo(SignatureHelpTriggerReason.TypeCharCommand, triggerChar.Value), position)
                                        .ConfigureAwait(false);

                    if (signatureHelp != null)
                    {
                        overloadProvider = new RoslynOverloadProvider(signatureHelp);
                    }
                }
                else
                {
                    isCompletion = await _interactiveManager.IsCompletionTriggerCharacter(position - 1).ConfigureAwait(false);
                }
            }

            if (overloadProvider == null && isCompletion != false)
            {
                var items = await _interactiveManager.GetCompletion(
                    triggerChar != null
                    ?CompletionTriggerInfo.CreateTypeCharTriggerInfo(triggerChar.Value)
                    : CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo(),
                    position).ConfigureAwait(false);

                completionData = items.Select(item => new AvalonEditCompletionData(item)).ToArray <ICompletionDataEx>();
            }

            return(new CompletionResult(completionData, overloadProvider));
        }
        protected void VerifySendEnterThroughToEnter(string initialMarkup, string textTypedSoFar, bool sendThroughEnterEnabled, bool expected)
        {
            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(initialMarkup))
            {
                var hostDocument = workspace.DocumentWithCursor;
                var documentId   = workspace.GetDocumentId(hostDocument);
                var document     = workspace.CurrentSolution.GetDocument(documentId);
                var position     = hostDocument.CursorPosition.Value;

                var completionList = GetCompletionList(document, position, CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo());
                var item           = completionList.Items.First(i => i.DisplayText.StartsWith(textTypedSoFar));

                var optionService = workspace.Services.GetService <IOptionService>();
                var options       = optionService.GetOptions().WithChangedOption(CSharpCompletionOptions.AddNewLineOnEnterAfterFullyTypedWord, sendThroughEnterEnabled);
                optionService.SetOptions(options);

                var completionService = document.Project.LanguageServices.GetService <ICompletionService>();
                var completionRules   = completionService.GetCompletionRules();

                Assert.Equal(expected, completionRules.SendEnterThroughToEditor(item, textTypedSoFar, workspace.Options));
            }
        }
예제 #10
0
        public async Task <CompletionResult> GetCompletionData(int position, char?triggerChar)
        {
            IList <ISelectedCompletionData> completionData = null;
            IOverloadProvider overloadProvider             = null;
            bool?isCompletion = null;

            if (triggerChar != null)
            {
                var isSignatureHelp = await IsSignatureHelpTriggerCharacter(position - 1).ConfigureAwait(false);

                if (isSignatureHelp)
                {
                    var signatureHelp = await GetSignatureHelp(
                        new SignatureHelpTriggerInfo(SignatureHelpTriggerReason.TypeCharCommand, triggerChar.Value), position)
                                        .ConfigureAwait(false);

                    if (signatureHelp != null)
                    {
                        overloadProvider = new OverloadProvider(signatureHelp);
                    }
                }
                else
                {
                    isCompletion = await IsCompletionTriggerCharacter(position - 1).ConfigureAwait(false);
                }
            }

            if (overloadProvider == null && isCompletion != false)
            {
                var items = GetCompletion(
                    triggerChar != null
                        ? CompletionTriggerInfo.CreateTypeCharTriggerInfo(triggerChar.Value)
                        : CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo(),
                    position);
                completionData = items.ToList();
            }

            return(new CompletionResult(completionData, overloadProvider));
        }
        public void CrefCompletionSpeculatesOutsideTrivia()
        {
            var text           = @"
/// <see cref=""$$
class C
{
}";
            var exportProvider = MinimalTestExportProvider.CreateExportProvider(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithPart(typeof(PickySemanticFactsService)));

            using (var workspace = TestWorkspaceFactory.CreateWorkspaceFromFiles(LanguageNames.CSharp, new CSharpCompilationOptions(OutputKind.ConsoleApplication), new CSharpParseOptions(), new[] { text }, exportProvider))
            {
                // This test uses MEF to compose in an ISyntaxFactsService that
                // asserts it isn't asked to speculate on nodes inside documentation trivia.
                // This verifies that the provider is asking for a speculative SemanticModel
                // by walking to the node the documentation is attached to.

                var provider     = new CrefCompletionProvider();
                var hostDocument = workspace.DocumentWithCursor;
                var document     = workspace.CurrentSolution.GetDocument(hostDocument.Id);
                var items        = provider.GetGroupAsync(document, hostDocument.CursorPosition.Value, CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo(), CancellationToken.None).Result;
            }
        }
예제 #12
0
 private bool StartNewModelComputation(ICompletionService completionService, bool filterItems, bool dismissIfEmptyAllowed = true)
 {
     return(StartNewModelComputation(
                completionService,
                CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo().WithIsDebugger(_isDebugger).WithIsImmediateWindow(_isImmediateWindow), filterItems, dismissIfEmptyAllowed));
 }
예제 #13
0
 private bool StartNewModelComputation(ICompletionService completionService, bool filterItems, bool dismissIfEmptyAllowed = true)
 {
     return(StartNewModelComputation(
                completionService,
                CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo(), filterItems, dismissIfEmptyAllowed));
 }
        private void VerifyProviderCommitCheckResults(Document document, int position, string itemToCommit, string expectedCodeAfterCommit, char?commitChar, string textTypedSoFar)
        {
            var textBuffer   = workspaceFixture.Workspace.Documents.Single().TextBuffer;
            var textSnapshot = textBuffer.CurrentSnapshot.AsText();

            var completions    = CompletionProvider.GetGroupAsync(document, position, CompletionTriggerInfo.CreateInvokeCompletionTriggerInfo()).Result.Items;
            var completionItem = completions.First(i => CompareItems(i.DisplayText, itemToCommit));

            var textChange = CompletionProvider.IsCommitCharacter(completionItem, commitChar.HasValue ? commitChar.Value : ' ', textTypedSoFar)
                ? CompletionProvider.GetTextChange(completionItem, commitChar, textTypedSoFar)
                : new TextChange();

            var oldText = document.GetTextAsync().Result;
            var newText = oldText.WithChanges(textChange);

            Assert.Equal(expectedCodeAfterCommit, newText.ToString());
        }