/// Where a name is used: every symbol in the module's own file, and the files /// it includes, that means the binding the name at a position means. /// /// Found as go to definition finds a name's binding (`Scope`), on the forms as /// the reader gives them. What is not a use is left: quoted data, the part of /// a `#'` template that is not unquoted, a type's declaration, and what the /// reader wrote itself, such as the `fun` of `#(...)`, which has no text of /// its own. Another module's uses are not looked for. module BjolangLsp.Uses open System.IO open Bjolang open Bjolang.Lexer open Bjolang.Ast type Use = { File: string Range: Range /// Whether this is where the name is bound or defined. IsDefinition: bool } /// A module's files: its own, and what it includes, each with its text and /// forms. `textOf` gives a file's text, the editor's where it has one. let moduleFiles (textOf: string -> string) (path: string) : (string * string * SExpr list) list = let rec go (seen: Set) (file: string) = let text = textOf file let fs = Symbols.forms file text |> Option.defaultValue [] let included = fs |> List.choose (fun form -> match form with | SList([ SAtom { Token = Symbol "include" }; SAtom { Token = StringLit rel } ], _) -> Some rel | _ -> Docs.fileForm form |> Option.map fst) |> List.map (fun rel -> Path.GetFullPath(Path.Combine(Path.GetDirectoryName file, rel))) |> List.filter (fun f -> File.Exists f && not (seen.Contains f)) (file, text, fs) :: (included |> List.collect (fun f -> go (Set.add f seen) f)) let full = Path.GetFullPath path go (Set.singleton full) full /// The symbols named `name` in `form` that can be uses: not quoted data, not a /// `#'` template's own names (an unquoted part of either is code), and nothing /// in a type's declaration. let rec symbolsNamed (name: string) (form: SExpr) : Range list = let rec quoted (items: SExpr list) = match items with | SAtom { Token = (Comma | CommaAt) } :: unquoted :: rest -> symbolsNamed name unquoted @ quoted rest | SList(inner, _) :: rest -> quoted inner @ quoted rest | _ :: rest -> quoted rest | [] -> [] match form with | SAtom { Token = Symbol n; Range = r } when n = name -> [ r ] | SList(SAtom { Token = Symbol("quoted-list" | "syntax-quote") } :: items, _) -> quoted items | SList(SAtom { Token = Symbol("type" | "type-rec") } :: _, _) -> [] | SList(items, _) -> items |> List.collect (symbolsNamed name) | _ -> [] /// Whether the source at `r` is `name`: the reader writes some symbols /// itself, at the range of the form that made them. let written (text: string) (name: string) (r: Range) = r.Start.Line = r.End.Line && Symbols.sliceOf text r = name /// The uses of the local binding `binder`, in the file of `forms`, the binding /// itself among them. let ofLocal (path: string) (text: string) (forms: SExpr list) (binder: Scope.Binder) : Use list = let uses = forms |> List.collect (symbolsNamed binder.Name) |> List.filter (fun r -> written text binder.Name r && (match Scope.find forms r.Start.Line r.Start.Column binder.Name with | Some b -> b.Range = binder.Range | None -> false)) |> List.map (fun r -> { File = path; Range = r; IsDefinition = r = binder.Range }) // A keyword parameter is bound by a keyword, `#:name`, not a symbol. if uses |> List.exists (fun u -> u.IsDefinition) then uses else { File = path; Range = binder.Range; IsDefinition = true } :: uses /// The uses of a name no local binding takes, in the module's files: one the /// module defines, imports, or has built in. Its definition is among them if /// the module has it. let ofTopLevel (textOf: string -> string) (path: string) (name: string) : Use list = let files = moduleFiles textOf path let definedAt = files |> List.collect (fun (file, t, _) -> Symbols.ofText file t |> Option.defaultValue [] |> Symbols.flatten) |> List.filter (fun s -> s.Name = name) |> List.map (fun s -> s.NameRange) |> Set.ofList let found = files |> List.collect (fun (file, t, fs) -> fs |> List.collect (symbolsNamed name) |> List.filter (fun r -> written t name r && (Scope.find fs r.Start.Line r.Start.Column name).IsNone) |> List.map (fun r -> { File = file; Range = r; IsDefinition = definedAt.Contains r })) // A type's or a constructor's name is not a use in its declaration, so // the definition is added when the search left it out. let missed = definedAt |> Seq.filter (fun r -> not (found |> List.exists (fun u -> u.Range = r))) |> Seq.map (fun r -> { File = r.File; Range = r; IsDefinition = true }) |> List.ofSeq missed @ found /// The name at `line` (1-based) and `column` (0-based) of `path`, and its /// uses in the module, in the order they are written. let at (textOf: string -> string) (path: string) (line: int) (column: int) : (string * Use list) option = let path = Path.GetFullPath path let text = textOf path match Symbols.nameAt path text line column, Symbols.forms path text with | Some(name, _), Some forms -> let uses = match Scope.find forms line column name with | Some binder -> ofLocal path text forms binder | None -> ofTopLevel textOf path name Some( name, uses |> List.sortBy (fun u -> u.File <> path, u.File, u.Range.Start.Line, u.Range.Start.Column) ) | _ -> None