Skip to content

Commit

Permalink
Merge pull request #20063 from jasonmalinowski/log-exceptions-from-it…
Browse files Browse the repository at this point in the history
…extedit.apply

Log telemetry if we see ITextBufferEdit.Apply() throwing exceptions
  • Loading branch information
jasonmalinowski authored Jun 14, 2017
2 parents 2fba37a + 51e2ab2 commit b7f611d
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/EditorFeatures/Core/EditorFeatures.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
<Compile Include="ReferenceHighlighting\Tags\WrittenReferenceHighlightTag.cs" />
<Compile Include="ReferenceHighlighting\Tags\WrittenReferenceHighlightTagDefinition.cs" />
<Compile Include="Implementation\InfoBar\EditorInfoBarService.cs" />
<Compile Include="Shared\Extensions\ITextBufferEditExtensions.cs" />
<Compile Include="SymbolSearch\IAddReferenceDatabaseWrapper.cs" />
<Compile Include="SymbolSearch\IDatabaseFactoryService.cs" />
<Compile Include="SymbolSearch\IDelayService.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.BraceCompletion;
Expand Down Expand Up @@ -116,7 +117,7 @@ private void Start(CancellationToken cancellationToken)
}
else
{
snapshot = edit.Apply();
snapshot = edit.ApplyAndLogExceptions();
}
}

Expand Down Expand Up @@ -166,7 +167,7 @@ public void PreBackspace(out bool handledCommand)
// handle the command so the backspace does
// not go through since we've already cleared the braces
handledCommand = true;
edit.Apply();
edit.ApplyAndLogExceptions();
undo.Complete();
EndSession();
}
Expand Down Expand Up @@ -222,7 +223,7 @@ public void PreOverType(out bool handledCommand)
{
handledCommand = true;

edit.Apply();
edit.ApplyAndLogExceptions();

MoveCaretToClosingPoint();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected void ApplyReplacementText(ITextBuffer subjectBuffer, ITextUndoHistory
}
}

edit.Apply();
edit.ApplyAndLogExceptions();
if (!edit.HasEffectiveChanges && !this.UndoStack.Any())
{
transaction.Cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ internal void ApplyConflictResolutionEdits(IInlineRenameReplacementInfo conflict
edit.Replace(change.Span.Start, change.Span.Length, change.NewText);
}

edit.Apply();
edit.ApplyAndLogExceptions();
}
});

Expand Down Expand Up @@ -528,7 +528,7 @@ private IEnumerable<InlineRenameReplacement> GetMergedReplacementInfos(
buffer.Replace(change.Span.ToSpan(), change.NewText);
}

edit.Apply();
edit.ApplyAndLogExceptions();
}

yield return new InlineRenameReplacement(replacement.Kind, replacement.OriginalSpan, trackingSpan.GetSpan(buffer.CurrentSnapshot).Span.ToTextSpan());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void Commit(
using (var textEdit = this.SubjectBuffer.CreateEdit(editOptions, reiteratedVersionNumber: null, editTag: null))
{
textEdit.Replace(mappedSpan.Span, adjustedNewText);
textEdit.Apply();
textEdit.ApplyAndLogExceptions();
}

// If the completion change requested a new position for the caret to go,
Expand Down Expand Up @@ -204,7 +204,7 @@ private void RollbackToBeforeTypeChar(ITextSnapshot initialTextSnapshot)
textEdit.Replace(change.NewSpan, change.OldText);
}

textEdit.Apply();
textEdit.ApplyAndLogExceptions();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Text;

namespace Microsoft.CodeAnalysis.Editor.Shared.Extensions
{
internal static class ITextBufferEditExtensions
{
private static Exception s_lastException = null;

/// <summary>
/// Logs exceptions thrown during <see cref="ITextBufferEdit.Apply"/> as we look for issues.
/// </summary>
/// <param name="edit"></param>
/// <returns></returns>
public static ITextSnapshot ApplyAndLogExceptions(this ITextBufferEdit edit)
{
try
{
return edit.Apply();
}
catch (Exception e) when (ErrorReporting.FatalError.ReportWithoutCrash(e))
{
s_lastException = e;

// Since we don't know what is causing this yet, I don't feel safe that catching
// will not cause some further downstream failure. So we'll continue to propagate.
throw;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.VisualStudio.Text;
using Roslyn.Utilities;

Expand Down Expand Up @@ -113,7 +114,7 @@ public void ApplyReplacementText(string replacementText)
}
}

edit.Apply();
edit.ApplyAndLogExceptions();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration
Using edit = subjectBuffer.CreateEdit()
Dim aligningWhitespace = subjectBuffer.CurrentSnapshot.GetAligningWhitespace(state.TokenToLeft.Parent.Span.Start)
edit.Insert(state.CaretPosition, state.NewLineCharacter + aligningWhitespace)
edit.Apply()
edit.ApplyAndLogExceptions()
End Using

' And now just send down a normal enter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private SourceText UpdateBufferText()
using (var edit = _buffer.CreateEdit())
{
edit.Replace(child.GetSpan(), child.GetApplicableText());
edit.Apply();
edit.ApplyAndLogExceptions();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -88,7 +89,7 @@ private void ApplyDocumentToBuffer(TextDocument document, SpanChange spanSource,
using (var edit = TextView.TextBuffer.CreateEdit())
{
edit.Replace(new Span(0, TextView.TextBuffer.CurrentSnapshot.Length), documentText);
edit.Apply();
edit.ApplyAndLogExceptions();
}

container = TextView.TextBuffer.AsTextContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.Undo;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -238,8 +239,8 @@ private static void UpdateText(SourceText newText, ITextBuffer buffer, EditOptio
{
edit.Replace(change.Span.Start, change.Span.Length, change.NewText);
}

edit.Apply();
edit.ApplyAndLogExceptions();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ private static void ApplyChanges(
affectedSpans.Add(currentVisibleSpanIndex);
}

edit.Apply();
edit.ApplyAndLogExceptions();
}
}

Expand Down

0 comments on commit b7f611d

Please sign in to comment.