ide /actions/GenerateAutoCompleteWords.cs

Language C# Lines 595
MD5 Hash cb138e36ca9a687b9571f191edf50d23
Repository https://github.com/moscrif/ide.git View Raw File
  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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using System;
using Gtk;
using System.Reflection;
using Moscrif.IDE.Components;
using Moscrif.IDE.Controls;
using Mono.TextEditor;
using Mono.TextEditor.Highlighting;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using MessageDialogs = Moscrif.IDE.Controls.MessageDialog;
using System.Linq;

using Mono.Data.Sqlite;

using Moscrif.IDE.Tool;
using Moscrif.IDE.Completion;

//using Newtonsoft.Json;
//using Newtonsoft.Json.Linq;


namespace Moscrif.IDE.Actions
{

	public class GenerateAutoCompleteWords : Gtk.Action
	{
		public GenerateAutoCompleteWords():base("generateAutoComplete",MainClass.Languages.Translate("menu_generate_autoComplete"),MainClass.Languages.Translate("menu_title_generate_autoComplete"),null)
		{
		}

		private SqlLiteDal sqlLiteDal;


		protected override void OnActivated ()
		{

			MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.OkCancel, "Are you sure?", "", Gtk.MessageType.Question);
			int result = md.ShowDialog();
			if(result != (int)Gtk.ResponseType.Ok){
				return;
			}

			ProgressDialog progressDialog;

			string filename = System.IO.Path.Combine(MainClass.Paths.ConfingDir, "completedcache");
			sqlLiteDal = new SqlLiteDal(filename);

			string sql = "";

			if(sqlLiteDal.CheckExistTable("completed") ){
				sql = "DROP TABLE completed ;";
				sqlLiteDal.RunSqlScalar(sql);
			}

			sql = "CREATE TABLE completed (id INTEGER PRIMARY KEY, name TEXT, signature TEXT, type NUMERIC, parent TEXT,summary TEXT ,returnType TEXT ) ;";
			sqlLiteDal.RunSqlScalar(sql);

			SyntaxMode mode = new SyntaxMode();
			mode = SyntaxModeService.GetSyntaxMode("text/moscrif");

			progressDialog = new ProgressDialog("Generated...",ProgressDialog.CancelButtonType.None, mode.Keywords.Count() ,MainClass.MainWindow);

