Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit e98a46d

Browse files
allow public use of indentation-alignment logic from TextMarkerToolTipProvider
1 parent 6c0d5d2 commit e98a46d

3 files changed

Lines changed: 88 additions & 58 deletions

File tree

src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/TextMarkerToolTipProvider.cs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -67,64 +67,7 @@ public void HandleToolTipRequest(ToolTipRequestEventArgs args)
6767

6868
string GetTooltipTextForCollapsedSection(ToolTipRequestEventArgs args, FoldingSection foldingSection)
6969
{
70-
// This fixes SD-1394:
71-
// Each line is checked for leading indentation whitespaces. If
72-
// a line has the same or more indentation than the first line,
73-
// it is reduced. If a line is less indented than the first line
74-
// the indentation is removed completely.
75-
//
76-
// See the following example:
77-
// line 1
78-
// line 2
79-
// line 3
80-
// line 4
81-
//
82-
// is reduced to:
83-
// line 1
84-
// line 2
85-
// line 3
86-
// line 4
87-
88-
const int maxLineCount = 15;
89-
90-
TextDocument document = (TextDocument)args.Editor.Document;
91-
int startOffset = foldingSection.StartOffset;
92-
int endOffset = foldingSection.EndOffset;
93-
94-
DocumentLine startLine = document.GetLineByOffset(startOffset);
95-
DocumentLine endLine = document.GetLineByOffset(endOffset);
96-
StringBuilder builder = new StringBuilder();
97-
98-
DocumentLine current = startLine;
99-
ISegment startIndent = TextUtilities.GetLeadingWhitespace(document, startLine);
100-
int lineCount = 0;
101-
while (current != endLine.NextLine && lineCount < maxLineCount) {
102-
ISegment currentIndent = TextUtilities.GetLeadingWhitespace(document, current);
103-
104-
if (current == startLine && current == endLine)
105-
builder.Append(document.GetText(startOffset, endOffset - startOffset));
106-
else if (current == startLine) {
107-
if (current.EndOffset - startOffset > 0)
108-
builder.AppendLine(document.GetText(startOffset, current.EndOffset - startOffset).TrimStart());
109-
} else if (current == endLine) {
110-
if (startIndent.Length <= currentIndent.Length)
111-
builder.Append(document.GetText(current.Offset + startIndent.Length, endOffset - current.Offset - startIndent.Length));
112-
else
113-
builder.Append(document.GetText(current.Offset + currentIndent.Length, endOffset - current.Offset - currentIndent.Length));
114-
} else {
115-
if (startIndent.Length <= currentIndent.Length)
116-
builder.AppendLine(document.GetText(current.Offset + startIndent.Length, current.Length - startIndent.Length));
117-
else
118-
builder.AppendLine(document.GetText(current.Offset + currentIndent.Length, current.Length - currentIndent.Length));
119-
}
120-
121-
current = current.NextLine;
122-
lineCount++;
123-
}
124-
if (current != endLine.NextLine)
125-
builder.Append("...");
126-
127-
return builder.ToString();
70+
return ToolTipUtils.GetAlignedText(args.Editor.Document, foldingSection.StartOffset, foldingSection.EndOffset);
12871
}
12972
}
13073
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
<DependentUpon>ToolTipService.cs</DependentUpon>
172172
</Compile>
173173
<Compile Include="Editor\ToolTipService.cs" />
174+
<Compile Include="Editor\ToolTipUtils.cs" />
174175
<Compile Include="Editor\XmlDocFormatter.cs" />
175176
<Compile Include="Parser\CombinedAssemblySearcher.cs" />
176177
<Compile Include="Parser\DefaultAssemblySearcher.cs" />

0 commit comments

Comments
 (0)