@@ -22,6 +22,7 @@ Namespace Workspace
2222 Private _initialLoadSignaled As Integer
2323
2424 Private _workspace As MSBuildWorkspace
25+ Private _currentSolution As Solution
2526 Private _loadedSolutionPath As String
2627 Private ReadOnly _loadedProjectPaths As New List( Of String )()
2728
@@ -48,7 +49,7 @@ Namespace Workspace
4849 ''' </summary>
4950 Public ReadOnly Property CurrentSolution As Solution
5051 Get
51- Return If (_workspace?.CurrentSolution, Nothing )
52+ Return If (_currentSolution, _workspace?.CurrentSolution)
5253 End Get
5354 End Property
5455
@@ -57,7 +58,7 @@ Namespace Workspace
5758 ''' </summary>
5859 Public ReadOnly Property IsLoaded As Boolean
5960 Get
60- Dim solution = If (_workspace Is Nothing , Nothing , _workspace. CurrentSolution)
61+ Dim solution = CurrentSolution
6162 Return solution IsNot Nothing AndAlso solution.ProjectIds.Count > 0
6263 End Get
6364 End Property
@@ -105,6 +106,7 @@ Namespace Workspace
105106 End If
106107
107108 _workspace = MSBuildWorkspace.Create(properties)
109+ _currentSolution = _workspace.CurrentSolution
108110 AddHandler _workspace.WorkspaceFailed, AddressOf OnWorkspaceFailed
109111
110112 _logger.LogInformation( "MSBuildWorkspace created successfully" )
@@ -128,6 +130,7 @@ Namespace Workspace
128130 _logger.LogInformation( "Loading solution: {Path}" , solutionPath)
129131
130132 Dim solution = Await _workspace.OpenSolutionAsync(solutionPath, cancellationToken:=cancellationToken).ConfigureAwait( False )
133+ _currentSolution = solution
131134
132135 _loadedSolutionPath = solutionPath
133136 _loadedProjectPaths.Clear()
@@ -148,7 +151,7 @@ Namespace Workspace
148151 _logger.LogInformation( "Note: C# projects loaded but not served (VB.NET only in current phase)" )
149152 End If
150153
151- RaiseEvent SolutionChanged( Me , New SolutionChangedEventArgs(solution , changeKind))
154+ RaiseEvent SolutionChanged( Me , New SolutionChangedEventArgs(_currentSolution , changeKind))
152155
153156 Return vbProjects.Count > 0
154157 Catch ex As Exception
@@ -177,14 +180,15 @@ Namespace Workspace
177180 If IsProjectLoaded(projectPath) Then
178181 _logger.LogDebug( "Project already loaded, skipping: {Path}" , projectPath)
179182 If changeKind = SolutionChangeKind.Reloaded Then
180- RaiseEvent SolutionChanged( Me , New SolutionChangedEventArgs(_workspace. CurrentSolution, changeKind))
183+ RaiseEvent SolutionChanged( Me , New SolutionChangedEventArgs(CurrentSolution, changeKind))
181184 End If
182185 Return True
183186 End If
184187
185188 _logger.LogInformation( "Loading project: {Path}" , projectPath)
186189
187190 Dim project = Await _workspace.OpenProjectAsync(projectPath, cancellationToken:=cancellationToken).ConfigureAwait( False )
191+ _currentSolution = _workspace.CurrentSolution
188192
189193 If project.Language <> LanguageNames.VisualBasic Then
190194 _logger.LogWarning( "Project is not VB.NET: {Name} ({Language})" , project.Name, project.Language)
@@ -197,7 +201,7 @@ Namespace Workspace
197201
198202 _logger.LogInformation( "Project loaded: {Name} ({DocumentCount} documents)" , project.Name, project.DocumentIds.Count)
199203
200- RaiseEvent SolutionChanged( Me , New SolutionChangedEventArgs(_workspace. CurrentSolution, changeKind))
204+ RaiseEvent SolutionChanged( Me , New SolutionChangedEventArgs(CurrentSolution, changeKind))
201205
202206 Return True
203207 Catch ex As Exception
@@ -263,7 +267,7 @@ Namespace Workspace
263267 Return Nothing
264268 End If
265269
266- Dim solution = _workspace. CurrentSolution
270+ Dim solution = CurrentSolution
267271 Dim normalizedPath = Path.GetFullPath(filePath)
268272
269273 For Each projectId In solution.ProjectIds
@@ -292,7 +296,7 @@ Namespace Workspace
292296 Return Nothing
293297 End If
294298
295- Dim solution = _workspace. CurrentSolution
299+ Dim solution = CurrentSolution
296300 Dim normalizedPath = Path.GetFullPath(projectPath)
297301
298302 For Each projectId In solution.ProjectIds
@@ -314,7 +318,7 @@ Namespace Workspace
314318 Return Enumerable.Empty( Of Project)()
315319 End If
316320
317- Return _workspace. CurrentSolution.Projects.Where(Function (p) p.Language = LanguageNames.VisualBasic)
321+ Return CurrentSolution.Projects.Where( Function (p) p.Language = LanguageNames.VisualBasic)
318322 End Function
319323
320324 ''' <summary>
@@ -325,20 +329,15 @@ Namespace Workspace
325329 Return Nothing
326330 End If
327331
328- Dim solution = _workspace. CurrentSolution
332+ Dim solution = CurrentSolution
329333 Dim document = solution.GetDocument(documentId)
330334 If document Is Nothing Then
331335 Return Nothing
332336 End If
333337
334- Dim newSolution = solution.WithDocumentText(documentId, newText)
335-
336- If _workspace.TryApplyChanges(newSolution) Then
337- Return _workspace.CurrentSolution.GetDocument(documentId)
338- End If
339-
340- _logger.LogWarning( "Failed to apply text change to document: {Id}" , documentId)
341- Return Nothing
338+ ' Keep editor buffer changes in-memory. MSBuildWorkspace.TryApplyChanges can write back to disk.
339+ _currentSolution = solution.WithDocumentText(documentId, newText)
340+ Return _currentSolution.GetDocument(documentId)
342341 End Function
343342
344343 ''' <summary>
@@ -385,8 +384,8 @@ Namespace Workspace
385384 reloadedAny = reloadedAny OrElse reloaded
386385 Next
387386
388- If Not reloadedAny AndAlso _workspace?. CurrentSolution.ProjectIds.Count > 0 Then
389- RaiseEvent SolutionChanged( Me , New SolutionChangedEventArgs(_workspace. CurrentSolution, SolutionChangeKind.Reloaded))
387+ If Not reloadedAny AndAlso CurrentSolution? .ProjectIds.Count > 0 Then
388+ RaiseEvent SolutionChanged( Me , New SolutionChangedEventArgs(CurrentSolution, SolutionChangeKind.Reloaded))
390389 End If
391390
392391 Return reloadedAny
@@ -411,6 +410,7 @@ Namespace Workspace
411410 _workspace.Dispose()
412411 _workspace = Nothing
413412 End If
413+ _currentSolution = Nothing
414414 Return ValueTask.CompletedTask
415415 End Function
416416 End Class
0 commit comments