public async Task TestEnter() { const string markup = @" class c { public int value {set; get; }} class d { void goo() { c goo = new c { v$$ } }"; using (var workspace = TestWorkspace.CreateCSharp(markup)) { var hostDocument = workspace.Documents.Single(); var position = hostDocument.CursorPosition.Value; var document = workspace.CurrentSolution.GetDocument(hostDocument.Id); var triggerInfo = CompletionTrigger.CreateInsertionTrigger('a'); var service = GetCompletionService(workspace); var completionList = await GetCompletionListAsync(service, document, position, triggerInfo); var item = completionList.Items.First(); Assert.False(CommitManager.SendEnterThroughToEditor(service.GetRules(), item, string.Empty), "Expected false from SendEnterThroughToEditor()"); } }
protected async Task VerifyCommitCharactersAsync(string initialMarkup, string textTypedSoFar, char[] validChars, char[] invalidChars = null) { Assert.NotNull(validChars); invalidChars = invalidChars ?? new[] { 'x' }; using (var workspace = CreateWorkspace(initialMarkup)) { var hostDocument = workspace.DocumentWithCursor; var documentId = workspace.GetDocumentId(hostDocument); var document = workspace.CurrentSolution.GetDocument(documentId); var position = hostDocument.CursorPosition.Value; var service = GetCompletionService(workspace); var completionList = await GetCompletionListAsync(service, document, position, RoslynCompletion.CompletionTrigger.Invoke); var item = completionList.Items.First(i => i.DisplayText.StartsWith(textTypedSoFar)); foreach (var ch in validChars) { Assert.True(CommitManager.IsCommitCharacter( service.GetRules(), item, ch, textTypedSoFar + ch), $"Expected '{ch}' to be a commit character"); } foreach (var ch in invalidChars) { Assert.False(CommitManager.IsCommitCharacter( service.GetRules(), item, ch, textTypedSoFar + ch), $"Expected '{ch}' NOT to be a commit character"); } } }
internal virtual void VerifyCustomCommitWorker( CompletionService service, ICustomCommitCompletionProvider customCommitCompletionProvider, RoslynCompletion.CompletionItem completionItem, CompletionHelper completionRules, ITextView textView, ITextBuffer textBuffer, string codeBeforeCommit, string expectedCodeAfterCommit, char?commitChar = null) { MarkupTestFile.GetPosition(expectedCodeAfterCommit, out var actualExpectedCode, out int expectedCaretPosition); if (commitChar.HasValue && !CommitManager.IsCommitCharacter(service.GetRules(), completionItem, commitChar.Value, commitChar.Value.ToString())) { Assert.Equal(codeBeforeCommit, actualExpectedCode); return; } customCommitCompletionProvider.Commit(completionItem, textView, textBuffer, textView.TextSnapshot, commitChar); string actualCodeAfterCommit = textBuffer.CurrentSnapshot.AsText().ToString(); var caretPosition = textView.Caret.Position.BufferPosition.Position; Assert.Equal(actualExpectedCode, actualCodeAfterCommit); Assert.Equal(expectedCaretPosition, caretPosition); }
internal async Task VerifyCustomCommitWorkerAsync( CompletionServiceWithProviders service, Document document, RoslynCompletion.CompletionItem completionItem, string codeBeforeCommit, string expectedCodeAfterCommit, char?commitChar = null) { MarkupTestFile.GetPosition(expectedCodeAfterCommit, out var actualExpectedCode, out int expectedCaretPosition); if (commitChar.HasValue && !CommitManager.IsCommitCharacter(service.GetRules(), completionItem, commitChar.Value, commitChar.Value.ToString())) { Assert.Equal(codeBeforeCommit, actualExpectedCode); return; } var commit = await service.GetChangeAsync(document, completionItem, commitChar, CancellationToken.None); var text = await document.GetTextAsync(); var newText = text.WithChanges(commit.TextChange); var newDoc = document.WithText(newText); document.Project.Solution.Workspace.TryApplyChanges(newDoc.Project.Solution); var textBuffer = (WorkspaceFixture.GetWorkspace()).Documents.Single().TextBuffer; var textView = (WorkspaceFixture.GetWorkspace()).Documents.Single().GetTextView(); string actualCodeAfterCommit = textBuffer.CurrentSnapshot.AsText().ToString(); var caretPosition = commit.NewPosition != null ? commit.NewPosition.Value : textView.Caret.Position.BufferPosition.Position; Assert.Equal(actualExpectedCode, actualCodeAfterCommit); Assert.Equal(expectedCaretPosition, caretPosition); }
private bool IsCommitCharacter(char ch, Model model) { AssertIsForeground(); if (model == null || model.IsSoftSelection || model.SelectedItemOpt == null) { return(false); } if (model.SelectedItemOpt == model.SuggestionModeItem) { return(char.IsLetterOrDigit(ch)); } var completionService = GetCompletionService(); if (completionService == null) { return(false); } var textTypedSoFar = GetTextTypedSoFar(model, model.SelectedItemOpt); return(CommitManager.IsCommitCharacter(completionService.GetRules(), model.SelectedItemOpt, ch, textTypedSoFar)); }
private void CommitOnEnter(out bool sendThrough, out bool committed) { AssertIsForeground(); var model = WaitForModel(); // If there's no model, then there's nothing to commit. if (model == null) { // Make sure that the enter gets sent into the buffer. sendThrough = true; committed = false; return; } // If we're in a normal editor or the Immediate window, we'll send the enter through // to the editor. In single-line debugger windows (Watch, etc), however, we don't // want to send the enter though, because those windows don't support displaying // more than one line of text. sendThrough = !_isDebugger || _isImmediateWindow; // If the user used completion filters to empty the list, just dismiss if (model.SelectedItemOpt == null) { committed = false; return; } if (model.IsSoftSelection) { // If the completion list is soft selected, then don't commit on enter. // Instead, just dismiss the completion list. committed = false; return; } // If the selected item is the builder, dismiss if (model.SelectedItemOpt == model.SuggestionModeItem) { sendThrough = false; committed = false; return; } if (sendThrough) { // Get the text that the user has currently entered into the buffer var viewSpan = model.GetViewBufferSpan(model.SelectedItemOpt.Span); var textTypedSoFar = model.GetCurrentTextInSnapshot( viewSpan, this.TextView.TextSnapshot, this.GetCaretPointInViewBuffer()); var service = GetCompletionService(); sendThrough = CommitManager.SendEnterThroughToEditor( service.GetRules(), model.SelectedItemOpt, textTypedSoFar); } this.CommitOnNonTypeChar(model.SelectedItemOpt, model); committed = true; }
protected async Task VerifySendEnterThroughToEnterAsync(string initialMarkup, string textTypedSoFar, EnterKeyRule sendThroughEnterOption, bool expected) { using var workspace = TestWorkspace.CreateCSharp(initialMarkup); var hostDocument = workspace.DocumentWithCursor; var documentId = workspace.GetDocumentId(hostDocument); var document = workspace.CurrentSolution.GetDocument(documentId); var position = hostDocument.CursorPosition.Value; workspace.Options = workspace.Options.WithChangedOption( CompletionOptions.EnterKeyBehavior, LanguageNames.CSharp, sendThroughEnterOption); var service = GetCompletionService(workspace); var completionList = await GetCompletionListAsync(service, document, position, RoslynTrigger.Invoke); var item = completionList.Items.First(i => (i.DisplayText + i.DisplayTextSuffix).StartsWith(textTypedSoFar)); Assert.Equal(expected, CommitManager.SendEnterThroughToEditor(service.GetRules(), item, textTypedSoFar)); }
private async Task VerifyProviderCommitCheckResultsAsync( Document document, int position, string itemToCommit, string expectedCodeAfterCommit, char?commitCharOpt, string textTypedSoFar) { var workspace = WorkspaceFixture.GetWorkspace(); var textBuffer = workspace.Documents.Single().TextBuffer; var textSnapshot = textBuffer.CurrentSnapshot.AsText(); var service = GetCompletionService(workspace); var items = (await GetCompletionListAsync(service, document, position, RoslynCompletion.CompletionTrigger.Invoke)).Items; var firstItem = items.First(i => CompareItems(i.DisplayText + i.DisplayTextSuffix, itemToCommit)); var completionRules = GetCompletionHelper(document); var commitChar = commitCharOpt ?? '\t'; var text = await document.GetTextAsync(); if (commitChar == '\t' || CommitManager.IsCommitCharacter(service.GetRules(), firstItem, commitChar, textTypedSoFar + commitChar)) { var textChange = (await service.GetChangeAsync(document, firstItem, commitChar, CancellationToken.None)).TextChange; // 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 committed, but we should insert the commit character. var textChange = new TextChange(new TextSpan(firstItem.Span.End, 0), commitChar.ToString()); text = text.WithChanges(textChange); } Assert.Equal(expectedCodeAfterCommit, text.ToString()); }