예제 #1
0
        public void TestCreateProjectionBufferWithoutIndentation()
        {
            var exportProvider             = TestExportProvider.ExportProviderWithCSharpAndVisualBasic;
            var contentTypeRegistryService = exportProvider.GetExportedValue <IContentTypeRegistryService>();
            var textBuffer = exportProvider.GetExportedValue <ITextBufferFactoryService>().CreateTextBuffer(
                @"  line 1
  line 2
  line 3
    line 4", contentTypeRegistryService.GetContentType("text"));

            var projectionBuffer = IProjectionBufferFactoryServiceExtensions.CreateProjectionBufferWithoutIndentation(
                exportProvider.GetExportedValue <IProjectionBufferFactoryService>(),
                exportProvider.GetExportedValue <IContentTypeRegistryService>(),
                exportProvider.GetExportedValue <IEditorOptionsFactoryService>().GlobalOptions,
                textBuffer.CurrentSnapshot,
                "...",
                LineSpan.FromBounds(0, 1), LineSpan.FromBounds(2, 4));

            var projectionSnapshot = projectionBuffer.CurrentSnapshot;

            Assert.Equal(4, projectionSnapshot.LineCount);

            var lines = projectionSnapshot.Lines.ToList();

            Assert.Equal("line 1", lines[0].GetText());
            Assert.Equal("...", lines[1].GetText());
            Assert.Equal("line 3", lines[2].GetText());
            Assert.Equal("  line 4", lines[3].GetText());
        }
        private static List <LineSpan> GetExposedLineSpans(ITextSnapshot textSnapshot)
        {
            const string start = "//[";
            const string end   = "//]";

            var bufferText = textSnapshot.GetText().ToString();

            var lineSpans    = new List <LineSpan>();
            var lastEndIndex = 0;

            while (true)
            {
                var startIndex = bufferText.IndexOf(start, lastEndIndex, StringComparison.Ordinal);
                if (startIndex == -1)
                {
                    break;
                }

                var endIndex = bufferText.IndexOf(end, lastEndIndex, StringComparison.Ordinal);

                var startLine = textSnapshot.GetLineNumberFromPosition(startIndex) + 1;
                var endLine   = textSnapshot.GetLineNumberFromPosition(endIndex);

                lineSpans.Add(LineSpan.FromBounds(startLine, endLine));
                lastEndIndex = endIndex + end.Length;
            }

            return(lineSpans);
        }
예제 #3
0
        public void TestCreateProjectionBuffer()
        {
            var composition                = EditorTestCompositions.EditorFeatures;
            var exportProvider             = composition.ExportProviderFactory.CreateExportProvider();
            var contentTypeRegistryService = exportProvider.GetExportedValue <IContentTypeRegistryService>();
            var textBuffer = exportProvider.GetExportedValue <ITextBufferFactoryService>().CreateTextBuffer(
                @"  line 1
  line 2
  line 3
  line 4", contentTypeRegistryService.GetContentType("text"));

            var projectionBuffer = IProjectionBufferFactoryServiceExtensions.CreateProjectionBuffer(
                exportProvider.GetExportedValue <IProjectionBufferFactoryService>(),
                exportProvider.GetExportedValue <IContentTypeRegistryService>(),
                exportProvider.GetExportedValue <IEditorOptionsFactoryService>().GlobalOptions,
                textBuffer.CurrentSnapshot,
                "...",
                LineSpan.FromBounds(1, 2), LineSpan.FromBounds(3, 4));

            var projectionSnapshot = projectionBuffer.CurrentSnapshot;

            Assert.Equal(4, projectionSnapshot.LineCount);

            var lines = projectionSnapshot.Lines.ToList();

            Assert.Equal("...", lines[0].GetText());
            Assert.Equal("  line 2", lines[1].GetText());
            Assert.Equal("...", lines[2].GetText());
            Assert.Equal("  line 4", lines[3].GetText());
        }
예제 #4
0
        public TreeItemViewModel(
            TextSpan textSpan,
            SourceText sourceText,
            DocumentId documentId,
            string fileName,
            Glyph glyph,
            ImmutableArray <ClassifiedSpan> classifiedSpans,
            ValueTrackingTreeViewModel treeViewModel,
            IGlyphService glyphService,
            IThreadingContext threadingContext,
            Workspace workspace,
            ImmutableArray <TreeItemViewModel> children = default)
            : base()
        {
            FileName         = fileName;
            TextSpan         = textSpan;
            _sourceText      = sourceText;
            ClassifiedSpans  = classifiedSpans;
            TreeViewModel    = treeViewModel;
            ThreadingContext = threadingContext;

            _glyph        = glyph;
            _glyphService = glyphService;
            Workspace     = workspace;
            DocumentId    = documentId;

            if (!children.IsDefaultOrEmpty)
            {
                foreach (var child in children)
                {
                    ChildItems.Add(child);
                }
            }

            sourceText.GetLineAndOffset(textSpan.Start, out var lineStart, out var _);
            sourceText.GetLineAndOffset(textSpan.End, out var lineEnd, out var _);
            LineSpan = LineSpan.FromBounds(lineStart, lineEnd);

            PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == nameof(IsLoading))
                {
                    NotifyPropertyChanged(nameof(ShowGlyph));
                }
            };

            TreeViewModel.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == nameof(TreeViewModel.HighlightBrush))
                {
                    // If the highlight changes we need to recalculate the inlines so the
                    // highlighting is correct
                    NotifyPropertyChanged(nameof(Inlines));
                }
            };
        }
