예제 #1
0
        /// <summary>
        /// For a given <see cref="ITextSnapshotLine"/> gets the new line character to be inserted to the line based on
        /// either the given line, or the second last line or the default new line charcter provided by <see cref="IEditorOptions"/>
        /// </summary>
        /// <param name="line">The <see cref="ITextSnapshotLine"/> for whcih the new line character is to be decied for</param>
        /// <param name="editorOptions">The current set of <see cref="IEditorOptions"/> applicable for the given <see cref="ITextSnapshotLine"/></param>
        /// <returns>The new line character to be inserted</returns>
        public static string GetNewLineCharacterToInsert(ITextSnapshotLine line, IEditorOptions editorOptions)
        {
            string lineBreak = null;
            var    snapshot  = line.Snapshot;

            if (editorOptions.GetReplicateNewLineCharacter())
            {
                if (line.LineBreakLength > 0)
                {
                    // use the same line ending as the current line
                    lineBreak = line.GetLineBreakText();
                }
                else
                {
                    if (snapshot.LineCount > 1)
                    {
                        // use the same line ending as the penultimate line in the buffer
                        lineBreak = snapshot.GetLineFromLineNumber(snapshot.LineCount - 2).GetLineBreakText();
                    }
                }
            }
            string textToInsert = lineBreak ?? editorOptions.GetNewLineCharacter();

            return(textToInsert);
        }
        public override bool InsertNewLine()
        {
            string lineBreak = null;

            if (_editorOptions.GetReplicateNewLineCharacter())
            {
                ITextSnapshot     snapshot            = _textBuffer.AdvancedTextBuffer.CurrentSnapshot;
                int               position            = _trackingPoint.GetPosition(snapshot);
                ITextSnapshotLine currentSnapshotLine = snapshot.GetLineFromPosition(position);
                if (currentSnapshotLine.LineBreakLength > 0)
                {
                    // use the same line ending as the current line
                    lineBreak = currentSnapshotLine.GetLineBreakText();
                }
                else
                {
                    // we are on the last line of the buffer
                    if (snapshot.LineCount > 1)
                    {
                        // use the same line ending as the penultimate line in the buffer
                        lineBreak = snapshot.GetLineFromLineNumber(snapshot.LineCount - 2).GetLineBreakText();
                    }
                }
            }
            return(InsertText(lineBreak ?? _editorOptions.GetNewLineCharacter()));
        }
 private static string GetSubmissionFromSelectedSpans(
     IEditorOptions editorOptions,
     IEnumerable <SnapshotSpan> selectedSpans
     ) =>
 string.Join(
     editorOptions.GetNewLineCharacter(),
     selectedSpans.Select(ss => ss.GetText())
     );
예제 #4
0
    private static LineFormattingOptions GetLineFormattingOptionsImpl(ITextBuffer textBuffer, IEditorOptions editorOptions, IIndentationManagerService indentationManager, bool explicitFormat)
    {
        indentationManager.GetIndentation(textBuffer, explicitFormat, out var convertTabsToSpaces, out var tabSize, out var indentSize);

        return(new LineFormattingOptions()
        {
            UseTabs = !convertTabsToSpaces,
            IndentationSize = indentSize,
            TabSize = tabSize,
            NewLine = editorOptions.GetNewLineCharacter(),
        });
    }
예제 #5
0
        public static string GetLineBreak(this IEditorOptions editorOptions, SnapshotPoint pos)
        {
            if (editorOptions.GetReplicateNewLineCharacter())
            {
                var line = pos.GetContainingLine();
                if (line.LineBreakLength != 0)
                {
                    return(pos.Snapshot.GetText(line.Extent.End.Position, line.LineBreakLength));
                }
                if (line.LineNumber != 0)
                {
                    line = pos.Snapshot.GetLineFromLineNumber(line.LineNumber - 1);
                    return(pos.Snapshot.GetText(line.Extent.End.Position, line.LineBreakLength));
                }
            }
            var linebreak = editorOptions.GetNewLineCharacter();

            return(linebreak.Length != 0 ? linebreak : Environment.NewLine);
        }
