public static GetSharedCodeFullFileName ( IElement container, string baseProjectDirectory ) : string | ||
container | IElement | |
baseProjectDirectory | string | |
리턴 | string |
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); } } }
public static ParsedMethod GetParsedMethodFromAssociatedFile(this EventResponseSave instance) { string fullFileName = instance.GetSharedCodeFullFileName(); return(EventResponseSaveExtensionMethods.GetParsedMethodFromAssociatedFile(fullFileName, instance)); }
public static ParsedMethod GetParsedMethodFromAssociatedFile(this EventResponseSave instance, IElement container, string baseProjectDirectory) { string fullFileName = EventResponseSave.GetSharedCodeFullFileName(container, baseProjectDirectory); return(GetParsedMethodFromAssociatedFile(fullFileName, instance)); }
/// <summary> /// Injects the insideOfMethod into an event for the argument /// </summary> /// <param name="currentElement">The IElement containing the EventResponseSave.</param> /// <param name="eventResponseSave">The EventResponseSave which should have its contents set or replaced.</param> /// <param name="insideOfMethod">The inside of the methods to assign.</param> /// <returns>The full file name which contains the method contents.</returns> public static string InjectTextForEventAndSaveCustomFile(IElement currentElement, EventResponseSave eventResponseSave, string insideOfMethod) { // In case the user passes null we don't want to have null reference exceptions: if (insideOfMethod == null) { insideOfMethod = ""; } ParsedMethod parsedMethod = eventResponseSave.GetParsedMethodFromAssociatedFile(); string fullFileName = eventResponseSave.GetSharedCodeFullFileName(); bool forceRegenerate = false; string fileContents = null; if (File.Exists(fullFileName)) { fileContents = FileManager.FromFileText(fullFileName); forceRegenerate = fileContents.Contains("public partial class") == false && fileContents.Contains("{") == false; if (forceRegenerate) { GlueGui.ShowMessageBox("Forcing a regneration of " + fullFileName + " because it appears to be empty."); } } CreateEmptyCodeIfNecessary(currentElement, fullFileName, forceRegenerate); fileContents = FileManager.FromFileText(fullFileName); int indexToAddAt = 0; if (parsedMethod != null) { int startIndex; int endIndex; GetStartAndEndIndexForMethod(parsedMethod, fileContents, out startIndex, out endIndex); // We want to include the \r\n at the end, so add 2 endIndex += 2; string whatToRemove = fileContents.Substring(startIndex, endIndex - startIndex); fileContents = fileContents.Replace(whatToRemove, null); indexToAddAt = startIndex; // remove the method to re-add it } else { indexToAddAt = EventManager.GetLastLocationInClass(fileContents, startOfLine:true); } ICodeBlock codeBlock = new CodeDocument(2); codeBlock.TabCharacter = " "; insideOfMethod = "" + insideOfMethod.Replace("\r\n", "\r\n "); codeBlock = EventCodeGenerator.FillWithCustomEventCode(codeBlock, eventResponseSave, insideOfMethod, currentElement); string methodContents = codeBlock.ToString(); fileContents = fileContents.Insert(indexToAddAt, codeBlock.ToString()); eventResponseSave.Contents = null; try { FlatRedBall.Glue.IO.FileWatchManager.IgnoreNextChangeOnFile(fullFileName); FileManager.SaveText(fileContents, fullFileName); } catch (Exception e) { PluginManager.ReceiveError("Could not save file to " + fullFileName); } return fullFileName; }
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); }