|
| 1 | +// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +// software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +// without restriction, including without limitation the rights to use, copy, modify, merge, |
| 6 | +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 7 | +// to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +// substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 15 | +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 17 | +// DEALINGS IN THE SOFTWARE. |
| 18 | +using System; |
| 19 | +using System.Text; |
| 20 | +using ICSharpCode.AvalonEdit.Document; |
| 21 | +using ICSharpCode.NRefactory.Editor; |
| 22 | + |
| 23 | +namespace ICSharpCode.SharpDevelop.Editor |
| 24 | +{ |
| 25 | + public static class ToolTipUtils |
| 26 | + { |
| 27 | + public static string GetAlignedText(IDocument document, int startOffset, int endOffset, int maxLineCount = 15) |
| 28 | + { |
| 29 | + // This fixes SD-1394: |
| 30 | + // Each line is checked for leading indentation whitespaces. If |
| 31 | + // a line has the same or more indentation than the first line, |
| 32 | + // it is reduced. If a line is less indented than the first line |
| 33 | + // the indentation is removed completely. |
| 34 | + // |
| 35 | + // See the following example: |
| 36 | + // line 1 |
| 37 | + // line 2 |
| 38 | + // line 3 |
| 39 | + // line 4 |
| 40 | + // |
| 41 | + // is reduced to: |
| 42 | + // line 1 |
| 43 | + // line 2 |
| 44 | + // line 3 |
| 45 | + // line 4 |
| 46 | + |
| 47 | + if (maxLineCount < 1) |
| 48 | + maxLineCount = int.MaxValue; |
| 49 | + |
| 50 | + IDocumentLine startLine = document.GetLineByOffset(startOffset); |
| 51 | + IDocumentLine endLine = document.GetLineByOffset(endOffset); |
| 52 | + StringBuilder builder = new StringBuilder(); |
| 53 | + |
| 54 | + IDocumentLine current = startLine; |
| 55 | + ISegment startIndent = TextUtilities.GetWhitespaceAfter(document, startLine.Offset); |
| 56 | + int lineCount = 0; |
| 57 | + while (current != endLine.NextLine && lineCount < maxLineCount) { |
| 58 | + ISegment currentIndent = TextUtilities.GetWhitespaceAfter(document, current.Offset); |
| 59 | + |
| 60 | + if (current == startLine && current == endLine) |
| 61 | + builder.Append(document.GetText(startOffset, endOffset - startOffset)); |
| 62 | + else if (current == startLine) { |
| 63 | + if (current.EndOffset - startOffset > 0) |
| 64 | + builder.AppendLine(document.GetText(startOffset, current.EndOffset - startOffset).TrimStart()); |
| 65 | + } else if (current == endLine) { |
| 66 | + if (startIndent.Length <= currentIndent.Length) |
| 67 | + builder.Append(document.GetText(current.Offset + startIndent.Length, endOffset - current.Offset - startIndent.Length)); |
| 68 | + else |
| 69 | + builder.Append(document.GetText(current.Offset + currentIndent.Length, endOffset - current.Offset - currentIndent.Length)); |
| 70 | + } else { |
| 71 | + if (startIndent.Length <= currentIndent.Length) |
| 72 | + builder.AppendLine(document.GetText(current.Offset + startIndent.Length, current.Length - startIndent.Length)); |
| 73 | + else |
| 74 | + builder.AppendLine(document.GetText(current.Offset + currentIndent.Length, current.Length - currentIndent.Length)); |
| 75 | + } |
| 76 | + |
| 77 | + current = current.NextLine; |
| 78 | + lineCount++; |
| 79 | + } |
| 80 | + if (current != endLine.NextLine) |
| 81 | + builder.Append("..."); |
| 82 | + |
| 83 | + return builder.ToString(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments