Skip to content

Commit 433b191

Browse files
committed
Add project options to analyzer contexts
refs ionide/ionide-analyzers#147
1 parent 7bb647d commit 433b191

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

src/FSharp.Analyzers.Cli/Program.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ let runProject
173173
async {
174174
logger.LogInformation("Checking project {0}", fsharpOptions.ProjectFileName)
175175
let! checkProjectResults = fcs.ParseAndCheckProject(fsharpOptions)
176+
let analyzerOptions = BackgroundCompilerOptions fsharpOptions
176177

177178
let! messagesPerAnalyzer =
178179
fsharpOptions.SourceFiles
@@ -202,7 +203,7 @@ let runProject
202203
let! parseAndCheckResults = fcs.GetBackgroundCheckResultsForFileInProject(fileName, fsharpOptions)
203204

204205
let ctx =
205-
Utils.createContext checkProjectResults fileName sourceText parseAndCheckResults
206+
Utils.createContext checkProjectResults fileName sourceText parseAndCheckResults analyzerOptions
206207

207208
logger.LogInformation("Running analyzers for {0}", ctx.FileName)
208209
let! results = client.RunAnalyzers ctx

src/FSharp.Analyzers.SDK.Testing/FSharp.Analyzers.SDK.Testing.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ let getContextFor (opts: FSharpProjectOptions) isSignature source =
245245
fcs.NotifyFileChanged(fileName, opts) |> Async.RunSynchronously // workaround for https://github.com/dotnet/fsharp/issues/15960
246246
let checkProjectResults = fcs.ParseAndCheckProject(opts) |> Async.RunSynchronously
247247
let allSymbolUses = checkProjectResults.GetAllUsesOfAllSymbols()
248+
let analyzerOpts = BackgroundCompilerOptions opts
248249

249250
if Array.isEmpty allSymbolUses then
250251
failwith "no symboluses"
@@ -266,7 +267,7 @@ let getContextFor (opts: FSharpProjectOptions) isSignature source =
266267
raise (CompilerDiagnosticErrors diagErrors)
267268

268269
let sourceText = SourceText.ofString source
269-
Utils.createContext checkProjectResults fileName sourceText (parseFileResults, checkFileResults)
270+
Utils.createContext checkProjectResults fileName sourceText (parseFileResults, checkFileResults) analyzerOpts
270271
| Error e -> failwith $"typechecking file failed: %O{e}"
271272

272273
let getContext (opts: FSharpProjectOptions) source = getContextFor opts false source

src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.fs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ module EntityCache =
4949
with _ ->
5050
[]
5151

52+
[<AutoOpen>]
53+
module Extensions =
54+
open FSharp.Compiler.CodeAnalysis.ProjectSnapshot
55+
56+
type FSharpReferencedProjectSnapshot with
57+
58+
member x.ProjectFilePath =
59+
match x with
60+
| FSharpReferencedProjectSnapshot.FSharpReference(snapshot = snapshot) -> snapshot.ProjectFileName |> Some
61+
| _ -> None
62+
63+
type FSharpReferencedProject with
64+
65+
member x.ProjectFilePath =
66+
match x with
67+
| FSharpReferencedProject.FSharpReference(options = options) -> options.ProjectFileName |> Some
68+
| _ -> None
69+
5270
[<AbstractClass>]
5371
[<AttributeUsage(AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Field)>]
5472
type AnalyzerAttribute(name: string, shortDescription: string, helpUri: string) =
@@ -93,6 +111,45 @@ type EditorAnalyzerAttribute
93111

94112
type Context = interface end
95113

114+
type AnalyzerProjectOptions =
115+
| BackgroundCompilerOptions of FSharpProjectOptions
116+
| TransparentCompilerOptions of FSharpProjectSnapshot
117+
118+
member x.ProjectFileName =
119+
match x with
120+
| BackgroundCompilerOptions(options) -> options.ProjectFileName
121+
| TransparentCompilerOptions(snapshot) -> snapshot.ProjectFileName
122+
123+
member x.ProjectId =
124+
match x with
125+
| BackgroundCompilerOptions(options) -> options.ProjectId
126+
| TransparentCompilerOptions(snapshot) -> snapshot.ProjectId
127+
128+
member x.SourceFiles =
129+
match x with
130+
| BackgroundCompilerOptions(options) -> options.SourceFiles |> Array.toList
131+
| TransparentCompilerOptions(snapshot) -> snapshot.SourceFiles |> List.map (fun f -> f.FileName)
132+
|> List.map System.IO.Path.GetFullPath
133+
134+
member x.ReferencedProjectsPath =
135+
match x with
136+
| BackgroundCompilerOptions(options) ->
137+
options.ReferencedProjects
138+
|> Array.choose (fun p -> p.ProjectFilePath)
139+
|> Array.toList
140+
| TransparentCompilerOptions(snapshot) -> snapshot.ReferencedProjects |> List.choose (fun p -> p.ProjectFilePath)
141+
142+
member x.LoadTime =
143+
match x with
144+
| BackgroundCompilerOptions(options) -> options.LoadTime
145+
| TransparentCompilerOptions(snapshot) -> snapshot.LoadTime
146+
147+
member x.OtherOptions =
148+
match x with
149+
| BackgroundCompilerOptions(options) -> options.OtherOptions |> Array.toList
150+
| TransparentCompilerOptions(snapshot) -> snapshot.OtherOptions
151+
152+
96153
type CliContext =
97154
{
98155
FileName: string
@@ -101,6 +158,7 @@ type CliContext =
101158
CheckFileResults: FSharpCheckFileResults
102159
TypedTree: FSharpImplementationFileContents option
103160
CheckProjectResults: FSharpCheckProjectResults
161+
ProjectOptions: AnalyzerProjectOptions
104162
}
105163

106164
interface Context
@@ -122,6 +180,7 @@ type EditorContext =
122180
CheckFileResults: FSharpCheckFileResults option
123181
TypedTree: FSharpImplementationFileContents option
124182
CheckProjectResults: FSharpCheckProjectResults option
183+
ProjectOptions: AnalyzerProjectOptions
125184
}
126185

