Exemplo n.º 1
0
        /// <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);
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
            DVariableComparer comparer = new DVariableComparer();
            List <Statement>  afterSelected;
            List <Statement>  selectedStatements = (List <Statement>)HelpFunctions.GetSelectedStatements();

            if (selectedStatements.Count == 0)
            {
                return;
            }

            //start and end of all relevant stmts and not only the selected ones.
            var start = selectedStatements[0].Tok.pos;

            if (selectedStatements[0] is UpdateStmt)
            {
                if (((UpdateStmt)selectedStatements[0]).Lhss.Count > 0)
                {
                    start = ((UpdateStmt)selectedStatements[0]).Lhss[0].tok.pos;
                }
                else if (((UpdateStmt)selectedStatements[0]).Rhss.Count > 0)
                {
                    if (((UpdateStmt)selectedStatements[0]).Rhss[0] is ExprRhs)
                    {
                        if (((ExprRhs)((UpdateStmt)selectedStatements[0]).Rhss[0]).Expr is ApplySuffix)
                        {
                            start -= ((NameSegment)((ApplySuffix)((ExprRhs)((UpdateStmt)selectedStatements[0]).Rhss[0]).Expr).Lhs).Name.Length;
                        }
                    }
                }
            }
            var end = selectedStatements[selectedStatements.Count - 1].EndTok.pos;

            HelpFunctions.GetSelectedStatements(out afterSelected).ToList();
            List <DVariable> tempIns       = new List <DVariable>();
            List <DVariable> tempOuts      = new List <DVariable>();
            List <DVariable> tempToDeclare = new List <DVariable>();

            HelpFunctions.GetVariables(selectedStatements, afterSelected, out tempIns, out tempOuts, out tempToDeclare);
            tempOuts = tempOuts.Union(tempToDeclare).ToList();
            List <DVariable>            intersect = tempIns.Intersect(tempOuts, comparer).ToList <DVariable>();
            Dictionary <string, string> renameDic = new Dictionary <string, string>();

            foreach (var x in intersect)
            {
                renameDic.Add(x.name, x.name + HelpFunctions.getNextIndex(x.name, tempIns, tempOuts, tempToDeclare));
            }
            List <Formal> ins  = new List <Formal>();
            List <Formal> outs = new List <Formal>();

            foreach (var x in tempIns)
            {
                DVariable temp = new DVariable(x.name, x.type);
                if (renameDic.ContainsKey(temp.name))
                {
                    temp.name = renameDic[temp.name];
                }
                ins.Add(temp.ToFormal());
            }

            foreach (var x in tempOuts)
            {
                outs.Add(x.ToFormal());
            }


            var requires = HelpFunctions.getPreAsserts(selectedStatements);
            var ensures  = HelpFunctions.getPostAsserts(selectedStatements);

            selectedStatements = HelpFunctions.removeReqAndEnsFromStmts(selectedStatements);
            selectedStatements = HelpFunctions.removeVarDeclFromStmts(selectedStatements);
            var method = HelpFunctions.GetCurrentMethod();
            List <MaybeFreeExpression> req = new List <MaybeFreeExpression>();

            foreach (var x in requires)
            {
                //HelpFunctions.renameB(x.Expr, renameDic);
                req.Add(new MaybeFreeExpression(HelpFunctions.renameB(x.Expr, renameDic)));
            }
            List <MaybeFreeExpression> ens = new List <MaybeFreeExpression>();

            foreach (var x in ensures)
            {
                ens.Add(new MaybeFreeExpression(x.Expr));
            }
            var newMethod = new Method(null, this.textBox.Text, false, false, new List <TypeParameter>(), ins, outs, req, new Specification <FrameExpression>(null, null), ens, new Specification <Microsoft.Dafny.Expression>(null, null), null, null, null);
            List <Microsoft.Dafny.Expression> Lhs = new List <Microsoft.Dafny.Expression>();
            List <AssignmentRhs> Rhs = new List <AssignmentRhs>();

            foreach (var x in intersect)
            {
                Lhs.Add(new NameSegment(null, x.name, new List <Type>()));
                Rhs.Add(new ExprRhs(new NameSegment(null, renameDic[x.name], new List <Type>())));
            }
            if (Lhs.Count > 0)
            {
                UpdateStmt correctingNames = new UpdateStmt(null, null, Lhs, Rhs);
                selectedStatements.Insert(0, correctingNames);
            }
            newMethod.Body = new BlockStmt(null, null, selectedStatements);
            // HelpFunctions.renameBody(newMethod.Body, new Dictionary<string, string>());///debug purposes only!!!!
            string signature = "aklt 5ra";
            string body      = "ya m3lm";

            try
            {
                signature = Printer.MethodSignatureToString(newMethod);
                body      = Printer.StatementToString(newMethod.Body);
            }
            catch (System.Exception ee)
            {
            }
            // Place the new method implementation in the code.
            int       position = HelpFunctions.GetCurrentMethod().BodyEndTok.pos + 1;
            ITextEdit edit     = HelpFunctions.GetWpfView().TextBuffer.CreateEdit();

            edit.Insert(position, "\r\n\r\n" + signature + "\r\n" + body);

            string methodcall = HelpFunctions.generateMethodCall(newMethod, tempIns, tempOuts);

            //DTE dte = (DTE)ExtractMethodPackage.GetGlobalService(typeof(DTE));
            //var selection = (EnvDTE.TextSelection)dte.ActiveDocument.Selection;

            edit.Delete(start, end - start + 1);
            edit.Insert(start, HelpFunctions.toDeclareString(tempToDeclare) + methodcall);
            edit.Apply();
            //selection.Text = methodcall;
        }