/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void MenuItemCallback(object sender, EventArgs e) { DTE dte = (DTE)this.ServiceProvider.GetService(typeof(DTE)); dte.ActiveDocument.Save(); var fileName = dte.ActiveDocument.FullName; var selectedStatements = (List <Statement>)HelpFunctions.GetSelectedStatements(); if (selectedStatements.Count == 1 && selectedStatements[0] is UpdateStmt) { UpdateStmt curr = (UpdateStmt)selectedStatements[0]; var arg = ((ApplySuffix)((ExprRhs)curr.Rhss[0]).Expr).Args; Microsoft.Dafny.Type[] InTypes = getTypes(arg); var rets = curr.Lhss; Microsoft.Dafny.Type[] retTypes = getTypes(rets); Method m = HelpFunctions.FindMethod(fileName, ((NameSegment)((ApplySuffix)((ExprRhs)curr.Rhss[0]).Expr).Lhs).Name, InTypes, retTypes); edit = HelpFunctions.GetWpfView().TextBuffer.CreateEdit(); inline(curr, fileName, m); edit.Apply(); //////////////////////////////////// } else if (selectedStatements.Count == 0 && HelpFunctions.GetCurrentMethod() != null) { Method currMethod = HelpFunctions.GetCurrentMethod(); var program = HelpFunctions.GetProgram(fileName); var decls = program.Modules().SelectMany(m => m.TopLevelDecls).ToList(); var callables = ModuleDefinition.AllCallables(decls); edit = HelpFunctions.GetWpfView().TextBuffer.CreateEdit(); foreach (var curr in callables) { if (curr is Method) { var m = curr as Method; traverse(m.Body, currMethod); } } var textView = HelpFunctions.GetWpfView(); var start = currMethod.tok.pos; if (currMethod is Lemma) { start -= 6; } else { start -= 7; } var end = currMethod.BodyEndTok.pos + 1; edit.Delete(start, end - start); edit.Apply(); } //HelpFunctions.prettyPrint(fileName); }
//not used public static void prettyPrint(string fileName) { DTE dte = (DTE)ExtractMethodPackage.GetGlobalService(typeof(DTE)); TextWriter x = new StringWriter(); Printer p = new Printer(x); dte.ActiveDocument.Save(); p.PrintProgram(HelpFunctions.GetProgram(fileName), false); var resultText = x.ToString(); //get length of doc TextDocument doc = (TextDocument)(dte.ActiveDocument.Object("TextDocument")); var pp = doc.StartPoint.CreateEditPoint(); string s = pp.GetText(doc.EndPoint); ITextEdit edit = HelpFunctions.GetWpfView().TextBuffer.CreateEdit(); edit.Delete(0, s.Length); edit.Insert(0, resultText); edit.Apply(); dte.ActiveDocument.Save(); }