Skip to content

Commit

Permalink
fixes test discovery and analyzers for adaptive
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAngryByrd committed Feb 8, 2023
1 parent e0b635d commit c533543
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 62 deletions.
52 changes: 27 additions & 25 deletions src/FsAutoComplete.Core/Commands.fs
Expand Up @@ -896,30 +896,6 @@ module Commands =
highlights |> Array.collect expandParents)
|> Array.sortBy startToken

type Commands(checker: FSharpCompilerServiceChecker, state: State, hasAnalyzers: bool, rootPath: string option) =
let fileParsed = Event<FSharpParseFileResults>()

let fileChecked = Event<ParseAndCheckResults * string<LocalPath> * int>()

let scriptFileProjectOptions = Event<FSharpProjectOptions>()

let disposables = ResizeArray()

let mutable workspaceRoot: string option = rootPath
let mutable linterConfigFileRelativePath: string option = None
// let mutable linterConfiguration: FSharpLint.Application.Lint.ConfigurationParam = FSharpLint.Application.Lint.ConfigurationParam.Default
let mutable lastCheckResult: ParseAndCheckResults option = None

let notify = Event<NotificationEvent>()

let fileStateSet = Event<unit>()
let commandsLogger = LogProvider.getLoggerByName "Commands"

let checkerLogger = LogProvider.getLoggerByName "CheckerEvents"

let fantomasLogger = LogProvider.getLoggerByName "Fantomas"




