PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/CSEdit/CSEdit/Editor/Editor.cs

https://bitbucket.org/floAr/personal
C# | 308 lines | 254 code | 50 blank | 4 comment | 31 complexity | 7f9542aea9e3329e599f92e6c2b0612b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using CSParse;
  11. using CSStore;
  12. using System.Diagnostics;
  13. namespace CSEdit
  14. {
  15. public partial class Editor : Form
  16. {
  17. public Editor()
  18. {
  19. InitializeComponent();
  20. codeTextBox.Text = @"namespace N
  21. {
  22. class C : B
  23. {
  24. void Method(C p)
  25. {
  26. C d = new C();
  27. p.DoNothing();
  28. d.DoNothing();
  29. this.F2 = p.Prop.Field;
  30. }
  31. void DoNothing()
  32. {
  33. }
  34. B F2;
  35. B Prop
  36. {
  37. get { return this.F2; }
  38. }
  39. }
  40. class B
  41. {
  42. B Field;
  43. }
  44. }";
  45. codeTextBox.TextChanged += new EventHandler(codeTextBox_TextChanged);
  46. }
  47. string oldParse = string.Empty;
  48. void codeTextBox_TextChanged(object sender, EventArgs e)
  49. {
  50. try
  51. {
  52. List<Token> tokens = Lexer.Lex(codeTextBox.Text);
  53. FileNode file = FileParser.Parse(tokens);
  54. Resolver.ResolveReferences(new[] { file });
  55. oldParse = file.ToString();
  56. tokenTextBox.Text = oldParse;
  57. }
  58. catch (InvalidParseSource ex)
  59. {
  60. string text = ex.Message + Environment.NewLine + oldParse;
  61. tokenTextBox.Text = text;
  62. }
  63. }
  64. private void openToolStripMenuItem_Click(object sender, EventArgs e)
  65. {
  66. OpenFileDialog dialog = new OpenFileDialog();
  67. dialog.Filter = "CS file (*.cs)|*.cs";
  68. DialogResult result = dialog.ShowDialog();
  69. if (result == DialogResult.OK)
  70. {
  71. codeTextBox.Text = File.ReadAllText(dialog.FileName);
  72. //codeTextBox.Highlight(0, codeTextBox.Text.Length);
  73. }
  74. }
  75. private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  76. {
  77. SaveFileDialog dialog = new SaveFileDialog();
  78. dialog.DefaultExt = "cs";
  79. dialog.Filter = "CS file (*.cs)|*.cs";
  80. DialogResult result = dialog.ShowDialog();
  81. if (result == DialogResult.OK)
  82. {
  83. File.WriteAllText(dialog.FileName, codeTextBox.Text);
  84. }
  85. }
  86. private void tokenizeToolStripMenuItem_Click(object sender, EventArgs e)
  87. {
  88. List<Token> tokens = Lexer.Lex(codeTextBox.Text);
  89. StringBuilder sb = new StringBuilder();
  90. foreach (Token token in tokens)
  91. {
  92. sb.AppendLine(string.Format("Id = {0},\t\tValue = {1}", token.Id, token.Value));
  93. }
  94. tokenTextBox.Text = sb.ToString();
  95. }
  96. private void parseToolStripMenuItem_Click(object sender, EventArgs e)
  97. {
  98. List<Token> tokens = Lexer.Lex(codeTextBox.Text);
  99. FileNode file = FileParser.Parse(tokens);
  100. tokenTextBox.Text = file.ToString();
  101. }
  102. private IEnumerable<FileNode> ParseDirectory(DirectoryInfo dir, List<string> errors)
  103. {
  104. foreach (DirectoryInfo childDir in dir.GetDirectories())
  105. {
  106. foreach (FileNode fileNode in ParseDirectory(childDir, errors))
  107. {
  108. yield return fileNode;
  109. }
  110. }
  111. foreach (FileInfo file in dir.GetFiles("*.cs"))
  112. {
  113. FileNode fileNode = null;
  114. string error = null;
  115. try
  116. {
  117. fileNode = ParseFile(file);
  118. }
  119. catch (InvalidParseSource ex)
  120. {
  121. error = "Failed - " + file.FullName + " - " + ex.Message;
  122. }
  123. if (error != null)
  124. {
  125. errors.Add(error);
  126. }
  127. if (fileNode != null)
  128. {
  129. yield return fileNode;
  130. }
  131. }
  132. }
  133. private FileNode ParseFile(FileInfo file)
  134. {
  135. string data = File.ReadAllText(file.FullName);
  136. FileNode fileNode = FileParser.Parse(Lexer.Lex(data), false);
  137. fileNode.Path = file.FullName;
  138. return fileNode;
  139. }
  140. private void resolveDirToolStripMenuItem_Click(object sender, EventArgs e)
  141. {
  142. FolderBrowserDialog dialog = new FolderBrowserDialog();
  143. DialogResult result = dialog.ShowDialog();
  144. if (result == System.Windows.Forms.DialogResult.OK)
  145. {
  146. string root = dialog.SelectedPath;
  147. DirectoryInfo dir = new DirectoryInfo(root);
  148. List<string> errors = new List<string>();
  149. DateTime start = DateTime.Now;
  150. List<FileNode> files = ParseDirectory(dir, errors).ToList();
  151. DateTime end = DateTime.Now;
  152. GC.Collect();
  153. string text = string.Empty;
  154. text += string.Format("{0} files took {1:0.00} to parse{2}", files.Count, (end - start).TotalSeconds, Environment.NewLine);
  155. start = DateTime.Now;
  156. Resolver.ResolveReferences(files);
  157. end = DateTime.Now;
  158. ICollection<TypeNode> types = TypeVisitor.GetTypes(files);
  159. text += string.Format("{0} types took {1:0.00} to resolve{2}", types.Count, (end - start).TotalSeconds, Environment.NewLine);
  160. text += string.Join(Environment.NewLine, errors.ToArray());
  161. //text += Environment.NewLine;
  162. //text += string.Join(Environment.NewLine, types.Keys.ToArray());
  163. tokenTextBox.Text = text;
  164. GC.Collect();
  165. }
  166. }
  167. private void listDirToolStripMenuItem_Click(object sender, EventArgs e)
  168. {
  169. FolderBrowserDialog dialog = new FolderBrowserDialog();
  170. DialogResult result = dialog.ShowDialog();
  171. if (result == System.Windows.Forms.DialogResult.OK)
  172. {
  173. GetFileMappings(dialog.SelectedPath);
  174. }
  175. }
  176. private Dictionary<string, string> GetFileMappings(string root)
  177. {
  178. DirectoryInfo dir = new DirectoryInfo(root);
  179. Dictionary<string, string> elementFileMapping = new Dictionary<string, string>();
  180. List<string> errors = new List<string>();
  181. foreach (FileNode file in ParseDirectory(dir, errors).Where(f => !string.IsNullOrEmpty(f.Path)))
  182. {
  183. foreach (TypeNode type in TypeVisitor.GetTypes(file))
  184. {
  185. elementFileMapping[type.Name.ToString()] = file.Path;
  186. foreach (MemberNode member in type.Members)
  187. {
  188. elementFileMapping[member.Name.ToString()] = file.Path;
  189. }
  190. }
  191. }
  192. return elementFileMapping;
  193. }
  194. private void codeTextBox_KeyPress(object sender, KeyPressEventArgs e)
  195. {
  196. }
  197. private void codeTextBox_KeyDown(object sender, KeyEventArgs e)
  198. {
  199. if (e.KeyCode == Keys.F12)
  200. {
  201. try
  202. {
  203. FileNode file = FileParser.Parse(Lexer.Lex(codeTextBox.Text));
  204. Resolver.ResolveReferences(new[] { file });
  205. int caret = codeTextBox.SelectionStart;
  206. FinderVisitor finder = new FinderVisitor(caret);
  207. finder.Visit(file);
  208. if (finder.FoundReference != null && file.DeclarationLocations.ContainsKey(finder.FoundReference.Id))
  209. {
  210. codeTextBox.SelectionStart = file.DeclarationLocations[finder.FoundReference.Id].StartPos;
  211. }
  212. else if (finder.FoundExpression != null)
  213. {
  214. MemberExpression memberExpression = finder.FoundExpression as MemberExpression;
  215. LocalExpression localExpression = finder.FoundExpression as LocalExpression;
  216. if (memberExpression != null)
  217. {
  218. codeTextBox.SelectionStart = file.DeclarationLocations[memberExpression.Member.Name.Id].StartPos;
  219. }
  220. else if (localExpression != null)
  221. {
  222. if (localExpression.Declaration != null)
  223. {
  224. IdentifierExpression variable = localExpression.Declaration.Variables.First(v => v.Id.Name == localExpression.Variable);
  225. codeTextBox.SelectionStart = variable.StartToken.StartPos;
  226. }
  227. else if (localExpression.Parameter != null)
  228. {
  229. IdentifierExpression variable = localExpression.Parameter.Variable;
  230. codeTextBox.SelectionStart = variable.StartToken.StartPos;
  231. }
  232. }
  233. else
  234. {
  235. Debug.Fail("Finder found unknown expression.");
  236. }
  237. }
  238. }
  239. catch (InvalidParseSource)
  240. { }
  241. }
  242. }
  243. CodeStore store = new CodeStore();
  244. private void storeDirToolStripMenuItem_Click(object sender, EventArgs args)
  245. {
  246. throw new NotImplementedException();
  247. }
  248. private void gCToolStripMenuItem_Click(object sender, EventArgs e)
  249. {
  250. //MessageBox.Show(store.inMemoryStore.Count.ToString());
  251. GC.Collect();
  252. }
  253. }
  254. }