/// <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)ExtractMethodPackage.GetGlobalService(typeof(DTE));

            dte.ActiveDocument.Save();
            ExtractLemmaWindow extract = new ExtractLemmaWindow();

            extract.Show();
        }
        //laddon
        public static Method GetCurrentMethod()
        {
            DTE dte      = (DTE)ExtractMethodPackage.GetGlobalService(typeof(DTE));
            var fileName = dte.ActiveDocument.FullName;
            var textView = GetWpfView();
            //var x=textView.Selection.Start.Position;
            var caretPosition = textView.Caret.Position.BufferPosition.Position;

            return(FindMethod(fileName, caretPosition));
        }
        //laddon
        public static IWpfTextView GetWpfView()
        {
            var         textManager    = (IVsTextManager)ExtractMethodPackage.GetGlobalService(typeof(SVsTextManager));
            var         componentModel = (IComponentModel)ExtractMethodPackage.GetGlobalService(typeof(SComponentModel));
            var         editor         = componentModel.GetService <IVsEditorAdaptersFactoryService>();
            IVsTextView textViewCurrent;

            textManager.GetActiveView(1, null, out textViewCurrent);
            return(editor.GetWpfTextView(textViewCurrent));
        }
        //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();
        }
Ejemplo n.º 5
0
        public static void inline(UpdateStmt stmt, string fileName, Method m /*the called method*/)
        {
            DTE dte = (DTE)ExtractMethodPackage.GetGlobalService(typeof(DTE));

            if (!(((ExprRhs)stmt.Rhss[0]).Expr is ApplySuffix))
            {
                return;
            }
            //var arg = ((ApplySuffix)((ExprRhs)stmt.Rhss[0]).Expr).Args;
            //Microsoft.Dafny.Type[] InTypes = getTypes(arg);

            //var rets = stmt.Lhss;
            //Microsoft.Dafny.Type[] retTypes = getTypes(rets);

            //Method m = HelpFunctions.FindMethod(fileName, ((NameSegment)((ApplySuffix)((ExprRhs)stmt.Rhss[0]).Expr).Lhs).Name, InTypes, retTypes);
            //Method m = HelpFunctions.FindMethod(fileName, "MinAndMax");
            Dictionary <string, string>       rename   = new Dictionary <string, string>();
            List <Microsoft.Dafny.Expression> currArgs = ((ApplySuffix)((ExprRhs)stmt.Rhss[0]).Expr).Args;
            List <Microsoft.Dafny.Expression> currRet  = stmt.Lhss;

            for (int i = 0; i < m.Ins.Count && i < currArgs.Count; i++)
            {
                if (currArgs[i] is Microsoft.Dafny.Expression)
                {
                    rename.Add(m.Ins[i].Name, Printer.ExprToString(currArgs[i]));
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            for (int i = 0; i < m.Outs.Count && i < currRet.Count; i++)
            {
                rename.Add(m.Outs[i].Name, Printer.ExprToString(currRet[i]));
            }
            List <MaybeFreeExpression> Ens = new List <MaybeFreeExpression>(m.Ens);
            List <MaybeFreeExpression> Req = new List <MaybeFreeExpression>(m.Req);
            //foreach (var x in Ens)
            //{
            //    HelpFunctions.renameBody(x, rename);
            //    //ens.Add(new AssertStmt());
            //}

            StringBuilder res          = new StringBuilder();
            int           indentLength = (stmt.Tok.col - ((NameSegment)((ApplySuffix)((ExprRhs)stmt.Rhss[0]).Expr).Lhs).Name.Length) - 1;

            if (stmt.Lhss.Count > 0)
            {
                indentLength = stmt.Lhss[0].tok.col - 1;
            }
            string indent = new string('\t', indentLength);

            foreach (var x in Req)
            {
                //HelpFunctions.renameBody(x, rename);
                res.Append(indent + "assert " + Printer.ExprToString(HelpFunctions.renameB(x.E, rename)) + ";\r\n");
            }

            //HelpFunctions.renameBody(m.Body, rename);
            m.Body = (BlockStmt)HelpFunctions.renameB(m.Body, rename);

            foreach (var x in m.Body.Body)
            {
                res.Append(indent + Printer.StatementToString(x, indentLength * 4) + "\r\n");
            }

            foreach (var x in Ens)
            {
                res.Append(indent + "assert " + Printer.ExprToString(HelpFunctions.renameB(x.E, rename)) + ";\r\n");
            }

            string result = res.ToString();

            if (Ens.Count > 0)
            {
                result = result.Remove(result.Length - 2, 2);
            }

            //ITextEdit edit = HelpFunctions.GetWpfView().TextBuffer.CreateEdit();
            int from = stmt.Tok.pos - m.Name.Length;

            if (stmt.Lhss.Count > 0)
            {
                from = stmt.Lhss[0].tok.pos;
            }
            edit.Delete(from, stmt.EndTok.pos - from + 1);
            if (result.Length > 1)
            {
                edit.Insert(from, result.Substring(indentLength));
            }
            //edit.Apply();
            //((EnvDTE.TextSelection)dte.ActiveDocument.Selection).Text = "";
            ////((EnvDTE.TextSelection)dte.ActiveDocument.Selection).Delete();
            //if (result.Length > 1) ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).Insert(result.Substring(1));
        }
Ejemplo n.º 6
0
        public static void traverse(Statement statement, Method m)
        {
            if (statement is AssignStmt)
            {
                statement = statement as AssignStmt;
                traverse(((AssignStmt)statement).Lhs, m);
            }
            else if (statement is UpdateStmt)
            {
                DTE dte      = (DTE)ExtractMethodPackage.GetGlobalService(typeof(DTE));
                var fileName = dte.ActiveDocument.FullName;

                UpdateStmt curr = (UpdateStmt)statement;
                if (!(((ExprRhs)curr.Rhss[0]).Expr is ApplySuffix))
                {
                    return;
                }
                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 temp = HelpFunctions.FindMethod(fileName, ((NameSegment)((ApplySuffix)((ExprRhs)curr.Rhss[0]).Expr).Lhs).Name, InTypes, retTypes);

                //check name of called func first;
                if (MethodsAreEqual(m, temp))
                {
                    inline((UpdateStmt)statement, fileName, m);
                }
            }
            else if (statement is WhileStmt)
            {
                traverse(((WhileStmt)statement).Body, m);
                traverse(((WhileStmt)statement).Guard, m);
            }
            else if (statement is IfStmt)
            {
                traverse(((IfStmt)statement).Guard, m);
                traverse(((IfStmt)statement).Thn, m);
                traverse(((IfStmt)statement).Els, m);
            }
            else if (statement is BlockStmt)
            {
                foreach (var st in ((BlockStmt)statement).SubStatements)
                {
                    traverse(st, m);
                }
            }
            else if (statement is AssertStmt)
            {
                traverse(((AssertStmt)statement).Proof, m);
            }
            else if (statement is AlternativeStmt)
            {
            }
            else if (statement is VarDeclStmt)
            {
                if (((VarDeclStmt)statement).Update != null)
                {
                    traverse(((VarDeclStmt)statement).Update, m);
                }
                foreach (var localvar in ((VarDeclStmt)statement).Locals)
                {
                    //traverse(localvar);
                }
            }
            else if (statement is CallStmt)
            {
                foreach (var expr in ((CallStmt)statement).Lhs)
                {
                    traverse(expr, m);
                }
                foreach (var expr in ((CallStmt)statement).Args)
                {
                    traverse(expr, m);
                }
            }
            else if (statement is PrintStmt)
            {
                foreach (var expr in ((PrintStmt)statement).Args)
                {
                    traverse(expr, m);
                }
            }
            else if (statement is AssignSuchThatStmt)
            {
                foreach (var expr in ((AssignSuchThatStmt)statement).Lhss)
                {
                    traverse(expr, m);
                }
                traverse(((AssignSuchThatStmt)statement).Expr, m);
            }
            else if (statement is CalcStmt)
            {
            }
            else if (statement is MatchStmt)
            {
            }
            else
            {
                if (statement != null)
                {
                    throw new NotImplementedException(statement.ToString());
                }
            }
        }