|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.ComponentModel.Composition; |
| 3 | +using Microsoft.VisualStudio; |
| 4 | +using Microsoft.VisualStudio.Shell; |
| 5 | +using Microsoft.VisualStudio.Shell.Interop; |
| 6 | +using Microsoft.VisualStudio.TextManager.Interop; |
| 7 | +using GitHub.Models; |
| 8 | + |
| 9 | +namespace GitHub.Services |
| 10 | +{ |
| 11 | + [Export(typeof(IPullRequestEditorService))] |
| 12 | + public class PullRequestEditorService : IPullRequestEditorService |
| 13 | + { |
| 14 | + readonly IGitHubServiceProvider serviceProvider; |
| 15 | + |
| 16 | + // If the target line doesn't have a unique match, search this number of lines above looking for a match. |
| 17 | + public const int MatchLinesAboveTarget = 4; |
| 18 | + |
| 19 | + [ImportingConstructor] |
| 20 | + public PullRequestEditorService(IGitHubServiceProvider serviceProvider) |
| 21 | + { |
| 22 | + this.serviceProvider = serviceProvider; |
| 23 | + } |
| 24 | + |
| 25 | + public IVsTextView NavigateToEquivalentPosition(IVsTextView sourceView, string targetFile) |
| 26 | + { |
| 27 | + int line; |
| 28 | + int column; |
| 29 | + ErrorHandler.ThrowOnFailure(sourceView.GetCaretPos(out line, out column)); |
| 30 | + var text1 = GetText(sourceView); |
| 31 | + |
| 32 | + var view = OpenDocument(targetFile); |
| 33 | + var text2 = VsShellUtilities.GetRunningDocumentContents(serviceProvider, targetFile); |
| 34 | + |
| 35 | + var fromLines = ReadLines(text1); |
| 36 | + var toLines = ReadLines(text2); |
| 37 | + var matchingLine = FindMatchingLine(fromLines, toLines, line, matchLinesAbove: MatchLinesAboveTarget); |
| 38 | + if (matchingLine == -1) |
| 39 | + { |
| 40 | + // If we can't match line use orignal as best guess. |
| 41 | + matchingLine = line < toLines.Count ? line : toLines.Count - 1; |
| 42 | + column = 0; |
| 43 | + } |
| 44 | + |
| 45 | + ErrorHandler.ThrowOnFailure(view.SetCaretPos(matchingLine, column)); |
| 46 | + ErrorHandler.ThrowOnFailure(view.CenterLines(matchingLine, 1)); |
| 47 | + |
| 48 | + return view; |
| 49 | + } |
| 50 | + |
| 51 | + public IVsTextView FindActiveView() |
| 52 | + { |
| 53 | + var textManager = serviceProvider.GetService<SVsTextManager, IVsTextManager2>(); |
| 54 | + IVsTextView view; |
| 55 | + var hresult = textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out view); |
| 56 | + return hresult == VSConstants.S_OK ? view : null; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Find the closest matching line in <see cref="toLines"/>. |
| 61 | + /// </summary> |
| 62 | + /// <remarks> |
| 63 | + /// When matching we prioritize unique matching lines in <see cref="toLines"/>. If the target line isn't |
| 64 | + /// unique, continue searching the lines above for a better match and use this as anchor with an offset. |
| 65 | + /// The closest match to <see cref="line"/> with the fewest duplicate matches will be used for the matching line. |
| 66 | + /// </remarks> |
| 67 | + /// <param name="fromLines">The document we're navigating from.</param> |
| 68 | + /// <param name="toLines">The document we're navigating to.</param> |
| 69 | + /// <param name="line">The 0-based line we're navigating from.</param> |
| 70 | + /// <returns>The best matching line in <see cref="toLines"/></returns> |
| 71 | + public int FindMatchingLine(IList<string> fromLines, IList<string> toLines, int line, int matchLinesAbove = 0) |
| 72 | + { |
| 73 | + var matchingLine = -1; |
| 74 | + var minMatchedLines = -1; |
| 75 | + for (var offset = 0; offset <= matchLinesAbove; offset++) |
| 76 | + { |
| 77 | + var targetLine = line - offset; |
| 78 | + if (targetLine < 0) |
| 79 | + { |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + int matchedLines; |
| 84 | + var nearestLine = FindNearestMatchingLine(fromLines, toLines, targetLine, out matchedLines); |
| 85 | + if (nearestLine != -1) |
| 86 | + { |
| 87 | + if (matchingLine == -1 || minMatchedLines >= matchedLines) |
| 88 | + { |
| 89 | + matchingLine = nearestLine + offset; |
| 90 | + minMatchedLines = matchedLines; |
| 91 | + } |
| 92 | + |
| 93 | + if (minMatchedLines == 1) |
| 94 | + { |
| 95 | + break; // We've found a unique matching line! |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (matchingLine >= toLines.Count) |
| 101 | + { |
| 102 | + matchingLine = toLines.Count - 1; |
| 103 | + } |
| 104 | + |
| 105 | + return matchingLine; |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Find the nearest matching line to <see cref="line"/> and the number of similar matched lines in the text. |
| 110 | + /// </summary> |
| 111 | + /// <param name="fromLines">The document we're navigating from.</param> |
| 112 | + /// <param name="toLines">The document we're navigating to.</param> |
| 113 | + /// <param name="line">The 0-based line we're navigating from.</param> |
| 114 | + /// <param name="matchedLines">The number of similar matched lines in <see cref="toLines"/></param> |
| 115 | + /// <returns>Find the nearest matching line in <see cref="toLines"/>.</returns> |
| 116 | + public int FindNearestMatchingLine(IList<string> fromLines, IList<string> toLines, int line, out int matchedLines) |
| 117 | + { |
| 118 | + line = line < fromLines.Count ? line : fromLines.Count - 1; // VS shows one extra line at end |
| 119 | + var fromLine = fromLines[line]; |
| 120 | + |
| 121 | + matchedLines = 0; |
| 122 | + var matchingLine = -1; |
| 123 | + for (var offset = 0; true; offset++) |
| 124 | + { |
| 125 | + var lineAbove = line + offset; |
| 126 | + var checkAbove = lineAbove < toLines.Count; |
| 127 | + if (checkAbove && toLines[lineAbove] == fromLine) |
| 128 | + { |
| 129 | + if (matchedLines == 0) |
| 130 | + { |
| 131 | + matchingLine = lineAbove; |
| 132 | + } |
| 133 | + |
| 134 | + matchedLines++; |
| 135 | + } |
| 136 | + |
| 137 | + var lineBelow = line - offset; |
| 138 | + var checkBelow = lineBelow >= 0; |
| 139 | + if (checkBelow && offset > 0 && lineBelow < toLines.Count && toLines[lineBelow] == fromLine) |
| 140 | + { |
| 141 | + if (matchedLines == 0) |
| 142 | + { |
| 143 | + matchingLine = lineBelow; |
| 144 | + } |
| 145 | + |
| 146 | + matchedLines++; |
| 147 | + } |
| 148 | + |
| 149 | + if (!checkAbove && !checkBelow) |
| 150 | + { |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return matchingLine; |
| 156 | + } |
| 157 | + |
| 158 | + string GetText(IVsTextView textView) |
| 159 | + { |
| 160 | + IVsTextLines buffer; |
| 161 | + ErrorHandler.ThrowOnFailure(textView.GetBuffer(out buffer)); |
| 162 | + |
| 163 | + int line; |
| 164 | + int index; |
| 165 | + ErrorHandler.ThrowOnFailure(buffer.GetLastLineIndex(out line, out index)); |
| 166 | + |
| 167 | + string text; |
| 168 | + ErrorHandler.ThrowOnFailure(buffer.GetLineText(0, 0, line, index, out text)); |
| 169 | + return text; |
| 170 | + } |
| 171 | + |
| 172 | + IVsTextView OpenDocument(string fullPath) |
| 173 | + { |
| 174 | + var logicalView = VSConstants.LOGVIEWID.TextView_guid; |
| 175 | + IVsUIHierarchy hierarchy; |
| 176 | + uint itemID; |
| 177 | + IVsWindowFrame windowFrame; |
| 178 | + IVsTextView view; |
| 179 | + VsShellUtilities.OpenDocument(serviceProvider, fullPath, logicalView, out hierarchy, out itemID, out windowFrame, out view); |
| 180 | + return view; |
| 181 | + } |
| 182 | + |
| 183 | + static IList<string> ReadLines(string text) |
| 184 | + { |
| 185 | + var lines = new List<string>(); |
| 186 | + var reader = new DiffUtilities.LineReader(text); |
| 187 | + string line; |
| 188 | + while ((line = reader.ReadLine()) != null) |
| 189 | + { |
| 190 | + lines.Add(line); |
| 191 | + } |
| 192 | + |
| 193 | + return lines; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments