Exemple #1
0
        /// <summary>
        /// Builds the source file.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="extensionSource">The extensionSource.</param>
        /// <param name="extensionDestination">The extension destination.</param>
        /// <param name="friendlyName">Name of the friendly.</param>
        internal void BuildSourceFile(
            Project project,
            string extensionSource,
            string extensionDestination,
            string friendlyName)
        {
            try
            {
                const string PlaceHolderText = "#PlaceHolder#";

                string message = string.Format("BuildSourceFile Project Name={0} friendlyName={1}", project.Name, friendlyName);

                TraceService.WriteLine(message);

                string sourceFile = friendlyName + "PluginBootstrap.cs";

                //// now we need to sort out the item template!
                project.AddToFolderFromTemplate("Bootstrap", "MvvmCross.Plugin.zip", sourceFile);

                ProjectItem projectItem = project.GetProjectItem(sourceFile);

                //// if we find the project item replace the text in it else use the find/replace window.
                if (projectItem != null)
                {
                    TextSelection textSelection = projectItem.DTE.ActiveDocument.Selection;
                    textSelection.SelectAll();
                    textSelection.ReplacePattern(PlaceHolderText, friendlyName);
                    projectItem.Save();
                }
            }
            catch (Exception exception)
            {
                TraceService.WriteError("BuildSourceFile " + exception.Message);
            }
        }
        /// <summary>
        /// Replaces the pattern.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="replacementText">The replacement text.</param>
        public void ReplacePattern(
            string text,
            string replacementText)
        {
            TextSelection textSelection = this.projectItem.DTE.ActiveDocument.Selection;

            textSelection.SelectAll();
            textSelection.ReplacePattern(text, replacementText);
        }
Exemple #3
0
        public void RemoveRegions(Proc replaceString)
        {
            Document      currentDocument     = _applicationObject.ActiveDocument;
            TextDocument  currentTextDocument = currentDocument.Object("") as TextDocument;
            TextSelection txtSel = (TextSelection)_applicationObject.ActiveDocument.Selection;

            if (currentTextDocument != null)
            {
                TextRanges refRanges = null;

                if (currentTextDocument.Language == "CSharp")
                {
                    try
                    {
                        txtSel.SelectAll();
                        txtSel.ReplacePattern(replaceString(), " ",
                                              (int)vsFindOptions.vsFindOptionsFromStart +
                                              (int)vsFindOptions.vsFindOptionsMatchInHiddenText +
                                              (int)vsFindOptions.vsFindOptionsKeepModifiedDocumentsOpen +
                                              (int)vsFindOptions.vsFindOptionsRegularExpression,
                                              ref refRanges);

                        //currentTextDocument.ReplacePattern(replaceString(), string.Empty,
                        //                                   (int) vsFindOptions.vsFindOptionsFromStart +
                        //                                   (int) vsFindOptions.vsFindOptionsMatchInHiddenText +
                        //                                   (int) vsFindOptions.vsFindOptionsKeepModifiedDocumentsOpen +
                        //                                   (int) vsFindOptions.vsFindOptionsRegularExpression,
                        //                                   ref refRanges);
                    }
                    catch (Exception e)
                    {
                        Console.Out.WriteLine("e = {0}", e);
                    }
                }

                #region some other crap

                #region inner region

                #  region

                #endregion

                #endregion

                #endregion

                #region

                #endregion
            }
        }
        /// <summary>
        /// Replaces the text.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="text">The text.</param>
        /// <param name="replacementText">The replacement text.</param>
        /// <param name="findOptions">The find options.</param>
        public static void ReplaceText(
            this ProjectItem instance,
            string text,
            string replacementText,
            int findOptions = (int)vsFindOptions.vsFindOptionsMatchCase)
        {
            TraceService.WriteLine("ProjectItemExtensions::ReplaceText in file " + instance.Name + " from '" + text + "' to '" + replacementText + "'");

            if (instance.Kind == VSConstants.VsProjectItemKindPhysicalFolder)
            {
                foreach (ProjectItem projectItem in instance.ProjectItems.Cast <ProjectItem>())
                {
                    projectItem.ReplaceText(text, replacementText);
                }

                return;
            }

            try
            {
                Window window = instance.Open(VSConstants.VsViewKindCode);

                if (window != null)
                {
                    window.Activate();

                    TextSelection textSelection = instance.DTE.ActiveDocument.Selection;
                    textSelection.SelectAll();

                    bool replaced = textSelection.ReplacePattern(text, replacementText, findOptions);

                    TraceService.WriteLine(replaced ? "Replaced" : "NOT replaced");

                    instance.Save();
                }
                else
                {
                    TraceService.WriteLine("Could not open window to do replacement for " + instance.Name);
                }
            }
            catch (Exception exception)
            {
                TraceService.WriteError("Replacing Text failed for " + instance.Name + " exception=" + exception);
            }
        }
