Skip to content

Commit b7f611d

Browse files
Merge pull request #20063 from jasonmalinowski/log-exceptions-from-itextedit.apply
Log telemetry if we see ITextBufferEdit.Apply() throwing exceptions
2 parents 2fba37a + 51e2ab2 commit b7f611d

File tree

12 files changed

+55
-15
lines changed

12 files changed

+55
-15
lines changed

src/EditorFeatures/Core/EditorFeatures.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<Compile Include="ReferenceHighlighting\Tags\WrittenReferenceHighlightTag.cs" />
180180
<Compile Include="ReferenceHighlighting\Tags\WrittenReferenceHighlightTagDefinition.cs" />
181181
<Compile Include="Implementation\InfoBar\EditorInfoBarService.cs" />
182+
<Compile Include="Shared\Extensions\ITextBufferEditExtensions.cs" />
182183
<Compile Include="SymbolSearch\IAddReferenceDatabaseWrapper.cs" />
183184
<Compile Include="SymbolSearch\IDatabaseFactoryService.cs" />
184185
<Compile Include="SymbolSearch\IDelayService.cs" />

src/EditorFeatures/Core/Implementation/AutomaticCompletion/BraceCompletionSessionProvider.BraceCompletionSession.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System.Diagnostics;
44
using System.Threading;
5+
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
56
using Microsoft.CodeAnalysis.Text;
67
using Microsoft.VisualStudio.Text;
78
using Microsoft.VisualStudio.Text.BraceCompletion;
@@ -116,7 +117,7 @@ private void Start(CancellationToken cancellationToken)
116117
}
117118
else
118119
{
119-
snapshot = edit.Apply();
120+
snapshot = edit.ApplyAndLogExceptions();
120121
}
121122
}
122123

@@ -166,7 +167,7 @@ public void PreBackspace(out bool handledCommand)
166167
// handle the command so the backspace does
167168
// not go through since we've already cleared the braces
168169
handledCommand = true;
169-
edit.Apply();
170+
edit.ApplyAndLogExceptions();
170171
undo.Complete();
171172
EndSession();
172173
}
@@ -222,7 +223,7 @@ public void PreOverType(out bool handledCommand)
222223
{
223224
handledCommand = true;
224225

225-
edit.Apply();
226+
edit.ApplyAndLogExceptions();
226227

227228
MoveCaretToClosingPoint();
228229

src/EditorFeatures/Core/Implementation/InlineRename/AbstractInlineRenameUndoManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ protected void ApplyReplacementText(ITextBuffer subjectBuffer, ITextUndoHistory
133133
}
134134
}
135135

136-
edit.Apply();
136+
edit.ApplyAndLogExceptions();
137137
if (!edit.HasEffectiveChanges && !this.UndoStack.Any())
138138
{
139139
transaction.Cancel();

src/EditorFeatures/Core/Implementation/InlineRename/InlineRenameSession.OpenTextBufferManager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ internal void ApplyConflictResolutionEdits(IInlineRenameReplacementInfo conflict
346346
edit.Replace(change.Span.Start, change.Span.Length, change.NewText);
347347
}
348348

349-
edit.Apply();
349+
edit.ApplyAndLogExceptions();
350350
}
351351
});
352352

@@ -528,7 +528,7 @@ private IEnumerable<InlineRenameReplacement> GetMergedReplacementInfos(
528528
buffer.Replace(change.Span.ToSpan(), change.NewText);
529529
}
530530

531-
edit.Apply();
531+
edit.ApplyAndLogExceptions();
532532
}
533533

534534
yield return new InlineRenameReplacement(replacement.Kind, replacement.OriginalSpan, trackingSpan.GetSpan(buffer.CurrentSnapshot).Span.ToTextSpan());

