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

ParameterDataProvider.cs « Gui « ValaBinding « extras - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d047bca3efecc66071acbe6037149d380df40b52 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// DataProvider.cs
//
// Authors:
//  Levi Bard <taktaktaktaktaktaktaktaktaktak@gmail.com> 
//
// Copyright (C) 2008 Levi Bard
// Based on CBinding by Marcos David Marin Amador <MarcosMarin@gmail.com>
//
// This source code is licenced under The MIT License:
//
// 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.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;

using MonoDevelop.Core;
 
using MonoDevelop.Ide.Gui;
using MonoDevelop.Ide.CodeCompletion;

using MonoDevelop.ValaBinding.Parser;
using MonoDevelop.ValaBinding.Parser.Afrodite;

namespace MonoDevelop.ValaBinding
{
	public class ParameterDataProvider : IParameterDataProvider
	{
		Document document;
		private IList<Symbol> functions;
		private string functionName;
		
		public ParameterDataProvider (Document document, ProjectInformation info, string functionName)
		{
			this.document = document;
			this.functionName = functionName;

			functions = new List<Symbol> ();
			Symbol function = info.GetFunction (functionName, document.FileName, document.Editor.Caret.Line + 1, document.Editor.Caret.Column + 1);
			if (null != function){ functions.Add (function); }
		}// member function constructor
		
		/// <summary>
		/// Create a ParameterDataProvider for a constructor
		/// </summary>
		/// <param name="constructorOverload">
		/// A <see cref="System.String"/>: The named of the pertinent constructor overload
		/// </param>
		public ParameterDataProvider (Document document, ProjectInformation info, string typename, string constructorOverload)
		{
			this.functionName = constructorOverload;
			this.document = document;
			
			List<Symbol> myfunctions = info.GetConstructorsForType (typename, document.FileName, document.Editor.Caret.Line + 1, document.Editor.Caret.Column + 1, null); // bottleneck
			if (1 < myfunctions.Count) {
				foreach (Symbol function in myfunctions) {
					if (functionName.Equals (function.Name, StringComparison.Ordinal)) {
						functions = new List<Symbol> () {function};
						return;
					}
				}
			}
			
			functions = myfunctions;
		}// constructor constructor
		
		/// <summary>
		/// The number of overloads for this method
		/// </summary>
		public int OverloadCount {
			get { return functions.Count; }
		}
		
		/// <summary>
		/// Get the index of the parameter where the cursor is currently positioned.
		/// </summary>
		/// <param name="ctx">
		/// A <see cref="CodeCompletionContext"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Int32"/>: The index of the parameter, 
		/// 0 for no parameter entered, 
		/// -1 for outside the list
		/// </returns>
		public int GetCurrentParameterIndex (ICompletionWidget widget, CodeCompletionContext ctx)
		{
			int cursor = document.Editor.Caret.Offset;
			int i = ctx.TriggerOffset;
			
			if (i > cursor)
				return -1;
			else if (i == cursor)
				return 1;
			
			int parameterIndex = 1;
			
			while (i++ < cursor) {
				char ch = document.Editor.GetCharAt (i-1);
				if (ch == ',')
					parameterIndex++;
				else if (ch == ')')
					return -1;
			}
			
			return parameterIndex;
		}
		
		/// <summary>
		/// Get the markup to use to represent the specified method overload
		/// in the parameter information window.
		/// </summary>
		public string GetMethodMarkup (int overload, string[] parameterMarkup, int currentParameter)
		{
			string paramTxt = string.Join (", ", parameterMarkup);
			Symbol function = functions[overload];
			
			int len = function.FullyQualifiedName.LastIndexOf (".");
			string prename = null;
			
			if (len > 0)
				prename = function.FullyQualifiedName.Substring (0, len + 1);
			
//			string cons = string.Empty;
			
//			if (function.IsConst)
//				cons = " const";

			return string.Format ("{2} {3}<b>{0}</b>({1})", GLib.Markup.EscapeText (function.Name), 
			                                                paramTxt, 
			                                                GLib.Markup.EscapeText (function.ReturnType.TypeName), 
			                                                GLib.Markup.EscapeText (prename));
			// return prename + "<b>" + function.Name + "</b>" + " (" + paramTxt + ")" + cons;
		}
		
		/// <summary>
		/// Get the text to use to represent the specified parameter
		/// </summary>
		public string GetParameterMarkup (int overload, int paramIndex)
		{
			Symbol function = functions[overload];
			
			if (null != function && null != function.Parameters[paramIndex]) {
				string name = function.Parameters[paramIndex].Name;
				string type = function.Parameters[paramIndex].TypeName;
				return GLib.Markup.EscapeText (string.Format ("{1} {0}", name, type));
			}
			
			return string.Empty;
		}
		
		/// <summary>
		/// Get the number of parameters of the specified method
		/// </summary>
		public int GetParameterCount (int overload)
		{
			if (null != functions && null != functions[overload] && null != functions[overload].Parameters) {
				return functions[overload].Parameters.Count;
			}
			return 0;
		}
	}
	
	/// <summary>
	/// Data for Vala completion
	/// </summary>
	internal class CompletionData : MonoDevelop.Ide.CodeCompletion.CompletionData
	{
		private string image;
		private string text;
		private string description;
		private string completion_string;
		
		public CompletionData (Symbol item)
		{
			this.text = item.Name;
			this.completion_string = item.Name;
			this.description = item.DisplayText;
			this.image = item.Icon;
			DisplayFlags = DisplayFlags.None;
			CompletionCategory = new ValaCompletionCategory (text, image);
		}
		
		public override IconId Icon {
			get { return image; }
		}
		
		public override string DisplayText {
			get { return text; }
		}

		public override string Description {
			get { return description; }
		}

		public override string CompletionText {
			get { return completion_string; }
		}
	}

	internal class ValaCompletionDataList: CompletionDataList, IMutableCompletionDataList
	{
		public ValaCompletionDataList (): base ()
		{
			IsChanging = true;
			Add (string.Empty);
		}
		
		internal virtual void AddRange (IEnumerable<CompletionData> vals)
		{
			foreach (CompletionData item in vals) {
				Add (item);
			}
		}
		
		#region IMutableCompletionDataList implementation
		
		public event EventHandler Changed;
		public event EventHandler Changing;
		
		
		public bool IsChanging {
			get { return isChanging; }
			set {
				isChanging = value;
				if (value){ OnChanging (this, null); }
				else{ OnChanged (this, null); }
			}
		}
		private bool isChanging;
		
		#endregion
		
		protected virtual void OnChanging (object sender, EventArgs args)
		{
			if (null != Changing) {
				Changing (sender, args);
			}
		}
		
		protected virtual void OnChanged (object sender, EventArgs args)
		{
			if (null != Changed) {
				Changed (sender, args);
			}
		}
		
		public void Dispose ()
		{
		}
	}// ValaCompletionDataList
	
	internal class ValaCompletionCategory: CompletionCategory
	{
		public ValaCompletionCategory (string text, string image): base (text, image)
		{
		}
		
		public override int CompareTo (CompletionCategory other)
		{
			return DisplayText.CompareTo (other.DisplayText);
		}
	}
}