~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/SearchPopupSearchPattern.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// SearchPopupSearchPattern.cs
 
3
//
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Threading;
 
29
using System.Threading.Tasks;
 
30
using MonoDevelop.Core;
 
31
using System.Collections.Generic;
 
32
using Gtk;
 
33
using System.Linq;
 
34
using ICSharpCode.NRefactory.TypeSystem;
 
35
using MonoDevelop.Ide;
 
36
using MonoDevelop.Ide.CodeCompletion;
 
37
 
 
38
namespace MonoDevelop.Components.MainToolbar
 
39
{
 
40
        public struct SearchPopupSearchPattern
 
41
        {
 
42
                public readonly string Tag;
 
43
                public readonly string Pattern;
 
44
                public readonly int    LineNumber;
 
45
                public readonly int    Column;
 
46
 
 
47
                public bool HasLineNumber {
 
48
                        get {
 
49
                                return LineNumber >= 0;
 
50
                        }
 
51
                }
 
52
                public bool HasColumn {
 
53
                        get {
 
54
                                return Column >= 0;
 
55
                        }
 
56
                }
 
57
 
 
58
                public SearchPopupSearchPattern (string tag, string pattern, int lineNumber = -1, int column = -1)
 
59
                {
 
60
                        Tag = tag;
 
61
                        Pattern = pattern;
 
62
                        LineNumber = lineNumber;
 
63
                        Column = column;
 
64
                }
 
65
 
 
66
                public static SearchPopupSearchPattern ParsePattern (string searchPattern)
 
67
                {
 
68
                        string tag = null;
 
69
                        string pattern = null;
 
70
                        int lineNumber = -1;
 
71
                        int column = -1;
 
72
 
 
73
                        const int maxTag = 4;
 
74
                        string[] parts = new string[maxTag];
 
75
                        int foundTags = 0;
 
76
                        int idx = 0;
 
77
                        for (int i = 0; i < searchPattern.Length; i++) {
 
78
                                if (searchPattern[i] == ':') {
 
79
                                        parts[foundTags++] = searchPattern.Substring (idx, i - idx);
 
80
                                        idx = i + 1;
 
81
                                        if (foundTags >= maxTag)
 
82
                                                break;
 
83
                                }
 
84
                        }
 
85
                        if (foundTags < maxTag)
 
86
                                parts[foundTags++] = searchPattern.Substring (idx,searchPattern.Length - idx);
 
87
                        switch (foundTags) {
 
88
                        case 1:
 
89
                                pattern = parts [0];
 
90
                                break;
 
91
                        case 2:
 
92
                                if (!string.IsNullOrEmpty (parts[1]) && TryParseLineColumn (parts[1], ref lineNumber, ref column)) {
 
93
                                        if (!string.IsNullOrEmpty (parts [0]))
 
94
                                                pattern = parts [0];
 
95
                                } else {
 
96
                                        tag = parts [0];
 
97
                                        pattern = parts [1];
 
98
                                }
 
99
                                break;
 
100
                        case 3:
 
101
                                if (IsNumber (parts [1]) && IsNumber (parts [2])) {
 
102
                                        if (!string.IsNullOrEmpty (parts [0]))
 
103
                                                pattern = parts [0];
 
104
                                        if (!TryParseLineColumn (parts [1] + "," + parts[2], ref lineNumber, ref column))
 
105
                                                lineNumber = 0;
 
106
                                } else if (IsNumber (parts [1]) && string.IsNullOrEmpty (parts [2])) {
 
107
                                        if (!string.IsNullOrEmpty (parts [0]))
 
108
                                                pattern = parts [0];
 
109
                                        if (!TryParseLineColumn (parts [1] + ",0", ref lineNumber, ref column))
 
110
                                                lineNumber = 0;
 
111
                                } else {
 
112
                                        tag = parts [0];
 
113
                                        pattern = parts [1] ?? "";
 
114
                                        if (!TryParseLineColumn (parts [2], ref lineNumber, ref column))
 
115
                                                lineNumber = 0;
 
116
                                }
 
117
                                break;
 
118
                        case 4:
 
119
                                tag = parts [0];
 
120
                                pattern = parts [1];
 
121
                                if (!TryParseLineColumn (parts [2] +","+parts[3], ref lineNumber, ref column))
 
122
                                        lineNumber = 0;
 
123
                                break;
 
124
                        }
 
125
                        return new SearchPopupSearchPattern (tag, pattern, lineNumber, column);
 
126
                }
 
127
 
 
128
                static bool TryParseLineColumn (string str, ref int lineNumber, ref int columnNumber)
 
129
                {
 
130
                        int idx = str.IndexOf (',');
 
131
                        string line = str;
 
132
                        string col = null;
 
133
 
 
134
                        if (idx >= 0) {
 
135
                                line = str.Substring (0, idx).Trim ();
 
136
                                col = str.Substring (idx + 1).Trim ();
 
137
                        }
 
138
 
 
139
                        try {
 
140
                                lineNumber = string.IsNullOrEmpty (line) ? 0 : int.Parse (line);
 
141
                        } catch {
 
142
                                return false;
 
143
                        }
 
144
 
 
145
                        try {
 
146
                                if (col != null)
 
147
                                        columnNumber = string.IsNullOrEmpty (col) ? 0 : int.Parse (col);
 
148
                        } catch {
 
149
                                return false;
 
150
                        }
 
151
                        return true;
 
152
                }
 
153
 
 
154
                static bool IsNumber (string text)
 
155
                {
 
156
                        try {
 
157
                                int.Parse (text);
 
158
                                return true;
 
159
                        } catch (Exception) {
 
160
                                return false;
 
161
                        }
 
162
                }
 
163
 
 
164
                public override int GetHashCode ()
 
165
                {
 
166
                        return (Tag != null ? Tag.GetHashCode () : 0) ^ (Pattern != null ? Pattern.GetHashCode () : 0) ^ LineNumber.GetHashCode () ^ Column.GetHashCode ();
 
167
                }
 
168
 
 
169
                public override bool Equals (object obj)
 
170
                {
 
171
                        if (!(obj is SearchPopupSearchPattern))
 
172
                                return false;
 
173
                        var other = (SearchPopupSearchPattern)obj;
 
174
                        return Tag == other.Tag && Pattern == other.Pattern && LineNumber == other.LineNumber && Column == other.Column;
 
175
                }
 
176
 
 
177
                public static bool operator ==(SearchPopupSearchPattern l, SearchPopupSearchPattern r)
 
178
                {
 
179
                        return l.Equals (r);
 
180
                }
 
181
 
 
182
                public static bool operator !=(SearchPopupSearchPattern l, SearchPopupSearchPattern r)
 
183
                {
 
184
                        return !(l == r);
 
185
                }
 
186
 
 
187
                static string FormatString (string pattern)
 
188
                {
 
189
                        if (pattern == null)
 
190
                                return "<null>";
 
191
                        return '"' + pattern + '"';
 
192
                }
 
193
 
 
194
                public override string ToString ()
 
195
                {
 
196
                        return string.Format ("[SearchPopupSearchPattern: Tag={0}, Pattern={1}, LineNumber={2}, Column={3}]", FormatString(Tag), FormatString(Pattern), LineNumber, Column);
 
197
                }
 
198
        }
 
199
}
 
 
b'\\ No newline at end of file'