/// <summary> /// Call this method to insert a new piece of code /// </summary> public void InsertCode <T>() where T : ParsedScopeBlock { IProCode codeCode; string insertText; string blockDescription; // in case of an incorrect document, warn the user var parserErrors = _parser.ParseErrorsInHtml; if (!string.IsNullOrEmpty(parserErrors)) { if (UserCommunication.Message("The internal parser of 3P has found inconsistencies in your document :<br>" + parserErrors + "<br>You can still insert a new piece of code but the insertion position might not be calculated correctly; take caution of what is generated if you decide to go through with it.", MessageImg.MsgQuestion, "Generate code", "Problems spotted", new List <string> { "Continue", "Abort" }) != 0) { return; } } if (typeof(ParsedImplementation) == typeof(T)) { object input = new ProCodeFunction(); if (UserCommunication.Input(ref input, "Please provide information about the procedure that will be created", MessageImg.MsgQuestion, "Generate code", "Insert a new function") != 0) { return; } codeCode = (IProCode)input; codeCode.Name = codeCode.Name.MakeValidVariableName(); blockDescription = @"_FUNCTION " + codeCode.Name + " Procedure"; insertText = Encoding.Default.GetString(DataResources.FunctionImplementation).Trim(); insertText = insertText.Replace("{&type}", ((ProCodeFunction)codeCode).Type); insertText = insertText.Replace("{&private}", ((ProCodeFunction)codeCode).IsPrivate ? " PRIVATE" : ""); } else if (typeof(ParsedProcedure) == typeof(T)) { object input = new ProCodeProcedure(); if (UserCommunication.Input(ref input, "Please provide information about the procedure that will be created", MessageImg.MsgQuestion, "Generate code", "Insert a new procedure") != 0) { return; } codeCode = (IProCode)input; blockDescription = @"_PROCEDURE " + codeCode.Name + " Procedure"; insertText = Encoding.Default.GetString(DataResources.InternalProcedure).Trim(); insertText = insertText.Replace("{&private}", ((ProCodeProcedure)codeCode).IsPrivate ? " PRIVATE" : ""); } else { return; } if (string.IsNullOrEmpty(codeCode.Name)) { return; } // check if the code already exists if (_parsedItems.Exists(item => item.GetType() == typeof(T) && item.Name.EqualsCi(codeCode.Name))) { UserCommunication.Notify("Sorry, this name is already taken by another existing instance", MessageImg.MsgHighImportance, "Invalid name", "Existing name", 5); return; } insertText = insertText.Replace("{&name}", codeCode.Name); // reposition caret and insert bool insertBefore; int insertPos = GetCaretPositionForInsertion <T>(codeCode.Name, codeCode.InsertPosition, out insertBefore); if (insertPos < 0) { insertPos = Sci.GetPosFromLineColumn(Sci.Line.CurrentLine, 0); } insertText = FormatInsertion(insertText, blockDescription, insertBefore); int internalCaretPos = insertText.IndexOf("|||", StringComparison.Ordinal); insertText = insertText.Replace("|||", ""); Sci.SetSelection(insertPos); Sci.ModifyTextAroundCaret(0, 0, insertText); Sci.GoToLine(Sci.LineFromPosition(insertPos)); Sci.GotoPosition(insertPos + (internalCaretPos > 0 ? internalCaretPos : 0)); // in the case of a new function, update the prototype if needed if (typeof(ParsedImplementation) == typeof(T)) { ParseNow(); UpdateFunctionPrototypes(true); } }