|
| 1 | +namespace FSharp.Compiler.Service.Tests |
| 2 | + |
| 3 | +open System |
| 4 | +open FSharp.Compiler.CodeAnalysis |
| 5 | +open FSharp.Compiler.EditorServices |
| 6 | +open FSharp.Compiler.Text |
| 7 | +open FSharp.Compiler.Tokenization |
| 8 | + |
| 9 | +type SourceContext = |
| 10 | + { Source: string |
| 11 | + LineText: string |
| 12 | + CaretPos: pos } |
| 13 | + |
| 14 | +type ResolveContext = |
| 15 | + { SourceContext: SourceContext |
| 16 | + Pos: pos |
| 17 | + Names: string list } |
| 18 | + |
| 19 | + member this.Source = this.SourceContext.Source |
| 20 | + member this.LineText = this.SourceContext.LineText |
| 21 | + |
| 22 | +type CodeCompletionContext = |
| 23 | + { SourceContext: SourceContext |
| 24 | + Pos: pos |
| 25 | + PartialIdentifier: PartialLongName } |
| 26 | + |
| 27 | + member this.Source = this.SourceContext.Source |
| 28 | + member this.LineText = this.SourceContext.LineText |
| 29 | + |
| 30 | +[<RequireQualifiedAccess>] |
| 31 | +module SourceContext = |
| 32 | + let fromMarkedSource (markedSource: string) : SourceContext = |
| 33 | + let lines = markedSource.Split([|"\r\n"; "\n"|], StringSplitOptions.None) |
| 34 | + let line = lines |> Seq.findIndex _.Contains("{caret}") |
| 35 | + let lineText = lines[line] |
| 36 | + let column = lineText.IndexOf("{caret}") |
| 37 | + |
| 38 | + let source = markedSource.Replace("{caret}", "") |
| 39 | + let pos = Position.mkPos (line + 1) (column - 1) |
| 40 | + let lineText = lineText.Replace("{caret}", "") |
| 41 | + { Source = source; CaretPos = pos; LineText = lineText } |
| 42 | + |
| 43 | + |
| 44 | +[<AutoOpen>] |
| 45 | +module CheckResultsExtensions = |
| 46 | + type FSharpCheckFileResults with |
| 47 | + member this.GetSymbolUses(context: ResolveContext) = |
| 48 | + this.GetSymbolUsesAtLocation(context.Pos.Line, context.Pos.Column, context.LineText, context.Names) |
| 49 | + |
| 50 | + member this.GetSymbolUse(context: ResolveContext) = |
| 51 | + this.GetSymbolUses(context) |> List.exactlyOne |
| 52 | + |
| 53 | + member this.GetTooltip(context: ResolveContext) = |
| 54 | + this.GetToolTip(context.Pos.Line, context.Pos.Column, context.LineText, context.Names, FSharpTokenTag.Identifier) |
| 55 | + |
| 56 | + member this.GetTooltip(context: ResolveContext, width) = |
| 57 | + this.GetToolTip(context.Pos.Line, context.Pos.Column, context.LineText, context.Names, FSharpTokenTag.Identifier, width) |
| 58 | + |
| 59 | + member this.GetCodeCompletionSuggestions(context: CodeCompletionContext, parseResults: FSharpParseFileResults) = |
| 60 | + this.GetDeclarationListInfo(Some parseResults, context.Pos.Line, context.LineText, context.PartialIdentifier) |
| 61 | + |
| 62 | +[<RequireQualifiedAccess>] |
| 63 | +module Checker = |
| 64 | + let getResolveContext (markedSource: string) = |
| 65 | + let context = SourceContext.fromMarkedSource markedSource |
| 66 | + let pos = |
| 67 | + match QuickParse.GetCompleteIdentifierIsland false context.LineText context.CaretPos.Column with |
| 68 | + | Some(_, column, _) -> Position.mkPos context.CaretPos.Line column |
| 69 | + | _ -> context.CaretPos |
| 70 | + |
| 71 | + let plid = QuickParse.GetPartialLongNameEx(context.LineText, pos.Column - 1) |
| 72 | + let names = plid.QualifyingIdents @ [plid.PartialIdent] |
| 73 | + { SourceContext = context; Pos = pos; Names = names } |
| 74 | + |
| 75 | + let getCompletionContext (markedSource: string) = |
| 76 | + let context = SourceContext.fromMarkedSource markedSource |
| 77 | + let plid = QuickParse.GetPartialLongNameEx(context.LineText, context.CaretPos.Column) |
| 78 | + let names = plid.QualifyingIdents @ [plid.PartialIdent] |
| 79 | + { SourceContext = context; Pos = context.CaretPos; PartialIdentifier = plid } |
| 80 | + |
| 81 | + let getCheckedResolveContext (markedSource: string) = |
| 82 | + let context = getResolveContext markedSource |
| 83 | + let _, checkResults = getParseAndCheckResults context.Source |
| 84 | + context, checkResults |
| 85 | + |
| 86 | + let getCompletionInfo (markedSource: string) = |
| 87 | + let context = getCompletionContext markedSource |
| 88 | + let parseResults, checkResults = getParseAndCheckResults context.Source |
| 89 | + checkResults.GetCodeCompletionSuggestions(context, parseResults) |
| 90 | + |
| 91 | + let getSymbolUses (markedSource: string) = |
| 92 | + let context, checkResults = getCheckedResolveContext markedSource |
| 93 | + checkResults.GetSymbolUses(context) |
| 94 | + |
| 95 | + let getSymbolUse (markedSource: string) = |
| 96 | + let symbolUses = getSymbolUses markedSource |
| 97 | + symbolUses |> List.exactlyOne |
| 98 | + |
| 99 | + let getTooltipWithOptions (options: string array) (markedSource: string) = |
| 100 | + let context = getResolveContext markedSource |
| 101 | + let _, checkResults = getParseAndCheckResultsWithOptions options context.Source |
| 102 | + checkResults.GetToolTip(context.Pos.Line, context.Pos.Column, context.LineText, context.Names, FSharpTokenTag.Identifier) |
| 103 | + |
| 104 | + let getTooltip (markedSource: string) = |
| 105 | + getTooltipWithOptions [||] markedSource |
0 commit comments