private static string GetLeadingWhitespaceOfLineAtPosition(string code, int position) { var exportProvider = EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider(); var snapshot = EditorFactory.CreateBuffer(exportProvider, code).CurrentSnapshot; return(snapshot.GetLeadingWhitespaceOfLineAtPosition(position)); }
public void CreateBackspaceCommandArgsWithNullTextView() { var buffer = EditorFactory.CreateBuffer(TestExportProvider.ExportProviderWithCSharpAndVisualBasic, "class C { }"); Assert.Throws <ArgumentNullException>(() => new BackspaceKeyCommandArgs(null, buffer)); }
public AdornmentManagerTester() { _subjectBuffer = EditorFactory.CreateBuffer(TestExportProvider.ExportProviderWithCSharpAndVisualBasic, "Hi There"); _textView = new Mock <IWpfTextView>(); var aggregatorService = new Mock <IViewTagAggregatorFactoryService>(); _adornmentLayer = new Mock <IAdornmentLayer>(); _aggregator = new Mock <ITagAggregator <Tag> >(); var layerName = "LayerName"; _textView.Setup(tv => tv.GetAdornmentLayer(layerName)).Returns(_adornmentLayer.Object); _textView.SetupGet(tv => tv.VisualElement).Returns(new FrameworkElement()); aggregatorService.Setup(a => a.CreateTagAggregator <Tag>(_textView.Object)).Returns(_aggregator.Object); var textViewModel = new Mock <ITextViewModel>(); textViewModel.Setup(tvm => tvm.VisualBuffer).Returns(_subjectBuffer); _textView.Setup(tv => tv.TextViewModel).Returns(textViewModel.Object); var workspace = new TestWorkspace(); var listener = new AggregateAsynchronousOperationListener( Enumerable.Empty <Lazy <IAsynchronousOperationListener, FeatureMetadata> >(), FeatureAttribute.LineSeparators); Manager = AdornmentManager <Tag> .Create(_textView.Object, aggregatorService.Object, listener, adornmentLayerName : layerName); }
private Tuple <ITextSnapshot, SourceText> Create(params string[] lines) { var buffer = EditorFactory.CreateBuffer(TestExportProvider.ExportProviderWithCSharpAndVisualBasic, lines); var text = buffer.CurrentSnapshot.AsText(); return(Tuple.Create(buffer.CurrentSnapshot, (SourceText)text)); }
private static ITextSnapshotLine GetLine(string codeLine) { var exportProvider = EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider(); var snapshot = EditorFactory.CreateBuffer(exportProvider, codeLine).CurrentSnapshot; return(snapshot.GetLineFromLineNumber(0)); }
private TagSpanIntervalTree <ITextMarkerTag> CreateTree(string text, params Span[] spans) { var buffer = EditorFactory.CreateBuffer(TestExportProvider.ExportProviderWithCSharpAndVisualBasic, text); var tags = spans.Select(s => new TagSpan <ITextMarkerTag>(new SnapshotSpan(buffer.CurrentSnapshot, s), new TextMarkerTag(string.Empty))); return(new TagSpanIntervalTree <ITextMarkerTag>(buffer, SpanTrackingMode.EdgeInclusive, tags)); }
internal static TestHostDocument CreateDocument( XElement documentElement, ExportProvider exportProvider, HostLanguageServices languageServiceProvider, ImmutableArray <string> roles) { string markupCode = documentElement.NormalizedValue(); var folders = GetFolders(documentElement); var optionsElement = documentElement.Element(ParseOptionsElementName); var codeKind = SourceCodeKind.Regular; if (optionsElement != null) { var attr = optionsElement.Attribute(KindAttributeName); codeKind = attr == null ? SourceCodeKind.Regular : (SourceCodeKind)Enum.Parse(typeof(SourceCodeKind), attr.Value); } MarkupTestFile.GetPositionAndSpans(markupCode, out var code, out var cursorPosition, out IDictionary <string, ImmutableArray <TextSpan> > spans); var documentServiceProvider = GetDocumentServiceProvider(documentElement); var contentTypeLanguageService = languageServiceProvider.GetService <IContentTypeLanguageService>(); var contentType = contentTypeLanguageService.GetDefaultContentType(); var textBuffer = EditorFactory.CreateBuffer(contentType.TypeName, exportProvider, code); return(new TestHostDocument( exportProvider, languageServiceProvider, textBuffer, filePath: string.Empty, cursorPosition, spans, codeKind, folders, isLinkFile: false, documentServiceProvider, roles: roles)); }
public AdornmentManagerTester() { _subjectBuffer = EditorFactory.CreateBuffer(TestExportProvider.ExportProviderWithCSharpAndVisualBasic, "Hi There"); _textView = new Mock <IWpfTextView>(); var aggregatorService = new Mock <IViewTagAggregatorFactoryService>(); _adornmentLayer = new Mock <IAdornmentLayer>(); _aggregator = new Mock <ITagAggregator <Tag> >(); var layerName = "LayerName"; _textView.Setup(tv => tv.GetAdornmentLayer(layerName)).Returns(_adornmentLayer.Object); _textView.SetupGet(tv => tv.VisualElement).Returns(new FrameworkElement()); aggregatorService.Setup(a => a.CreateTagAggregator <Tag>(_textView.Object)).Returns(_aggregator.Object); var textViewModel = new Mock <ITextViewModel>(); textViewModel.Setup(tvm => tvm.VisualBuffer).Returns(_subjectBuffer); _textView.Setup(tv => tv.TextViewModel).Returns(textViewModel.Object); var workspace = new TestWorkspace(); Manager = AdornmentManager <Tag> .Create( workspace.ExportProvider.GetExportedValue <IThreadingContext>(), _textView.Object, aggregatorService.Object, AsynchronousOperationListenerProvider.NullListener, adornmentLayerName : layerName); }
private static Tuple <ITextSnapshot, SourceText> Create(params string[] lines) { var exportProvider = EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider(); var buffer = EditorFactory.CreateBuffer(exportProvider, lines); var text = buffer.CurrentSnapshot.AsText(); return(Tuple.Create(buffer.CurrentSnapshot, text)); }
private static TagSpanIntervalTree <ITextMarkerTag> CreateTree(string text, params Span[] spans) { var exportProvider = EditorTestCompositions.Editor.ExportProviderFactory.CreateExportProvider(); var buffer = EditorFactory.CreateBuffer(exportProvider, text); var tags = spans.Select(s => new TagSpan <ITextMarkerTag>(new SnapshotSpan(buffer.CurrentSnapshot, s), new TextMarkerTag(string.Empty))); return(new TagSpanIntervalTree <ITextMarkerTag>(buffer, SpanTrackingMode.EdgeInclusive, tags)); }
private static void TextContainsRegionOrUsing(string input, bool expected, string language) { var exportProvider = EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider(); var buffer = EditorFactory.CreateBuffer(exportProvider, input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)); var textSnapshot = buffer.CurrentSnapshot; var actual = AbstractStructureTaggerProvider.ContainsRegionOrImport(textSnapshot, collapseRegions: true, collapseImports: true, language); Assert.Equal(expected, actual); }
public void ConvertToSnapshotSpan2() { var snapshot = EditorFactory.CreateBuffer(TestExportProvider.ExportProviderWithCSharpAndVisualBasic, new string('a', 10)).CurrentSnapshot; var textSpan = new TextSpan(0, 10); var ss = textSpan.ToSnapshotSpan(snapshot); Assert.Same(snapshot, ss.Snapshot); Assert.Equal(0, ss.Start); Assert.Equal(10, ss.Length); }
public void ConvertToSnapshotSpan2() { var snapshot = EditorFactory.CreateBuffer(EditorServicesUtil.ExportProvider, new string('a', 10)).CurrentSnapshot; var textSpan = new TextSpan(0, 10); var ss = textSpan.ToSnapshotSpan(snapshot); Assert.Same(snapshot, ss.Snapshot); Assert.Equal(0, ss.Start); Assert.Equal(10, ss.Length); }
private static IList <TestHostProject> CreateSubmissions( TestWorkspace workspace, IEnumerable <XElement> submissionElements, ExportProvider exportProvider) { var submissions = new List <TestHostProject>(); var submissionIndex = 0; foreach (var submissionElement in submissionElements) { var submissionName = "Submission" + (submissionIndex++); var languageName = GetLanguage(workspace, submissionElement); // The document var markupCode = submissionElement.NormalizedValue(); string code; int? cursorPosition; IDictionary <string, IList <TextSpan> > spans; MarkupTestFile.GetPositionAndSpans(markupCode, out code, out cursorPosition, out spans); var languageServices = workspace.Services.GetLanguageServices(languageName); var contentTypeLanguageService = languageServices.GetService <IContentTypeLanguageService>(); var contentType = contentTypeLanguageService.GetDefaultContentType(); var textBuffer = EditorFactory.CreateBuffer(contentType.TypeName, exportProvider, code); // The project var document = new TestHostDocument(exportProvider, languageServices, textBuffer, submissionName, cursorPosition, spans, SourceCodeKind.Interactive); var syntaxFactory = languageServices.GetService <ISyntaxTreeFactoryService>(); var compilationFactory = languageServices.GetService <ICompilationFactoryService>(); var compilationOptions = compilationFactory.GetDefaultCompilationOptions().WithOutputKind(OutputKind.DynamicallyLinkedLibrary); var parseOptions = syntaxFactory.GetDefaultParseOptions().WithKind(SourceCodeKind.Interactive); var references = CreateCommonReferences(workspace, submissionElement); var project = new TestHostProject( languageServices, compilationOptions, parseOptions, submissionName, references, new List <TestHostDocument> { document }, isSubmission: true); submissions.Add(project); } return(submissions); }
private static ITextSnapshot GetSampleCodeSnapshot() { var exportProvider = EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider(); // to make verification simpler, each line of code is 4 characters and will be joined to other lines // with a single newline character making the formula to calculate the offset from a given line and // column thus: // position = row * 5 + column var lines = new string[] { "goo1", "bar1", "goo2", "bar2", "goo3", "bar3", }; var code = string.Join("\n", lines); var snapshot = EditorFactory.CreateBuffer(exportProvider, code).CurrentSnapshot; return(snapshot); }
protected async Task AssertFormatAsync(string expected, string code, bool debugMode = false, Dictionary <OptionKey, object> changedOptionSet = null, bool useTab = false, bool testWithTransformation = true) { using (var workspace = TestWorkspace.CreateCSharp(code)) { var hostdoc = workspace.Documents.First(); // get original buffer var buffer = hostdoc.GetTextBuffer(); // create new buffer with cloned content var clonedBuffer = EditorFactory.CreateBuffer( buffer.ContentType.TypeName, workspace.ExportProvider, buffer.CurrentSnapshot.GetText()); var document = workspace.CurrentSolution.GetDocument(hostdoc.Id); var syntaxTree = await document.GetSyntaxTreeAsync(); var formattingRuleProvider = workspace.Services.GetService <IHostDependentFormattingRuleFactoryService>(); var options = workspace.Options; if (changedOptionSet != null) { foreach (var entry in changedOptionSet) { options = options.WithChangedOption(entry.Key, entry.Value); } } options = options.WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, useTab); var root = await syntaxTree.GetRootAsync(); var rules = formattingRuleProvider.CreateRule(workspace.CurrentSolution.GetDocument(syntaxTree), 0).Concat(Formatter.GetDefaultFormattingRules(workspace, root.Language)); AssertFormat(workspace, expected, options, rules, clonedBuffer, root); if (testWithTransformation) { // format with node and transform AssertFormatWithTransformation(workspace, expected, options, rules, root); } } }
protected async Task AssertFormatAsync(string expected, string code, IEnumerable <TextSpan> spans, bool debugMode = false, Dictionary <OptionKey, object> changedOptionSet = null, int?baseIndentation = null) { using var workspace = TestWorkspace.CreateCSharp(code); var hostdoc = workspace.Documents.First(); var buffer = hostdoc.GetTextBuffer(); var document = workspace.CurrentSolution.GetDocument(hostdoc.Id); var syntaxTree = await document.GetSyntaxTreeAsync(); // create new buffer with cloned content var clonedBuffer = EditorFactory.CreateBuffer( workspace.ExportProvider, buffer.ContentType, buffer.CurrentSnapshot.GetText()); var formattingRuleProvider = workspace.Services.GetService <IHostDependentFormattingRuleFactoryService>(); if (baseIndentation.HasValue) { var factory = formattingRuleProvider as TestFormattingRuleFactoryServiceFactory.Factory; factory.BaseIndentation = baseIndentation.Value; factory.TextSpan = spans.First(); } var options = workspace.Options; if (changedOptionSet != null) { foreach (var entry in changedOptionSet) { options = options.WithChangedOption(entry.Key, entry.Value); } } var root = await syntaxTree.GetRootAsync(); var rules = formattingRuleProvider.CreateRule(workspace.CurrentSolution.GetDocument(syntaxTree), 0).Concat(Formatter.GetDefaultFormattingRules(workspace, root.Language)); AssertFormat(workspace, expected, options, rules, clonedBuffer, root, spans); // format with node and transform AssertFormatWithTransformation(workspace, expected, options, rules, root, spans); }
internal ITextBuffer GetOrCreateBufferForPath(string?filePath, IContentType contentType, string languageName, string initialText) { // If we don't have a file path we'll just make something up for the purpose of this dictionary so all // buffers are still held onto. This isn't a file name used in the workspace itself so it's unobservable. if (RoslynString.IsNullOrEmpty(filePath)) { filePath = Guid.NewGuid().ToString(); } return(_createdTextBuffers.GetOrAdd(filePath, _ => { var textBuffer = EditorFactory.CreateBuffer(ExportProvider, contentType, initialText); // Ensure that the editor options on the text buffer matches that of the options that can be directly set in the workspace var editorOptions = ExportProvider.GetExportedValue <IEditorOptionsFactoryService>().GetOptions(textBuffer); var workspaceOptions = this.Options; editorOptions.SetOptionValue(DefaultOptions.ConvertTabsToSpacesOptionId, !workspaceOptions.GetOption(FormattingOptions.UseTabs, languageName)); editorOptions.SetOptionValue(DefaultOptions.TabSizeOptionId, workspaceOptions.GetOption(FormattingOptions.TabSize, languageName)); editorOptions.SetOptionValue(DefaultOptions.IndentSizeOptionId, workspaceOptions.GetOption(FormattingOptions.IndentationSize, languageName)); return textBuffer; })); }
private static TestHostDocument CreateDocument( TestWorkspace workspace, XElement workspaceElement, XElement documentElement, string language, ExportProvider exportProvider, HostLanguageServices languageServiceProvider, Dictionary <string, ITextBuffer> filePathToTextBufferMap, ref int documentId) { string markupCode; string filePath; var isLinkFileAttribute = documentElement.Attribute(IsLinkFileAttributeName); bool isLinkFile = isLinkFileAttribute != null && ((bool?)isLinkFileAttribute).HasValue && ((bool?)isLinkFileAttribute).Value; if (isLinkFile) { // This is a linked file. Use the filePath and markup from the referenced document. var originalProjectName = documentElement.Attribute(LinkAssemblyNameAttributeName); var originalDocumentPath = documentElement.Attribute(LinkFilePathAttributeName); if (originalProjectName == null || originalDocumentPath == null) { throw new ArgumentException("Linked file specified without LinkAssemblyName or LinkFilePath."); } var originalProjectNameStr = originalProjectName.Value; var originalDocumentPathStr = originalDocumentPath.Value; var originalProject = workspaceElement.Elements(ProjectElementName).First(p => { var assemblyName = p.Attribute(AssemblyNameAttributeName); return(assemblyName != null && assemblyName.Value == originalProjectNameStr); }); if (originalProject == null) { throw new ArgumentException("Linked file's LinkAssemblyName '{0}' project not found.", originalProjectNameStr); } var originalDocument = originalProject.Elements(DocumentElementName).First(d => { var documentPath = d.Attribute(FilePathAttributeName); return(documentPath != null && documentPath.Value == originalDocumentPathStr); }); if (originalDocument == null) { throw new ArgumentException("Linked file's LinkFilePath '{0}' file not found.", originalDocumentPathStr); } markupCode = originalDocument.NormalizedValue(); filePath = GetFilePath(workspace, originalDocument, ref documentId); } else { markupCode = documentElement.NormalizedValue(); filePath = GetFilePath(workspace, documentElement, ref documentId); } var folders = GetFolders(documentElement); var optionsElement = documentElement.Element(ParseOptionsElementName); // TODO: Allow these to be specified. var codeKind = SourceCodeKind.Regular; if (optionsElement != null) { var attr = optionsElement.Attribute(KindAttributeName); codeKind = attr == null ? SourceCodeKind.Regular : (SourceCodeKind)Enum.Parse(typeof(SourceCodeKind), attr.Value); } var contentTypeLanguageService = languageServiceProvider.GetService <IContentTypeLanguageService>(); var contentType = contentTypeLanguageService.GetDefaultContentType(); string code; int? cursorPosition; IDictionary <string, IList <TextSpan> > spans; MarkupTestFile.GetPositionAndSpans(markupCode, out code, out cursorPosition, out spans); // For linked files, use the same ITextBuffer for all linked documents ITextBuffer textBuffer; if (!filePathToTextBufferMap.TryGetValue(filePath, out textBuffer)) { textBuffer = EditorFactory.CreateBuffer(contentType.TypeName, exportProvider, code); filePathToTextBufferMap.Add(filePath, textBuffer); } return(new TestHostDocument(exportProvider, languageServiceProvider, textBuffer, filePath, cursorPosition, spans, codeKind, folders, isLinkFile)); }
private string GetLeadingWhitespaceOfLineAtPosition(string code, int position) { var snapshot = EditorFactory.CreateBuffer(TestExportProvider.ExportProviderWithCSharpAndVisualBasic, code).CurrentSnapshot; return(snapshot.GetLeadingWhitespaceOfLineAtPosition(position)); }
private static TestHostDocument CreateDocument( TestWorkspace workspace, XElement workspaceElement, XElement documentElement, string language, ExportProvider exportProvider, HostLanguageServices languageServiceProvider, Dictionary <string, ITextBuffer> filePathToTextBufferMap, IDocumentServiceProvider documentServiceProvider, ref int documentId) { string markupCode; string filePath; var isLinkFileAttribute = documentElement.Attribute(IsLinkFileAttributeName); var isLinkFile = isLinkFileAttribute != null && ((bool?)isLinkFileAttribute).HasValue && ((bool?)isLinkFileAttribute).Value; if (isLinkFile) { // This is a linked file. Use the filePath and markup from the referenced document. var originalAssemblyName = documentElement.Attribute(LinkAssemblyNameAttributeName)?.Value; var originalProjectName = documentElement.Attribute(LinkProjectNameAttributeName)?.Value; if (originalAssemblyName == null && originalProjectName == null) { throw new ArgumentException($"Linked files must specify either a {LinkAssemblyNameAttributeName} or {LinkProjectNameAttributeName}"); } var originalProject = workspaceElement.Elements(ProjectElementName).FirstOrDefault(p => { if (originalAssemblyName != null) { return(p.Attribute(AssemblyNameAttributeName)?.Value == originalAssemblyName); } else { return(p.Attribute(ProjectNameAttribute)?.Value == originalProjectName); } }); if (originalProject == null) { if (originalProjectName != null) { throw new ArgumentException($"Linked file's {LinkProjectNameAttributeName} '{originalProjectName}' project not found."); } else { throw new ArgumentException($"Linked file's {LinkAssemblyNameAttributeName} '{originalAssemblyName}' project not found."); } } var originalDocumentPath = documentElement.Attribute(LinkFilePathAttributeName)?.Value; if (originalDocumentPath == null) { throw new ArgumentException($"Linked files must specify a {LinkFilePathAttributeName}"); } documentElement = originalProject.Elements(DocumentElementName).FirstOrDefault(d => { return(d.Attribute(FilePathAttributeName)?.Value == originalDocumentPath); }); if (documentElement == null) { throw new ArgumentException($"Linked file's LinkFilePath '{originalDocumentPath}' file not found."); } } markupCode = documentElement.NormalizedValue(); filePath = GetFilePath(workspace, documentElement, ref documentId); var folders = GetFolders(documentElement); var optionsElement = documentElement.Element(ParseOptionsElementName); // TODO: Allow these to be specified. var codeKind = SourceCodeKind.Regular; if (optionsElement != null) { var attr = optionsElement.Attribute(KindAttributeName); codeKind = attr == null ? SourceCodeKind.Regular : (SourceCodeKind)Enum.Parse(typeof(SourceCodeKind), attr.Value); } var contentTypeLanguageService = languageServiceProvider.GetService <IContentTypeLanguageService>(); var contentType = contentTypeLanguageService.GetDefaultContentType(); MarkupTestFile.GetPositionAndSpans(markupCode, out var code, out var cursorPosition, out IDictionary <string, ImmutableArray <TextSpan> > spans); // For linked files, use the same ITextBuffer for all linked documents if (!filePathToTextBufferMap.TryGetValue(filePath, out var textBuffer)) { textBuffer = EditorFactory.CreateBuffer(contentType.TypeName, exportProvider, code); filePathToTextBufferMap.Add(filePath, textBuffer); } var testDocumentServiceProvider = GetDocumentServiceProvider(documentElement); if (documentServiceProvider == null) { documentServiceProvider = testDocumentServiceProvider; } else if (testDocumentServiceProvider != null) { AssertEx.Fail($"The document attributes on file {filePath} conflicted"); } return(new TestHostDocument( exportProvider, languageServiceProvider, textBuffer, filePath, cursorPosition, spans, codeKind, folders, isLinkFile, documentServiceProvider)); }
private static ITextSnapshotLine GetLine(string codeLine) { var snapshot = EditorFactory.CreateBuffer(TestExportProvider.ExportProviderWithCSharpAndVisualBasic, codeLine).CurrentSnapshot; return(snapshot.GetLineFromLineNumber(0)); }