예제 #6
0
        public bool TryGetDocumentOption(Document document, OptionKey option, OptionSet underlyingOptions, out object value)
        {
            // From the document, look up the ITextBuffer, and find its IEditorOptions.
            // Note that IEditorOptions are also available from an ITextView. Not sure if those options can differ from the ones on the buffer.
            var            workspace     = PrimaryWorkspace.Workspace as VisualStudioWorkspace;
            var            textBuffer    = workspace?.GetTextBufferForDocument(document.Id);
            IEditorOptions editorOptions = textBuffer?.Properties[typeof(IEditorOptions)] as IEditorOptions;

            if (editorOptions == null)
            {
                value = null;
                return(false);
            }

            // Check if the OptionKey is one of the ones we want to override with values from the IEditorOptions
            if (option.Option == FormattingOptions.UseTabs)
            {
                value = !editorOptions.IsConvertTabsToSpacesEnabled();
                return(true);
            }
            else if (option.Option == FormattingOptions.TabSize)
            {
                value = editorOptions.GetTabSize();
                return(true);
            }
            else if (option.Option == FormattingOptions.IndentationSize)
            {
                value = editorOptions.GetIndentSize();
                return(true);
            }
            else if (option.Option == FormattingOptions.NewLine)
            {
                value = editorOptions.GetNewLineCharacter();
                return(true);
            }
            else
            {
                value = null;
                return(false);
            }
        }
        private static IProjectionBuffer CreateProjectionBuffer(
            IProjectionBufferFactoryService factoryService,
            IContentTypeRegistryService registryService,
            IEditorOptions editorOptions,
            ITextSnapshot snapshot,
            string separator,
            object?suffixOpt,
            bool trim,
            params LineSpan[] exposedLineSpans
            )
        {
            var spans = new List <object>();

            if (exposedLineSpans.Length > 0)
            {
                if (exposedLineSpans[0].Start > 0 && !string.IsNullOrEmpty(separator))
                {
                    spans.Add(separator);
                    spans.Add(editorOptions.GetNewLineCharacter());
                }

                var snapshotSpanRanges = CreateSnapshotSpanRanges(snapshot, exposedLineSpans);
                var indentColumn       = trim
                    ? DetermineIndentationColumn(editorOptions, snapshotSpanRanges.Flatten())
                    : 0;

                foreach (var snapshotSpanRange in snapshotSpanRanges)
                {
                    foreach (var snapshotSpan in snapshotSpanRange)
                    {
                        var line           = snapshotSpan.Snapshot.GetLineFromPosition(snapshotSpan.Start);
                        var indentPosition =
                            line.GetLineOffsetFromColumn(indentColumn, editorOptions) + line.Start;
                        var mappedSpan = new SnapshotSpan(
                            snapshotSpan.Snapshot,
                            Span.FromBounds(indentPosition, snapshotSpan.End)
                            );

                        var trackingSpan = mappedSpan.CreateTrackingSpan(
                            SpanTrackingMode.EdgeExclusive
                            );

                        spans.Add(trackingSpan);

                        // Add a newline between every line.
                        if (snapshotSpan != snapshotSpanRange.Last())
                        {
                            spans.Add(editorOptions.GetNewLineCharacter());
                        }
                    }

                    // Add a separator between every set of lines.
                    if (snapshotSpanRange != snapshotSpanRanges.Last())
                    {
                        spans.Add(editorOptions.GetNewLineCharacter());
                        spans.Add(separator);
                        spans.Add(editorOptions.GetNewLineCharacter());
                    }
                }

                if (
                    snapshot.GetLineNumberFromPosition(snapshotSpanRanges.Last().Last().End)
                    < snapshot.LineCount - 1
                    )
                {
                    spans.Add(editorOptions.GetNewLineCharacter());
                    spans.Add(separator);
                }
            }

            if (suffixOpt != null)
            {
                if (spans.Count >= 0)
                {
                    if (!separator.Equals(spans.Last()))
                    {
                        spans.Add(editorOptions.GetNewLineCharacter());
                        spans.Add(separator);
                    }

                    spans.Add(editorOptions.GetNewLineCharacter());
                }

                spans.Add(suffixOpt);
            }

            return(factoryService.CreateProjectionBuffer(
                       projectionEditResolver: null,
                       sourceSpans: spans,
                       options: ProjectionBufferOptions.None,
                       contentType: registryService.GetContentType(RoslynPreviewContentType)
                       ));
        }
        private static IProjectionBuffer CreateProjectionBuffer(
            IProjectionBufferFactoryService factoryService,
            IContentTypeRegistryService registryService,
            IEditorOptions editorOptions,
            ITextSnapshot snapshot,
            string separator,
            object suffixOpt,
            bool trim,
            params LineSpan[] exposedLineSpans)
        {
            var spans = new List<object>();
            if (exposedLineSpans.Length > 0)
            {
                if (exposedLineSpans[0].Start > 0 && !string.IsNullOrEmpty(separator))
                {
                    spans.Add(separator);
                    spans.Add(editorOptions.GetNewLineCharacter());
                }

                var snapshotSpanRanges = CreateSnapshotSpanRanges(snapshot, exposedLineSpans);
                var indentColumn = trim
                    ? DetermineIndentationColumn(editorOptions, snapshotSpanRanges.Flatten())
                    : 0;

                foreach (var snapshotSpanRange in snapshotSpanRanges)
                {
                    foreach (var snapshotSpan in snapshotSpanRange)
                    {
                        var line = snapshotSpan.Snapshot.GetLineFromPosition(snapshotSpan.Start);
                        var indentPosition = line.GetLineOffsetFromColumn(indentColumn, editorOptions) + line.Start;
                        var mappedSpan = new SnapshotSpan(snapshotSpan.Snapshot,
                            Span.FromBounds(indentPosition, snapshotSpan.End));

                        var trackingSpan = mappedSpan.CreateTrackingSpan(SpanTrackingMode.EdgeExclusive);

                        spans.Add(trackingSpan);

                        // Add a newline between every line.
                        if (snapshotSpan != snapshotSpanRange.Last())
                        {
                            spans.Add(editorOptions.GetNewLineCharacter());
                        }
                    }

                    // Add a separator between every set of lines.
                    if (snapshotSpanRange != snapshotSpanRanges.Last())
                    {
                        spans.Add(editorOptions.GetNewLineCharacter());
                        spans.Add(separator);
                        spans.Add(editorOptions.GetNewLineCharacter());
                    }
                }

                if (snapshot.GetLineNumberFromPosition(snapshotSpanRanges.Last().Last().End) < snapshot.LineCount - 1)
                {
                    spans.Add(editorOptions.GetNewLineCharacter());
                    spans.Add(separator);
                }
            }

            if (suffixOpt != null)
            {
                if (spans.Count >= 0)
                {
                    if (!separator.Equals(spans.Last()))
                    {
                        spans.Add(editorOptions.GetNewLineCharacter());
                        spans.Add(separator);
                    }

                    spans.Add(editorOptions.GetNewLineCharacter());
                }

                spans.Add(suffixOpt);
            }

            return factoryService.CreateProjectionBuffer(
                projectionEditResolver: null,
                sourceSpans: spans,
                options: ProjectionBufferOptions.None,
                contentType: registryService.GetContentType(ShaderPreviewContentType));
        }
 private static string GetSubmissionFromSelectedSpans(IEditorOptions editorOptions, IEnumerable<SnapshotSpan> selectedSpans)
 {
     return string.Join(editorOptions.GetNewLineCharacter(), selectedSpans.Select(ss => ss.GetText()));
 }