예제 #5
0
        // Adds a line span to the spans we've been collecting.  If the line span overlaps or
        // abuts a previous span then the two are merged.
        private static void MergeLineSpans(List <LineSpan> lineSpans, LineSpan nextLineSpan)
        {
            if (lineSpans.Count > 0)
            {
                var lastLineSpan = lineSpans.Last();

                // We merge them if there's no more than one line between the two.  Otherwise
                // we'd show "..." between two spans where we could just show the actual code.
                if (nextLineSpan.Start >= lastLineSpan.Start && nextLineSpan.Start <= (lastLineSpan.End + 1))
                {
                    nextLineSpan = LineSpan.FromBounds(lastLineSpan.Start, nextLineSpan.End);
                    lineSpans.RemoveAt(lineSpans.Count - 1);
                }
            }

            lineSpans.Add(nextLineSpan);
        }
예제 #6
0
        // Find the lines that surround the span of the difference.  Try to expand the span to
        // include both the previous and next lines so that we can show more context to the
        // user.
        private LineSpan GetLineSpan(
            ITextSnapshot snapshot,
            Span span)
        {
            var startLine = snapshot.GetLineNumberFromPosition(span.Start);
            var endLine   = snapshot.GetLineNumberFromPosition(span.End);

            if (startLine > 0)
            {
                startLine--;
            }

            if (endLine < snapshot.LineCount)
            {
                endLine++;
            }

            return(LineSpan.FromBounds(startLine, endLine));
        }
        public void UpdatePreview(string text)
        {
            const string start = "//[";
            const string end   = "//]";

            var service   = MefV1HostServices.Create(_componentModel.DefaultExportProvider);
            var workspace = new PreviewWorkspace(service);

            var document  = workspace.OpenDocument(DocumentId.CreateNewId("document"), SourceText.From(text), Language);
            var formatted = Formatter.FormatAsync(document, this.Options).WaitAndGetResult(CancellationToken.None);

            var textBuffer = _textBufferFactoryService.CreateTextBuffer(formatted.SourceText.ToString(), _contentType);

            var container = textBuffer.AsTextContainer();
            var documentBackedByTextBuffer = document.WithText(container.CurrentText);

            var bufferText = textBuffer.CurrentSnapshot.GetText().ToString();
            var startIndex = bufferText.IndexOf(start, StringComparison.Ordinal);
            var endIndex   = bufferText.IndexOf(end, StringComparison.Ordinal);
            var startLine  = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(startIndex) + 1;
            var endLine    = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(endIndex);

            var projection = _projectionBufferFactory.CreateProjectionBufferWithoutIndentation(_contentTypeRegistryService,
                                                                                               _editorOptions.CreateOptions(),
                                                                                               textBuffer.CurrentSnapshot,
                                                                                               "",
                                                                                               LineSpan.FromBounds(startLine, endLine));

            var textView = _textEditorFactoryService.CreateTextView(projection,
                                                                    _textEditorFactoryService.CreateTextViewRoleSet());

            this.TextViewHost = _textEditorFactoryService.CreateTextViewHost(textView, setFocus: false);

            workspace.CloseDocument(document.Id);
            workspace.OpenDocument(document.Id, documentBackedByTextBuffer.SourceText, Language);
            //workspace.UpdateDocument(documentBackedByTextBuffer.Id, documentBackedByTextBuffer.SourceText);
        }
        public void UpdatePreview(string text)
        {
            const string start = "//[";
            const string end   = "//]";

            var service   = MefV1HostServices.Create(_componentModel.DefaultExportProvider);
            var workspace = new PreviewWorkspace(service);
            var fileName  = string.Format("project.{0}", Language == "C#" ? "csproj" : "vbproj");
            var project   = workspace.CurrentSolution.AddProject(fileName, "assembly.dll", Language);

            // use the mscorlib, system, and system.core that are loaded in the current process.
            string[] references =
            {
                "mscorlib",
                "System",
                "System.Core"
            };

            var metadataService = workspace.Services.GetService <IMetadataService>();

            var referenceAssemblies = Thread.GetDomain().GetAssemblies()
                                      .Where(x => references.Contains(x.GetName(true).Name, StringComparer.OrdinalIgnoreCase))
                                      .Select(a => metadataService.GetReference(a.Location, MetadataReferenceProperties.Assembly));

            project = project.WithMetadataReferences(referenceAssemblies);

            var document  = project.AddDocument("document", SourceText.From(text, Encoding.UTF8));
            var formatted = Formatter.FormatAsync(document, this.Options).WaitAndGetResult(CancellationToken.None);

            var textBuffer = _textBufferFactoryService.CreateTextBuffer(formatted.GetTextAsync().Result.ToString(), _contentType);

            var container = textBuffer.AsTextContainer();
            var documentBackedByTextBuffer = document.WithText(container.CurrentText);

            var bufferText = textBuffer.CurrentSnapshot.GetText().ToString();
            var startIndex = bufferText.IndexOf(start, StringComparison.Ordinal);
            var endIndex   = bufferText.IndexOf(end, StringComparison.Ordinal);
            var startLine  = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(startIndex) + 1;
            var endLine    = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(endIndex);

            var projection = _projectionBufferFactory.CreateProjectionBufferWithoutIndentation(_contentTypeRegistryService,
                                                                                               _editorOptions.CreateOptions(),
                                                                                               textBuffer.CurrentSnapshot,
                                                                                               "",
                                                                                               LineSpan.FromBounds(startLine, endLine));

            var textView = _textEditorFactoryService.CreateTextView(projection,
                                                                    _textEditorFactoryService.CreateTextViewRoleSet(PredefinedTextViewRoles.Analyzable));

            this.TextViewHost = _textEditorFactoryService.CreateTextViewHost(textView, setFocus: false);

            workspace.TryApplyChanges(documentBackedByTextBuffer.Project.Solution);
            workspace.OpenDocument(document.Id);

            this.TextViewHost.Closed += (s, a) =>
            {
                workspace.Dispose();
                workspace = null;
            };
        }
