~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Navigation/TextFileNavigationPoint.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-07-05 13:00:05 UTC
  • mfrom: (1.2.8 upstream) (1.3.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100705130005-d6hp4k5gcn1xkj8c
Tags: 2.4+dfsg-1ubuntu1
* debian/patches/remove_support_for_moonlight.patch,
  debian/patches/dont_add_moonlight_to_core_addins.patch,
  debian/control:
  + Enable support for Moonlight
* debian/rules:
  + Ensure Moonlight addin isn't shipped in main MonoDevelop package by
    mistake

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// TextFileNavigationPoint.cs
 
3
// 
 
4
// Author:
 
5
//   Michael Hutchinson <mhutchinson@novell.com>
 
6
//   Lluis Sanchez Gual <lluis@novell.com>
 
7
// 
 
8
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
9
// 
 
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:
 
17
// 
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
// 
 
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.
 
28
//
 
29
 
 
30
using System;
 
31
using MonoDevelop.Ide.Gui.Content;
 
32
using MonoDevelop.Core;
 
33
using MonoDevelop.Ide.Gui;
 
34
 
 
35
namespace MonoDevelop.Ide.Navigation
 
36
{
 
37
        public class TextFileNavigationPoint : DocumentNavigationPoint
 
38
        {
 
39
                int line;
 
40
                int column;
 
41
                
 
42
                public TextFileNavigationPoint (Document doc, IEditableTextBuffer buffer)
 
43
                        : base (doc)
 
44
                {
 
45
                        int col;
 
46
                        buffer.GetLineColumnFromPosition (buffer.CursorPosition, out line, out col);
 
47
                }
 
48
                
 
49
                public TextFileNavigationPoint (FilePath file, int line, int column)
 
50
                        : base (file)
 
51
                {
 
52
                        this.line = line;
 
53
                        this.column = column;
 
54
                }
 
55
                
 
56
                public override bool ShouldReplace (NavigationPoint oldPoint)
 
57
                {
 
58
                        TextFileNavigationPoint tf = oldPoint as TextFileNavigationPoint;
 
59
                        if (tf == null)
 
60
                                return false;
 
61
                        return base.Equals (tf) && Math.Abs (line - tf.line) < 5;
 
62
                }
 
63
                
 
64
                
 
65
                public int Line {
 
66
                        get { return line; }
 
67
                }
 
68
                
 
69
                public int Column {
 
70
                        get { return this.column; }
 
71
                }
 
72
                
 
73
                public override string DisplayName {
 
74
                        get {
 
75
                                return string.Format ("{0} : {1}", base.DisplayName, Line);
 
76
                        }
 
77
                }
 
78
                
 
79
                protected override Document DoShow ()
 
80
                {
 
81
                        Document doc = base.DoShow ();
 
82
                        if (doc != null) {
 
83
                                IEditableTextBuffer buf = doc.GetContent<IEditableTextBuffer> ();
 
84
                                if (buf != null)
 
85
                                        buf.SetCaretTo (Math.Max (line, 1), 1);
 
86
                        }
 
87
                        return doc;
 
88
                }
 
89
                
 
90
                /*
 
91
                
 
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.
 
94
                
 
95
                public override string Tooltip {
 
96
                        get { return snippet; }
 
97
                }
 
98
                
 
99
                public void UpdateLine (int line, MonoDevelop.Ide.Gui.Content.IEditableTextBuffer buffer)
 
100
                {
 
101
                        this.line = line;
 
102
                        UpdateSnippet (buffer);
 
103
                }
 
104
                
 
105
                //gets a snippet for the tooltip
 
106
                void UpdateSnippet (MonoDevelop.Ide.Gui.Content.IEditableTextBuffer buffer)
 
107
                {
 
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);
 
111
                        if (endPos < 0)
 
112
                                endPos = buffer.Length; 
 
113
                        
 
114
                        string [] lines = buffer.GetText (startPos, endPos).Split ('\n', '\r');
 
115
                        System.Text.StringBuilder fragment = new System.Text.StringBuilder ();
 
116
                        
 
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)
 
121
                                        continue;
 
122
                                int indentLength = GetIndentLength (line);
 
123
                                if (indentLength < minIndentLength)
 
124
                                        minIndentLength = indentLength;
 
125
                        }
 
126
                        
 
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)
 
131
                                        continue;
 
132
                                
 
133
                                int length = Math.Min (MAX_LINE_LENGTH, line.Length) - minIndentLength;
 
134
                                if (length > 0)
 
135
                                        fragment.AppendLine (line.Substring (minIndentLength, length));
 
136
                                else
 
137
                                        fragment.AppendLine ();
 
138
                        }
 
139
                        
 
140
                        snippet = fragment.ToString ();
 
141
                }
 
142
                
 
143
                int GetIndentLength (string line)
 
144
                {
 
145
                        int indent = 0;
 
146
                        for (int i = 0; i < line.Length; i++) {
 
147
                                if (line[i] == ' ')
 
148
                                        indent++;
 
149
                                else if (line[i] == '\t')
 
150
                                        indent += 4;
 
151
                                else
 
152
                                        break;
 
153
                        }
 
154
                        return indent;
 
155
                }*/
 
156
                
 
157
                public override bool Equals (object o)
 
158
                {
 
159
                        TextFileNavigationPoint other = o as TextFileNavigationPoint;
 
160
                        return other != null && other.line == line && base.Equals (other);
 
161
                }
 
162
                
 
163
                public override int GetHashCode ()
 
164
                {
 
165
                        unchecked {
 
166
                                return line + base.GetHashCode ();
 
167
                        }
 
168
                }
 
169
        }
 
170
}