@@ -69,6 +69,7 @@ module LineCol : Point with type t = int * int = struct
6969      if  line1 =  line &&  line2 =  line then  col1 < =  col &&  col <  col2
7070      else  (line1 =  line &&  col1 < =  col) ||  (line2 =  line &&  col <  col2)
7171
72+   (*  Point is beyond [range] *) 
7273  let  gt_range  ?range   (line , col ) = 
7374    match  range with 
7475    |  None  -> false 
@@ -172,6 +173,18 @@ module Goals = struct
172173end 
173174
174175module  Completion  =  struct 
176+   let  obind  f  x  =  Option. bind x f
177+ 
178+   (*  let find ~doc ~point approx = *) 
179+   (*    let res = find ~doc ~point approx in *) 
180+   (*    match res with *) 
181+   (*    | Some res -> *) 
182+   (*      Io.Log.trace "info:find" ("found node at " ^ P.to_string point); *) 
183+   (*      Some res *) 
184+   (*    | None -> *) 
185+   (*      Io.Log.trace "info:find" ("failed at " ^ P.to_string point); *) 
186+   (*      None *) 
187+ 
175188  (*  XXX: This belongs in Coq *) 
176189  let  pr_extref  gr  = 
177190    match  gr with 
@@ -180,13 +193,57 @@ module Completion = struct
180193
181194  (*  XXX This may fail when passed "foo." for example, so more sanitizing is
182195     needed *)  
183-   let  to_qualid  p  =  try  Some  (Libnames. qualid_of_string p) with  _  ->  None 
196+   let  remove_dot_if_last  p  : string  = 
197+     let  l =  String. length p in 
198+     if  l >  1  then  if  p.[l -  1 ] =  '.'  then  String. sub p 0  (l -  1 ) else  p else  p
199+ 
200+   (*  let candidates ~token ~st prefix = *) 
201+   (*    let ( let* ) = Option.bind in *) 
202+   (*    Coq.State.in_state ~token ~st prefix ~f:(fun prefix -> *) 
203+   (*        let* p = to_qualid prefix in *) 
204+   (*        Nametab.completion_canditates p *) 
205+   (*        |> List.map (fun x -> Pp.string_of_ppcmds (pr_extref x)) *) 
206+   (*        |> some) *) 
207+   let  to_qualid  p  = 
208+     let  p =  remove_dot_if_last p in 
209+     try  Some  (Libnames. qualid_of_string p)
210+     with  _  -> 
211+       Io.Log. trace " completion"   (" broken qualid_of_string: "   ^  p);
212+       None 
213+ 
214+   let  completion  ~token   ~st   prefix  = 
215+     Coq.State. in_state ~token  ~st  prefix ~f: (fun  prefix  ->
216+         to_qualid prefix
217+         |>  obind (fun  p  ->
218+                Nametab. completion_canditates p
219+                |>  List. map (fun  x  -> Pp. string_of_ppcmds (pr_extref x))
220+                |>  List. append
221+                     (Notgram_ops. get_defined_notations ()  |>  List. map snd)
222+                |>  some))
223+ 
224+   let  _get_id_at_node_point  offset  range  text  =  Span. find ~offset  ~range  text
184225
226+   let  debug_completion  cat  msg  = 
227+     if  Debug. completion then  Io.Log. trace (" completion: "   ^  cat) msg
228+ 
229+   let  pr_completion_res  =  function 
230+     |  None  -> " no results" 
231+     |  Some  res  -> string_of_int (List. length res) ^  "  results" 
232+ 
233+   (*  This is still buggy for the case that find doesn't work (i.e. no ast) *) 
234+   (*  let candidates ~st ~point:_ () = *) 
185235  let  candidates  ~token   ~st   prefix  = 
186-     let  ( let *  ) =  Option. bind in 
187-     Coq.State. in_state ~token  ~st  prefix ~f: (fun  prefix  ->
188-         let *  p =  to_qualid prefix in 
189-         Nametab. completion_canditates p
190-         |>  List. map (fun  x  -> Pp. string_of_ppcmds (pr_extref x))
191-         |>  some)
236+     (*  we do exact matching for... *) 
237+     (*  let range = node.Doc.Node.range in *) 
238+     (*  let text = doc.Doc.contents.text in *) 
239+     (*  let span = Span.make ~contents:doc.Doc.contents ~loc in *) 
240+     (*  let offset = P.to_offset point text in *) 
241+     (*  debug_completion "offset" (string_of_int offset); *) 
242+     (*  let prefix = get_id_at_node_point offset range text in *) 
243+     (*  let prefix = "" in *) 
244+     debug_completion " prefix"   prefix;
245+     let  open  Coq.Protect.E.O  in 
246+     let +  res =  completion ~token  ~st  prefix in 
247+     debug_completion " n results"   (pr_completion_res res);
248+     res
192249end 
0 commit comments