Skip to content

Commit

Permalink
Issue dotnet#17301
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave committed Sep 22, 2024
1 parent ef3b34d commit 6ac4191
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
81 changes: 60 additions & 21 deletions fcs/fcs-test/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ open FSharp.Compiler
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Symbols
// open FSharp.Compiler.Text
open Buildalyzer

let getProjectOptionsFromProjectFile (isMain: bool) (projFile: string) =
Expand Down Expand Up @@ -100,6 +102,7 @@ let mkProjectCommandLineArgsForScript (dllName, fileNames) =
yield "--fullpaths"
yield "--flaterrors"
yield "--target:library"
yield "--langversion:preview"
for x in fileNames do
yield x
let references = mkStandardProjectReferences ()
Expand Down Expand Up @@ -143,36 +146,72 @@ let main argv =
let checker = InteractiveChecker.Create(projectOptions)
let sourceReader _fileName = (hash source, lazy source)

// parse and typecheck a project
let projectResults =
checker.ParseAndCheckProject(projName, fileNames, sourceReader)
|> Async.RunSynchronously
projectResults.Diagnostics |> Array.iter (fun e -> printfn "%A: %A" (e.Severity) e)
printAst "ParseAndCheckProject" projectResults
// // parse and typecheck a project
// let projectResults =
// checker.ParseAndCheckProject(projName, fileNames, sourceReader)
// |> Async.RunSynchronously
// projectResults.Diagnostics |> Array.iter (fun e -> printfn "%A: %A" (e.Severity) e)
// printAst "ParseAndCheckProject" projectResults

// or just parse and typecheck a file in project
let (parseResults, typeCheckResults, projectResults) =
checker.ParseAndCheckFileInProject(projName, fileNames, sourceReader, fileName)
|> Async.RunSynchronously
projectResults.Diagnostics |> Array.iter (fun e -> printfn "%A: %A" (e.Severity) e)

printAst "ParseAndCheckFileInProject" projectResults
// printAst "ParseAndCheckFileInProject" projectResults

let inputLines = source.Split('\n')

// Get tool tip at the specified location
let tip = typeCheckResults.GetToolTip(4, 7, inputLines.[3], ["foo"], Tokenization.FSharpTokenTag.IDENT)
(sprintf "%A" tip).Replace("\n","") |> printfn "\n---> ToolTip Text = %A" // should print "FSharpToolTipText [...]"

// Get declarations (autocomplete) for msg
let partialName = { QualifyingIdents = []; PartialIdent = "msg"; EndColumn = 17; LastDotPos = None }
let decls = typeCheckResults.GetDeclarationListInfo(Some parseResults, 6, inputLines.[5], partialName, (fun _ -> []))
[ for item in decls.Items -> item.NameInList ] |> printfn "\n---> msg AutoComplete = %A" // should print string methods

// Get declarations (autocomplete) for canvas
let partialName = { QualifyingIdents = []; PartialIdent = "canvas"; EndColumn = 10; LastDotPos = None }
let decls = typeCheckResults.GetDeclarationListInfo(Some parseResults, 8, inputLines.[7], partialName, (fun _ -> []))
[ for item in decls.Items -> item.NameInList ] |> printfn "\n---> canvas AutoComplete = %A"
let isCustomSymbolUse =
typeCheckResults.GetSymbolUseAtLocation(8, 18, inputLines.[7], [ "IsCustom" ])
match isCustomSymbolUse with
| Some v ->
match v.Symbol with
| :? FSharpMemberOrFunctionOrValue as mfv ->
printfn $"""
--- Using GetSymbolUseAtLocation ---
{mfv.CompiledName}:
IsProperty = {mfv.IsProperty}
IsUnionCaseTester = {mfv.IsUnionCaseTester}
IsMethod = {mfv.IsMethod}
IsFunction = {mfv.IsFunction}
IsPropertyGetterMethod = {mfv.IsPropertyGetterMethod}
IsValue = {mfv.IsValue}"""
| _ -> printfn "Expected FSharpMemberOrFunctionOrValue"
| _ -> printfn "Expected Symbol"

for impl_file in projectResults.AssemblyContents.ImplementationFiles do
for file_decl in impl_file.Declarations do
match file_decl with
| FSharpImplementationFileDeclaration.Entity (ent, ent_decls) ->
for ent_decl in ent_decls do
match ent_decl with
| FSharpImplementationFileDeclaration.MemberOrFunctionOrValue (mfv, args, body) ->
if mfv.CompiledName.StartsWith("get_Is") then
printfn $"""
--- when enumerating declarations ---
{mfv.CompiledName}:
IsProperty = {mfv.IsProperty}
IsMethod = {mfv.IsMethod}
IsFunction = {mfv.IsFunction}
IsPropertyGetterMethod = {mfv.IsPropertyGetterMethod}
IsValue = {mfv.IsValue}"""
| _ -> ()
| _ -> ()

// // Get tool tip at the specified location
// let tip = typeCheckResults.GetToolTip(4, 7, inputLines.[3], ["foo"], Tokenization.FSharpTokenTag.IDENT)
// (sprintf "%A" tip).Replace("\n","") |> printfn "\n---> ToolTip Text = %A" // should print "FSharpToolTipText [...]"

// // Get declarations (autocomplete) for msg
// let partialName = { QualifyingIdents = []; PartialIdent = "msg"; EndColumn = 17; LastDotPos = None }
// let decls = typeCheckResults.GetDeclarationListInfo(Some parseResults, 6, inputLines.[5], partialName, (fun _ -> []))
// [ for item in decls.Items -> item.NameInList ] |> printfn "\n---> msg AutoComplete = %A" // should print string methods

// // Get declarations (autocomplete) for canvas
// let partialName = { QualifyingIdents = []; PartialIdent = "canvas"; EndColumn = 10; LastDotPos = None }
// let decls = typeCheckResults.GetDeclarationListInfo(Some parseResults, 8, inputLines.[7], partialName, (fun _ -> []))
// [ for item in decls.Items -> item.NameInList ] |> printfn "\n---> canvas AutoComplete = %A"

printfn "Done."
0
1 change: 1 addition & 0 deletions fcs/fcs-test/fcs-test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 8 additions & 7 deletions fcs/fcs-test/test_script.fsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
open System
open Fable.Import
module Lib

let foo() =
let msg = String.Concat("Hello"," ","world")
let len = msg.Length
let canvas = Browser.document.createElement_canvas ()
canvas.width <- 1000.
type HTMLAttr =
| Href of string
| Custom

let a = Custom
let b = a.IsCustom
printfn $"IsCustom = {b}"

0 comments on commit 6ac4191

Please sign in to comment.