			foreach (Keywords kw in mode.Keywords){

				progressDialog.Update(kw.ToString());
				foreach (string wrd in kw.Words){
						insertNewRow(wrd,wrd,(int)CompletionDataTyp.keywords,"","","");
					}
			}


			Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("data.json", null, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
			fc.TransientFor = MainClass.MainWindow;
			fc.SetCurrentFolder(@"d:\Work\docs-api\output\");

			if (fc.Run() != (int)ResponseType.Accept) {
				return;
			}
			string json ;
			string fileName = fc.Filename;
			progressDialog.Destroy();
			fc.Destroy();
			progressDialog = new ProgressDialog("Generated...",ProgressDialog.CancelButtonType.None,100 ,MainClass.MainWindow);

			using (StreamReader file = new StreamReader(fileName)) {
				json = file.ReadToEnd();
				file.Close();
				file.Dispose();
			}

			//XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
			//doc.Save(fileName+"xml");

			/*JObject jDoc= JObject.Parse(json);
			//classes
			Console.WriteLine("o.Count->"+jDoc.Count);

			foreach (JProperty jp in jDoc.Properties()){
				Console.WriteLine(jp.Name);
			}
			Console.WriteLine("------------");
			JObject classes = (JObject)jDoc["classes"];
			foreach (JProperty jp in classes.Properties()){
				Console.WriteLine(jp.Name);
				JObject classDefin = (JObject)classes[jp.Name];
				string name = (string)classDefin["name"];
				string shortname = (string)classDefin["shortname"];
				string description = (string)classDefin["description"];
				//string type = (string)classDefin["type"];
				insertNewRow(name,name,(int)CompletionDataTyp.types,"",description,name);
			}
			Console.WriteLine("------------");

			JArray classitems = (JArray)jDoc["classitems"];
			foreach (JObject classitem in classitems){

				string name = (string)classitem["name"];
				Console.WriteLine(name);

				string description = (string)classitem["description"];
				string itemtype = (string)classitem["itemtype"];
				string classParent = (string)classitem["class"];
				string signature = (string)classitem["name"];
				CompletionDataTyp type = CompletionDataTyp.noting;
				string returnType= classParent;

				switch (itemtype){
					case "method":{
						JArray paramsArray = (JArray)classitem["params"];
						signature = signature+ GetParams(paramsArray);	
						type = CompletionDataTyp.members;
						JObject returnJO =(JObject)classitem["return"] ;
						if(returnJO!=null){
							returnType = (string)returnJO["type"];
						}

						break;
					}
					case "property":{
						
						string tmpType = (string)classitem["type"];
						if(!String.IsNullOrEmpty(tmpType)){
							returnType=tmpType.Replace("{","").Replace("}","");
						}
						type = CompletionDataTyp.properties;
						break;
					}
					case "event":{
						JArray paramsArray = (JArray)classitem["params"];
						signature = signature+ GetParams(paramsArray);
						type = CompletionDataTyp.events;
						break;
					}
					case "attribute":{
						continue;
						break;
					} 
					default:{
						type = CompletionDataTyp.noting;
						break;
					}

				}

				insertNewRow(name,signature,(int)type,classParent,description,returnType);
			}*/
			//classitems

//			string name = (string)o["project"]["name"];
//			Console.WriteLine(name);

			progressDialog.Destroy();
			
			md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "Done", "", Gtk.MessageType.Info);
			md.ShowDialog();

			return;
			/*

			Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("Select DOC Directory (with xml)", null, FileChooserAction.SelectFolder, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
			FileInfo[] xmls = new FileInfo[]{};

			if (fc.Run() == (int)ResponseType.Accept) {

				DirectoryInfo di = new DirectoryInfo(fc.Filename);
				xmls = di.GetFiles("*.xml");
				//List<string> output = new List<string>();

				//MainClass.CompletedCache.ListTypes= listSignature.Distinct().ToList();
				//MainClass.CompletedCache.ListMembers= listMemberSignature.Distinct().ToList();
			}
			progressDialog.Destroy();
			fc.Destroy();
			progressDialog = new ProgressDialog("Generated...",ProgressDialog.CancelButtonType.None,xmls.Length ,MainClass.MainWindow);
		            foreach (FileInfo xml in xmls)
		            {
		                try
		                {
					progressDialog.Update(xml.Name);
					if(!xml.Name.StartsWith("_"))
						getObject(xml.FullName);
		                }
		                catch(Exception ex) {
		                    Console.WriteLine(ex.Message);
		                    Console.WriteLine(ex.StackTrace);
		                    return;
		                }
		            }
			progressDialog.Destroy();

			md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "Done", "", Gtk.MessageType.Info);
			md.ShowDialog();*/
		}

		private void getObject(string file ){
			XmlDocument rssDoc;
			XmlNode nodeRss = new XmlDocument();

			//XmlNode nodeChannel = new XmlDocument();
			XmlNode nodeItem;

			try{
				XmlTextReader reader = new XmlTextReader(file);
 				rssDoc = new XmlDocument();
				rssDoc.Load(reader);
 				for (int i = 0; i < rssDoc.ChildNodes.Count; i++) {
                    			if (rssDoc.ChildNodes[i].Name == "doc"){
						nodeRss = rssDoc.ChildNodes[i];
					}
				}
				/*for (int i = 0; i < nodeRss.ChildNodes.Count; i++){
					if (nodeRss.ChildNodes[i].Name == "members"){
						nodeChannel = nodeRss.ChildNodes[i];
					}
				}*/
				string parent = "";

				for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
      				{
					if (nodeRss.ChildNodes[i].Name == "member")
  					{

						nodeItem = nodeRss.ChildNodes[i];
						string name="";

						string signature="";
						string summary = GetSummary(nodeItem);
						bool visibility = false;

						foreach (XmlAttribute atrb  in nodeItem.Attributes){

							if (atrb.Name == "name"){
								name =atrb.InnerText;
							}

							if (atrb.Name == "signature"){
								signature =atrb.InnerText;
							}

							if (atrb.Name == "visibility"){
								if (atrb.InnerText.Trim() == "private"){
									visibility = true;
								}
							}
						}
						if(visibility) continue;


						if(!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(signature)){

							if(signature.Trim().StartsWith("(") ||
								signature.Trim().StartsWith("{") ||
								signature.Trim().StartsWith("[") ||
								signature.Trim().StartsWith("!")){

								continue;
							}


							if(name.StartsWith("T:")){
								parent =signature;
								insertNewRow(signature,signature,(int)CompletionDataTyp.types,"",summary,"");

							}

							if(name.Contains("this"))
								continue;

							if (name.StartsWith("P:")){
								int indx = signature.Trim().IndexOf(".");
								if(indx>0){
									string tmp = signature.Substring(0,indx);

									if(tmp.Length<2)
										continue;

									insertNewRow(tmp,signature,(int)CompletionDataTyp.properties,parent,summary,"");
									//listMemberSignature.Add(tmp);
									continue;
								}
								if(signature.Length<2)
										continue;

								insertNewRow(signature,signature,(int)CompletionDataTyp.properties,parent,summary,"");

							} else if(name.StartsWith("M:")){
								int indx = signature.IndexOf("(");
								if(indx>0){
									string tmp = signature.Substring(0,indx);

									if(tmp.Length<2)
										continue;

									insertNewRow(tmp,signature,(int)CompletionDataTyp.members,parent,summary,"");
									continue;
								}
								if(signature.Length<2)
										continue;
								insertNewRow(signature,signature,(int)CompletionDataTyp.members,parent,summary,"");

							} else if(name.StartsWith("E:")){
								int indx = signature.IndexOf("(");
								if(indx>0){
									string tmp = signature.Substring(0,indx);

									insertNewRow(tmp,signature,(int)CompletionDataTyp.events,parent,summary,"");

									continue;
								}
								insertNewRow(signature,signature,(int)CompletionDataTyp.events,parent,summary,"");
							}


						}

					}
				}
			} catch(Exception ex){
				MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "ERROR", ex.Message, Gtk.MessageType.Error);
					md.ShowDialog();
				Console.WriteLine(file);
				Console.WriteLine(ex.Message);
			}
		}

