Skip to content

Commit

Permalink
conn: Fix incremental update when a new title-ish is added
Browse files Browse the repository at this point in the history
  • Loading branch information
artempyanykh committed Nov 16, 2024
1 parent 697bd1c commit b23d49d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
31 changes: 26 additions & 5 deletions Marksman/Conn.fs
Original file line number Diff line number Diff line change
Expand Up @@ -332,24 +332,45 @@ module Conn =
| Doc
| Header(1, _) ->
// Whenever a new title is added, links that were previously pointing at the Doc
// or the other titles need to be invalidate
// or the other titles need to be invalidated
let affectedDefs =
defs
|> MMap.tryFind scope
|> Option.defaultValue Set.empty
|> Seq.filter Def.isTitle
|> Seq.append [ Doc ]
|> Seq.map (fun x -> (scope, x))
|> Seq.append [ (scope, Doc) ]

// Similarly, other doc/titles could resolve to the same scope group
let scopeSlug = ScopeSlug.ofScopedDef (scope, def)

// TODO: we could do this faster if the groups were maintained in a set
let affectedDefsInOtherScopes =
match scopeSlug with
| None -> Seq.empty
| Some scopeSlug ->
defs
|> MMap.toSeq
|> Seq.choose (fun scopedDef ->
ScopeSlug.ofScopedDef scopedDef
|> Option.map (fun slug -> scopedDef, slug))
|> Seq.filter (fun (_, slug) -> slug = scopeSlug)
|> Seq.map fst

let affectedDefs =
affectedDefs |> Seq.append affectedDefsInOtherScopes |> Set.ofSeq

let affectedRefs =
Seq.fold
(fun acc def -> acc + Graph.edges (scope, Sym.Def def) resolved)
(fun acc (scope, def) ->
acc + Graph.edges (scope, Sym.Def def) resolved)
Set.empty
affectedDefs
|> Seq.choose ScopedSym.asRef

resolved <-
Seq.fold
(fun g def -> Graph.removeVertex (scope, Sym.Def def) g)
(fun g (scope, def) -> Graph.removeVertex (scope, Sym.Def def) g)
resolved
affectedDefs

Expand Down Expand Up @@ -416,7 +437,7 @@ module Conn =
logger.trace (
Log.setMessage "Finished updating conn"
>> Log.addContext "#touched" (Set.count lastTouched)
>> Log.addContext "elapsed_ms" (stopwatch.ElapsedMilliseconds)
>> Log.addContext "elapsed_ms" stopwatch.ElapsedMilliseconds
)

{ refs = refs
Expand Down
25 changes: 25 additions & 0 deletions Marksman/Syms.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Marksman.Syms

open Marksman.Misc
open Marksman.Names
open Marksman.Paths


[<StructuredFormatDisplay("{AsString}")>]
Expand Down Expand Up @@ -149,6 +150,12 @@ type Scope =

member this.CompactFormat = this.ToString()

module Scope =
let asDoc =
function
| Scope.Doc id -> Some id
| Scope.Global -> None

type ScopedSym = Scope * Sym

module ScopedSym =
Expand All @@ -157,6 +164,24 @@ module ScopedSym =
| scope, Sym.Ref ref -> Some(scope, ref)
| _ -> None

type ScopeSlug = ScopeSlug of Slug

module ScopeSlug =
let private ofDocId (docId: DocId) =
docId.Path |> RootedRelPath.filenameStem |> Slug.ofString |> ScopeSlug

let private ofScopedDefAux (docId: DocId) (def: Def) =
match def with
| Def.LinkDef _ -> None
| Def.Doc -> Some(ofDocId docId)
| Def.Header(1, id) -> Some(ScopeSlug(Slug.ofString id))
| Def.Header _ -> None

let ofScopedDef (scope: Scope, def: Def) =
match Scope.asDoc scope with
| Some docId -> ofScopedDefAux docId def
| None -> None

module Sym =
let asRef =
function
Expand Down

0 comments on commit b23d49d

Please sign in to comment.