Exemple #5
0
        /// <summary>
        /// Substitutes all occurrences in the specified text selection of the specified pattern
        /// string with the specified replacement string.
        /// </summary>
        /// <param name="textSelection">The text selection.</param>
        /// <param name="patternString">The pattern string.</param>
        /// <param name="replacementString">The replacement string.</param>
        internal static void SubstituteAllStringMatches(TextSelection textSelection, string patternString, string replacementString)
        {
            TextRanges dummy     = null;
            int        lastCount = -1;

            while (textSelection.ReplacePattern(patternString, replacementString, StandardFindOptions, ref dummy))
            {
                // it is possible that the replacements aren't actually being done. In such a case,
                // we can detect the situation by seeing if the count always remains the same, and
                // if so exiting early.
                if (lastCount == dummy.Count)
                {
                    OutputWindowHelper.WarningWriteLine("Forced a break out of TextDocumentHelper's SubstituteAllStringMatches for a selection.");
                    break;
                }
                lastCount = dummy.Count;
            }
        }
        /// <summary>
        /// Replaces the text.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="text">The text.</param>
        /// <param name="replacementText">The replacement text.</param>
        public static void ReplaceText(
            this ProjectItem instance,
            string text,
            string replacementText)
        {
            TraceService.WriteLine("ProjectItemExtensions::ReplaceText in file " + instance.Name + " from '" + text + "' to '" + replacementText + "'");

            Window window = instance.Open(VSConstants.VsViewKindCode);

            if (window != null)
            {
                window.Activate();

                TextSelection textSelection = instance.DTE.ActiveDocument.Selection;
                textSelection.SelectAll();
                textSelection.ReplacePattern(text, replacementText, (int)vsFindOptions.vsFindOptionsMatchCase);
                instance.Save();
            }
        }
 /// <summary>
 /// Substitutes all occurrences in the specified text selection of the specified pattern
 /// string with the specified replacement string.
 /// </summary>
 /// <param name="textSelection">The text selection.</param>
 /// <param name="patternString">The pattern string.</param>
 /// <param name="replacementString">The replacement string.</param>
 internal static void SubstituteAllStringMatches(TextSelection textSelection, string patternString, string replacementString)
 {
     TextRanges dummy = null;
     int lastCount = -1;
     while (textSelection.ReplacePattern(patternString, replacementString, StandardFindOptions, ref dummy))
     {
         // it is possible that the replacements aren't actually being done. In such a case,
         // we can detect the situation by seeing if the count always remains the same, and
         // if so exiting early.
         if (lastCount == dummy.Count)
         {
             OutputWindowHelper.WarningWriteLine("Forced a break out of TextDocumentHelper's SubstituteAllStringMatches for a selection.");
             break;
         }
         lastCount = dummy.Count;
     }
 }
 internal static void SubstituteAllStringMatches(TextSelection textSelection, string patternString, string replacementString)
 {
     TextRanges dummy = null;
     int lastCount = -1;
     while (textSelection.ReplacePattern(patternString, replacementString, StandardFindOptions, ref dummy))
     {
         if (lastCount == dummy.Count)
         {
             break;
         }
         lastCount = dummy.Count;
     }
 }
Exemple #9
0
        public static void ApplyPrefeence(DTE2 dte, string region, string controller, string field, string property, string preferredValue)
        {
            //string preferredValue = GetMostPreferredValue(region, controller, field, property);
            List <PreviousPropertyValue> listDefault = new List <PreviousPropertyValue>();

            foreach (ProjectItem pi in dte.Solution.Projects.Item(1).ProjectItems)
            {
                if (pi.ProjectItems != null)
                {
                    foreach (ProjectItem p in pi.ProjectItems)
                    {
                        if (p.Name.EndsWith(".Designer.cs"))
                        {
                            p.Open(EnvDTE.Constants.vsViewKindCode);
                            p.Document.Activate();
                            TextSelection     ts           = (TextSelection)dte.ActiveDocument.Selection;
                            TextSelection     ts2          = (TextSelection)dte.ActiveDocument.Selection;
                            string            srchPattern1 = "new System.Windows.Forms.Button();";
                            EnvDTE.TextRanges textRanges   = null;

                            ts.StartOfDocument(false);

                            int count = 0;

                            string   nameLine = "";
                            string   name     = "";
                            string[] np       = new string[50];

                            while (ts.FindPattern(srchPattern1, 0, ref textRanges))
                            {
                                ts.SelectLine();
                                nameLine = ts.Text;
                                count++;
                                string[] sp  = nameLine.Split('.');
                                string   spi = sp[1];
                                string[] sp2 = spi.Split('=');
                                name      = sp2[0];
                                np[count] = name;
                            }

                            int i = 1;
                            while (ts2.FindPattern(".BackColor = System.Drawing.Color", 0, ref textRanges))
                            {
                                PreviousPropertyValue def = new PreviousPropertyValue();

                                ts2.SelectLine();
                                string codeLine = ts2.Text;
                                codeLine = codeLine.Trim();
                                foreach (string s in np)
                                {
                                    string ss = s;
                                    if (ss != null)
                                    {
                                        ss = ss.Trim();
                                        if (codeLine.Contains(ss) == true)
                                        {
                                            ts2.ReplacePattern(codeLine, "this." + s + ".BackColor = System.Drawing.Color." + preferredValue + ";", 0, ref textRanges);
                                            np                 = np.Where(w => w != s).ToArray();
                                            def.FileName       = p.Name;
                                            def.ControllerType = controller;
                                            def.Property       = property;
                                            def.ControllerName = ss;
                                            def.DefaultValue   = codeLine;
                                            listDefault.Add(def);
                                        }
                                        //else
                                        //{
                                        //    ts2.LineDown();
                                        //    ts2.NewLine();
                                        //    ts2.Insert("this." + np[i] + ".BackColor = System.Drawing.Color." + preferredValue + ";");
                                        //}
                                        //def.FileName = p.Name;
                                        //def.ControllerType = controller;
                                        //def.Property = property;
                                        //def.ControllerName = ss;
                                        //def.DefaultValue = codeLine;
                                        //listDefault.Add(def);
                                    }
                                }

                                //i++;
                            }
                            if (np != null)
                            {
                                foreach (string s in np)
                                {
                                    if (s != null)
                                    {
                                        ts2.EndOfLine();

                                        ts2.NewLine();
                                        ts2.Insert("this." + np[i] + ".BackColor = System.Drawing.Color." + preferredValue + ";");
                                        np = np.Where(w => w != s).ToArray();
                                    }
                                }
                            }
                            SaveDefaultValues(listDefault);
                            dte.ActiveDocument.Save(p.Document.FullName);
                            dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesNo);
                        }
                    }
                }
            }
        }
