|
| 1 | +open System.IO |
| 2 | +open System.Collections.Generic |
| 3 | +open FSharp.Compiler |
| 4 | +open FSharp.Compiler.SourceCodeServices |
| 5 | + |
| 6 | +let getProjectOptions (folder: string) (projectFile: string) = |
| 7 | + let runProcess (workingDir: string) (exePath: string) (args: string) = |
| 8 | + let psi = System.Diagnostics.ProcessStartInfo() |
| 9 | + psi.FileName <- exePath |
| 10 | + psi.WorkingDirectory <- workingDir |
| 11 | + psi.RedirectStandardOutput <- false |
| 12 | + psi.RedirectStandardError <- false |
| 13 | + psi.Arguments <- args |
| 14 | + psi.CreateNoWindow <- true |
| 15 | + psi.UseShellExecute <- false |
| 16 | + |
| 17 | + use p = new System.Diagnostics.Process() |
| 18 | + p.StartInfo <- psi |
| 19 | + p.Start() |> ignore |
| 20 | + p.WaitForExit() |
| 21 | + |
| 22 | + let exitCode = p.ExitCode |
| 23 | + exitCode, () |
| 24 | + |
| 25 | + let runCmd exePath args = runProcess folder exePath (args |> String.concat " ") |
| 26 | + let msbuildExec = Dotnet.ProjInfo.Inspect.dotnetMsbuild runCmd |
| 27 | + let result = Dotnet.ProjInfo.Inspect.getProjectInfo ignore msbuildExec Dotnet.ProjInfo.Inspect.getFscArgs projectFile |
| 28 | + match result with |
| 29 | + | Ok (Dotnet.ProjInfo.Inspect.GetResult.FscArgs x) -> x |
| 30 | + | _ -> [] |
| 31 | + |
| 32 | +let mkStandardProjectReferences () = |
| 33 | + let projFile = "fcs-test.fsproj" |
| 34 | + let projDir = __SOURCE_DIRECTORY__ |
| 35 | + getProjectOptions projDir projFile |
| 36 | + |> List.filter (fun s -> s.StartsWith("-r:")) |
| 37 | + |> List.map (fun s -> s.Replace("-r:", "")) |
| 38 | + |
| 39 | +let mkProjectCommandLineArgsForScript (dllName, fileNames) = |
| 40 | + [| yield "--simpleresolution" |
| 41 | + yield "--noframework" |
| 42 | + yield "--debug:full" |
| 43 | + yield "--define:DEBUG" |
| 44 | + yield "--optimize-" |
| 45 | + yield "--out:" + dllName |
| 46 | + yield "--doc:test.xml" |
| 47 | + yield "--warn:3" |
| 48 | + yield "--fullpaths" |
| 49 | + yield "--flaterrors" |
| 50 | + yield "--target:library" |
| 51 | + for x in fileNames do |
| 52 | + yield x |
| 53 | + let references = mkStandardProjectReferences () |
| 54 | + for r in references do |
| 55 | + yield "-r:" + r |
| 56 | + |] |
| 57 | + |
| 58 | +let getProjectOptionsFromCommandLineArgs(projName, argv) = |
| 59 | + { ProjectFileName = projName |
| 60 | + ProjectId = None |
| 61 | + SourceFiles = [| |] |
| 62 | + OtherOptions = argv |
| 63 | + ReferencedProjects = [| |] |
| 64 | + IsIncompleteTypeCheckEnvironment = false |
| 65 | + UseScriptResolutionRules = false |
| 66 | + LoadTime = System.DateTime.MaxValue |
| 67 | + UnresolvedReferences = None |
| 68 | + OriginalLoadReferences = [] |
| 69 | + ExtraProjectInfo = None |
| 70 | + Stamp = None } |
| 71 | + |
| 72 | +let printAst title (projectResults: FSharpCheckProjectResults) = |
| 73 | + let implFiles = projectResults.AssemblyContents.ImplementationFiles |
| 74 | + let decls = implFiles |
| 75 | + |> Seq.collect (fun file -> AstPrint.printFSharpDecls "" file.Declarations) |
| 76 | + |> String.concat "\n" |
| 77 | + printfn "%s Typed AST:" title |
| 78 | + decls |> printfn "%s" |
| 79 | + |
| 80 | +[<EntryPoint>] |
| 81 | +let main argv = |
| 82 | + let projName = "Project.fsproj" |
| 83 | + let fileName = "test_script.fsx" |
| 84 | + let fileNames = [| fileName |] |
| 85 | + let source = File.ReadAllText (fileName, System.Text.Encoding.UTF8) |
| 86 | + let sources = [| source |] |
| 87 | + |
| 88 | + let dllName = Path.ChangeExtension(fileName, ".dll") |
| 89 | + let args = mkProjectCommandLineArgsForScript (dllName, fileNames) |
| 90 | + // for arg in args do printfn "%s" arg |
| 91 | + |
| 92 | + let projectOptions = getProjectOptionsFromCommandLineArgs (projName, args) |
| 93 | + let checker = InteractiveChecker.Create(projectOptions) |
| 94 | + |
| 95 | + // // parse and typecheck a project |
| 96 | + // let projectResults = checker.ParseAndCheckProject(projName, fileNames, sources) |
| 97 | + // projectResults.Errors |> Array.iter (fun e -> printfn "%A: %A" (e.Severity) e) |
| 98 | + // printAst "ParseAndCheckProject" projectResults |
| 99 | + |
| 100 | + // or just parse and typecheck a file in project |
| 101 | + let parseResults, tcResultsOpt, projectResults = |
| 102 | + checker.ParseAndCheckFileInProject(fileName, projName, fileNames, sources) |
| 103 | + projectResults.Errors |> Array.iter (fun e -> printfn "%A: %A" (e.Severity) e) |
| 104 | + |
| 105 | + match tcResultsOpt with |
| 106 | + | Some typeCheckResults -> |
| 107 | + printAst "ParseAndCheckFileInProject" projectResults |
| 108 | + |
| 109 | + let inputLines = source.Split('\n') |
| 110 | + |
| 111 | + // Get tool tip at the specified location |
| 112 | + let tip = typeCheckResults.GetToolTipText(4, 7, inputLines.[3], ["foo"], FSharpTokenTag.IDENT) |
| 113 | + (sprintf "%A" tip).Replace("\n","") |> printfn "\n---> ToolTip Text = %A" // should be "FSharpToolTipText [...]" |
| 114 | + |
| 115 | + // Get declarations (autocomplete) for msg |
| 116 | + let partialName = { QualifyingIdents = []; PartialIdent = "msg"; EndColumn = 17; LastDotPos = None } |
| 117 | + let decls = typeCheckResults.GetDeclarationListInfo(Some parseResults, 6, inputLines.[5], partialName, (fun _ -> [])) |
| 118 | + [ for item in decls.Items -> item.Name ] |> printfn "\n---> msg AutoComplete = %A" // should be string methods |
| 119 | + |
| 120 | + // Get declarations (autocomplete) for canvas |
| 121 | + let partialName = { QualifyingIdents = []; PartialIdent = "canvas"; EndColumn = 10; LastDotPos = None } |
| 122 | + let decls = typeCheckResults.GetDeclarationListInfo(Some parseResults, 8, inputLines.[7], partialName, (fun _ -> [])) |
| 123 | + [ for item in decls.Items -> item.Name ] |> printfn "\n---> canvas AutoComplete = %A" |
| 124 | + |
| 125 | + | _ -> () |
| 126 | + 0 |
0 commit comments