예제 #10
0
        // Code taken from https://github.com/dotnet/roslyn/blob/master/src/EditorFeatures/Core/Shared/Extensions/IProjectionBufferFactoryServiceExtensions.cs
        public static IProjectionBuffer CreateProjectionBufferWithoutIndentation(
            this IProjectionBufferFactoryService projectionBufferFactoryService,
            IEditorOptions editorOptions,
            ITextSnapshot textSnapshot,
            string separator,
            params LineSpan[] exposedLineSpans)
        {
            var spans = new List <object>();

            if (exposedLineSpans.Length > 0)
            {
                if (exposedLineSpans[0].Start > 0 && !string.IsNullOrEmpty(separator))
                {
                    spans.Add(separator);
                    spans.Add(editorOptions.GetNewLineCharacter());
                }

                IList <IList <SnapshotSpan> > snapshotSpanRanges = CreateSnapshotSpanRanges(textSnapshot, exposedLineSpans);
                int indentColumn = DetermineIndentationColumn(editorOptions, snapshotSpanRanges.SelectMany(s => s));

                foreach (IList <SnapshotSpan> snapshotSpanRange in snapshotSpanRanges)
                {
                    foreach (SnapshotSpan snapshotSpan in snapshotSpanRange)
                    {
                        ITextSnapshotLine line = snapshotSpan.Snapshot.GetLineFromPosition(snapshotSpan.Start);
                        int indentPosition     = line.GetText().GetLineOffsetFromColumn(indentColumn, editorOptions.GetTabSize()) + line.Start;
                        var mappedSpan         = new SnapshotSpan(snapshotSpan.Snapshot, Span.FromBounds(indentPosition, snapshotSpan.End));

                        ITrackingSpan trackingSpan = mappedSpan.Snapshot.CreateTrackingSpan(mappedSpan, SpanTrackingMode.EdgeExclusive);

                        spans.Add(trackingSpan);

                        // Add a newline between every line.
                        if (snapshotSpan != snapshotSpanRange.Last())
                        {
                            spans.Add(editorOptions.GetNewLineCharacter());
                        }
                    }

                    // Add a separator between every set of lines.
                    if (snapshotSpanRange != snapshotSpanRanges.Last())
                    {
                        spans.Add(editorOptions.GetNewLineCharacter());
                        spans.Add(separator);
                        spans.Add(editorOptions.GetNewLineCharacter());
                    }
                }

                if (textSnapshot.GetLineNumberFromPosition(snapshotSpanRanges.Last().Last().End) < textSnapshot.LineCount - 1)
                {
                    spans.Add(editorOptions.GetNewLineCharacter());
                    spans.Add(separator);
                }
            }

            return(projectionBufferFactoryService.CreateProjectionBuffer(
                       projectionEditResolver: null,
                       sourceSpans: spans,
                       options: ProjectionBufferOptions.None,
                       contentType: textSnapshot.ContentType));
        }