2
// TextFileNavigationPoint.cs
5
// Michael Hutchinson <mhutchinson@novell.com>
6
// Lluis Sanchez Gual <lluis@novell.com>
8
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
10
// Permission is hereby granted, free of charge, to any person obtaining
11
// a copy of this software and associated documentation files (the
12
// "Software"), to deal in the Software without restriction, including
13
// without limitation the rights to use, copy, modify, merge, publish,
14
// distribute, sublicense, and/or sell copies of the Software, and to
15
// permit persons to whom the Software is furnished to do so, subject to
16
// the following conditions:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using MonoDevelop.Ide.Gui.Content;
32
using MonoDevelop.Core;
33
using MonoDevelop.Ide.Gui;
35
namespace MonoDevelop.Ide.Navigation
37
public class TextFileNavigationPoint : DocumentNavigationPoint
42
public TextFileNavigationPoint (Document doc, IEditableTextBuffer buffer)
46
buffer.GetLineColumnFromPosition (buffer.CursorPosition, out line, out col);
49
public TextFileNavigationPoint (FilePath file, int line, int column)
56
public override bool ShouldReplace (NavigationPoint oldPoint)
58
TextFileNavigationPoint tf = oldPoint as TextFileNavigationPoint;
61
return base.Equals (tf) && Math.Abs (line - tf.line) < 5;
70
get { return this.column; }
73
public override string DisplayName {
75
return string.Format ("{0} : {1}", base.DisplayName, Line);
79
protected override Document DoShow ()
81
Document doc = base.DoShow ();
83
IEditableTextBuffer buf = doc.GetContent<IEditableTextBuffer> ();
85
buf.SetCaretTo (Math.Max (line, 1), 1);
92
//FIXME: this currently isn't hooked up to any GUI. In addition, it should be done lazily, since it's expensive
93
// and the nav menus are shown much less frequently than nav points are created.
95
public override string Tooltip {
96
get { return snippet; }
99
public void UpdateLine (int line, MonoDevelop.Ide.Gui.Content.IEditableTextBuffer buffer)
102
UpdateSnippet (buffer);
105
//gets a snippet for the tooltip
106
void UpdateSnippet (MonoDevelop.Ide.Gui.Content.IEditableTextBuffer buffer)
108
//get some lines from the file
109
int startPos = buffer.GetPositionFromLineColumn (Math.Max (Line - 1, 1), 1);
110
int endPos = buffer.GetPositionFromLineColumn (Line + 2, 1);
112
endPos = buffer.Length;
114
string [] lines = buffer.GetText (startPos, endPos).Split ('\n', '\r');
115
System.Text.StringBuilder fragment = new System.Text.StringBuilder ();
117
//calculate the minimum indent of any of these lines, using an approximation that tab == 4 spaces
118
int minIndentLength = int.MaxValue;
119
foreach (string line in lines) {
120
if (line.Length == 0)
122
int indentLength = GetIndentLength (line);
123
if (indentLength < minIndentLength)
124
minIndentLength = indentLength;
127
//strip off the indents and truncate the length
128
const int MAX_LINE_LENGTH = 40;
129
foreach (string line in lines) {
130
if (line.Length == 0)
133
int length = Math.Min (MAX_LINE_LENGTH, line.Length) - minIndentLength;
135
fragment.AppendLine (line.Substring (minIndentLength, length));
137
fragment.AppendLine ();
140
snippet = fragment.ToString ();
143
int GetIndentLength (string line)
146
for (int i = 0; i < line.Length; i++) {
149
else if (line[i] == '\t')
157
public override bool Equals (object o)
159
TextFileNavigationPoint other = o as TextFileNavigationPoint;
160
return other != null && other.line == line && base.Equals (other);
163
public override int GetHashCode ()
166
return line + base.GetHashCode ();