14
14
using Microsoft . VisualStudio . Text ;
15
15
using Microsoft . VisualStudio . Text . Editor ;
16
16
using Microsoft . VisualStudio . TextManager . Interop ;
17
+ using Microsoft . VisualStudio . Threading ;
17
18
using Microsoft . VisualStudio . Utilities ;
18
19
using IServiceProvider = System . IServiceProvider ;
19
20
@@ -42,7 +43,8 @@ internal class RazorLSPTextViewConnectionListener : ITextViewConnectionListener
42
43
private readonly ILspEditorFeatureDetector _editorFeatureDetector ;
43
44
private readonly IEditorOptionsFactoryService _editorOptionsFactory ;
44
45
private readonly IClientSettingsManager _editorSettingsManager ;
45
- private readonly IVsTextManager4 _textManager ;
46
+ private readonly JoinableTaskContext _joinableTaskContext ;
47
+ private IVsTextManager4 ? _textManager ;
46
48
47
49
/// <summary>
48
50
/// Protects concurrent modifications to _activeTextViews and _textBuffer's
@@ -62,16 +64,27 @@ public RazorLSPTextViewConnectionListener(
62
64
IVsEditorAdaptersFactoryService editorAdaptersFactory ,
63
65
ILspEditorFeatureDetector editorFeatureDetector ,
64
66
IEditorOptionsFactoryService editorOptionsFactory ,
65
- IClientSettingsManager editorSettingsManager )
67
+ IClientSettingsManager editorSettingsManager ,
68
+ JoinableTaskContext joinableTaskContext )
66
69
{
67
70
_serviceProvider = serviceProvider ;
68
71
_editorAdaptersFactory = editorAdaptersFactory ;
69
72
_editorFeatureDetector = editorFeatureDetector ;
70
73
_editorOptionsFactory = editorOptionsFactory ;
71
74
_editorSettingsManager = editorSettingsManager ;
72
- _textManager = ( IVsTextManager4 ) serviceProvider . GetService ( typeof ( SVsTextManager ) ) ;
75
+ _joinableTaskContext = joinableTaskContext ;
76
+ }
73
77
74
- Assumes . Present ( _textManager ) ;
78
+ /// <summary>
79
+ /// Gets instance of <see cref="IVsTextManager4"/>. This accesses COM object and requires to be called on the UI thread.
80
+ /// </summary>
81
+ private IVsTextManager4 TextManager
82
+ {
83
+ get
84
+ {
85
+ _joinableTaskContext . AssertUIThread ( ) ;
86
+ return _textManager ??= ( IVsTextManager4 ) _serviceProvider . GetService ( typeof ( SVsTextManager ) ) ;
87
+ }
75
88
}
76
89
77
90
public void SubjectBuffersConnected ( ITextView textView , ConnectionReason reason , IReadOnlyCollection < ITextBuffer > subjectBuffers )
@@ -133,7 +146,8 @@ public void SubjectBuffersConnected(ITextView textView, ConnectionReason reason,
133
146
134
147
// Initialize TextView options. We only need to do this once per TextView, as the options should
135
148
// automatically update and they aren't options we care about keeping track of.
136
- InitializeRazorTextViewOptions ( _textManager , optionsTracker ) ;
149
+ Assumes . Present ( TextManager ) ;
150
+ InitializeRazorTextViewOptions ( TextManager , optionsTracker ) ;
137
151
138
152
// A change in Tools->Options settings only kicks off an options changed event in the view
139
153
// and not the buffer, i.e. even if we listened for TextBuffer option changes, we would never
@@ -201,7 +215,7 @@ private void RazorOptions_OptionChanged(object? sender, EditorOptionChangedEvent
201
215
202
216
// Retrieve current space/tabs settings from from Tools->Options and update options in
203
217
// the actual editor.
204
- ( ClientSpaceSettings ClientSpaceSettings , ClientCompletionSettings ClientCompletionSettings ) settings = UpdateRazorEditorOptions ( _textManager , optionsTracker ) ;
218
+ ( ClientSpaceSettings ClientSpaceSettings , ClientCompletionSettings ClientCompletionSettings ) settings = UpdateRazorEditorOptions ( TextManager , optionsTracker ) ;
205
219
206
220
// Keep track of accurate settings on the client side so we can easily retrieve the
207
221
// options later when the server sends us a workspace/configuration request.
0 commit comments