private static async Task TestAsync(string contents) { using var workspace = TestWorkspace.CreateWorkspace( TestWorkspace.CreateWorkspaceElement(LanguageNames.CSharp, files: new[] { contents.Replace("|", " ") }, isMarkup: false)); var document = workspace.CurrentSolution.GetRequiredDocument(workspace.Documents.First().Id); var root = await document.GetRequiredSyntaxRootAsync(default);
protected override TestWorkspace CreateWorkspace(string codeWithMarker) { return(TestWorkspace.CreateWorkspace(XElement.Parse( $@"<Workspace> <Project Language=""NoCompilation""> <Document>{codeWithMarker}</Document> </Project> </Workspace>"), composition: EditorTestCompositions.EditorFeatures.AddParts( typeof(NoCompilationContentTypeDefinitions), typeof(NoCompilationContentTypeLanguageService), typeof(NoCompilationTodoCommentService)))); }
/// <summary> /// This can use input files with an (optionally) annotated span 'Selection' and a cursor position ($$), /// and use it to create a selected span in the TextView. /// /// For instance, the following will create a TextView that has a multiline selection with the cursor at the end. /// /// Sub Foo /// {|Selection:SomeMethodCall() /// AnotherMethodCall()$$|} /// End Sub /// </summary> public AbstractCommandHandlerTestState( XElement workspaceElement, ExportProvider exportProvider, string workspaceKind) { this.Workspace = TestWorkspace.CreateWorkspace( workspaceElement, exportProvider: exportProvider, workspaceKind: workspaceKind); var cursorDocument = this.Workspace.Documents.First(d => d.CursorPosition.HasValue); _textView = cursorDocument.GetTextView(); _subjectBuffer = cursorDocument.GetTextBuffer(); IList <Text.TextSpan> selectionSpanList; if (cursorDocument.AnnotatedSpans.TryGetValue("Selection", out selectionSpanList)) { var span = selectionSpanList.First(); var cursorPosition = cursorDocument.CursorPosition.Value; Assert.True(cursorPosition == span.Start || cursorPosition == span.Start + span.Length, "cursorPosition wasn't at an endpoint of the 'Selection' annotated span"); _textView.Selection.Select( new SnapshotSpan(_subjectBuffer.CurrentSnapshot, new Span(span.Start, span.Length)), isReversed: cursorPosition == span.Start); if (selectionSpanList.Count > 1) { _textView.Selection.Mode = TextSelectionMode.Box; foreach (var additionalSpan in selectionSpanList.Skip(1)) { _textView.Selection.Select( new SnapshotSpan(_subjectBuffer.CurrentSnapshot, new Span(additionalSpan.Start, additionalSpan.Length)), isReversed: false); } } } else { _textView.Caret.MoveTo( new SnapshotPoint( _textView.TextBuffer.CurrentSnapshot, cursorDocument.CursorPosition.Value)); } this.EditorOperations = GetService <IEditorOperationsFactoryService>().GetEditorOperations(_textView); this.UndoHistoryRegistry = GetService <ITextUndoHistoryRegistry>(); }
protected override TestWorkspace CreateWorkspace(string codeWithMarker) { var workspace = TestWorkspace.CreateWorkspace(XElement.Parse( $@"<Workspace> <Project Language=""NoCompilation""> <Document>{codeWithMarker}</Document> </Project> </Workspace>"), composition: EditorTestCompositions.EditorFeatures.AddParts( typeof(NoCompilationContentTypeDefinitions), typeof(NoCompilationContentTypeLanguageService), typeof(NoCompilationTodoCommentService))); workspace.SetOptions(workspace.Options.WithChangedOption(TodoCommentOptions.TokenList, DefaultTokenList)); return(workspace); }
public void OrderingOfEditorConfigMaintained() { using var tempRoot = new TempRoot(); var tempDirectory = tempRoot.CreateDirectory(); // Write out an .editorconfig. We'll write out 100 random GUIDs var expectedKeysInOrder = new List <string>(); using (var writer = new StreamWriter(tempDirectory.CreateFile(".editorconfig").Path)) { writer.WriteLine("root = true"); writer.WriteLine("[*.cs]"); for (int i = 0; i < 100; i++) { var key = Guid.NewGuid().ToString(); expectedKeysInOrder.Add(key); writer.WriteLine($"{key} = value"); } } // Create a workspace with a file in that path var codingConventionsCatalog = ExportProviderCache.GetOrCreateAssemblyCatalog(typeof(ICodingConventionsManager).Assembly).WithPart(typeof(MockFileWatcher)); var exportProvider = ExportProviderCache.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(codingConventionsCatalog)).CreateExportProvider(); using var workspace = TestWorkspace.CreateWorkspace( new XElement("Workspace", new XElement("Project", new XAttribute("Language", "C#"), new XElement("Document", new XAttribute("FilePath", tempDirectory.CreateFile("Test.cs").Path)))), exportProvider: exportProvider); var document = workspace.CurrentSolution.Projects.Single().Documents.Single(); var providerFactory = workspace.ExportProvider.GetExportedValues <IDocumentOptionsProviderFactory>().OfType <LegacyEditorConfigDocumentOptionsProviderFactory>().Single(); var provider = providerFactory.TryCreate(workspace); var option = new Option <List <string> >(nameof(LegacyEditorConfigDocumentOptionsProviderTests), nameof(OrderingOfEditorConfigMaintained), null, new[] { new KeysReturningStorageLocation() }); var optionKey = new OptionKey(option); // Fetch the underlying option order with a "option" that returns the keys provider.GetOptionsForDocumentAsync(document, CancellationToken.None).Result.TryGetDocumentOption(optionKey, workspace.Options, out object actualKeysInOrderObject); var actualKeysInOrder = Assert.IsAssignableFrom <IEnumerable <string> >(actualKeysInOrderObject); Assert.Equal(expectedKeysInOrder, actualKeysInOrder); }
/// <summary> /// This can use input files with an (optionally) annotated span 'Selection' and a cursor position ($$), /// and use it to create a selected span in the TextView. /// /// For instance, the following will create a TextView that has a multiline selection with the cursor at the end. /// /// Sub Goo /// {|Selection:SomeMethodCall() /// AnotherMethodCall()$$|} /// End Sub /// /// You can use multiple selection spans to create box selections. /// /// Sub Goo /// {|Selection:$$box|}11111 /// {|Selection:sel|}111 /// {|Selection:ect|}1 /// {|Selection:ion|}1111111 /// End Sub /// </summary> public AbstractCommandHandlerTestState( XElement workspaceElement, ExportProvider exportProvider, string workspaceKind) { this.Workspace = TestWorkspace.CreateWorkspace( workspaceElement, exportProvider: exportProvider, workspaceKind: workspaceKind); var cursorDocument = this.Workspace.Documents.First(d => d.CursorPosition.HasValue); _textView = cursorDocument.GetTextView(); _subjectBuffer = cursorDocument.GetTextBuffer(); if (cursorDocument.AnnotatedSpans.TryGetValue("Selection", out var selectionSpanList)) { var firstSpan = selectionSpanList.First(); var lastSpan = selectionSpanList.Last(); var cursorPosition = cursorDocument.CursorPosition.Value; Assert.True(cursorPosition == firstSpan.Start || cursorPosition == firstSpan.End || cursorPosition == lastSpan.Start || cursorPosition == lastSpan.End, "cursorPosition wasn't at an endpoint of the 'Selection' annotated span"); _textView.Selection.Mode = selectionSpanList.Length > 1 ? TextSelectionMode.Box : TextSelectionMode.Stream; SnapshotPoint boxSelectionStart, boxSelectionEnd; bool isReversed; if (cursorPosition == firstSpan.Start || cursorPosition == lastSpan.End) { // Top-left and bottom-right corners used as anchor points. boxSelectionStart = new SnapshotPoint(_subjectBuffer.CurrentSnapshot, firstSpan.Start); boxSelectionEnd = new SnapshotPoint(_subjectBuffer.CurrentSnapshot, lastSpan.End); isReversed = cursorPosition == firstSpan.Start; } else { // Top-right and bottom-left corners used as anchor points. boxSelectionStart = new SnapshotPoint(_subjectBuffer.CurrentSnapshot, firstSpan.End); boxSelectionEnd = new SnapshotPoint(_subjectBuffer.CurrentSnapshot, lastSpan.Start); isReversed = cursorPosition == firstSpan.End; } _textView.Selection.Select( new SnapshotSpan(boxSelectionStart, boxSelectionEnd), isReversed: isReversed); } else { _textView.Caret.MoveTo( new SnapshotPoint( _textView.TextBuffer.CurrentSnapshot, cursorDocument.CursorPosition.Value)); } this.EditorOperations = GetService <IEditorOperationsFactoryService>().GetEditorOperations(_textView); this.UndoHistoryRegistry = GetService <ITextUndoHistoryRegistry>(); }
public AbstractCommandHandlerTestState( XElement workspaceElement, TestComposition composition, string?workspaceKind = null, bool makeSeparateBufferForCursor = false, ImmutableArray <string> roles = default) { Workspace = TestWorkspace.CreateWorkspace( workspaceElement, composition: composition, workspaceKind: workspaceKind); if (makeSeparateBufferForCursor) { var languageName = Workspace.Projects.First().Language; var contentType = Workspace.Services.GetLanguageServices(languageName).GetRequiredService <IContentTypeLanguageService>().GetDefaultContentType(); _createdTextView = EditorFactory.CreateView(Workspace.ExportProvider, contentType, roles); _textView = _createdTextView.TextView; _subjectBuffer = _textView.TextBuffer; } else { var cursorDocument = Workspace.Documents.First(d => d.CursorPosition.HasValue); _textView = cursorDocument.GetTextView(); _subjectBuffer = cursorDocument.GetTextBuffer(); if (cursorDocument.AnnotatedSpans.TryGetValue("Selection", out var selectionSpanList)) { var firstSpan = selectionSpanList.First(); var lastSpan = selectionSpanList.Last(); var cursorPosition = cursorDocument.CursorPosition !.Value; Assert.True(cursorPosition == firstSpan.Start || cursorPosition == firstSpan.End || cursorPosition == lastSpan.Start || cursorPosition == lastSpan.End, "cursorPosition wasn't at an endpoint of the 'Selection' annotated span"); _textView.Selection.Mode = selectionSpanList.Length > 1 ? TextSelectionMode.Box : TextSelectionMode.Stream; SnapshotPoint boxSelectionStart, boxSelectionEnd; bool isReversed; if (cursorPosition == firstSpan.Start || cursorPosition == lastSpan.End) { // Top-left and bottom-right corners used as anchor points. boxSelectionStart = new SnapshotPoint(_subjectBuffer.CurrentSnapshot, firstSpan.Start); boxSelectionEnd = new SnapshotPoint(_subjectBuffer.CurrentSnapshot, lastSpan.End); isReversed = cursorPosition == firstSpan.Start; } else { // Top-right and bottom-left corners used as anchor points. boxSelectionStart = new SnapshotPoint(_subjectBuffer.CurrentSnapshot, firstSpan.End); boxSelectionEnd = new SnapshotPoint(_subjectBuffer.CurrentSnapshot, lastSpan.Start); isReversed = cursorPosition == firstSpan.End; } _textView.Selection.Select( new SnapshotSpan(boxSelectionStart, boxSelectionEnd), isReversed: isReversed); } } this.EditorOperations = GetService <IEditorOperationsFactoryService>().GetEditorOperations(_textView); this.UndoHistoryRegistry = GetService <ITextUndoHistoryRegistry>(); }