Exemple #10
0
        public static bool ChangeToPreveiousValue(DTE2 dte, string controller, string property)
        {
            List <PreviousPropertyValue> listDefault = new List <PreviousPropertyValue>();
            List <PreviousPropertyValue> list        = LoadDefaultValues();

            if (list == null)
            {
                return(false);
            }
            else
            {
                foreach (ProjectItem pi in
                         dte.Solution.Projects.Item(1).ProjectItems)
                {
                    if (pi.ProjectItems != null)
                    {
                        foreach (ProjectItem p in pi.ProjectItems)
                        {
                            if (p.Name.EndsWith(".Designer.cs"))
                            {
                                p.Open(EnvDTE.Constants.vsViewKindCode);
                                p.Document.Activate();
                                TextSelection ts2 = (TextSelection)dte.ActiveDocument.Selection;

                                EnvDTE.TextRanges textRanges = null;

                                ts2.StartOfDocument(false);
                                //Find2 findWin = (Find2)dte.Find;
                                int count = 0;
                                //c = findWin.FindReplace(vsFindAction.vsFindActionFindAll, "button1", 0);
                                string   s    = "";
                                string   name = "";
                                string[] np   = new string[50];
                                // Advance to the next Visual Basic function beginning or end by
                                // searching for  "Sub" with white space before and after it.
                                //while
                                //while (ts.FindPattern(srchPattern1, 0, ref textRanges))
                                //{

                                //    //    //  Select the entire line.

                                //    count++;
                                //    ts.SelectLine();
                                //    s = ts.Text;

                                //    string[] sp = s.Split('.');
                                //    string spi = sp[1];


                                //    string[] sp2 = spi.Split('=');
                                //    name = sp2[0];

                                //    np[count] = name;
                                //    //ts.FindPattern("this." + name + ".BackColor = System.Drawing.Color", 0, ref textRanges);
                                //    //ts.SelectLine();
                                //    //s = ts.Text;
                                //    //ts2.StartOfDocument(false);
                                //    //while (ts.FindText("this." + name + ".BackColor = System.Drawing.Color", 0))
                                //    //{

                                //    //    ts.SelectLine();
                                //    //    string sd = ts.Text;
                                //    //    bool t = ts.ReplacePattern(sd, "this.button1.BackColor = System.Drawing.Color.Red;", 0, ref textRanges);
                                //    //}

                                //}

                                int i = 1;
                                //for(int i=1; i<=5;i++)
                                //{
                                //ts2 = null;


                                while (ts2.FindPattern(".BackColor = System.Drawing.Color", 0, ref textRanges))
                                {
                                    ts2.SelectLine();
                                    string sd = ts2.Text;
                                    sd = sd.Trim();
                                    foreach (PreviousPropertyValue dfcon in list)
                                    {
                                        if (dfcon.FileName == p.Name && sd.Contains(dfcon.ControllerName) && dfcon.Property == property && dfcon.ControllerType == controller)
                                        {
                                            ts2.ReplacePattern(sd, dfcon.DefaultValue, 0, ref textRanges);
                                        }
                                        i++;
                                        //}
                                    }
                                }
                                //ts.NewLine(1);


                                dte.ActiveDocument.Save(p.Document.FullName);
                                dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesNo);
                            }
                        }
                    }
                }
                return(true);
            }
        }