PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/actions/GenerateAutoCompleteWords.cs

https://github.com/moscrif/ide
C# | 594 lines | 191 code | 71 blank | 332 comment | 42 complexity | cb138e36ca9a687b9571f191edf50d23 MD5 | raw file
  1. using System;
  2. using Gtk;
  3. using System.Reflection;
  4. using Moscrif.IDE.Components;
  5. using Moscrif.IDE.Controls;
  6. using Mono.TextEditor;
  7. using Mono.TextEditor.Highlighting;
  8. using System.IO;
  9. using System.Xml;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using MessageDialogs = Moscrif.IDE.Controls.MessageDialog;
  14. using System.Linq;
  15. using Mono.Data.Sqlite;
  16. using Moscrif.IDE.Tool;
  17. using Moscrif.IDE.Completion;
  18. //using Newtonsoft.Json;
  19. //using Newtonsoft.Json.Linq;
  20. namespace Moscrif.IDE.Actions
  21. {
  22. public class GenerateAutoCompleteWords : Gtk.Action
  23. {
  24. public GenerateAutoCompleteWords():base("generateAutoComplete",MainClass.Languages.Translate("menu_generate_autoComplete"),MainClass.Languages.Translate("menu_title_generate_autoComplete"),null)
  25. {
  26. }
  27. private SqlLiteDal sqlLiteDal;
  28. protected override void OnActivated ()
  29. {
  30. MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.OkCancel, "Are you sure?", "", Gtk.MessageType.Question);
  31. int result = md.ShowDialog();
  32. if(result != (int)Gtk.ResponseType.Ok){
  33. return;
  34. }
  35. ProgressDialog progressDialog;
  36. string filename = System.IO.Path.Combine(MainClass.Paths.ConfingDir, "completedcache");
  37. sqlLiteDal = new SqlLiteDal(filename);
  38. string sql = "";
  39. if(sqlLiteDal.CheckExistTable("completed") ){
  40. sql = "DROP TABLE completed ;";
  41. sqlLiteDal.RunSqlScalar(sql);
  42. }
  43. sql = "CREATE TABLE completed (id INTEGER PRIMARY KEY, name TEXT, signature TEXT, type NUMERIC, parent TEXT,summary TEXT ,returnType TEXT ) ;";
  44. sqlLiteDal.RunSqlScalar(sql);
  45. SyntaxMode mode = new SyntaxMode();
  46. mode = SyntaxModeService.GetSyntaxMode("text/moscrif");
  47. progressDialog = new ProgressDialog("Generated...",ProgressDialog.CancelButtonType.None, mode.Keywords.Count() ,MainClass.MainWindow);
  48. foreach (Keywords kw in mode.Keywords){
  49. progressDialog.Update(kw.ToString());
  50. foreach (string wrd in kw.Words){
  51. insertNewRow(wrd,wrd,(int)CompletionDataTyp.keywords,"","","");
  52. }
  53. }
  54. Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("data.json", null, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
  55. fc.TransientFor = MainClass.MainWindow;
  56. fc.SetCurrentFolder(@"d:\Work\docs-api\output\");
  57. if (fc.Run() != (int)ResponseType.Accept) {
  58. return;
  59. }
  60. string json ;
  61. string fileName = fc.Filename;
  62. progressDialog.Destroy();
  63. fc.Destroy();
  64. progressDialog = new ProgressDialog("Generated...",ProgressDialog.CancelButtonType.None,100 ,MainClass.MainWindow);
  65. using (StreamReader file = new StreamReader(fileName)) {
  66. json = file.ReadToEnd();
  67. file.Close();
  68. file.Dispose();
  69. }
  70. //XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
  71. //doc.Save(fileName+"xml");
  72. /*JObject jDoc= JObject.Parse(json);
  73. //classes
  74. Console.WriteLine("o.Count->"+jDoc.Count);
  75. foreach (JProperty jp in jDoc.Properties()){
  76. Console.WriteLine(jp.Name);
  77. }
  78. Console.WriteLine("------------");
  79. JObject classes = (JObject)jDoc["classes"];
  80. foreach (JProperty jp in classes.Properties()){
  81. Console.WriteLine(jp.Name);
  82. JObject classDefin = (JObject)classes[jp.Name];
  83. string name = (string)classDefin["name"];
  84. string shortname = (string)classDefin["shortname"];
  85. string description = (string)classDefin["description"];
  86. //string type = (string)classDefin["type"];
  87. insertNewRow(name,name,(int)CompletionDataTyp.types,"",description,name);
  88. }
  89. Console.WriteLine("------------");
  90. JArray classitems = (JArray)jDoc["classitems"];
  91. foreach (JObject classitem in classitems){
  92. string name = (string)classitem["name"];
  93. Console.WriteLine(name);
  94. string description = (string)classitem["description"];
  95. string itemtype = (string)classitem["itemtype"];
  96. string classParent = (string)classitem["class"];
  97. string signature = (string)classitem["name"];
  98. CompletionDataTyp type = CompletionDataTyp.noting;
  99. string returnType= classParent;
  100. switch (itemtype){
  101. case "method":{
  102. JArray paramsArray = (JArray)classitem["params"];
  103. signature = signature+ GetParams(paramsArray);
  104. type = CompletionDataTyp.members;
  105. JObject returnJO =(JObject)classitem["return"] ;
  106. if(returnJO!=null){
  107. returnType = (string)returnJO["type"];
  108. }
  109. break;
  110. }
  111. case "property":{
  112. string tmpType = (string)classitem["type"];
  113. if(!String.IsNullOrEmpty(tmpType)){
  114. returnType=tmpType.Replace("{","").Replace("}","");
  115. }
  116. type = CompletionDataTyp.properties;
  117. break;
  118. }
  119. case "event":{
  120. JArray paramsArray = (JArray)classitem["params"];
  121. signature = signature+ GetParams(paramsArray);
  122. type = CompletionDataTyp.events;
  123. break;
  124. }
  125. case "attribute":{
  126. continue;
  127. break;
  128. }
  129. default:{
  130. type = CompletionDataTyp.noting;
  131. break;
  132. }
  133. }
  134. insertNewRow(name,signature,(int)type,classParent,description,returnType);
  135. }*/
  136. //classitems
  137. // string name = (string)o["project"]["name"];
  138. // Console.WriteLine(name);
  139. progressDialog.Destroy();
  140. md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "Done", "", Gtk.MessageType.Info);
  141. md.ShowDialog();
  142. return;
  143. /*
  144. Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("Select DOC Directory (with xml)", null, FileChooserAction.SelectFolder, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
  145. FileInfo[] xmls = new FileInfo[]{};
  146. if (fc.Run() == (int)ResponseType.Accept) {
  147. DirectoryInfo di = new DirectoryInfo(fc.Filename);
  148. xmls = di.GetFiles("*.xml");
  149. //List<string> output = new List<string>();
  150. //MainClass.CompletedCache.ListTypes= listSignature.Distinct().ToList();
  151. //MainClass.CompletedCache.ListMembers= listMemberSignature.Distinct().ToList();
  152. }
  153. progressDialog.Destroy();
  154. fc.Destroy();
  155. progressDialog = new ProgressDialog("Generated...",ProgressDialog.CancelButtonType.None,xmls.Length ,MainClass.MainWindow);
  156. foreach (FileInfo xml in xmls)
  157. {
  158. try
  159. {
  160. progressDialog.Update(xml.Name);
  161. if(!xml.Name.StartsWith("_"))
  162. getObject(xml.FullName);
  163. }
  164. catch(Exception ex) {
  165. Console.WriteLine(ex.Message);
  166. Console.WriteLine(ex.StackTrace);
  167. return;
  168. }
  169. }
  170. progressDialog.Destroy();
  171. md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "Done", "", Gtk.MessageType.Info);
  172. md.ShowDialog();*/
  173. }
  174. private void getObject(string file ){
  175. XmlDocument rssDoc;
  176. XmlNode nodeRss = new XmlDocument();
  177. //XmlNode nodeChannel = new XmlDocument();
  178. XmlNode nodeItem;
  179. try{
  180. XmlTextReader reader = new XmlTextReader(file);
  181. rssDoc = new XmlDocument();
  182. rssDoc.Load(reader);
  183. for (int i = 0; i < rssDoc.ChildNodes.Count; i++) {
  184. if (rssDoc.ChildNodes[i].Name == "doc"){
  185. nodeRss = rssDoc.ChildNodes[i];
  186. }
  187. }
  188. /*for (int i = 0; i < nodeRss.ChildNodes.Count; i++){
  189. if (nodeRss.ChildNodes[i].Name == "members"){
  190. nodeChannel = nodeRss.ChildNodes[i];
  191. }
  192. }*/
  193. string parent = "";
  194. for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
  195. {
  196. if (nodeRss.ChildNodes[i].Name == "member")
  197. {
  198. nodeItem = nodeRss.ChildNodes[i];
  199. string name="";
  200. string signature="";
  201. string summary = GetSummary(nodeItem);
  202. bool visibility = false;
  203. foreach (XmlAttribute atrb in nodeItem.Attributes){
  204. if (atrb.Name == "name"){
  205. name =atrb.InnerText;
  206. }
  207. if (atrb.Name == "signature"){
  208. signature =atrb.InnerText;
  209. }
  210. if (atrb.Name == "visibility"){
  211. if (atrb.InnerText.Trim() == "private"){
  212. visibility = true;
  213. }
  214. }
  215. }
  216. if(visibility) continue;
  217. if(!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(signature)){
  218. if(signature.Trim().StartsWith("(") ||
  219. signature.Trim().StartsWith("{") ||
  220. signature.Trim().StartsWith("[") ||
  221. signature.Trim().StartsWith("!")){
  222. continue;
  223. }
  224. if(name.StartsWith("T:")){
  225. parent =signature;
  226. insertNewRow(signature,signature,(int)CompletionDataTyp.types,"",summary,"");
  227. }
  228. if(name.Contains("this"))
  229. continue;
  230. if (name.StartsWith("P:")){
  231. int indx = signature.Trim().IndexOf(".");
  232. if(indx>0){
  233. string tmp = signature.Substring(0,indx);
  234. if(tmp.Length<2)
  235. continue;
  236. insertNewRow(tmp,signature,(int)CompletionDataTyp.properties,parent,summary,"");
  237. //listMemberSignature.Add(tmp);
  238. continue;
  239. }
  240. if(signature.Length<2)
  241. continue;
  242. insertNewRow(signature,signature,(int)CompletionDataTyp.properties,parent,summary,"");
  243. } else if(name.StartsWith("M:")){
  244. int indx = signature.IndexOf("(");
  245. if(indx>0){
  246. string tmp = signature.Substring(0,indx);
  247. if(tmp.Length<2)
  248. continue;
  249. insertNewRow(tmp,signature,(int)CompletionDataTyp.members,parent,summary,"");
  250. continue;
  251. }
  252. if(signature.Length<2)
  253. continue;
  254. insertNewRow(signature,signature,(int)CompletionDataTyp.members,parent,summary,"");
  255. } else if(name.StartsWith("E:")){
  256. int indx = signature.IndexOf("(");
  257. if(indx>0){
  258. string tmp = signature.Substring(0,indx);
  259. insertNewRow(tmp,signature,(int)CompletionDataTyp.events,parent,summary,"");
  260. continue;
  261. }
  262. insertNewRow(signature,signature,(int)CompletionDataTyp.events,parent,summary,"");
  263. }
  264. }
  265. }
  266. }
  267. } catch(Exception ex){
  268. MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "ERROR", ex.Message, Gtk.MessageType.Error);
  269. md.ShowDialog();
  270. Console.WriteLine(file);
  271. Console.WriteLine(ex.Message);
  272. }
  273. }
  274. private string GetSummary (XmlNode nodeItem){
  275. for (int j = 0; j < nodeItem.ChildNodes.Count; j++)
  276. {
  277. if (nodeItem.ChildNodes[j].Name == "summary")
  278. {
  279. string summary =nodeItem.ChildNodes[j].InnerText;
  280. summary = summary.Trim();
  281. summary = summary.Replace("\t"," ");
  282. return summary;
  283. }
  284. }
  285. return "";
  286. }
  287. /*
  288. private string GetParams(JArray paramsArray){
  289. if(paramsArray == null)
  290. return "()";
  291. string paramsString = "(";
  292. foreach (JObject jo in paramsArray){
  293. StringBuilder param = new StringBuilder();
  294. string name = (string)jo["name"];
  295. string description = (string)jo["description"];
  296. string type = (string)jo["type"];
  297. JValue optionalStr = (JValue)jo["optional"];
  298. bool optional = false;
  299. if(optionalStr!=null){
  300. if (!bool.TryParse(optionalStr.ToString(),out optional)){
  301. optional = false;
  302. }
  303. }
  304. string optdefault = (string)jo["optdefault"];
  305. JValue multipleStr = (JValue)jo["multiple"];
  306. bool multiple = false;
  307. if(multipleStr!=null){
  308. if (!bool.TryParse(multipleStr.ToString(),out multiple)){
  309. multiple = false;
  310. }
  311. }
  312. param.Append(name);
  313. if(!String.IsNullOrEmpty(optdefault))
  314. param.Append("="+optdefault);
  315. if((optional) && (String.IsNullOrEmpty(optdefault)))
  316. param.Append("=undefined");
  317. if(multiple)
  318. param.Append(",..");
  319. paramsString = paramsString + param.ToString()+",";
  320. }
  321. paramsString = paramsString.TrimEnd(',');
  322. paramsString = paramsString+ ")";
  323. return paramsString;
  324. }
  325. */
  326. private void insertNewRow(string name,string signature,int type,string parent,string summary,string returnType){
  327. string sql;
  328. signature = signature.Replace("'", "" );
  329. if(!String.IsNullOrEmpty(summary))
  330. summary = summary.Replace("'", "" );
  331. if(String.IsNullOrEmpty(parent))
  332. sql = String.Format("INSERT INTO completed (name,signature,type,summary,returnType) values ( '{0}' , '{1}' , '{2}', '{3}','{4}' ) ;",name,signature,type,summary,returnType);
  333. else
  334. 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);
  335. sqlLiteDal.RunSqlScalar(sql);
  336. }
  337. /*
  338. protected override void OnActivated ()
  339. {
  340. MainClass.CompletedCache.ListKeywords = new System.Collections.Generic.List<string>();
  341. MainClass.CompletedCache.ListTypes = new System.Collections.Generic.List<string>();
  342. SyntaxMode mode = new SyntaxMode();
  343. mode = SyntaxModeService.GetSyntaxMode("text/moscrif");
  344. foreach (Keywords kw in mode.Keywords){
  345. foreach (string wrd in kw.Words){
  346. MainClass.CompletedCache.ListKeywords.Add(wrd);
  347. }
  348. }
  349. MainClass.CompletedCache.SaveCompletedCache();
  350. List<string> listSignature = new List<string>();
  351. List<string> listMemberSignature = new List<string>();
  352. Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("Select DOC Directory (with xml)", null, FileChooserAction.SelectFolder, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
  353. if (fc.Run() == (int)ResponseType.Accept) {
  354. DirectoryInfo di = new DirectoryInfo(fc.Filename);
  355. FileInfo[] xmls = di.GetFiles("*.xml");
  356. List<string> output = new List<string>();
  357. foreach (FileInfo xml in xmls)
  358. {
  359. try
  360. {
  361. getObject(xml.FullName, ref listSignature, ref listMemberSignature);
  362. }
  363. catch(Exception ex) {
  364. Console.WriteLine(ex.Message);
  365. Console.WriteLine(ex.StackTrace);
  366. return;
  367. }
  368. }
  369. MainClass.CompletedCache.ListTypes= listSignature.Distinct().ToList();
  370. MainClass.CompletedCache.ListMembers= listMemberSignature.Distinct().ToList();
  371. }
  372. fc.Destroy();
  373. MainClass.CompletedCache.SaveCompletedCache();
  374. MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "Done", "", Gtk.MessageType.Info);
  375. md.ShowDialog();
  376. }
  377. private void getObject(string file , ref List<string> listSignature,ref List<string> listMemberSignature){
  378. XmlDocument rssDoc;
  379. XmlNode nodeRss = new XmlDocument();
  380. XmlNode nodeChannel = new XmlDocument();
  381. XmlNode nodeItem;
  382. try{
  383. XmlTextReader reader = new XmlTextReader(file);
  384. rssDoc = new XmlDocument();
  385. rssDoc.Load(reader);
  386. for (int i = 0; i < rssDoc.ChildNodes.Count; i++) {
  387. if (rssDoc.ChildNodes[i].Name == "doc"){
  388. nodeRss = rssDoc.ChildNodes[i];
  389. }
  390. }
  391. for (int i = 0; i < nodeRss.ChildNodes.Count; i++){
  392. if (nodeRss.ChildNodes[i].Name == "members") {
  393. nodeChannel = nodeRss.ChildNodes[i];
  394. }
  395. }
  396. for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
  397. {
  398. if (nodeChannel.ChildNodes[i].Name == "member")
  399. {
  400. nodeItem = nodeChannel.ChildNodes[i];
  401. string name="";
  402. string signature="";
  403. foreach (XmlAttribute atrb in nodeItem.Attributes){
  404. if (atrb.Name == "name"){
  405. name =atrb.InnerText;
  406. }
  407. if (atrb.Name == "signature"){
  408. signature =atrb.InnerText;
  409. }
  410. }
  411. if(!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(signature)){
  412. if(signature.Trim().StartsWith("(") ||
  413. signature.Trim().StartsWith("{") ||
  414. signature.Trim().StartsWith("[") ||
  415. signature.Trim().StartsWith("!")){
  416. continue;
  417. }
  418. if(name.StartsWith("T:")){
  419. listSignature.Add(signature);
  420. } else if (name.StartsWith("P:")){
  421. int indx = signature.Trim().IndexOf(".");
  422. if(indx>0){
  423. string tmp = signature.Substring(0,indx);
  424. if(tmp.Length<2)
  425. continue;
  426. listMemberSignature.Add(tmp);
  427. continue;
  428. }
  429. if(signature.Length<2)
  430. continue;
  431. listMemberSignature.Add(signature);
  432. } else if(name.StartsWith("M:")){
  433. int indx = signature.IndexOf("(");
  434. if(indx>0){
  435. string tmp = signature.Substring(0,indx);
  436. if(tmp.Length<2)
  437. continue;
  438. listMemberSignature.Add(tmp);
  439. continue;
  440. }
  441. if(signature.Length<2)
  442. continue;
  443. listMemberSignature.Add(signature);
  444. } else if(name.StartsWith("E:")){
  445. int indx = signature.IndexOf("(");
  446. if(indx>0){
  447. string tmp = signature.Substring(0,indx);
  448. listMemberSignature.Add(tmp);
  449. continue;
  450. }
  451. listMemberSignature.Add(signature);
  452. }
  453. }
  454. }
  455. }
  456. } catch(Exception ex){
  457. MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, "ERROR", ex.Message, Gtk.MessageType.Error);
  458. md.ShowDialog();
  459. Console.WriteLine(file);
  460. Console.WriteLine(ex.Message);
  461. }
  462. }*/
  463. }
  464. }