private void SaveText()
        {
            if (EditorLogic.CurrentEventResponseSave != null && mIsCodeValid)
            {
                IElement          currentElement = EditorLogic.CurrentElement;
                EventResponseSave currentEvent   = EditorLogic.CurrentEventResponseSave;


                if (this.syntaxBoxControl1.Document.Text != mLastSavedText)
                {
                    if (HasMatchingBrackets(this.syntaxBoxControl1.Document.Text))
                    {
                        mLastSavedText = this.syntaxBoxControl1.Document.Text;

                        EventCodeGenerator.InjectTextForEventAndSaveCustomFile(currentElement, EditorLogic.CurrentEventResponseSave, mLastSavedText);
                        PluginManager.ReceiveOutput("Saved " + EditorLogic.CurrentEventResponseSave);
                        GlueCommands.Self.GenerateCodeCommands.GenerateCurrentElementCode();
                        GluxCommands.Self.SaveGlux();
                    }
                    else
                    {
                        PluginManager.ReceiveError("Mismatch of } and { in event " + EditorLogic.CurrentEventResponseSave);
                    }
                }
            }
        }
Esempio n. 2
0
        public void TestRenamingEvents()
        {
            EventResponseSave ers = new EventResponseSave();

            ers.EventName    = "Whatever";
            ers.DelegateType = "System.EventHandler";
            mEntitySave.Events.Add(ers);
            string fileName = ers.GetSharedCodeFullFileName();

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            string directory = FileManager.CurrentDirectory + fileName;

            string contents = "int m = 33;";


            string contentsFileName = EventCodeGenerator.InjectTextForEventAndSaveCustomFile(
                mEntitySave, ers, contents);

            string entireFileContents = FileManager.FromFileText(contentsFileName);

            // Make sure that this file contains the contents
            if (!entireFileContents.Contains(contents))
            {
                throw new Exception("The entire files aren't being added to the event");
            }

            // Test renaming now
            string oldName = ers.EventName;

            string newName = "AfterRename";

            ers.EventName = newName;

            // I can't write unit tests for this yet because it requires that
            // all generate code be moved out of BaseElementTreeNode into CodeWriter:
            //EventResponseSavePropertyChangeHandler.Self.ReactToChange("EventName", oldName, ers, mEntitySave);

            //entireFileContents = FileManager.FromFileText(contentsFileName);
        }
Esempio n. 3
0
        private static void ReactToEventRename(object oldValue, EventResponseSave ers, IElement container)
        {
            string oldName = oldValue as string;
            string newName = ers.EventName;
            // The code
            // inside this
            // event handler
            // are saved in the
            // Element.Event.cs file
            // so that it can be edited
            // in Visual Studio.  If the
            // EventResponseSave changes then
            // it will use a new method.  We need
            // to take out the old method and move
            // the contents to the new method.

            // We'll "cheat" by setting the name to the old
            // one and getting the contents, then switching it
            // back to the new:
            string fullFileName = ers.GetSharedCodeFullFileName();

            if (!System.IO.File.Exists(fullFileName))
            {
                PluginManager.ReceiveError("Could not find the file " + fullFileName);
            }
            else if (CodeEditorControl.DetermineIfCodeFileIsValid(fullFileName) == false)
            {
                PluginManager.ReceiveError("Invalid code file " + fullFileName);
            }
            else
            {
                ers.EventName = oldName;
                string contents =
                    CodeEditorControl.RemoveWhiteSpaceForCodeWindow(ers.GetEventContents());

                ers.EventName = newName;
                // Now save the contents into the new method:

                if (string.IsNullOrEmpty(contents) || CodeEditorControl.HasMatchingBrackets(contents))
                {
                    EventCodeGenerator.InjectTextForEventAndSaveCustomFile(
                        container, ers, contents);
                    PluginManager.ReceiveOutput("Saved " + ers);
                    GlueCommands.Self.GenerateCodeCommands.GenerateCurrentElementCode();

                    DialogResult result = MessageBox.Show("Would you like to delete the old method On" + oldName + "?", "Delete old function?", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        int startIndex;
                        int endIndex;

                        contents = FileManager.FromFileText(fullFileName);

                        EventCodeGenerator.GetStartAndEndIndexForMethod(contents,
                                                                        "On" + oldName, out startIndex, out endIndex);

                        contents = contents.Remove(startIndex, endIndex - startIndex);

                        FileManager.SaveText(contents, fullFileName);
                    }
                }
                else
                {
                    PluginManager.ReceiveError("Mismatch of } and { in event " + ers);
                }
            }
        }