		private string GetSummary (XmlNode nodeItem){

			for (int j = 0; j < nodeItem.ChildNodes.Count; j++)
			{
				if (nodeItem.ChildNodes[j].Name == "summary")
				{
					string summary =nodeItem.ChildNodes[j].InnerText;
					summary = summary.Trim();
					summary = summary.Replace("\t"," ");
					return summary;
				}
			}
			return "";
		}
		/*
		private string GetParams(JArray paramsArray){
			if(paramsArray == null)
				return "()";
			string paramsString = "(";
			foreach (JObject jo in paramsArray){
				StringBuilder param = new StringBuilder();
				string name = (string)jo["name"];
				string description = (string)jo["description"];
				string type = (string)jo["type"];

				JValue optionalStr = (JValue)jo["optional"];
				bool optional = false;
				if(optionalStr!=null){
					if (!bool.TryParse(optionalStr.ToString(),out optional)){
						optional = false;
					}
				}

				string optdefault = (string)jo["optdefault"];

				JValue multipleStr = (JValue)jo["multiple"];
				bool multiple = false;
				if(multipleStr!=null){
					if (!bool.TryParse(multipleStr.ToString(),out multiple)){
						multiple = false;
					}
				}

				param.Append(name);

				if(!String.IsNullOrEmpty(optdefault))
					param.Append("="+optdefault);
				if((optional) && (String.IsNullOrEmpty(optdefault)))
				   	param.Append("=undefined");

				if(multiple)
					param.Append(",..");

				paramsString = paramsString + param.ToString()+",";
			}
			paramsString = paramsString.TrimEnd(',');
			paramsString = paramsString+ ")";

			return paramsString;

		}
		*/
		private void insertNewRow(string name,string signature,int type,string  parent,string summary,string returnType){

			string	sql;

			signature = signature.Replace("'", "" );
			if(!String.IsNullOrEmpty(summary))
				summary = summary.Replace("'", "" );

			if(String.IsNullOrEmpty(parent))
				sql = String.Format("INSERT INTO completed (name,signature,type,summary,returnType) values ( '{0}' , '{1}' , '{2}', '{3}','{4}' ) ;",name,signature,type,summary,returnType);
			else
				sql = String.Format("INSERT INTO completed (name,signature,type, parent,summary,returnType) values ( '{0}' , '{3}.{1}' , '{2}', '{3}', '{4}','{5}' ) ;",name,signature,type,parent,summary,returnType);

			sqlLiteDal.RunSqlScalar(sql);

		}


		/*
		protected override void OnActivated ()
		{
			MainClass.CompletedCache.ListKeywords = new System.Collections.Generic.List<string>();
			MainClass.CompletedCache.ListTypes = new System.Collections.Generic.List<string>();

			SyntaxMode mode = new SyntaxMode();
			mode = SyntaxModeService.GetSyntaxMode("text/moscrif");
			foreach (Keywords kw in mode.Keywords){

				foreach (string wrd in kw.Words){
						MainClass.CompletedCache.ListKeywords.Add(wrd);
					}
			}

			MainClass.CompletedCache.SaveCompletedCache();

			List<string> listSignature = new List<string>();

			List<string> listMemberSignature = new List<string>();

			Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("Select DOC Directory (with xml)", null, FileChooserAction.SelectFolder, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);

			if (fc.Run() == (int)ResponseType.Accept) {

				DirectoryInfo di = new DirectoryInfo(fc.Filename);
					FileInfo[] xmls = di.GetFiles("*.xml");
					List<string> output = new List<string>();
			            foreach (FileInfo xml in xmls)
			            {
			                try
			                {
			                    getObject(xml.FullName, ref listSignature, ref listMemberSignature);
			                }
			                catch(Exception ex) {
			                    Console.WriteLine(ex.Message);
			                    Console.WriteLine(ex.StackTrace);
			                    return;
			                }
			            }


				MainClass.CompletedCache.ListTypes= listSignature.Distinct().ToList();
				MainClass.CompletedCache.ListMembers= listMemberSignature.Distinct().ToList();
			}
			fc.Destroy();

			MainClass.CompletedCache.SaveCompletedCache();

			MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "Done", "", Gtk.MessageType.Info);
			md.ShowDialog();

		}

		private void getObject(string file , ref List<string> listSignature,ref List<string> listMemberSignature){
			XmlDocument rssDoc;
			XmlNode nodeRss = new XmlDocument();
			XmlNode nodeChannel = new XmlDocument();
			XmlNode nodeItem;

			try{
				XmlTextReader reader = new XmlTextReader(file);
 				rssDoc = new XmlDocument();
				rssDoc.Load(reader);
 				for (int i = 0; i < rssDoc.ChildNodes.Count; i++) {
                    			if (rssDoc.ChildNodes[i].Name == "doc"){
						nodeRss = rssDoc.ChildNodes[i];
					}
				}
				for (int i = 0; i < nodeRss.ChildNodes.Count; i++){
					if (nodeRss.ChildNodes[i].Name == "members")                {
						nodeChannel = nodeRss.ChildNodes[i];
					}
				}
				for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
      				{
					if (nodeChannel.ChildNodes[i].Name == "member")
  					{

						nodeItem = nodeChannel.ChildNodes[i];
						string name="";
						string signature="";

						foreach (XmlAttribute atrb  in nodeItem.Attributes){

							if (atrb.Name == "name"){
								name =atrb.InnerText;
							}

							if (atrb.Name == "signature"){
								signature =atrb.InnerText;
							}
						}


						if(!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(signature)){

							if(signature.Trim().StartsWith("(") ||
								signature.Trim().StartsWith("{") ||
								signature.Trim().StartsWith("[") ||
								signature.Trim().StartsWith("!")){

								continue;
							}



							if(name.StartsWith("T:")){
								listSignature.Add(signature);

							} else if (name.StartsWith("P:")){
								int indx = signature.Trim().IndexOf(".");
								if(indx>0){
									string tmp = signature.Substring(0,indx);

									if(tmp.Length<2)
										continue;

									listMemberSignature.Add(tmp);
									continue;
								}
								if(signature.Length<2)
										continue;

								listMemberSignature.Add(signature);

							} else if(name.StartsWith("M:")){
								int indx = signature.IndexOf("(");
								if(indx>0){
									string tmp = signature.Substring(0,indx);

									if(tmp.Length<2)
										continue;

									listMemberSignature.Add(tmp);
									continue;
								}
								if(signature.Length<2)
										continue;

								listMemberSignature.Add(signature);

							} else if(name.StartsWith("E:")){
								int indx = signature.IndexOf("(");
								if(indx>0){
									string tmp = signature.Substring(0,indx);
									listMemberSignature.Add(tmp);
									continue;
								}
								listMemberSignature.Add(signature);
							}
						}

					}
				}
			} catch(Exception ex){
				MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "ERROR", ex.Message, Gtk.MessageType.Error);
					md.ShowDialog();
				Console.WriteLine(file);
				Console.WriteLine(ex.Message);
			}
		}*/

	}
}
Back to Top