Skip to content

Commit 739bdf0

Browse files
authored
feature(lsp): low level zipper api (#1078)
Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 915dcd5 commit 739bdf0

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

lsp/src/string_zipper.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ let of_string s =
3939
; line = 0
4040
}
4141

42+
let offset t = t.abs_pos + t.rel_pos
43+
4244
let length =
4345
let f acc sub = acc + Substring.length sub in
4446
fun { abs_pos = _; current; left; right; rel_pos = _; line = _ } ->

lsp/src/string_zipper.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ val drop_until : t -> t -> t
3030
val apply_change :
3131
t -> Types.Range.t -> [ `UTF16 | `UTF8 ] -> replacement:string -> t
3232

33+
val offset : t -> int
34+
3335
module Private : sig
3436
type zipper := t
3537

lsp/src/text_document.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,11 @@ let apply_text_document_edits t (edits : TextEdit.t list) =
104104
let text = apply_changes t.zipper t.position_encoding edits in
105105
let zipper = String_zipper.of_string text in
106106
{ t with text = Some text; zipper }
107+
108+
module Expert = struct
109+
let goto t pos =
110+
let zipper = String_zipper.goto_position t.zipper pos `UTF8 in
111+
{ t with zipper }
112+
113+
let offset t = String_zipper.offset t.zipper
114+
end

lsp/src/text_document.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ val set_version : t -> version:int -> t
3030
when multiple inserts are done in the same position. All the offsets are
3131
interpreted relative to the original document. *)
3232
val apply_text_document_edits : t -> TextEdit.t list -> t
33+
34+
module Expert : sig
35+
(** These functions allow one to work with the underlying zipper. This gives
36+
the opportunity for better performance on chained edits. *)
37+
38+
(** [goto t pos] move the zipper of [t] to [pos]. [pos] must be in utf8 *)
39+
val goto : t -> Position.t -> t
40+
41+
(** [offset t] return the global offset in the string where the zipper is
42+
currently focused *)
43+
val offset : t -> int
44+
end

0 commit comments

Comments
 (0)