Skip to content

Commit 475d3ec

Browse files
committed
code lens refresh initial stab
1 parent 2b5f670 commit 475d3ec

3 files changed

Lines changed: 46 additions & 53 deletions

File tree

helix-lsp/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ pub enum MethodCall {
555555
RegisterCapability(lsp::RegistrationParams),
556556
UnregisterCapability(lsp::UnregistrationParams),
557557
ShowDocument(lsp::ShowDocumentParams),
558+
CodeLensRefresh,
558559
}
559560

560561
impl MethodCall {
@@ -586,6 +587,7 @@ impl MethodCall {
586587
let params: lsp::ShowDocumentParams = params.parse()?;
587588
Self::ShowDocument(params)
588589
}
590+
lsp::request::CodeLensRefresh::METHOD => Self::CodeLensRefresh,
589591
_ => {
590592
return Err(Error::Unhandled);
591593
}

helix-term/src/application.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,10 @@ impl Application {
10951095
let result = self.handle_show_document(params, offset_encoding);
10961096
Ok(json!(result))
10971097
}
1098+
Ok(MethodCall::CodeLensRefresh) => {
1099+
log::warn!("unhandled workspace/codeLens/refresh");
1100+
Ok(serde_json::Value::Null)
1101+
}
10981102
};
10991103

11001104
tokio::spawn(language_server!().reply(id, reply));

helix-term/src/commands/lsp.rs

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ fn compute_inlay_hints_for_view(
13431343
Some(callback)
13441344
}
13451345

1346-
fn map_code_lens(
1346+
pub(crate) fn map_code_lens(
13471347
doc_text: &Rope,
13481348
cl: &lsp::CodeLens,
13491349
offset_enc: OffsetEncoding,
@@ -1365,71 +1365,63 @@ fn map_code_lens(
13651365

13661366
pub fn code_lens_under_cursor(cx: &mut Context) {
13671367
let (view, doc) = current!(cx.editor);
1368-
let doc_text = doc.text();
13691368

13701369
let language_server =
13711370
language_server_with_feature!(cx.editor, doc, LanguageServerFeature::CodeLens);
13721371

13731372
let offset_encoding = language_server.offset_encoding();
13741373
let pos = doc.position(view.id, offset_encoding);
1375-
let path = match doc.path() {
1376-
Some(path) => path,
1377-
None => {
1378-
return;
1379-
}
1380-
};
13811374

1382-
if let Some(lenses) = cx.editor.code_lenses.get(path) {
1383-
let lenses: Vec<CodeLens> = lenses
1384-
.iter()
1385-
.filter(|cl| {
1386-
// TODO: fix the check
1387-
cl.range.start.line == pos.line
1388-
})
1389-
.map(|cl| map_code_lens(doc_text, cl, offset_encoding, language_server.id()))
1390-
.collect();
1375+
let current_line_lenses: Vec<CodeLens> = doc
1376+
.code_lens()
1377+
.iter()
1378+
.filter(|cl| {
1379+
// TODO: fix the check
1380+
cl.line == pos.line as usize
1381+
})
1382+
.cloned()
1383+
.collect();
1384+
1385+
if current_line_lenses.is_empty() {
1386+
cx.editor.set_status("No code lens available");
1387+
return;
1388+
}
13911389

1392-
if lenses.is_empty() {
1393-
cx.editor.set_status("No code lens available");
1390+
let mut picker = ui::Menu::new(current_line_lenses, (), move |editor, code_lens, event| {
1391+
if event != PromptEvent::Validate {
13941392
return;
13951393
}
13961394

1397-
let mut picker = ui::Menu::new(lenses, (), move |editor, code_lens, event| {
1398-
if event != PromptEvent::Validate {
1399-
return;
1400-
}
1401-
1402-
let code_lens = code_lens.unwrap();
1403-
let Some(language_server) = editor.language_server_by_id(code_lens.language_server_id)
1395+
let code_lens = code_lens.unwrap();
1396+
let Some(language_server) = editor.language_server_by_id(code_lens.language_server_id)
14041397
else {
14051398
editor.set_error("Language Server disappeared");
14061399
return;
14071400
};
14081401

1409-
let lens = code_lens.clone();
1410-
if let Some(cmd) = lens.command {
1411-
let future = match language_server.command(cmd) {
1412-
Some(future) => future,
1413-
None => {
1414-
editor.set_error("Language server does not support executing commands");
1415-
return;
1416-
}
1417-
};
1402+
let lens = code_lens.clone();
1403+
if let Some(cmd) = lens.command {
1404+
let future = match language_server.command(cmd) {
1405+
Some(future) => future,
1406+
None => {
1407+
editor.set_error("Language server does not support executing commands");
1408+
return;
1409+
}
1410+
};
14181411

1419-
tokio::spawn(async move {
1420-
let res = future.await;
1412+
tokio::spawn(async move {
1413+
let res = future.await;
14211414

1422-
if let Err(e) = res {
1423-
log::error!("execute LSP command: {}", e);
1424-
}
1425-
});
1426-
}
1427-
});
1428-
picker.move_down(); // pre-select the first item
1415+
if let Err(e) = res {
1416+
log::error!("execute LSP command: {}", e);
1417+
}
1418+
});
1419+
}
1420+
});
1421+
picker.move_down(); // pre-select the first item
14291422

1430-
let popup = Popup::new("code-lens", picker).with_scrollbar(false);
1431-
cx.push_layer(Box::new(popup));
1432-
};
1423+
let popup = Popup::new("code-lens", picker).with_scrollbar(false);
1424+
cx.push_layer(Box::new(popup));
14331425
}
14341426

14351427
// TODO: should be run the same way as diagnostic - shouldn't require manual
@@ -1455,17 +1447,12 @@ pub fn request_code_lenses(cx: &mut Context) {
14551447

14561448
cx.callback(
14571449
request,
1458-
move |editor, compositor, lenses: Option<Vec<lsp::CodeLens>>| {
1450+
move |editor, _compositor, lenses: Option<Vec<lsp::CodeLens>>| {
14591451
if let Some(lenses) = lenses {
14601452
log::error!("lenses got: {:?}", lenses);
14611453

14621454
let doc = doc_mut!(editor, &doc_id);
14631455
let doc_text = doc.text();
1464-
if let Some(current_url) = doc.path() {
1465-
editor
1466-
.code_lenses
1467-
.insert(current_url.into(), lenses.clone());
1468-
};
14691456

14701457
let lenses: Vec<CodeLens> = lenses
14711458
.iter()

0 commit comments

Comments
 (0)