private void scintillaControl_InsertCheck(object sender, InsertCheckEventArgs e) { if ((e.Text.EndsWith("\r") || e.Text.EndsWith("\n"))) { //var curLine = scintilla1.LineFromPosition(e.Position); //var curLineText = scintilla1.Lines[curLine].Text; //var indent = Regex.Match(curLineText, @"^\s*"); //e.Text += indent.Value; // Add indent following "\r\n" // Current line end with bracket? //if (Regex.IsMatch(curLineText, @"{\s*$")) // e.Text += '\t'; // Add tab int indent = _scintillaControl.zGetLineIndent(_scintillaControl.zGetCurrentLineNumber()); int position = _scintillaControl.CurrentPosition - 1; if (position >= 0) { char c = (char)_scintillaControl.GetCharAt(position); if (c == '{') indent += _tabWidth; } e.Text += new string(' ', indent); // Add indent following "\r\n" } }
/// <summary> /// On insert text event, auto format the text. /// </summary> /// <param name="s"></param> /// <param name="e"></param> private void HandleInsertCheck(object s, InsertCheckEventArgs e) { // do not insert text when // the Ctrl key is pressed if (DisableEditing) e.Text = string.Empty; // auto indent if (e.Text.EndsWith("\n")) { // get text of line above var text = Lines[LineFromPosition(CurrentPosition)].Text; // insert indent of line above e.Text += Regex.Match(text, @"^[ \t]*").Value; // if line above ends with '{' insert indent if (Regex.IsMatch(text, @"{\s*$")) e.Text += '\t'; } }
/// <summary> /// On insert text event, auto format the text. /// </summary> /// <param name="s"></param> /// <param name="e"></param> private void HandleInsertCheck(object s, InsertCheckEventArgs e) { // do not insert text when // the Ctrl key is pressed if (DisableEditing) { e.Text = string.Empty; } // auto indent if (e.Text.EndsWith("\n")) { // get text of line above var text = Lines[LineFromPosition(CurrentPosition)].Text; // insert indent of line above e.Text += Regex.Match(text, @"^[ \t]*").Value; // if line above ends with '{' insert indent if (Regex.IsMatch(text, @"{\s*$")) { e.Text += '\t'; } } }
private void ScnModified(ref NativeMethods.SCNotification scn) { // The InsertCheck, BeforeInsert, BeforeDelete, Insert, and Delete events can all potentially require // the same conversions: byte to char position, char* to string, etc.... To avoid doing the same work // multiple times we share that data between events. if ((scn.modificationType & NativeMethods.SC_MOD_INSERTCHECK) > 0) { var eventArgs = new InsertCheckEventArgs(this, scn.position, scn.length, scn.text); OnInsertCheck(eventArgs); cachedPosition = eventArgs.CachedPosition; cachedText = eventArgs.CachedText; } const int sourceMask = (NativeMethods.SC_PERFORMED_USER | NativeMethods.SC_PERFORMED_UNDO | NativeMethods.SC_PERFORMED_REDO); if ((scn.modificationType & (NativeMethods.SC_MOD_BEFOREDELETE | NativeMethods.SC_MOD_BEFOREINSERT)) > 0) { var source = (ModificationSource)(scn.modificationType & sourceMask); var eventArgs = new BeforeModificationEventArgs(this, source, scn.position, scn.length, scn.text); eventArgs.CachedPosition = cachedPosition; eventArgs.CachedText = cachedText; if ((scn.modificationType & NativeMethods.SC_MOD_BEFOREINSERT) > 0) { OnBeforeInsert(eventArgs); } else { OnBeforeDelete(eventArgs); } cachedPosition = eventArgs.CachedPosition; cachedText = eventArgs.CachedText; } if ((scn.modificationType & (NativeMethods.SC_MOD_DELETETEXT | NativeMethods.SC_MOD_INSERTTEXT)) > 0) { var source = (ModificationSource)(scn.modificationType & sourceMask); var eventArgs = new ModificationEventArgs(this, source, scn.position, scn.length, scn.text, scn.linesAdded); eventArgs.CachedPosition = cachedPosition; eventArgs.CachedText = cachedText; if ((scn.modificationType & NativeMethods.SC_MOD_INSERTTEXT) > 0) { OnInsert(eventArgs); } else { OnDelete(eventArgs); } // Always clear the cache cachedPosition = null; cachedText = null; // For backward compatibility.... Of course this means that we'll raise two // TextChanged events for replace (insert/delete) operations, but that's life. OnTextChanged(EventArgs.Empty); } if ((scn.modificationType & NativeMethods.SC_MOD_CHANGEANNOTATION) > 0) { var eventArgs = new ChangeAnnotationEventArgs(scn.line); OnChangeAnnotation(eventArgs); } }
/// <summary> /// Raises the <see cref="InsertCheck" /> event. /// </summary> /// <param name="e">An <see cref="InsertCheckEventArgs" /> that contains the event data.</param> protected virtual void OnInsertCheck(InsertCheckEventArgs e) { var handler = Events[insertCheckEventKey] as EventHandler<InsertCheckEventArgs>; if (handler != null) handler(this, e); }
private void textBox_InsertCheck(object sender, InsertCheckEventArgs e) { var text = e.Text; // Replace tabs with 3 whitespaces text = text.Replace("\t", " "); var x = this.textBox.Lines.Select(l => l.Indentation).ToList(); // TODO: Handle newlines and indention here somehow? Or can Indentation do that? // Return the modified text. e.Text = text; }
private void codeBox_InsertCheck(object sender, InsertCheckEventArgs e) { if (e.Text.EndsWith("\r") || e.Text.EndsWith("\n")) { int startPos = _codeBox.Lines[_codeBox.LineFromPosition(_codeBox.CurrentPosition)].Position; int endPos = e.Position; string curLineText = _codeBox.GetTextRange(startPos, (endPos - startPos)); Match indent = Regex.Match(curLineText, "^[ \\t]*"); e.Text = (e.Text + indent.Value); if (Regex.IsMatch(curLineText, "{\\s*$")) { e.Text = (e.Text + "\t"); } } }
private void scintilla1_InsertCheck(object sender, InsertCheckEventArgs e) { bool removed = false; string text = e.Text; // remove ctrl-B code 0x02 if (e.Text == "\u0002") { e.Text = null; removed = true; } WriteMessage("insert \"{0}\" code {1}{2}", text, (int)text[0], removed ? " removed" : ""); }
/// <summary> /// Triggered when some text (like line ending) is added to current document /// </summary> /// <param name="e"></param> protected override void OnInsertCheck(InsertCheckEventArgs e) { base.OnInsertCheck(e); if (AutoIndent) AutoIndentInsertCheck(e); }
/// <summary> /// Performs automatic indentation of line inserted based on current line indentation /// Increases indentation if the trimmed line ends with opening brace or colon /// </summary> /// <param name="e"></param> private void AutoIndentInsertCheck(InsertCheckEventArgs e) { var inserted = e.Text; if (inserted[0] != '\r' && inserted[0] != '\n') return; var line = Lines[CurrentLine].Text; var lineIndent = ""; var lineTrimmed = line.TrimEnd(); var lineTrimmedLength = lineTrimmed.Length; var lineEnd = lineTrimmedLength > 0 ? lineTrimmed[lineTrimmedLength - 1] : '*'; char c; for (int i = 0; i < line.Length; i++) { c = line[i]; if (c == ' ') lineIndent += c; else if (c == '\t') lineIndent += IndentationUnit; else break; } if (Lexer == Lexer.Cpp || CurrentLine > 0) { // switch case lineTrimmed = lineTrimmed.Trim().TrimEnd(';'); var unitLength = IndentationUnit.Length; if (lineTrimmed == "break" && lineIndent.Length > unitLength) { lineIndent = lineIndent.Substring(unitLength); } } e.Text += lineIndent; if (lineEnd == '{' || lineEnd == '[' || lineEnd == '(' || lineEnd == ':') e.Text += IndentationUnit; }
private void scintilla_InsertCheck(object sender, InsertCheckEventArgs e) { switch (e.Text) { // remove ctrl-A code 0x01 (SaveAs) case "\u0001": // remove ctrl-B code 0x02 (CompileCode) case "\u0002": // remove ctrl-C code 0x03 (AbortThreadExecution) case "\u0003": // remove ctrl-N code 0x03 (New) case "\u000E": // remove ctrl-O code 0x0F (Open) case "\u000F": // remove shift-ctrl-R code 0x12 (_RestartRunSource) case "\u0012": // remove ctrl-S code 0x12 (Save) case "\u0013": // remove ctrl-U code 0x14 (_UpdateRunSource) case "\u0014": e.Text = null; break; } }