127186
interface Context
@@ -196,6 +255,7 @@ module Utils =
196255
(fileName: string)
197256
(sourceText: ISourceText)
198257
((parseFileResults: FSharpParseFileResults, checkFileResults: FSharpCheckFileResults))
258+
(projectOptions: AnalyzerProjectOptions)
199259
: CliContext
200260
=
201261
{
@@ -205,6 +265,7 @@ module Utils =
205265
CheckFileResults = checkFileResults
206266
TypedTree = checkFileResults.ImplementationFile
207267
CheckProjectResults = checkProjectResults
268+
ProjectOptions = projectOptions
208269
}
209270

210271
let createFCS documentSource =

src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.fsi

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ type EditorAnalyzerAttribute =
4747
/// Marker interface which both the CliContext and EditorContext implement
4848
type Context = interface end
4949

50+
/// Options related to the project being analyzed.
51+
type AnalyzerProjectOptions =
52+
| BackgroundCompilerOptions of FSharpProjectOptions
53+
| TransparentCompilerOptions of FSharpProjectSnapshot
54+
55+
/// The current project name.
56+
member ProjectFileName: string
57+
/// The identifier of the current project.
58+
member ProjectId: string option
59+
/// The set of source files in the current project.
60+
member SourceFiles: string list
61+
/// Projects referenced by this project.
62+
member ReferencedProjectsPath: string list
63+
/// The time at which the project was loaded.
64+
member LoadTime: DateTime
65+
/// Additional command line argument options for the project.
66+
member OtherOptions: string list
67+
5068
/// All the relevant compiler information for a given file.
5169
/// Contains the source text, untyped and typed tree information.
5270
type CliContext =
@@ -69,6 +87,8 @@ type CliContext =
6987
/// A handle to the results of the entire project
7088
/// See <a href="https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-codeanalysis-fsharpcheckprojectresults.html">FSharpCheckProjectResults Type</a>
7189
CheckProjectResults: FSharpCheckProjectResults
90+
/// Options related to the project being analyzed.
91+
ProjectOptions: AnalyzerProjectOptions
7292
}
7393

7494
interface Context
@@ -101,6 +121,8 @@ type EditorContext =
101121
/// A handle to the results of the entire project
102122
/// See <a href="https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-codeanalysis-fsharpcheckprojectresults.html">FSharpCheckProjectResults Type</a>
103123
CheckProjectResults: FSharpCheckProjectResults option
124+
// Options related to the project being analyzed.
125+
ProjectOptions: AnalyzerProjectOptions
104126
}
105127

106128
interface Context
@@ -184,4 +206,5 @@ module Utils =
184206
fileName: string ->
185207
sourceText: ISourceText ->
186208
parseFileResults: FSharpParseFileResults * checkFileResults: FSharpCheckFileResults ->
209+
projectOptions: AnalyzerProjectOptions ->
187210
CliContext

0 commit comments

Comments
 (0)