PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/dcomposer/elements/src/sym_assist_ui.d

http://github.com/LysanderG/DComposer
D | 307 lines | 224 code | 68 blank | 15 comment | 28 complexity | 9474c5d44eeb7d51cae3f7b999a6195a MD5 | raw file
  1. module sym_assist_ui;
  2. import std.conv;
  3. import std.string;
  4. import std.path;
  5. import std.xml;
  6. import dcore;
  7. import ui;
  8. import elements;
  9. export extern (C) string GetClassName()
  10. {
  11. return "sym_assist_ui.SYM_ASSIST_UI";
  12. }
  13. class SYM_ASSIST_UI :ELEMENT
  14. {
  15. public:
  16. string Name(){
  17. return "Symbol Assistant";
  18. }
  19. string Info(){
  20. return "View Symbol information";
  21. }
  22. string Version(){
  23. return "00.01";
  24. }
  25. string CopyRight() {
  26. return "Anthony Goins © 2015";
  27. }
  28. string License() {
  29. return "New BSD license";
  30. }
  31. string[] Authors() {
  32. return ["Anthony Goins <neontotem@gmail.com>"];
  33. }
  34. PREFERENCE_PAGE PreferencePage(){
  35. return null;
  36. }
  37. void Engage()
  38. {
  39. auto sauBuilder = new Builder;
  40. sauBuilder.addFromFile(ElementPath(Config.GetValue("sym_assist_ui", "glade_file", "resources/sym_assist_ui.glade")));
  41. mRootBox = cast(Box)sauBuilder.getObject("box1");
  42. //mMatchingLabel = cast(Label)sauBuilder.getObject("label1");
  43. mCandidatesComboBox = cast(ComboBoxText)sauBuilder.getObject("comboboxtext1");
  44. mPathLabel = cast(Label)sauBuilder.getObject("label2");
  45. //mSignatureLabel = cast(Label)sauBuilder.getObject("label3");
  46. mPane = cast(Paned)sauBuilder.getObject("paned1");
  47. mHierarchyView = cast(TreeView)sauBuilder.getObject("treeview1");
  48. mInterfaceView = cast(TreeView)sauBuilder.getObject("treeview2");
  49. mParentButton = cast(Button)sauBuilder.getObject("button2");
  50. mTypeLabel = cast(Label)sauBuilder.getObject("label9");
  51. mLocationButton = cast(Button)sauBuilder.getObject("button1");
  52. mMembersView = cast(TreeView)sauBuilder.getObject("treeview3");
  53. mDocCommentsLabel = cast(Label)sauBuilder.getObject("label4");
  54. mHierachyStore = cast(TreeStore)sauBuilder.getObject("treestore1");
  55. mInterfaceStore = cast(ListStore)sauBuilder.getObject("liststore1");
  56. mMembersStore = cast(ListStore)sauBuilder.getObject("liststore2");
  57. mCandidatesComboBox.addOnChanged(delegate void(ComboBoxText cbt){UpdateUI();});
  58. mHierarchyView.addOnRowActivated(&SelectSymbol);
  59. mInterfaceView.addOnRowActivated(&SelectSymbol);
  60. //mMembersView.addOnRowActivated(&SelectSymbol);
  61. mMembersView.addOnRowActivated(delegate void(TreePath tp, TreeViewColumn tvc, TreeView tv)
  62. {
  63. auto ti = new TreeIter;
  64. if(tv.getModel().getIter(ti, tp))
  65. {
  66. int idx = tv.getModel().getValueInt(ti, 2);
  67. dwrite(mCandidatesComboBox.getActive(), " ",idx);
  68. mCandidates = [mCandidates[mCandidatesComboBox.getActive()].Children[idx]];
  69. UpdateUI();
  70. }
  71. });
  72. mLocationButton.addOnClicked(delegate void(Button me)
  73. {
  74. auto idx = mCandidatesComboBox.getActive();
  75. if((idx < 0) || (idx > mCandidates.length)) return;
  76. DocMan.Open(mCandidates[idx].File, mCandidates[idx].Line);
  77. });
  78. mParentButton.addOnClicked(delegate void(Button me)
  79. {
  80. UpScope();
  81. });
  82. AddIcon("dcmp-sym-assist", ElementPath(Config.GetValue("icons", "sym-assist-ui", "resources/question-frame.png")));
  83. auto ActSymAssistUi = "ActSymAssistUI".AddAction("Symbol Assist", "See documentation for symbol", "dcmp-sym-assist", "F1",delegate void (Action){ActionAssist();});
  84. mActionMenuItem = AddToMenuBar("ActSymAssistUI", "E_lements");
  85. uiContextMenu.AddAction("ActSymAssistUI");
  86. Symbols.connect(&CatchSymbols);
  87. mPane.setPosition(Config.GetValue!int("sym_assist_ui", "pane_pos", 15));
  88. AddExtraPage(mRootBox, "Symbol Assist");
  89. Log.Entry("Engaged");
  90. }
  91. void Disengage()
  92. {
  93. RemoveFromMenuBar(mActionMenuItem, "E_lements");
  94. "ActSymAssistUI".RemoveAction();
  95. Config.SetValue("sym_assist_ui", "pane_pos", mPane.getPosition());
  96. Symbols.disconnect(&CatchSymbols);
  97. RemoveExtraPage(mRootBox);
  98. mRootBox.destroy();
  99. Log.Entry("Disengaged");
  100. }
  101. void Configure()
  102. {
  103. }
  104. private:
  105. Box mRootBox;
  106. //Label mMatchingLabel;
  107. ComboBoxText mCandidatesComboBox;
  108. Label mPathLabel;
  109. //Label mSignatureLabel;
  110. Paned mPane;
  111. TreeView mHierarchyView;
  112. TreeView mInterfaceView;
  113. Button mParentButton;
  114. Label mTypeLabel;
  115. Button mLocationButton;
  116. TreeView mMembersView;
  117. Label mDocCommentsLabel;
  118. TreeStore mHierachyStore;
  119. ListStore mInterfaceStore;
  120. ListStore mAliasThisStore;
  121. ListStore mMembersStore;
  122. MenuItem mActionMenuItem;
  123. DSYMBOL[] mCandidates;
  124. void UpdateUI()
  125. {
  126. TreeIter ti = new TreeIter;
  127. auto ndx = mCandidatesComboBox.getActive();
  128. if((ndx < 0) || (ndx >= mCandidates.length)) return;
  129. auto candi = mCandidates[ndx];
  130. mPathLabel.setText(candi.Path);
  131. //mSignatureLabel.setText(candi.Signature);
  132. string parentname = "(none)";
  133. auto parentscopeindex = lastIndexOf(candi.Path, '.');
  134. if(parentscopeindex > 0)
  135. {
  136. parentname = candi.Path[0..parentscopeindex];
  137. }
  138. mParentButton.setLabel(parentname);
  139. mHierachyStore.clear();
  140. auto ancestors = Symbols.FindAncestors(candi.Path);
  141. auto descendants = Symbols.FindDescendants(candi.Path);
  142. foreach(index,oldy; ancestors)
  143. {
  144. mHierachyStore.prepend(ti, null);
  145. if(index == 0) mHierachyStore.setValue(ti, 0, "\t"~oldy.Path~"\t");
  146. else mHierachyStore.setValue(ti, 0, oldy.Path);
  147. mHierachyStore.setValue(ti,1, oldy.Path);
  148. }
  149. if((ancestors.length == 0) && (candi.Base.length > 0))
  150. {
  151. //ancestor not in symbol library but we know it
  152. mHierachyStore.prepend(ti, null);
  153. mHierachyStore.setValue(ti, 0, candi.Base);
  154. mHierachyStore.setValue(ti, 1, candi.Base);
  155. }
  156. foreach (youngun; descendants)
  157. {
  158. mHierachyStore.append(ti, null);
  159. mHierachyStore.setValue(ti, 0, "\t\t"~youngun.Path);
  160. mHierachyStore.setValue(ti, 1, youngun.Path);
  161. }
  162. mInterfaceStore.clear();
  163. /*foreach(iface; candi.Interfaces)
  164. {
  165. mInterfaceStore.append(ti);
  166. mInterfaceStore.setValue(ti, 0, iface);
  167. //mInterfaceStore.setValue(ti, 1, iface);
  168. }*/
  169. string type;
  170. if(candi.Type.length > 0)type = candi.Type;
  171. else type = to!string(candi.Kind);
  172. if(candi.Protection.length > 0) type = candi.Protection ~ " " ~ type;
  173. mTypeLabel.setText(type);
  174. string file;
  175. if(candi.File.length > 0) file = format("Open %s:%s",baseName(candi.File), candi.Line);
  176. else file = "No source file (package)";
  177. mLocationButton.setLabel(file);
  178. mMembersStore.clear();
  179. foreach(int index, member; candi.Children)
  180. {
  181. mMembersStore.append(ti);
  182. mMembersStore.setValue(ti, 0, member.Name);
  183. mMembersStore.setValue(ti, 1, member.Path);
  184. mMembersStore.setValue(ti, 2, index);
  185. }
  186. string initialisedComment = "<span foreground=\"red\">Failed to parse markup text</span>";
  187. string comments = initialisedComment;
  188. mDocCommentsLabel.setMarkup(comments);
  189. if(candi.Signature.length > 0)
  190. comments = "<big><u>" ~ candi.Signature ~ "</u></big>\n";
  191. else comments = "<big><u>" ~ to!string(candi.Kind) ~ " " ~ candi.Name ~ "</u></big>\n";
  192. comments = comments ~ `<span font="monospace 14" >` ~ Ddoc2Pango(candi.Comment.encode()) ~ "</span>";
  193. mDocCommentsLabel.setMarkup(comments);
  194. if(initialisedComment == mDocCommentsLabel.getText())
  195. {
  196. Log.Entry("Unable to parse documentation comments", "Error");
  197. mDocCommentsLabel.setText(comments);
  198. }
  199. }
  200. void CatchSymbols(DSYMBOL[] Candidates)
  201. {
  202. if(Candidates.length < 1) return;
  203. mCandidates = Candidates;
  204. mCandidatesComboBox.removeAll();
  205. foreach(int ndx,candi; mCandidates)
  206. {
  207. mCandidatesComboBox.insertText(ndx,candi.Path);
  208. }
  209. mCandidatesComboBox.setActive(0);
  210. }
  211. void SelectSymbol(TreePath tp, TreeViewColumn tvc, TreeView tv)
  212. {
  213. auto ti = new TreeIter;
  214. if(tv.getModel().getIter(ti, tp))
  215. {
  216. string candidate = tv.getModel().getValueString(ti, 1);
  217. //auto syms = Symbols.FindExact(candidate.strip());
  218. auto syms = Symbols.FindExact(candidate);
  219. if(syms.length == 0) syms = Symbols.GetCompletions([candidate]);
  220. CatchSymbols(syms);
  221. }
  222. }
  223. void UpScope()
  224. {
  225. string ParentPath = mParentButton.getLabel();
  226. auto syms = Symbols.FindExact(ParentPath);
  227. CatchSymbols(syms);
  228. }
  229. void ActionAssist()
  230. {
  231. if(DocMan.Current is null)return;
  232. auto symCandidate = DocMan.Current.FullSymbol();
  233. if(symCandidate.length < 1) return;
  234. //auto dsyms = Symbols.GetCompletions(symCandidate.split('.'));
  235. auto dsyms = Symbols.FindExact(symCandidate);
  236. if(dsyms.length < 1) return;
  237. Symbols.emit(dsyms);
  238. }
  239. }