Exemple #1
0
        private void AdjustIndentation(IProjectionBuffer subjectBuffer, IEnumerable <int> visibleSpanIndex)
        {
            if (!visibleSpanIndex.Any())
            {
                return;
            }

            var snapshot = subjectBuffer.CurrentSnapshot;
            var document = _workspace.CurrentSolution.GetDocument(this.Id);

            if (!document.SupportsSyntaxTree)
            {
                return;
            }

            var originalText = document.GetTextSynchronously(CancellationToken.None);

            Debug.Assert(object.ReferenceEquals(originalText, snapshot.AsText()));

            var root = document.GetSyntaxRootSynchronously(CancellationToken.None);

            var editorOptionsFactory = _componentModel.GetService <IEditorOptionsFactoryService>();
            var editorOptions        = editorOptionsFactory.GetOptions(DataBuffer);
            var options = _workspace.Options
                          .WithChangedOption(FormattingOptions.NewLine, root.Language, editorOptions.GetNewLineCharacter())
                          .WithChangedOption(FormattingOptions.UseTabs, root.Language, !editorOptions.IsConvertTabsToSpacesEnabled())
                          .WithChangedOption(FormattingOptions.TabSize, root.Language, editorOptions.GetTabSize())
                          .WithChangedOption(FormattingOptions.IndentationSize, root.Language, editorOptions.GetIndentSize());

            using (var pooledObject = SharedPools.Default <List <TextSpan> >().GetPooledObject())
            {
                var spans = pooledObject.Object;

                spans.AddRange(this.GetEditorVisibleSpans());
                using (var edit = subjectBuffer.CreateEdit(s_venusEditOptions, reiteratedVersionNumber: null, editTag: null))
                {
                    foreach (var spanIndex in visibleSpanIndex)
                    {
                        var rule = GetBaseIndentationRule(root, originalText, spans, spanIndex);

                        var visibleSpan = spans[spanIndex];
                        AdjustIndentationForSpan(document, edit, visibleSpan, rule, options);
                    }

                    edit.Apply();
                }
            }
        }
        private static void ApplyChanges(
            IProjectionBuffer subjectBuffer,
            IEnumerable <TextChange> changes,
            IList <TextSpan> visibleSpansInOriginal,
            out IEnumerable <int> affectedVisibleSpansInNew)
        {
            using var edit = subjectBuffer.CreateEdit(s_venusEditOptions, reiteratedVersionNumber: null, editTag: null);

            var affectedSpans = SharedPools.Default <HashSet <int> >().AllocateAndClear();

            affectedVisibleSpansInNew = affectedSpans;

            var currentVisibleSpanIndex = 0;

            foreach (var change in changes)
            {
                // Find the next visible span that either overlaps or intersects with
                while (currentVisibleSpanIndex < visibleSpansInOriginal.Count &&
                       visibleSpansInOriginal[currentVisibleSpanIndex].End < change.Span.Start)
                {
                    currentVisibleSpanIndex++;
                }

                // no more place to apply text changes
                if (currentVisibleSpanIndex >= visibleSpansInOriginal.Count)
                {
                    break;
                }

                var newText = change.NewText;
                var span    = change.Span.ToSpan();

                edit.Replace(span, newText);

                affectedSpans.Add(currentVisibleSpanIndex);
            }

            edit.ApplyAndLogExceptions();
        }
Exemple #3
0
        private void AdjustIndentation(IProjectionBuffer subjectBuffer, IEnumerable<int> visibleSpanIndex)
        {
            if (!visibleSpanIndex.Any())
            {
                return;
            }

            var snapshot = subjectBuffer.CurrentSnapshot;
            var document = _workspace.CurrentSolution.GetDocument(this.Id);
            if (!document.SupportsSyntaxTree)
            {
                return;
            }

            var originalText = document.GetTextAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None);
            Contract.Requires(object.ReferenceEquals(originalText, snapshot.AsText()));

            var root = document.GetSyntaxRootAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None);

            var editorOptionsFactory = _componentModel.GetService<IEditorOptionsFactoryService>();
            var editorOptions = editorOptionsFactory.GetOptions(_containedLanguage.DataBuffer);
            var options = _workspace.Options
                                        .WithChangedOption(FormattingOptions.UseTabs, root.Language, !editorOptions.IsConvertTabsToSpacesEnabled())
                                        .WithChangedOption(FormattingOptions.TabSize, root.Language, editorOptions.GetTabSize())
                                        .WithChangedOption(FormattingOptions.IndentationSize, root.Language, editorOptions.GetIndentSize());

            using (var pooledObject = SharedPools.Default<List<TextSpan>>().GetPooledObject())
            {
                var spans = pooledObject.Object;

                spans.AddRange(this.GetEditorVisibleSpans());
                using (var edit = subjectBuffer.CreateEdit(s_venusEditOptions, reiteratedVersionNumber: null, editTag: null))
                {
                    foreach (var spanIndex in visibleSpanIndex)
                    {
                        var rule = GetBaseIndentationRule(root, originalText, spans, spanIndex);

                        var visibleSpan = spans[spanIndex];
                        AdjustIndentationForSpan(document, edit, visibleSpan, rule, options);
                    }

                    edit.Apply();
                }
            }
        }
Exemple #4
0
        private static void ApplyChanges(
            IProjectionBuffer subjectBuffer,
            IEnumerable<TextChange> changes,
            IList<TextSpan> visibleSpansInOriginal,
            out IEnumerable<int> affectedVisibleSpansInNew)
        {
            using (var edit = subjectBuffer.CreateEdit(s_venusEditOptions, reiteratedVersionNumber: null, editTag: null))
            {
                var affectedSpans = SharedPools.Default<HashSet<int>>().AllocateAndClear();
                affectedVisibleSpansInNew = affectedSpans;

                var currentVisibleSpanIndex = 0;
                foreach (var change in changes)
                {
                    // Find the next visible span that either overlaps or intersects with 
                    while (currentVisibleSpanIndex < visibleSpansInOriginal.Count &&
                           visibleSpansInOriginal[currentVisibleSpanIndex].End < change.Span.Start)
                    {
                        currentVisibleSpanIndex++;
                    }

                    // no more place to apply text changes
                    if (currentVisibleSpanIndex >= visibleSpansInOriginal.Count)
                    {
                        break;
                    }

                    var newText = change.NewText;
                    var span = change.Span.ToSpan();

                    edit.Replace(span, newText);

                    affectedSpans.Add(currentVisibleSpanIndex);
                }

                edit.Apply();
            }
        }