src/EditorFeatures/Core/Implementation/IntelliSense/Completion/Controller_Commit.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private void Commit(
119119
using (var textEdit = this.SubjectBuffer.CreateEdit(editOptions, reiteratedVersionNumber: null, editTag: null))
120120
{
121121
textEdit.Replace(mappedSpan.Span, adjustedNewText);
122-
textEdit.Apply();
122+
textEdit.ApplyAndLogExceptions();
123123
}
124124

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

207-
textEdit.Apply();
207+
textEdit.ApplyAndLogExceptions();
208208
}
209209
}
210210
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.VisualStudio.Text;
7+
8+
namespace Microsoft.CodeAnalysis.Editor.Shared.Extensions
9+
{
10+
internal static class ITextBufferEditExtensions
11+
{
12+
private static Exception s_lastException = null;
13+
14+
/// <summary>
15+
/// Logs exceptions thrown during <see cref="ITextBufferEdit.Apply"/> as we look for issues.
16+
/// </summary>
17+
/// <param name="edit"></param>
18+
/// <returns></returns>
19+
public static ITextSnapshot ApplyAndLogExceptions(this ITextBufferEdit edit)
20+
{
21+
try
22+
{
23+
return edit.Apply();
24+
}
25+
catch (Exception e) when (ErrorReporting.FatalError.ReportWithoutCrash(e))
26+
{
27+
s_lastException = e;
28+
29+
// Since we don't know what is causing this yet, I don't feel safe that catching
30+
// will not cause some further downstream failure. So we'll continue to propagate.
31+
throw;
32+
}
33+
}
34+
}
35+
}

src/EditorFeatures/Core/Shared/Utilities/LinkedEditsTracker.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System.Collections.Generic;
44
using System.Linq;
5+
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
56
using Microsoft.VisualStudio.Text;
67
using Roslyn.Utilities;
78

@@ -113,7 +114,7 @@ public void ApplyReplacementText(string replacementText)
113114
}
114115
}
115116

116-
edit.Apply();
117+
edit.ApplyAndLogExceptions();
117118
}
118119
}
119120
}

src/EditorFeatures/VisualBasic/EndConstructGeneration/VisualBasicEndConstructGenerationService.vb

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration
301301
Using edit = subjectBuffer.CreateEdit()
302302
Dim aligningWhitespace = subjectBuffer.CurrentSnapshot.GetAligningWhitespace(state.TokenToLeft.Parent.Span.Start)
303303
edit.Insert(state.CaretPosition, state.NewLineCharacter + aligningWhitespace)
304-
edit.Apply()
304+
edit.ApplyAndLogExceptions()
305305
End Using
306306

307307
' And now just send down a normal enter

src/VisualStudio/Core/Def/Implementation/Preview/FileChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private SourceText UpdateBufferText()
187187
using (var edit = _buffer.CreateEdit())
188188
{
189189
edit.Replace(child.GetSpan(), child.GetApplicableText());
190-
edit.Apply();
190+
edit.ApplyAndLogExceptions();
191191
}
192192
}
193193

src/VisualStudio/Core/Def/Implementation/Preview/PreviewUpdater.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Microsoft.CodeAnalysis;
44
using Microsoft.CodeAnalysis.Editor;
5+
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
56
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
67
using Microsoft.CodeAnalysis.Shared.Extensions;
78
using Microsoft.CodeAnalysis.Text;
@@ -88,7 +89,7 @@ private void ApplyDocumentToBuffer(TextDocument document, SpanChange spanSource,
8889
using (var edit = TextView.TextBuffer.CreateEdit())
8990
{
9091
edit.Replace(new Span(0, TextView.TextBuffer.CurrentSnapshot.Length), documentText);
91-
edit.Apply();
92+
edit.ApplyAndLogExceptions();
9293
}
9394

9495
container = TextView.TextBuffer.AsTextContainer();

src/VisualStudio/Core/Def/Implementation/ProjectSystem/DocumentProvider.StandardTextDocument.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.IO;
77
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
89
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
910
using Microsoft.CodeAnalysis.Editor.Undo;
1011
using Microsoft.CodeAnalysis.Text;
@@ -238,8 +239,8 @@ private static void UpdateText(SourceText newText, ITextBuffer buffer, EditOptio
238239
{
239240
edit.Replace(change.Span.Start, change.Span.Length, change.NewText);
240241
}
241-
242-
edit.Apply();
242+
243+
edit.ApplyAndLogExceptions();
243244
}
244245
}
245246

src/VisualStudio/Core/Def/Implementation/Venus/ContainedDocument.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ private static void ApplyChanges(
775775
affectedSpans.Add(currentVisibleSpanIndex);
776776
}
777777

778-
edit.Apply();
778+
edit.ApplyAndLogExceptions();
779779
}
780780
}
781781

0 commit comments

Comments
 (0)