void HandleTextReplacing(object sender, Core.Text.TextChangeEventArgs e) { var handler = TextChanged; if (handler != null) { lock (replaceLock) { var oldText = CurrentText; var changes = new Microsoft.CodeAnalysis.Text.TextChange[e.TextChanges.Count]; var changeRanges = new TextChangeRange[e.TextChanges.Count]; for (int i = 0; i < e.TextChanges.Count; ++i) { var c = e.TextChanges[i]; var span = new TextSpan(c.Offset, c.RemovalLength); changes[i] = new Microsoft.CodeAnalysis.Text.TextChange(span, c.InsertedText.Text); changeRanges[i] = new TextChangeRange(span, c.InsertionLength); } var newText = oldText.WithChanges(changes); currentText = newText; try { handler(this, new Microsoft.CodeAnalysis.Text.TextChangeEventArgs(oldText, newText, changeRanges)); } catch (ArgumentException ae) { LoggingService.LogWarning(ae.Message + " re opening " + editor.FileName + " as roslyn source text."); workspace.InformDocumentClose(Id, editor.FileName); Dispose(); // 100% ensure that this object is disposed if (workspace.GetDocument(Id) != null) { TypeSystemService.InformDocumentOpen(Id, editor); } } catch (Exception ex) { LoggingService.LogError("Error while text replacing", ex); } } } }