Debian Codegraph @

ide/rocqide/wg_Completion.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 module StringOrd =
12 struct
13 type t = string
14 let compare s1 s2 =
15 (* we use first size, then usual comparison *)
16 let d = String.length s1 - String.length s2 in
17 if d <> 0 then d
18 else compare s1 s2
19 end
20
21 module Proposals = Set.Make(StringOrd)
22
23 (** Retrieve completion proposals in the buffer *)
24 let get_syntactic_completion (buffer : GText.buffer) pattern accu =
25 let rec get_aux accu (iter : GText.iter) =
26 match iter#forward_search pattern with
27 | None -> accu
28 | Some (start, stop) ->
29 if Gtk_parsing.starts_word start then
30 let ne = Gtk_parsing.find_word_end stop in
31 if ne#compare stop = 0 then get_aux accu stop
32 else
33 let proposal = buffer#get_text ~start ~stop:ne () in
34 let accu = Proposals.add proposal accu in
35 get_aux accu stop
36 else get_aux accu stop
37 in
38 get_aux accu buffer#start_iter
39
40 (** Retrieve completion proposals in Rocq libraries *)
41 let get_semantic_completion pattern accu =
42 let flags = [Interface.Name_Pattern ("^" ^ pattern), true] in
43 (* Only get the last part of the qualified name *)
44 let rec last accu = function
45 | [] -> accu
46 | [basename] -> Proposals.add basename accu
47 | _ :: l -> last accu l
48 in
49 let next = function
50 | Interface.Good l ->
51 let fold accu elt = last accu elt.Interface.coq_object_qualid in
52 let ans = List.fold_left fold accu l in
53 RocqDriver.return ans
54 | _ -> RocqDriver.return accu
55 in
56 RocqDriver.bind (RocqDriver.search flags) next
57
58 let is_substring s1 s2 =
59 let s1 = Glib.Utf8.to_unistring s1 in
60 let s2 = Glib.Utf8.to_unistring s2 in
61 let break = ref true in
62 let i = ref 0 in
63 let len1 = Array.length s1 in
64 let len2 = Array.length s2 in
65 while !break && !i < len1 && !i < len2 do
66 break := s1.(!i) = s2.(!i);
67 incr i;
68 done;
69 if !break then len2 - len1
70 else -1
71
72 class completion_provider buffer rocqtop =
73 let self_provider = ref None in
74 let active = ref true in
75 let provider = object (self)
76
77 val mutable auto_complete_length = 3
78 val mutable cache = (-1, "", Proposals.empty)
79 val mutable insert_offset = -1
80
81 method name = ""
82
83 method icon = None
84
85 method private update_proposals pref =
86 let (_, _, props) = cache in
87 let filter prop = 0 <= is_substring pref prop in
88 let props = Proposals.filter filter props in
89 props
90
91 method private add_proposals ctx props =
92 let mk text =
93 let item = GSourceView3.source_completion_item ~text ~label:text () in
94 (item :> GSourceView3.source_completion_proposal)
95 in
96 let props = List.map mk (Proposals.elements props) in
97 ctx#add_proposals (Option.get !self_provider) props true
98
99 method populate ctx =
100 let iter = buffer#get_iter_at_mark `INSERT in
101 let () = insert_offset <- iter#offset in
102 let () = Minilib.log (Printf.sprintf "Completion at offset: %i" insert_offset) in
103 let buffer = new GText.buffer iter#buffer in
104 if not (Gtk_parsing.ends_word iter#backward_char) then self#add_proposals ctx Proposals.empty else
105 let start = Gtk_parsing.find_word_start iter in
106 if iter#offset - start#offset < auto_complete_length then self#add_proposals ctx Proposals.empty else
107 let w = start#get_text ~stop:iter in
108 let () = Minilib.log ("Completion of prefix: '" ^ w ^ "'") in
109 let (off, prefix, props) = cache in
110 let start_offset = start#offset in
111 (* check whether we have the last request in cache *)
112 if (start_offset = off) && (0 <= is_substring prefix w) then
113 let props = self#update_proposals w in
114 self#add_proposals ctx props
115 else
116 let cancel = ref false in
117 let _ = ctx#connect#cancelled ~callback:(fun () -> cancel := true) in
118 let update props =
119 let () = cache <- (start_offset, w, props) in
120 if not !cancel then self#add_proposals ctx props
121 in
122 (* If not in the cache, we recompute it: first syntactic *)
123 let synt = get_syntactic_completion buffer w Proposals.empty in
124 (* Then semantic *)
125 let next props =
126 update props;
127 RocqDriver.return ()
128 in
129 let query = RocqDriver.bind (get_semantic_completion w synt) next in
130 (* If rocqtop is computing, do the syntactic completion altogether *)
131 let occupied () = update synt in
132 ignore @@ RocqDriver.try_grab rocqtop query occupied
133
134 method matched ctx = !active
135
136 method activation = [`INTERACTIVE; `USER_REQUESTED]
137
138 method info_widget proposal = None
139
140 method update_info proposal info = ()
141
142 method start_iter ctx proposal iter = false
143
144 method activate_proposal proposal iter = false
145
146 method interactive_delay = (-1)
147
148 method priority = 0
149
150 end in
151 let provider = GSourceView3.source_completion_provider provider in
152 object (self)
153
154 inherit GSourceView3.source_completion_provider provider#as_source_completion_provider
155
156 method active = !active
157
158 method set_active b = active := b
159
160 initializer
161 self_provider := Some (self :> GSourceView3.source_completion_provider)
162
163 end