let analyzerHandler (file: string<LocalPath>, content, pt, tast, symbols, getAllEnts) =
Expand Down Expand Up @@ -967,6 +943,32 @@ type Commands(checker: FSharpCompilerServiceChecker, state: State, hasAnalyzers:

[||]


type Commands(checker: FSharpCompilerServiceChecker, state: State, hasAnalyzers: bool, rootPath: string option) =
let fileParsed = Event<FSharpParseFileResults>()

let fileChecked = Event<ParseAndCheckResults * string<LocalPath> * int>()

let scriptFileProjectOptions = Event<FSharpProjectOptions>()

let disposables = ResizeArray()

let mutable workspaceRoot: string option = rootPath
let mutable linterConfigFileRelativePath: string option = None
// let mutable linterConfiguration: FSharpLint.Application.Lint.ConfigurationParam = FSharpLint.Application.Lint.ConfigurationParam.Default
let mutable lastCheckResult: ParseAndCheckResults option = None

let notify = Event<NotificationEvent>()

let fileStateSet = Event<unit>()
let commandsLogger = LogProvider.getLoggerByName "Commands"

let checkerLogger = LogProvider.getLoggerByName "CheckerEvents"

let fantomasLogger = LogProvider.getLoggerByName "Fantomas"



do
disposables.Add
<| state.ProjectController.Notifications.Subscribe(NotificationEvent.Workspace >> notify.Trigger)
Expand Down Expand Up @@ -1034,7 +1036,7 @@ type Commands(checker: FSharpCompilerServiceChecker, state: State, hasAnalyzers:
| true, fileData ->

let res =
analyzerHandler (
Commands.analyzerHandler (
file,
fileData.Lines.ToString().Split("\n"),
parseAndCheck.GetParseResults.ParseTree,
Expand Down
181 changes: 159 additions & 22 deletions src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs
Expand Up @@ -183,23 +183,64 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar

let config = cval<FSharpConfig> FSharpConfig.Default

let checker =
config
|> AVal.map (fun c -> c.EnableAnalyzers) // Maps will cache values and we don't want to recreate FSharpCompilerServiceChecker unless only EnableAnalyzers changed
|> AVal.map (FSharpCompilerServiceChecker)
let analyzersEnabled = config |> AVal.map (fun c -> c.EnableAnalyzers)

let checker = analyzersEnabled |> AVal.map (FSharpCompilerServiceChecker)

let rootPath = cval<string option> None

let mutableConfigChanges =
let toCompilerToolArgument (path: string) = sprintf "--compilertool:%s" path

aval {
let! config = config
and! checker = checker
and! rootPath = rootPath

checker.SetFSIAdditionalArguments
[| yield! config.FSICompilerToolLocations |> Array.map toCompilerToolArgument
yield! config.FSIExtraParameters |]

return ()
if config.EnableAnalyzers then
Loggers.analyzers.info (
Log.setMessage "Using analyzer roots of {roots}"
>> Log.addContextDestructured "roots" config.AnalyzersPath
)

config.AnalyzersPath
|> Array.iter (fun analyzerPath ->
match rootPath with
| None -> ()
| Some workspacePath ->
let dir =
if
System.IO.Path.IsPathRooted analyzerPath
// if analyzer is using absolute path, use it as is
then
analyzerPath
// otherwise, it is a relative path and should be combined with the workspace path
else
System.IO.Path.Combine(workspacePath, analyzerPath)

Loggers.analyzers.info (
Log.setMessage "Loading analyzers from {dir}"
>> Log.addContextDestructured "dir" dir
)

let (n, m) = dir |> FSharp.Analyzers.SDK.Client.loadAnalyzers

Loggers.analyzers.info (
Log.setMessage "From {name}: {dllNo} dlls including {analyzersNo} analyzers"
>> Log.addContextDestructured "name" analyzerPath
>> Log.addContextDestructured "dllNo" n
>> Log.addContextDestructured "analyzersNo" m
))

return ()
// otherwise, it is a relative path and should be combined with the workspace path
else
Loggers.analyzers.info (Log.setMessage "Analyzers disabled")
return ()
}

let updateConfig c =
Expand Down Expand Up @@ -233,6 +274,90 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar

let scriptFileProjectOptions = Event<FSharpProjectOptions>()

let fileParsed =
Event<FSharpParseFileResults * FSharpProjectOptions * CancellationToken>()

let fileChecked = Event<ParseAndCheckResults * VolatileFile * CancellationToken>()


do
disposables.Add
<| fileParsed.Publish.Subscribe(fun (parseResults, proj, ct) ->
logger.info (
Log.setMessage "Test Detection of {file} started"
>> Log.addContextDestructured "file" parseResults.FileName
)

let fn = UMX.tag parseResults.FileName

let res =
if proj.OtherOptions |> Seq.exists (fun o -> o.Contains "Expecto.dll") then
TestAdapter.getExpectoTests parseResults.ParseTree
elif proj.OtherOptions |> Seq.exists (fun o -> o.Contains "nunit.framework.dll") then
TestAdapter.getNUnitTest parseResults.ParseTree
elif proj.OtherOptions |> Seq.exists (fun o -> o.Contains "xunit.assert.dll") then
TestAdapter.getXUnitTest parseResults.ParseTree
else
[]

logger.info (
Log.setMessage "Test Detection of {file} - {res}"
>> Log.addContextDestructured "file" parseResults.FileName
>> Log.addContextDestructured "res" res
)

notifications.Trigger(NotificationEvent.TestDetected(fn, res |> List.toArray), ct))

do
disposables.Add
<| fileChecked.Publish.Subscribe(fun (parseAndCheck, volatileFile, ct) ->
async {
if analyzersEnabled |> AVal.force then
let file = volatileFile.FileName

try
Loggers.analyzers.info (
Log.setMessage "begin analysis of {file}"
>> Log.addContextDestructured "file" file
)

match parseAndCheck.GetCheckResults.ImplementationFile with
| Some tast ->

let res =
Commands.analyzerHandler (
file,
volatileFile.Lines.ToString().Split("\n"),
parseAndCheck.GetParseResults.ParseTree,
tast,
parseAndCheck.GetCheckResults.PartialAssemblySignature.Entities |> Seq.toList,
parseAndCheck.GetAllEntities
)

notifications.Trigger(NotificationEvent.AnalyzerMessage(res, file), ct)

Loggers.analyzers.info (
Log.setMessage "end analysis of {file}"
>> Log.addContextDestructured "file" file
)

| _ ->
Loggers.analyzers.info (
Log.setMessage "missing components of {file} to run analyzers, skipped them"
>> Log.addContextDestructured "file" file
)

()
with ex ->
Loggers.analyzers.error (
Log.setMessage "Run failed for {file}"
>> Log.addContextDestructured "file" file
>> Log.addExn ex
)
}
|> Async.StartWithCT ct)


let handleCommandEvents (n: NotificationEvent, ct: CancellationToken) =
try
async {
Expand Down Expand Up @@ -457,7 +582,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar

let loader = cval<Ionide.ProjInfo.IWorkspaceLoader> workspaceLoader

let rootPath = cval<string option> None


let binlogConfig =
Expand Down Expand Up @@ -583,7 +707,7 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
// Set some default values as FCS uses these for identification/caching purposes
let fso =
{ fso with
SourceFiles = fso.SourceFiles |> Array.map(Utils.normalizePath >> UMX.untag)
SourceFiles = fso.SourceFiles |> Array.map (Utils.normalizePath >> UMX.untag)
Stamp = fso.Stamp |> Option.orElse (Some DateTime.UtcNow.Ticks)
ProjectId = fso.ProjectId |> Option.orElse (Some(Guid.NewGuid().ToString())) }

Expand Down Expand Up @@ -875,7 +999,7 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
match! sourceFileToProjectOptions |> AMap.tryFind filePath with
| None ->
// openFilesToChangesAndProjectOptions contains script files that we may need to look through
match! openFilesToChangesAndProjectOptions |> AMap.tryFindA filePath with
match! openFilesToChangesAndProjectOptions |> AMap.tryFindA filePath with
| None -> return []
| Some (_, projs) -> return projs
| Some projs -> return projs
Expand Down Expand Up @@ -1013,6 +1137,9 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar

Async.Start(
async {

fileParsed.Trigger(parseAndCheck.GetParseResults, opts, ct)
fileChecked.Trigger(parseAndCheck, file, ct)
let checkErrors = parseAndCheck.GetParseResults.Diagnostics
let parseErrors = parseAndCheck.GetCheckResults.Diagnostics

Expand Down Expand Up @@ -1068,13 +1195,14 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
let! opts = List.tryHead projectOptions
and! cts = tryGetOpenFileToken file

return!
Debug.measure "parseFile"
<| fun () ->
let opts = Utils.projectOptionsToParseOptions opts
let parseOpts = Utils.projectOptionsToParseOptions opts

checker.ParseFile(file, info.Lines, opts)
|> Async.RunSynchronouslyWithCTSafe(fun () -> cts.Token)
let! result =
checker.ParseFile(file, info.Lines, parseOpts)
|> Async.RunSynchronouslyWithCTSafe(fun () -> cts.Token)

fileParsed.Trigger(result, opts, cts.Token)
return result
}

})
Expand Down Expand Up @@ -1919,6 +2047,10 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
do! bypassAdaptiveAndCheckDepenenciesForFile filePath
do! lspClient.CodeLensRefresh()

logger.info (
Log.setMessage "TextDocumentDidSave Request Finished: {parms}"
>> Log.addContextDestructured "parms" p
)

return ()
with e ->
Expand Down Expand Up @@ -2221,16 +2353,21 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
and! tyRes = forceGetTypeCheckResultsStale filePath |> Result.ofStringErr

match tyRes.TryGetToolTipEnhanced pos lineStr with
| Ok (Some (tip, signature, footer, typeDoc)) ->
| Ok (Some (tip, signature, footer, typeDoc) as x) ->
logger.info (
Log.setMessage "TryGetToolTipEnhanced : {parms}"
>> Log.addContextDestructured "parms" x
)

let formatCommentStyle =
let config = AVal.force config

if config.TooltipMode = "full" then
TipFormatter.FormatCommentStyle.FullEnhanced
else if config.TooltipMode = "summary" then
TipFormatter.FormatCommentStyle.SummaryOnly
else
TipFormatter.FormatCommentStyle.Legacy
TipFormatter.FormatCommentStyle.FullEnhanced
// if config.TooltipMode = "full" then
// TipFormatter.FormatCommentStyle.FullEnhanced
// else if config.TooltipMode = "summary" then
// TipFormatter.FormatCommentStyle.SummaryOnly
// else
// TipFormatter.FormatCommentStyle.Legacy

match TipFormatter.formatTipEnhanced tip signature footer typeDoc formatCommentStyle with
| (sigCommentFooter :: _) :: _ ->
Expand Down
3 changes: 2 additions & 1 deletion test/FsAutoComplete.Tests.Lsp/CompletionTests.fs
Expand Up @@ -27,7 +27,8 @@ let tests state =
}
|> Async.Cache

testList
testSequenced
<| testList
"Completion Tests"
[ testCaseAsync
"simple module member completion on dot"
Expand Down
6 changes: 3 additions & 3 deletions test/FsAutoComplete.Tests.Lsp/CoreTests.fs
Expand Up @@ -227,7 +227,7 @@ let tooltipTests state =
let expectedDescription = concatLines expectedDescription

testCaseAsync
(sprintf "description for line %d character %d should be '%s" line character expectedDescription)
(sprintf "description for line %d character %d" line character)
(async {
let! server, scriptPath = server

Expand All @@ -242,8 +242,8 @@ let tooltipTests state =
| Result.Error errors -> failtestf "Error while getting description: %A" errors
})


testList
testSequenced
<| testList
"tooltip evaluation"
[ testList
"tests"
Expand Down

0 comments on commit c533543

Please sign in to comment.