-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphpinspect-edtrack.el
269 lines (216 loc) · 10.2 KB
/
phpinspect-edtrack.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
;;; phpinspect-edtrack.el --- PHP parsing and completion package -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2023 Free Software Foundation, Inc
;; Author: Hugo Thunnissen <[email protected]>
;; Keywords: php, languages, tools, convenience
;; Version: 2.1.0
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'phpinspect-util)
(require 'phpinspect-token-predicates)
(eval-when-compile
(require 'phpinspect-meta)
(phpinspect--declare-log-group 'edtrack))
(cl-defstruct (phpinspect-edtrack (:constructor phpinspect-make-edtrack))
(edits nil
:type list)
(taint-pool nil
:type list)
(last-edit nil
:type cons
:documentation "Last registered edit")
(last-edit-start -1
:type integer
:documentation "Last registered edit start position"))
(define-inline phpinspect-edit-local-delta (edit)
"The change in the end position of EDIT's region.
This is equal to [after-change-length] - [pre-change-length]."
(inline-quote (or (cdar ,edit) 0)))
(define-inline phpinspect-edit-original-end (edit)
"The original end position of EDIT."
(inline-quote (or (caar ,edit) 0)))
(define-inline phpinspect-edit-preceding (edit)
(inline-quote (cdr ,edit)))
(defsubst phpinspect-edit-delta (edit)
(let ((delta (phpinspect-edit-local-delta edit))
(previous-edit edit))
(while (setq previous-edit (cdr previous-edit))
(setq delta (+ delta (phpinspect-edit-local-delta previous-edit))))
delta))
(define-inline phpinspect-edit-end (edit)
"The current end position of EDIT.
This is equal to the original end position of EDIT + the delta's
of all known edits before EDIT's start."
(inline-letevals (edit)
(inline-quote
(+ (phpinspect-edit-original-end ,edit)
(phpinspect-edit-delta (phpinspect-edit-preceding ,edit))))))
(defsubst phpinspect-edtrack-original-position-at-point (track point)
"Calculate position of POINT if no edits had been made."
(let ((edit (phpinspect-edtrack-edits track))
;; `end' will be the end position of the last edit before POINT
end)
(let (last-end)
;; Find last edit before point
(while (and edit (<= point (setq last-end (phpinspect-edit-end edit))))
(setq end last-end
edit (cdr edit)))
;; If edit is non-nil, last-end will be the end of edit. If edit is nil,
;; there were no edits before POINT.
(setq end (if edit last-end 0)))
(if edit
;; Subtract cumulative edit delta
(let ((pos (- point (phpinspect-edit-delta edit))))
;; When point is within the edit delta's range, correct the delta by the
;; amount of encroachment.
(let ((encroached (- (+ end (phpinspect-edit-local-delta edit)) point)))
(if (< 0 encroached)
(+ pos encroached)
pos)))
;; else: no edits have taken place, return POINT
point)))
(defsubst phpinspect-edtrack-current-position-at-point (track point)
(let ((edit (phpinspect-edtrack-edits track))
(encroached)
(pos))
(while (and edit (<= point (phpinspect-edit-original-end edit)))
(setq edit (cdr edit)))
(setq pos (+ point (phpinspect-edit-delta edit)))
(if (< 0 (setq encroached (- (+ (phpinspect-edit-original-end edit) (or (cdar edit) 0)) point)))
(- pos encroached)
pos)))
(define-inline phpinspect-taint-start (taint)
(inline-quote (car ,taint)))
(define-inline phpinspect-taint-end (taint)
(inline-quote (cdr ,taint)))
(define-inline phpinspect-make-taint (start end)
(inline-quote (cons ,start ,end)))
(defsubst phpinspect-taint-overlaps-point (taint point)
(and (> (phpinspect-taint-end taint) point)
(<= (phpinspect-taint-start taint) point)))
(defsubst phpinspect-taint-overlaps-region (taint start end)
(or (phpinspect-taint-overlaps-point taint start)
(phpinspect-taint-overlaps-point taint end)
(and (> end (phpinspect-taint-start taint))
(<= start (phpinspect-taint-start taint)))
(and (> end (phpinspect-taint-end taint))
(<= start (phpinspect-taint-end taint)))))
(defsubst phpinspect-taint-overlaps (taint1 taint2)
(or (phpinspect-taint-overlaps-point taint1 (phpinspect-taint-start taint2))
(phpinspect-taint-overlaps-point taint1 (phpinspect-taint-end taint2))
(phpinspect-taint-overlaps-point taint2 (phpinspect-taint-start taint1))
(phpinspect-taint-overlaps-point taint2 (phpinspect-taint-end taint1))))
(defsubst phpinspect-taint-overlaps-meta (taint meta)
(or (phpinspect-taint-overlaps-point taint (phpinspect-meta-start meta))
(phpinspect-taint-overlaps-point taint (phpinspect-meta-end meta))
(phpinspect-meta-overlaps-point meta (phpinspect-taint-start taint))
(phpinspect-meta-overlaps-point meta (phpinspect-taint-end taint))))
(defsubst phpinspect-edtrack-register-taint (track start end)
(let ((pool (phpinspect-edtrack-taint-pool track))
(idx 0)
(overlap-start)
(overlap-end)
(left-neighbour)
(taint (phpinspect-make-taint start end)))
(catch 'break
(while pool
(if (phpinspect-taint-overlaps taint (car pool))
(progn
(when (< (phpinspect-taint-start (car pool)) start)
(setcar taint (phpinspect-taint-start (car pool))))
(when (> (phpinspect-taint-end (car pool)) end)
(setcdr taint (phpinspect-taint-end (car pool))))
(when (not overlap-start)
(setq overlap-start idx))
(setq overlap-end idx))
;; Else
(when overlap-start
(throw 'break nil))
(when (> start (phpinspect-taint-end (car pool)))
(setq left-neighbour pool)
(throw 'break nil)))
(setq pool (cdr pool)
idx (+ idx 1))))
(cond (overlap-start
(setq pool (phpinspect-edtrack-taint-pool track))
(setcar (nthcdr overlap-start pool) taint)
(setcdr (nthcdr overlap-start pool) (nthcdr (+ 1 overlap-end) pool)))
(left-neighbour
(setcdr left-neighbour (cons taint (cdr left-neighbour))))
(t
(push taint (phpinspect-edtrack-taint-pool track))))))
(defsubst phpinspect-edtrack-register-edit (track start end pre-change-length)
(phpinspect--log
"Edtrack registered change: [start: %d, end: %d, pre-change-length: %d]"
start end pre-change-length)
(let ((original-start (phpinspect-edtrack-original-position-at-point track start)))
(phpinspect-edtrack-register-taint
track original-start (+ original-start pre-change-length)))
(let ((edit-before (phpinspect-edtrack-edits track)))
(while (and edit-before (< end (phpinspect-edit-end edit-before)))
(setq edit-before (cdr edit-before)))
(let ((delta ;; The delta of this edit.
(- (- end start) pre-change-length))
new-edit)
(setq new-edit (cons
;; The end location of the edited region, before being
;; edited, with the delta of edits that happened at
;; preceding points in the buffer subtracted. This
;; corresponds with the original position of the region
;; end before the buffer was ever edited.
(phpinspect-edtrack-original-position-at-point
track (+ start pre-change-length))
delta))
(if edit-before
(progn
(setcdr edit-before (cons (car edit-before) (cdr edit-before)))
(setcar edit-before new-edit))
(if (phpinspect-edtrack-edits track)
(push new-edit (cdr (last (phpinspect-edtrack-edits track))))
(push new-edit (phpinspect-edtrack-edits track)))))))
(defsubst phpinspect-edtrack-clear-taint-pool (track)
(setf (phpinspect-edtrack-taint-pool track) nil))
(defsubst phpinspect-edtrack-clear (track)
(setf (phpinspect-edtrack-edits track) nil)
(setf (phpinspect-edtrack-last-edit track) nil)
(setf (phpinspect-edtrack-last-edit-start track) -1)
(phpinspect-edtrack-clear-taint-pool track))
(defsubst phpinspect-edtrack-make-taint-iterator (track)
(cons (car (phpinspect-edtrack-taint-pool track))
(cl-copy-list (cdr (phpinspect-edtrack-taint-pool track)))))
(define-inline phpinspect-taint-iterator-current (iter)
(inline-quote (car ,iter)))
(define-inline phpinspect-taint-iterator-follow (iter pos)
(inline-letevals (iter pos)
(inline-quote
(or (while (and (phpinspect-taint-iterator-current ,iter)
(> ,pos (phpinspect-taint-end
(phpinspect-taint-iterator-current ,iter))))
(setf (phpinspect-taint-iterator-current ,iter) (pop (cdr ,iter))))
(phpinspect-taint-iterator-current ,iter)))))
(define-inline phpinspect-taint-iterator-token-is-tainted-p (iter meta)
(inline-letevals (iter meta)
(inline-quote
(or (and (phpinspect-taint-iterator-follow ,iter (phpinspect-meta-start ,meta))
(phpinspect-taint-overlaps-meta
(phpinspect-taint-iterator-current ,iter) ,meta))
;; Incomplete tokens should be regarded as tainted regardless of
;; region, as their end-position is unlikely to be correct.
(phpinspect-incomplete-token-p (phpinspect-meta-token ,meta))))))
(define-inline phpinspect-taint-iterator-region-is-tainted-p (iter start end)
(inline-letevals (iter start end)
(inline-quote
(and (phpinspect-taint-iterator-follow ,iter ,start)
(phpinspect-taint-overlaps-region
(phpinspect-taint-iterator-current ,iter) ,start ,end)))))
(provide 'phpinspect-edtrack)
;;; phpinspect-edtrack.el ends here