~/bend-docscommunity

bolt/lsp/path.bend source

bolt/lsp/path.bend on the hub · documented module

# lsp/path: just enough path arithmetic to follow a relative import.import Base# a segment lands on the stack (reversed): `..` pops, `.` and `` do nothingdef push(+seg: String, +acc: List<&2, String>) -> List<&2, String>:  Bool.pick(List<&2, String>, String.eq(seg, ".."), List.tail(&2, String, acc),    Bool.pick(List<&2, String>, Bool.or(String.eq(seg, "."), String.is_empty(seg)), acc, seg <> acc))# every segment pushed, in orderdef norm(segs: List<&2, String>, acc: List<&2, String>) -> List<&2, String>:  match segs:    case Nil{}:      acc    case Con{s, t}:      norm(t, push(s, acc))# dir and rel as one absolute path, its `.` and `..` resolveddef join(dir: String, rel: String) -> String:  "/" ++ String.join(List.reverse(&2, String, norm(String.split(dir ++ "/" ++ rel, '/'), [])), "/")# the directory of a file's pathdef dir(path: String) -> String:  join(path, "..")