Skip to content

Commit 314f869

Browse files
committed
feat: update code lens positions on change
1 parent 1c5feb4 commit 314f869

5 files changed

Lines changed: 45 additions & 12 deletions

File tree

helix-term/src/handlers.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::Arc;
22

33
use arc_swap::ArcSwap;
4+
use code_lenses::DocumentCodeLensesHandler;
45
use diagnostics::PullAllDocumentsDiagnosticHandler;
56
use helix_event::AsyncHook;
67

@@ -30,6 +31,7 @@ pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers {
3031
let signature_hints = SignatureHelpHandler::new().spawn();
3132
let auto_save = AutoSaveHandler::new().spawn();
3233
let document_colors = DocumentColorsHandler::default().spawn();
34+
let code_lenses = DocumentCodeLensesHandler::default().spawn();
3335
let word_index = word_index::Handler::spawn();
3436
let pull_diagnostics = PullDiagnosticsHandler::default().spawn();
3537
let pull_all_documents_diagnostics = PullAllDocumentsDiagnosticHandler::default().spawn();
@@ -39,6 +41,7 @@ pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers {
3941
signature_hints,
4042
auto_save,
4143
document_colors,
44+
code_lenses,
4245
word_index,
4346
pull_diagnostics,
4447
pull_all_documents_diagnostics,
@@ -52,6 +55,7 @@ pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers {
5255
snippet::register_hooks(&handlers);
5356
document_colors::register_hooks(&handlers);
5457
code_lenses::register_hooks(&handlers);
58+
code_lenses::register_hooks(&handlers);
5559
prompt::register_hooks(&handlers);
5660
handlers
5761
}

helix-term/src/handlers/code_lenses.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ pub fn request_document_code_lenses(editor: &mut Editor, doc_id: DocumentId) {
6363
let language_server = match language_server {
6464
Some(language_server) => language_server,
6565
None => {
66-
editor.set_error(format!(
67-
"No configured language server supports {}",
68-
LanguageServerFeature::CodeLens
69-
));
7066
return;
7167
}
7268
};
@@ -87,7 +83,7 @@ pub fn request_document_code_lenses(editor: &mut Editor, doc_id: DocumentId) {
8783
.collect(),
8884
Some(Ok(None)) => Vec::new(),
8985
Some(Err(err)) => {
90-
log::error!("document color request failed: {err}");
86+
log::error!("code lenses request failed: {err}");
9187
return;
9288
}
9389
None => return,
@@ -109,15 +105,32 @@ pub fn request_document_code_lenses(editor: &mut Editor, doc_id: DocumentId) {
109105
});
110106
}
111107

112-
pub(super) fn register_hooks(_handlers: &Handlers) {
108+
pub(super) fn register_hooks(handlers: &Handlers) {
113109
register_hook!(move |event: &mut DocumentDidOpen<'_>| {
114110
// when a document is initially opened, request code lenses for it
115111
request_document_code_lenses(event.editor, event.doc);
116112
Ok(())
117113
});
118114

119-
register_hook!(move |_event: &mut DocumentDidChange<'_>| {
120-
// TODO(matoous): use to update code lenses positions on document changes
115+
let tx = handlers.code_lenses.clone();
116+
register_hook!(move |event: &mut DocumentDidChange<'_>| {
117+
event.changes.update_positions(
118+
event
119+
.doc
120+
.code_lenses
121+
.iter_mut()
122+
.map(|lens| (&mut lens.char_idx, helix_core::Assoc::After)),
123+
);
124+
125+
// Avoid re-requesting code lenses if the change is a ghost transaction (completion)
126+
// because the language server will not know about the updates to the document and will
127+
// give out-of-date locations.
128+
if !event.ghost_transaction {
129+
// Cancel the ongoing request, if present.
130+
event.doc.code_lenses_controller.cancel();
131+
helix_event::send_blocking(&tx, CodeLensesEvent(event.doc.id()));
132+
}
133+
121134
Ok(())
122135
});
123136

helix-view/src/document.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ pub struct Document {
224224

225225
#[derive(Debug, Clone)]
226226
pub struct CodeLens {
227+
pub char_idx: usize,
227228
pub line: usize,
228229
pub lens: lsp::CodeLens,
229230
}
@@ -2184,11 +2185,26 @@ impl Document {
21842185
}
21852186

21862187
pub fn set_code_lenses(&mut self, lenses: Vec<lsp::CodeLens>) {
2188+
let text = self.text().clone();
2189+
let Some(language_server) = self
2190+
.language_servers_with_feature(LanguageServerFeature::CodeLens)
2191+
.next()
2192+
else {
2193+
return;
2194+
};
2195+
let offset_encoding = language_server.offset_encoding();
21872196
self.code_lenses = lenses
21882197
.into_iter()
2189-
.map(|l| CodeLens {
2190-
line: l.range.start.line as usize,
2191-
lens: l,
2198+
.filter_map(|l| {
2199+
if let Some(char_idx) = lsp_pos_to_pos(&text, l.range.start, offset_encoding) {
2200+
Some(CodeLens {
2201+
line: self.text.char_to_line(char_idx),
2202+
char_idx,
2203+
lens: l,
2204+
})
2205+
} else {
2206+
None
2207+
}
21922208
})
21932209
.collect();
21942210
}

helix-view/src/gutter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ pub fn lens<'doc>(
9595
) -> GutterFn<'doc> {
9696
let info = theme.get("info");
9797
let lenses = &doc.code_lenses;
98-
log::error!("lenses: {:?}", lenses);
9998

10099
Box::new(
101100
move |line: usize, _selected: bool, first_visual_line: bool, out: &mut String| {

helix-view/src/handlers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct Handlers {
2323
pub signature_hints: Sender<lsp::SignatureHelpEvent>,
2424
pub auto_save: Sender<AutoSaveEvent>,
2525
pub document_colors: Sender<lsp::DocumentColorsEvent>,
26+
pub code_lenses: Sender<lsp::CodeLensesEvent>,
2627
pub word_index: word_index::Handler,
2728
pub pull_diagnostics: Sender<lsp::PullDiagnosticsEvent>,
2829
pub pull_all_documents_diagnostics: Sender<lsp::PullAllDocumentsDiagnosticsEvent>,

0 commit comments

Comments
 (0)