Debian Codegraph @

ide/rocqide/sentence.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 (** {1 Sentences in rocqide buffers } *)
12
13 (** Cut a part of the buffer in sentences and tag them.
14 Invariant: either this slice ends the buffer, or it ends with ".".
15 May raise [Rocq_lex.Unterminated] when the zone ends with
16 an unterminated sentence. *)
17
18 let split_slice_lax (buffer:GText.buffer) start stop =
19 buffer#remove_tag ~start ~stop Tags.Script.sentence;
20 buffer#remove_tag ~start ~stop Tags.Script.error;
21 buffer#remove_tag ~start ~stop Tags.Script.warning;
22 buffer#remove_tag ~start ~stop Tags.Script.error_bg;
23 let slice = buffer#get_text ~start ~stop () in
24 let apply_tag off tag =
25 (* off is now a utf8-compliant char offset, cf Rocq_lex.utf8_adjust *)
26 let iter = start#forward_chars off in
27 buffer#apply_tag ~start:iter ~stop:iter#forward_char tag
28 in
29 Rocq_lex.delimit_sentences apply_tag slice
30
31 (** Searching forward and backward a position fulfilling some condition *)
32
33 let rec forward_search cond (iter:GText.iter) =
34 if iter#is_end || cond iter then iter
35 else forward_search cond iter#forward_char
36
37 let rec backward_search cond (iter:GText.iter) =
38 if iter#is_start || cond iter then iter
39 else backward_search cond iter#backward_char
40
41 let is_sentence_end s =
42 s#has_tag Tags.Script.sentence
43
44 let is_char s c = s#char = Char.code c
45
46 (** Search backward the first character of a sentence, starting at [iter]
47 and going at most up to [soi] (meant to be the end of the locked zone).
48 Raise [StartError] when no proper sentence start has been found.
49 A character following a ending "." is considered as a sentence start
50 only if this character is a blank. In particular, when a final "."
51 at the end of the locked zone isn't followed by a blank, then this
52 non-blank character will be signaled as erroneous in [tag_on_insert] below.
53 *)
54
55 exception StartError
56
57 let grab_sentence_start (iter:GText.iter) soi =
58 let cond iter =
59 if iter#compare soi < 0 then raise StartError;
60 let prev = iter#backward_char in
61 is_sentence_end prev &&
62 (not (is_char prev '.') ||
63 List.exists (is_char iter) [' ';'\n';'\r';'\t'])
64 in
65 backward_search cond iter
66
67 (** Search forward the first character immediately after a sentence end *)
68
69 let grab_sentence_stop (start:GText.iter) =
70 (forward_search is_sentence_end start)#forward_char
71
72 (** Search forward the first character immediately after a "." sentence end
73 (and not just a "\{" or "\}" or comment end *)
74
75 let grab_ending_dot (start:GText.iter) =
76 let is_ending_dot s = is_sentence_end s && s#char = Char.code '.' in
77 (forward_search is_ending_dot start)#forward_char
78
79 (** Retag a zone that has been edited *)
80
81 let tag_on_insert buffer =
82 (* the start of the non-locked zone *)
83 let soi = buffer#get_iter_at_mark (`NAME "start_of_input") in
84 (* the inserted zone is between [prev_insert] and [insert] *)
85 let insert = buffer#get_iter_at_mark `INSERT in
86 let prev = buffer#get_iter_at_mark (`NAME "prev_insert") in
87 (* [prev] is normally always before [insert] even when deleting.
88 Let's check this nonetheless *)
89 let prev, insert =
90 if insert#compare prev < 0 then insert, prev else prev, insert
91 in
92 try
93 let start = grab_sentence_start prev soi in
94 (* The status of "{" "}" as sentence delimiters is too fragile.
95 We retag up to the next "." instead. *)
96 let stop = grab_ending_dot insert in
97 try split_slice_lax buffer start#backward_char stop
98 with Rocq_lex.Unterminated ->
99 (* This shouldn't happen frequently. Either:
100 - we are at eof, with indeed an unfinished sentence.
101 - we have just inserted an opening of comment or string.
102 - the inserted text ends with a "." that interacts with the "."
103 found by [grab_ending_dot] to form a non-ending "..".
104 In any case, we retag up to eof, since this isn't that costly. *)
105 if not stop#is_end then
106 let eoi = buffer#get_iter_at_mark (`NAME "stop_of_input") in
107 try split_slice_lax buffer start eoi
108 with Rocq_lex.Unterminated -> ()
109 with StartError ->
110 buffer#apply_tag ~start:soi ~stop:soi#forward_char Tags.Script.error
111
112 let tag_all buffer =
113 let soi = buffer#get_iter_at_mark (`NAME "start_of_input") in
114 let eoi = buffer#get_iter_at_mark (`NAME "stop_of_input") in
115 try split_slice_lax buffer soi eoi
116 with Rocq_lex.Unterminated -> ()
117
118 (** Search a sentence around some position *)
119
120 let find buffer (start:GText.iter) =
121 let soi = buffer#get_iter_at_mark (`NAME "start_of_input") in
122 try
123 let start = grab_sentence_start start soi in
124 let stop = grab_sentence_stop start in
125 (* Is this phrase non-empty and complete ? *)
126 if stop#compare start > 0 && is_sentence_end stop#backward_char
127 then Some (start,stop)
128 else None
129 with StartError -> None