예제 #9
0
        public void UpdatePreview(string text)
        {
            const string start = "//[";
            const string end   = "//]";

            var sourceText = SourceText.From(text);
            var syntaxTree = SyntaxFactory.ParseSyntaxTree(sourceText);
            var edits      = Formatter.GetEdits(syntaxTree, new TextSpan(sourceText, 0, text.Length), _optionsService.FormattingOptions);
            var formatted  = Formatter.ApplyEdits(text, edits);

            var textBuffer = _textBufferFactoryService.CreateTextBuffer(formatted, _contentType);

            var bufferText = textBuffer.CurrentSnapshot.GetText().ToString();
            var startIndex = bufferText.IndexOf(start, StringComparison.Ordinal);
            var endIndex   = bufferText.IndexOf(end, StringComparison.Ordinal);
            var startLine  = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(startIndex) + 1;
            var endLine    = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(endIndex);

            var projection = _projectionBufferFactory.CreateProjectionBufferWithoutIndentation(_contentTypeRegistryService,
                                                                                               _editorOptions.CreateOptions(),
                                                                                               textBuffer.CurrentSnapshot,
                                                                                               "",
                                                                                               LineSpan.FromBounds(startLine, endLine));

            var textView = _textEditorFactoryService.CreateTextView(projection,
                                                                    _textEditorFactoryService.CreateTextViewRoleSet(PredefinedTextViewRoles.Analyzable));

            this.TextViewHost = _textEditorFactoryService.CreateTextViewHost(textView, setFocus: false);
        }