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
//! Expand and Shrink Selection by the server's selection ranges, and highlighting the
//! other occurrences of the symbol under a resting caret.
use super::*;
impl App {
/// Expand Selection (#254): grow every cursor to the next
/// semantically meaningful range. Cached chain steps apply
/// instantly; otherwise the LSP `selectionRange` chains are fetched
/// (one request, all cursors), with tree-sitter node ancestry as the
/// serverless fallback — so the gesture works with no LSP at all.
pub(in crate::app) fn expand_selection(&mut self) {
if !self.editor_is_text() {
return;
}
self.editor.validate_expand_stacks();
if self.editor.expand_selection_from_stack() {
return;
}
let lang_known = self.editor_language().is_some();
if lang_known
&& self.lsp.is_some()
&& let Some(path) = self.editor.path.clone()
{
let seq = self.editor.edit_seq;
let positions = self.editor.cursor_positions_utf16();
// Reuse the pending request only when it was fired for these
// exact cursors; a caret move re-fires for the new spot.
if let Some(pending) = self.selection_range_request.as_mut()
&& pending.path == path
&& pending.seq == seq
&& pending.positions == positions
{
pending.presses += 1;
return;
}
if let Some(lsp) = self.lsp.as_mut() {
let id = lsp.request_selection_ranges(path.clone(), positions.clone());
self.selection_range_request = Some(PendingSelectionRanges {
id,
path,
seq,
presses: 1,
positions,
});
}
return;
}
if !self.editor.expand_selection_syntax() {
self.status = String::from("No larger range to select");
}
}
/// Shrink Selection: retrace the expand stack one step. Purely
/// local — the stack already remembers the exact ranges.
pub(in crate::app) fn shrink_selection(&mut self) {
if !self.editor_is_text() {
return;
}
self.editor.shrink_selection_step();
}
/// Drain `selectionRange` replies (#254): triple-gated like
/// occurrences (id + path + edit_seq), then either install the
/// server's chains or resolve the queued presses via tree-sitter
/// when the request came back unsupported/empty.
pub(in crate::app) fn drain_lsp_selection_ranges(&mut self) -> bool {
let Some(lsp) = self.lsp.as_ref() else {
return false;
};
let mut results = Vec::new();
while let Some(r) = lsp.drain_selection_ranges() {
results.push(r);
}
let mut changed = false;
for r in results {
let Some(pending) = self.selection_range_request.as_ref() else {
continue;
};
// Gate on id + path + edit_seq AND the cursors the request
// was fired for: a click between request and reply leaves
// `edit_seq` unchanged, but the chains describe the OLD
// caret and must not apply at the new one.
if r.request_id != pending.id
|| self.editor.path.as_deref() != Some(pending.path.as_path())
|| self.editor.edit_seq != pending.seq
|| self.editor.cursor_positions_utf16() != pending.positions
{
if r.request_id == pending.id {
self.selection_range_request = None;
}
continue;
}
let presses = pending.presses;
self.selection_range_request = None;
if r.unsupported || r.chains.is_empty() {
for _ in 0..presses {
if !self.editor.expand_selection_syntax() {
break;
}
}
changed = true;
continue;
}
self.editor.install_selection_chains(r.chains);
for _ in 0..presses {
if !self.editor.expand_selection_from_stack() {
break;
}
}
changed = true;
}
changed
}
/// Idle-caret occurrences trigger, run once per frame: when the caret has
/// rested on one buffer position for `OCCURRENCES_IDLE`, fire ONE
/// `documentHighlight` request for it; the moment it moves (or the buffer
/// edits), drop the painted set. Mirrors VS Code's word highlight, which
/// follows the cursor with a small debounce rather than every keystroke.
pub fn tick_occurrences(&mut self) -> bool {
const OCCURRENCES_IDLE: std::time::Duration = std::time::Duration::from_millis(250);
let eligible = self.editor.diff.is_none()
&& self.editor.sheet.is_none()
&& self.editor.image.is_none()
&& self.lsp.is_some();
let Some(path) = self.editor.path.clone().filter(|_| eligible) else {
self.occ_observed = None;
self.occurrences_request = None;
return self.editor.clear_occurrences();
};
let cur = CaretSpot {
path,
row: self.editor.cursor_row,
col: self.editor.cursor_col,
seq: self.editor.edit_seq,
};
if self.occ_observed.as_ref() != Some(&cur) {
self.occ_observed = Some(cur);
self.occ_observed_at = std::time::Instant::now();
// The old symbol's tints are stale the moment the caret leaves it
// (an edit already cleared them via mark_buffer_changed), and an
// in-flight reply for the old spot must never paint late.
self.occurrences_request = None;
return self.editor.clear_occurrences();
}
let already_requested = self
.occurrences_request
.as_ref()
.is_some_and(|request| request.at == cur);
if already_requested || self.occ_observed_at.elapsed() < OCCURRENCES_IDLE {
return false;
}
let (uline, uchar) = self.editor.pos_to_utf16(cur.row, cur.col);
let Some(lsp) = self.lsp.as_mut() else {
return false;
};
let id = lsp.request_document_highlights(cur.path.clone(), uline, uchar);
self.occurrences_request = Some(CaretRequest { id, at: cur });
false
}
/// Apply a documentHighlight reply to the editor, dropping it unless it
/// answers the in-flight request AND the editor still shows that file at
/// that edit seq (the columns were computed against that text).
pub fn drain_lsp_document_highlights(&mut self) -> bool {
let mut results = Vec::new();
{
let Some(lsp) = self.lsp.as_ref() else {
return false;
};
while let Some(r) = lsp.drain_document_highlights() {
results.push(r);
}
}
let mut changed = false;
for result in results {
let Some(CaretRequest {
id,
at: CaretSpot { path, seq, .. },
}) = self.occurrences_request.as_ref()
else {
continue;
};
if result.request_id != *id
|| self.editor.path.as_deref() != Some(path.as_path())
|| self.editor.edit_seq != *seq
{
continue;
}
self.editor.apply_occurrences(result.items);
changed = true;
}
changed
}
}