Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SearchPopupSearchPattern.cs « MonoDevelop.Components.MainToolbar « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4704ab4913c36e903ef25c56db99b849d6d5fb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// SearchPopupSearchPattern.cs
//
// Author:
//       Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Threading;
using System.Threading.Tasks;
using MonoDevelop.Core;
using System.Collections.Generic;
using Gtk;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using MonoDevelop.Ide;
using MonoDevelop.Ide.CodeCompletion;
using System.Text.RegularExpressions;

namespace MonoDevelop.Components.MainToolbar
{
	public readonly struct SearchPopupSearchPattern
	{
		public readonly string Tag;
		public readonly string Pattern;
		public readonly int    LineNumber;
		public readonly int    Column;
		public readonly string UnparsedPattern;

		public bool HasLineNumber {
			get {
				return LineNumber >= 0;
			}
		}
		public bool HasColumn {
			get {
				return Column >= 0;
			}
		}

		public SearchPopupSearchPattern (string tag, string pattern, int lineNumber = -1, int column = -1, string unparsedPattern = "")
		{
			Tag = tag;
			Pattern = pattern;
			LineNumber = lineNumber;
			Column = column;
			UnparsedPattern = unparsedPattern;
		}

		static readonly Regex githubRegex = new Regex ("((?<tag>.*):)?(?<pattern>[^#]+)#L(?<line>\\d+)$", RegexOptions.Compiled); // for example: ExceptionCaughtDialog.cs#L510
		static readonly Regex lineRegex = new Regex ("((?<tag>[^:]*\\S)\\s*:)?\\s*(?<pattern>[^0-:][^:\\s]*)\\s*:[a-zA-Z]*\\s*(?<line>\\d+)$", RegexOptions.Compiled); // for example: ExceptionCaughtDialog.cs:line 510

		public static SearchPopupSearchPattern ParsePattern (string searchPattern)
		{
			var githubMatch = githubRegex.Match (searchPattern);
			if (githubMatch.Success) {
				int parsedLine;
				try {
					parsedLine = int.Parse (githubMatch.Groups ["line"].Value);
				} catch (Exception) {
					parsedLine = -1;
				}
				return new SearchPopupSearchPattern (githubMatch.Groups["tag"].Success ? githubMatch.Groups["tag"].Value : null, 
				                                     githubMatch.Groups["pattern"].Success ? githubMatch.Groups["pattern"].Value : null,
				                                     parsedLine,
				                                     -1, 
				                                     searchPattern);

			}


			var lineRegexMatch = lineRegex.Match (searchPattern);
			if (lineRegexMatch.Success) {
				int parsedLine;
				try {
					parsedLine = int.Parse (lineRegexMatch.Groups ["line"].Value);
				} catch (Exception) {
					parsedLine = -1;
				}
				return new SearchPopupSearchPattern (lineRegexMatch.Groups["tag"].Success ? lineRegexMatch.Groups["tag"].Value : null, 
				                                     lineRegexMatch.Groups["pattern"].Value,
				                                     parsedLine,
				                                     -1, 
				                                     searchPattern);

			}

			string tag = null;
			string pattern = null;
			int lineNumber = -1;
			int column = -1;

			const int maxTag = 4;
			string[] parts = new string[maxTag];
			int foundTags = 0;
			int idx = 0;
			for (int i = 0; i < searchPattern.Length; i++) {
				if (searchPattern[i] == ':') {
					parts[foundTags++] = searchPattern.Substring (idx, i - idx);
					idx = i + 1;
					if (foundTags >= maxTag)
						break;
				}
			}
			if (foundTags < maxTag)
				parts[foundTags++] = searchPattern.Substring (idx,searchPattern.Length - idx);
			switch (foundTags) {
			case 1:
				pattern = parts [0];
				break;
			case 2:
				if (!string.IsNullOrEmpty (parts[1]) && TryParseLineColumn (parts[1], ref lineNumber, ref column)) {
					if (!string.IsNullOrEmpty (parts [0]))
						pattern = parts [0];
				} else {
					tag = parts [0];
					pattern = parts [1];
				}
				break;
			case 3:
				if (IsNumber (parts [1]) && IsNumber (parts [2])) {
					if (!string.IsNullOrEmpty (parts [0]))
						pattern = parts [0];
					if (!TryParseLineColumn (parts [1] + "," + parts[2], ref lineNumber, ref column))
						lineNumber = 0;
				} else if (IsNumber (parts [1]) && string.IsNullOrEmpty (parts [2])) {
					if (!string.IsNullOrEmpty (parts [0]))
						pattern = parts [0];
					if (!TryParseLineColumn (parts [1] + ",0", ref lineNumber, ref column))
						lineNumber = 0;
				} else {
					tag = parts [0];
					pattern = parts [1] ?? "";
					if (!TryParseLineColumn (parts [2], ref lineNumber, ref column))
						lineNumber = 0;
				}
				break;
			case 4:
				tag = parts [0];
				pattern = parts [1];
				if (!TryParseLineColumn (parts [2] +","+parts[3], ref lineNumber, ref column))
					lineNumber = 0;
				break;
			}
			return new SearchPopupSearchPattern (tag, pattern?.Trim (), lineNumber, column, searchPattern);
		}

		static bool TryParseLineColumn (string str, ref int lineNumber, ref int columnNumber)
		{
			int idx = str.IndexOf (',');
			string line = str;
			string col = null;

			if (idx >= 0) {
				line = str.Substring (0, idx).Trim ();
				col = str.Substring (idx + 1).Trim ();
			}

			try {
				lineNumber = string.IsNullOrEmpty (line) ? 0 : int.Parse (line);
			} catch {
				return false;
			}

			try {
				if (col != null)
					columnNumber = string.IsNullOrEmpty (col) ? 0 : int.Parse (col);
			} catch {
				return false;
			}
			return true;
		}

		static bool IsNumber (string text)
		{
			try {
				int.Parse (text);
				return true;
			} catch (Exception) {
				return false;
			}
		}

		public override int GetHashCode ()
		{
			return (Tag != null ? Tag.GetHashCode () : 0) ^ (Pattern != null ? Pattern.GetHashCode () : 0) ^ LineNumber.GetHashCode () ^ Column.GetHashCode ();
		}

		public override bool Equals (object obj)
		{
			if (!(obj is SearchPopupSearchPattern))
				return false;
			var other = (SearchPopupSearchPattern)obj;
			return Tag == other.Tag && Pattern == other.Pattern && LineNumber == other.LineNumber && Column == other.Column;
		}

		public static bool operator ==(SearchPopupSearchPattern l, SearchPopupSearchPattern r)
		{
			return l.Equals (r);
		}

		public static bool operator !=(SearchPopupSearchPattern l, SearchPopupSearchPattern r)
		{
			return !(l == r);
		}

		static string FormatString (string pattern)
		{
			if (pattern == null)
				return "<null>";
			return '"' + pattern + '"';
		}

		public override string ToString ()
		{
			return string.Format ("[SearchPopupSearchPattern: Tag={0}, Pattern={1}, LineNumber={2}, Column={3}]", FormatString(Tag), FormatString(Pattern), LineNumber, Column);
		}
	}
}