Skip to content

Commit 24b393c

Browse files
authored
VS: initialize package in the background (#18646)
1 parent 041a528 commit 24b393c

File tree

6 files changed

+244
-274
lines changed

6 files changed

+244
-274
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### Fixed
2+
* Split package init into foreground+background, fix background analysis setting ([Issue #18623](https://github.com/dotnet/fsharp/issues/18623), [Issue #18904](https://github.com/dotnet/fsharp/issues/18904), [PR #18646](https://github.com/dotnet/fsharp/pull/18646))
3+
4+
### Added
5+
6+
### Changed
7+
8+
### Breaking Changes

vsintegration/src/FSharp.Editor/Common/Logging.fs renamed to vsintegration/src/FSharp.Editor/Common/DebugHelpers.fs

Lines changed: 48 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
namespace Microsoft.VisualStudio.FSharp.Editor.Logging
1+
namespace Microsoft.VisualStudio.FSharp.Editor.DebugHelpers
22

33
open System
44
open System.Diagnostics
5-
open System.ComponentModel.Composition
65
open Microsoft.VisualStudio.Shell
76
open Microsoft.VisualStudio.Shell.Interop
8-
open Microsoft.VisualStudio.FSharp.Editor
97

108
open FSharp.Compiler.Diagnostics
119

@@ -32,75 +30,57 @@ module Config =
3230
open Config
3331
open System.Diagnostics.Metrics
3432
open System.Text
33+
open Microsoft.VisualStudio.Threading
3534

36-
[<Export>]
37-
type Logger [<ImportingConstructor>] ([<Import(typeof<SVsServiceProvider>)>] serviceProvider: IServiceProvider) =
38-
let outputWindow =
39-
serviceProvider.GetService<SVsOutputWindow, IVsOutputWindow>() |> Option.ofObj
40-
41-
let createPane () =
42-
outputWindow
43-
|> Option.iter (fun x ->
44-
x.CreatePane(ref fsharpOutputGuid, "F# Language Service", Convert.ToInt32 true, Convert.ToInt32 false)
45-
|> ignore)
46-
47-
do createPane ()
48-
49-
let getPane () =
50-
match outputWindow |> Option.map (fun x -> x.GetPane(ref fsharpOutputGuid)) with
51-
| Some(0, pane) ->
52-
pane.Activate() |> ignore
53-
Some pane
54-
| _ -> None
55-
56-
static let mutable globalServiceProvider: IServiceProvider option = None
57-
58-
static member GlobalServiceProvider
59-
with get () =
60-
globalServiceProvider
61-
|> Option.defaultValue (ServiceProvider.GlobalProvider :> IServiceProvider)
62-
and set v = globalServiceProvider <- Some v
63-
64-
member _.FSharpLoggingPane =
65-
getPane ()
66-
|> function
67-
| Some pane -> Some pane
68-
| None ->
69-
createPane ()
70-
getPane ()
71-
72-
member self.Log(msgType: LogType, msg: string) =
73-
let time = DateTime.Now.ToString("hh:mm:ss tt")
74-
75-
match self.FSharpLoggingPane, msgType with
76-
| None, _ -> ()
77-
| Some pane, LogType.Message ->
78-
String.Format("[{0}{1}] {2}{3}", "", time, msg, Environment.NewLine)
79-
|> pane.OutputString
80-
|> ignore
81-
| Some pane, LogType.Info ->
82-
String.Format("[{0}{1}] {2}{3}", "INFO ", time, msg, Environment.NewLine)
83-
|> pane.OutputString
84-
|> ignore
85-
| Some pane, LogType.Warn ->
86-
String.Format("[{0}{1}] {2}{3}", "WARN ", time, msg, Environment.NewLine)
87-
|> pane.OutputString
88-
|> ignore
89-
| Some pane, LogType.Error ->
90-
String.Format("[{0}{1}] {2}{3}", "ERROR ", time, msg, Environment.NewLine)
91-
|> pane.OutputString
92-
|> ignore
93-
94-
[<AutoOpen>]
95-
module Logging =
35+
module FSharpOutputPane =
9636

97-
let inline debug msg = Printf.kprintf Debug.WriteLine msg
37+
let private pane =
38+
AsyncLazy(
39+
fun () ->
40+
task {
41+
do! ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync()
42+
let! window = AsyncServiceProvider.GlobalProvider.GetServiceAsync<SVsOutputWindow, IVsOutputWindow>()
43+
44+
window.CreatePane(ref fsharpOutputGuid, "F# Language Service", Convert.ToInt32 true, Convert.ToInt32 false)
45+
|> ignore
9846

99-
let private logger = lazy Logger(Logger.GlobalServiceProvider)
47+
match window.GetPane(ref fsharpOutputGuid) with
48+
| 0, pane -> return pane
49+
| _ -> return failwith "Could not get F# output pane"
50+
}
51+
, ThreadHelper.JoinableTaskFactory
52+
)
53+
54+
let inline debug msg = Printf.kprintf Debug.WriteLine msg
10055

10156
let private log logType msg =
102-
logger.Value.Log(logType, msg)
103-
System.Diagnostics.Trace.TraceInformation(msg)
57+
task {
58+
System.Diagnostics.Trace.TraceInformation(msg)
59+
let time = DateTime.Now.ToString("hh:mm:ss tt")
60+
61+
let! pane = pane.GetValueAsync()
62+
63+
do! ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync()
64+
65+
match logType with
66+
| LogType.Message ->
67+
String.Format("[{0}{1}] {2}{3}", "", time, msg, Environment.NewLine)
68+
|> pane.OutputStringThreadSafe
69+
|> ignore
70+
| LogType.Info ->
71+
String.Format("[{0}{1}] {2}{3}", "INFO ", time, msg, Environment.NewLine)
72+
|> pane.OutputStringThreadSafe
73+
|> ignore
74+
| LogType.Warn ->
75+
String.Format("[{0}{1}] {2}{3}", "WARN ", time, msg, Environment.NewLine)
76+
|> pane.OutputStringThreadSafe
77+
|> ignore
78+
| LogType.Error ->
79+
String.Format("[{0}{1}] {2}{3}", "ERROR ", time, msg, Environment.NewLine)
80+
|> pane.OutputStringThreadSafe
81+
|> ignore
82+
}
83+
|> ignore
10484

10585
let logMsg msg = log LogType.Message msg
10686
let logInfo msg = log LogType.Info msg
@@ -145,7 +125,7 @@ module FSharpServiceTelemetry =
145125
ActivitySamplingResult.AllData
146126
else
147127
ActivitySamplingResult.None),
148-
ActivityStarted = (fun a -> logMsg $"{indent a}{a.OperationName} {collectTags a}")
128+
ActivityStarted = (fun a -> FSharpOutputPane.logMsg $"{indent a}{a.OperationName} {collectTags a}")
149129
)
150130

151131
ActivitySource.AddActivityListener(listener)

vsintegration/src/FSharp.Editor/Common/RoslynHelpers.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open FSharp.Compiler.EditorServices
1414
open FSharp.Compiler.Text
1515
open FSharp.Compiler.Text
1616
open FSharp.Compiler.Text.Range
17-
open Microsoft.VisualStudio.FSharp.Editor.Logging
17+
open Microsoft.VisualStudio.FSharp.Editor.DebugHelpers
1818
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics
1919

2020
type RoslynTaggedText = Microsoft.CodeAnalysis.TaggedText
@@ -221,7 +221,7 @@ module internal RoslynHelpers =
221221
try
222222
return! computation
223223
with e ->
224-
logExceptionWithContext (e, context)
224+
FSharpOutputPane.logExceptionWithContext (e, context)
225225
return Unchecked.defaultof<_>
226226
}
227227

vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<Compile Include="Common\Constants.fs" />
3939
<Compile Include="Common\Extensions.fs" />
4040
<Compile Include="Common\Error.fs" />
41-
<Compile Include="Common\Logging.fs" />
41+
<Compile Include="Common\DebugHelpers.fs" />
4242
<Compile Include="Common\RoslynHelpers.fs" />
4343
<Compile Include="Common\FSharpCodeAnalysisExtensions.fs" />
4444
<Compile Include="Common\CodeAnalysisExtensions.fs" />

vsintegration/src/FSharp.Editor/LanguageService/FSharpAnalysisSaveFileCommandHandler.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open Microsoft.CodeAnalysis
88
open Microsoft.CodeAnalysis.Text
99
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics
1010
open Microsoft.VisualStudio.FSharp.Editor
11-
open Microsoft.VisualStudio.FSharp.Editor.Logging
11+
open Microsoft.VisualStudio.FSharp.Editor.DebugHelpers
1212
open Microsoft.VisualStudio.Text.Editor.Commanding.Commands
1313
open Microsoft.VisualStudio.Commanding
1414
open Microsoft.VisualStudio.Utilities
@@ -90,7 +90,7 @@ type internal FSharpAnalysisSaveFileCommandHandler [<ImportingConstructor>] (ana
9090
analyzerService.Reanalyze(workspace, documentIds = docIdsToReanalyze)
9191
with ex ->
9292
TelemetryReporter.ReportFault(TelemetryEvents.AnalysisSaveFileHandler, e = ex)
93-
logException ex
93+
FSharpOutputPane.logException ex
9494
}
9595
|> CancellableTask.startWithoutCancellation
9696
|> ignore // fire and forget

0 commit comments

Comments
 (0)