Beispiel #1
0
        public ProjectionBufferManager(ITextBuffer diskBuffer,
                                       IProjectionBufferFactoryService projectionBufferFactoryService,
                                       IContentTypeRegistryService contentTypeRegistryService,
                                       string secondaryContentTypeName)
        {
            DiskBuffer = diskBuffer;

            _contentTypeRegistryService = contentTypeRegistryService;

            var snapshot = diskBuffer.CurrentSnapshot;

            var shaderLabSyntaxTree = SyntaxFactory.ParseUnitySyntaxTree(new Text.VisualStudioSourceText(snapshot, null, true));

            var cgBlockVisitor = new CgBlockVisitor();
            //cgBlockVisitor.Visit(shaderLabSyntaxTree.Root);
            var cgBlockSpans = cgBlockVisitor.CgBlockSpans;

            var dataBufferSpans = new List <object>();

            var secondaryContentType = _contentTypeRegistryService.GetContentType(secondaryContentTypeName);
            var primaryIndex         = 0;

            foreach (var cgBlockSpan in cgBlockSpans)
            {
                var primarySpan = Span.FromBounds(primaryIndex, cgBlockSpan.Start);
                if (!primarySpan.IsEmpty)
                {
                    dataBufferSpans.Add(snapshot.CreateTrackingSpan(primarySpan, SpanTrackingMode.EdgeExclusive));
                }

                var elisionBuffer = projectionBufferFactoryService.CreateElisionBuffer(null,
                                                                                       new NormalizedSnapshotSpanCollection(new SnapshotSpan(snapshot, cgBlockSpan)),
                                                                                       ElisionBufferOptions.None, secondaryContentType);

                dataBufferSpans.Add(elisionBuffer.CurrentSnapshot.CreateTrackingSpan(0, elisionBuffer.CurrentSnapshot.Length, SpanTrackingMode.EdgeInclusive));

                primaryIndex = cgBlockSpan.End;
            }

            // Last span.
            {
                var primarySpan = Span.FromBounds(primaryIndex, snapshot.Length);
                if (!primarySpan.IsEmpty)
                {
                    dataBufferSpans.Add(snapshot.CreateTrackingSpan(primarySpan, SpanTrackingMode.EdgeExclusive));
                }
            }

            ViewBuffer = projectionBufferFactoryService.CreateProjectionBuffer(null, dataBufferSpans, ProjectionBufferOptions.None);

            DiskBuffer.Properties.AddProperty(typeof(IProjectionBufferManager), this);
            ViewBuffer.Properties.AddProperty(typeof(IProjectionBufferManager), this);
        }
Beispiel #2
0
        private async Task UpdateBuffersAsync(Document document, CancellationToken cancellationToken)
        {
            var shaderLabSyntaxTree = (SyntaxTree)await document.GetSyntaxTreeAsync(cancellationToken);

            var cgBlockVisitor = new CgBlockVisitor(shaderLabSyntaxTree);

            cgBlockVisitor.Visit((SyntaxNode)shaderLabSyntaxTree.Root);
            var cgBlockSpans = cgBlockVisitor.CgBlockSpans;

            var snapshot = document.SourceText.FindCorrespondingEditorTextSnapshot();

            var dataBufferSpans = new List <object>();

            var primaryIndex = 0;

            foreach (var cgBlockSpan in cgBlockSpans)
            {
                var primarySpan = Span.FromBounds(primaryIndex, cgBlockSpan.Start);
                if (!primarySpan.IsEmpty)
                {
                    dataBufferSpans.Add(snapshot.CreateTrackingSpan(
                                            primarySpan,
                                            SpanTrackingMode.EdgeExclusive));
                }

                var elisionBuffer = _projectionBufferFactoryService.CreateElisionBuffer(
                    null,
                    new NormalizedSnapshotSpanCollection(new SnapshotSpan(snapshot, cgBlockSpan)),
                    ElisionBufferOptions.None,
                    _hlslContentType);

                dataBufferSpans.Add(elisionBuffer.CurrentSnapshot.CreateTrackingSpan(
                                        0,
                                        elisionBuffer.CurrentSnapshot.Length,
                                        SpanTrackingMode.EdgeInclusive));

                primaryIndex = cgBlockSpan.End;
            }

            // Last span.
            {
                var primarySpan = Span.FromBounds(primaryIndex, snapshot.Length);
                if (!primarySpan.IsEmpty)
                {
                    dataBufferSpans.Add(snapshot.CreateTrackingSpan(primarySpan, SpanTrackingMode.EdgeExclusive));
                }
            }

            // TODO: Make this a bit more type-safe.
            var viewBuffer = snapshot.TextBuffer.Properties.GetProperty <IProjectionBuffer>(typeof(IProjectionBuffer));

            _foregroundNotificationService.RegisterNotification(
                () =>
            {
                viewBuffer.ReplaceSpans(
                    0,
                    viewBuffer.CurrentSnapshot.SpanCount,
                    dataBufferSpans,
                    EditOptions.None,
                    null);
            },
                _listener.BeginAsyncOperation("ReplaceProjectionBufferSpans"),
                cancellationToken);
        }