ocaml
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add Analysis.word_at

authored by

Kento Okura and committed by
Jon Sterling
9521a2ff 21b1302b

+70 -1
+24 -1
lib/language_server/Analysis.ml
··· 86 86 Some (path :: paths_in_bindings bindings, loc) 87 87 | Patch {self; _} 88 88 | Object {self; _;} -> 89 - (* Option.to_list self; *) 90 89 Option.map (fun path -> [path], loc) self 91 90 | Fun (bindings, _) -> Some (paths_in_bindings bindings, loc) 92 91 | Subtree _ ··· 174 173 let analyse_syntax nodes = 175 174 let@ () = S.run in 176 175 List.iter analyse nodes 176 + 177 + exception Found of string 178 + 179 + let word_at ~position (doc : Lsp.Text_document.t) = 180 + let L.Position.{line; character;} = position in 181 + let line = List.nth_opt (String.split_on_char '\n' (Lsp.Text_document.text doc)) line in 182 + match line with 183 + | None -> None 184 + | Some line -> 185 + let words = String.split_on_char ' ' line in 186 + Logs.debug (fun m -> m "line has %d words" (List.length words)); 187 + try 188 + let acc = ref 0 in 189 + List.iter 190 + (fun word -> 191 + Logs.debug (fun m -> m "%s" word); 192 + let length = String.length word in 193 + if !acc + length + 1 > character then raise (Found word) 194 + else acc := !acc + length + 1 195 + ) 196 + words; 197 + None 198 + with 199 + | Found str -> Some str
+3
lib/language_server/Analysis.mli
··· 38 38 val flatten : Code.t -> Code.t 39 39 40 40 val analyse_syntax : Code.t -> [ `Addr of string | `Path of Trie.path ] Asai.Range.located Seq.t 41 + 42 + 43 + val word_at : position:Lsp.Types.Position.t -> Lsp.Text_document.t -> string option
+43
lib/language_server/test/Test_lsp.ml
··· 311 311 let result = Option.get @@ Analysis.addr_at ~position code in 312 312 Alcotest.(check string) "" "tfmt-0005" (Asai.Range.(result.value)) 313 313 314 + let test_word_at () = 315 + let text = 316 + {|Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 317 + incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 318 + nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 319 + consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse 320 + cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat 321 + non proident, sunt in culpa qui officia deserunt mollit anim id est laborum 322 + |} 323 + in 324 + let doc 325 + = 326 + Lsp.Text_document.make 327 + ~position_encoding: `UTF8 328 + { 329 + textDocument = 330 + L.TextDocumentItem.create 331 + ~languageId: "forester" 332 + ~uri: (Lsp.Uri.of_string "") 333 + ~version: 1 334 + ~text 335 + } 336 + in 337 + let lorem = 338 + Option.get @@ 339 + let position = L.Position.create ~character: 0 ~line: 0 in 340 + Analysis.word_at ~position doc 341 + in 342 + let dolor = 343 + Option.get @@ 344 + let position = L.Position.create ~character: 13 ~line: 0 in 345 + Analysis.word_at ~position doc 346 + in 347 + let irure = 348 + Option.get @@ 349 + let position = L.Position.create ~character: 25 ~line: 3 in 350 + Analysis.word_at ~position doc 351 + in 352 + Alcotest.(check string) "" "Lorem" lorem; 353 + Alcotest.(check string) "" "dolor" dolor; 354 + Alcotest.(check string) "" "irure" irure 355 + 314 356 let () = 315 357 Random.self_init (); 316 358 Printexc.record_backtrace true; ··· 340 382 "contains", `Quick, test_contains; 341 383 "node_at", `Quick, test_node_at; 342 384 "addr_at", `Quick, test_addr_at; 385 + "word_at", `Quick, test_word_at; 343 386 ]; 344 387 "Handlers", 345 388 [