|
| 1 | +using Microsoft.VisualStudio.Text; |
| 2 | +using Microsoft.VisualStudio.Text.Editor; |
| 3 | +using Microsoft.VisualStudio.Text.Editor.OptionsExtensionMethods; |
| 4 | +using Microsoft.VisualStudio.Utilities; |
| 5 | +using System; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.ComponentModel.Composition; |
| 8 | +using System.Linq; |
| 9 | + |
| 10 | +namespace Microsoft.Quantum.QsLanguageExtensionVS |
| 11 | +{ |
| 12 | + [ContentType("Q#")] |
| 13 | + [Export(typeof(ISmartIndentProvider))] |
| 14 | + internal class QsSmartIndentProvider : ISmartIndentProvider |
| 15 | + { |
| 16 | + public ISmartIndent CreateSmartIndent(ITextView textView) => new QsSmartIndent(textView); |
| 17 | + } |
| 18 | + |
| 19 | + internal class QsSmartIndent : ISmartIndent |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// A list of all opening and closing bracket pairs that affect indentation. |
| 23 | + /// </summary> |
| 24 | + private static readonly IImmutableList<(string open, string close)> brackets = ImmutableList.Create(new[] |
| 25 | + { |
| 26 | + ("[", "]"), |
| 27 | + ("(", ")"), |
| 28 | + ("{", "}") |
| 29 | + }); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// The text view that this smart indent is handling indentation for. |
| 33 | + /// </summary> |
| 34 | + private readonly ITextView textView; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Creates a new smart indent. |
| 38 | + /// </summary> |
| 39 | + /// <param name="textView">The text view that this smart indent is handling indentation for.</param> |
| 40 | + public QsSmartIndent(ITextView textView) |
| 41 | + { |
| 42 | + this.textView = textView; |
| 43 | + |
| 44 | + // The ISmartIndent interface is only for indenting blank lines or indenting after pressing enter. To |
| 45 | + // decrease the indent after typing a closing bracket, we have to watch for changes manually. |
| 46 | + textView.TextBuffer.ChangedHighPriority += TextBuffer_ChangedHighPriority; |
| 47 | + } |
| 48 | + |
| 49 | + public void Dispose() |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Returns the number of spaces to place at the start of the line, or null if there is no desired indentation. |
| 55 | + /// </summary> |
| 56 | + public int? GetDesiredIndentation(ITextSnapshotLine line) |
| 57 | + { |
| 58 | + // Note: The ISmartIndent interface requires that the return type is nullable, but we always return a |
| 59 | + // value. |
| 60 | + |
| 61 | + if (line.LineNumber == 0) |
| 62 | + return 0; |
| 63 | + |
| 64 | + ITextSnapshotLine lastNonEmptyLine = GetLastNonEmptyLine(line); |
| 65 | + int desiredIndent = GetIndentation(lastNonEmptyLine.GetText()); |
| 66 | + int indentSize = textView.Options.GetIndentSize(); |
| 67 | + if (StartsBlock(lastNonEmptyLine.GetText())) |
| 68 | + desiredIndent += indentSize; |
| 69 | + if (EndsBlock(line.GetText())) |
| 70 | + desiredIndent -= indentSize; |
| 71 | + return Math.Max(0, desiredIndent); |
| 72 | + } |
| 73 | + |
| 74 | + private void TextBuffer_ChangedHighPriority(object sender, TextContentChangedEventArgs e) |
| 75 | + { |
| 76 | + foreach (ITextChange change in e.Changes) |
| 77 | + { |
| 78 | + if (EndsBlock(change.NewText)) |
| 79 | + { |
| 80 | + ITextSnapshotLine line = e.After.GetLineFromPosition(change.NewPosition); |
| 81 | + int indent = GetIndentation(line.GetText()); |
| 82 | + int desiredIndent = GetDesiredIndentation(line) ?? 0; |
| 83 | + if (indent != desiredIndent) |
| 84 | + e.After.TextBuffer.Replace( |
| 85 | + new Span(line.Start.Position, line.GetText().TakeWhile(IsIndentation).Count()), |
| 86 | + CreateIndentation(desiredIndent)); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Returns the current indentation of the line in number of spaces. |
| 93 | + /// </summary> |
| 94 | + private int GetIndentation(string line) => |
| 95 | + line |
| 96 | + .TakeWhile(IsIndentation) |
| 97 | + .Aggregate(0, (indent, c) => indent + (c == '\t' ? textView.Options.GetTabSize() : 1)); |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Returns a string containing spaces or tabs (depending on the text view options) to match the given |
| 101 | + /// indentation. |
| 102 | + /// </summary> |
| 103 | + private string CreateIndentation(int indent) |
| 104 | + { |
| 105 | + if (textView.Options.IsConvertTabsToSpacesEnabled()) |
| 106 | + return new string(' ', indent); |
| 107 | + else |
| 108 | + return |
| 109 | + new string('\t', indent / textView.Options.GetTabSize()) + |
| 110 | + new string(' ', indent % textView.Options.GetTabSize()); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Returns true if the end of the line starts a block. |
| 115 | + /// </summary> |
| 116 | + private static bool StartsBlock(string line) => |
| 117 | + brackets.Any(bracket => line.TrimEnd().EndsWith(bracket.open)); |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Returns true if the beginning of the line ends a block. |
| 121 | + /// </summary> |
| 122 | + private static bool EndsBlock(string line) => |
| 123 | + brackets.Any(bracket => line.TrimStart().StartsWith(bracket.close)); |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Returns true if the character is an indentation character (a space or a tab). |
| 127 | + /// </summary> |
| 128 | + private static bool IsIndentation(char c) => c == ' ' || c == '\t'; |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Returns the last non-empty line before the given line. If all of the lines before the given line are empty, |
| 132 | + /// returns the first line of the snapshot instead. |
| 133 | + /// <para/> |
| 134 | + /// Returns null if the given line is the first line in the snapshot. |
| 135 | + /// </summary> |
| 136 | + private static ITextSnapshotLine GetLastNonEmptyLine(ITextSnapshotLine line) |
| 137 | + { |
| 138 | + int lineNumber = line.LineNumber - 1; |
| 139 | + if (lineNumber < 0) |
| 140 | + return null; |
| 141 | + while (lineNumber > 0 && line.Snapshot.GetLineFromLineNumber(lineNumber).Length == 0) |
| 142 | + lineNumber--; |
| 143 | + return line.Snapshot.GetLineFromLineNumber(lineNumber); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments