public void InsertText(string newText, bool smartButtony = false) { if (theInputField.isFocused) { // If selection, replace selection // Else, just insert int start = Mathf.Min(theInputField.selectionAnchorPosition, theInputField.selectionFocusPosition); int end = Mathf.Max(theInputField.selectionAnchorPosition, theInputField.selectionFocusPosition); string checkText; if (start == end) { // No selection checkText = theInputField.text.Insert(start, newText); } else { // Yes selection checkText = theInputField.text.Substring(0, start) + newText + theInputField.text.Substring(end); } // Try adding extra newline if it's the end of the line string checkText2 = null; string after = checkText.Substring(start + newText.Length); if (smartButtony && (after.Length == 0 || after[0] == '\n')) { int indent = IDEPARSER.getIndentLevel(start, checkText); // Add newline directly if (start > 0 && checkText[start - 1] == ':') { checkText = checkText.Insert(start, "\n" + new string('\t', indent)); start += 1 + indent; } checkText2 = checkText.Insert(start + newText.Length, "\n" + new string('\t', indent)); start += 1 + indent; } // Check if it fits if (checkText2 == null || !IDETextManipulation.validateText(checkText2, codeRowsLimit, maxChars)) { checkText2 = null; if (!IDETextManipulation.validateText(checkText, codeRowsLimit, maxChars)) { checkText = null; } } // One of them succeded if (checkText2 != null || checkText != null) { inserting = true; setNewCaretPos(start + newText.Length); theInputField.text = checkText2 ?? checkText; } } }
public static char MyValidate(char c, int charIndex, bool isPasting, List <IndentLevel> toAddLevels, string currentText, IDETextField theTextField) { if (isPasting) { return(c); } if (c == '\n') { toAddLevels.Clear(); toAddLevels.Add(new IndentLevel(charIndex, IDEPARSER.getIndentLevel(charIndex, currentText))); theTextField.setNewCaretPos(toAddLevels[0].getNewCaretPos()); return('\n'); } return(c); }