Skip to content

Commit

Permalink
code lens refresh initial stab
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Nov 26, 2023
1 parent 4d7203d commit aafe875
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
2 changes: 2 additions & 0 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ pub enum MethodCall {
WorkspaceConfiguration(lsp::ConfigurationParams),
RegisterCapability(lsp::RegistrationParams),
UnregisterCapability(lsp::UnregistrationParams),
CodeLensRefresh,
}

impl MethodCall {
Expand Down Expand Up @@ -577,6 +578,7 @@ impl MethodCall {
let params: lsp::UnregistrationParams = params.parse()?;
Self::UnregisterCapability(params)
}
lsp::request::CodeLensRefresh::METHOD => Self::CodeLensRefresh,
_ => {
return Err(Error::Unhandled);
}
Expand Down
4 changes: 4 additions & 0 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,10 @@ impl Application {
}
Ok(serde_json::Value::Null)
}
Ok(MethodCall::CodeLensRefresh) => {
log::warn!("unhandled workspace/codeLens/refresh");
Ok(serde_json::Value::Null)
}
};

tokio::spawn(language_server!().reply(id, reply));
Expand Down
85 changes: 40 additions & 45 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ fn compute_inlay_hints_for_view(
Some(callback)
}

fn map_code_lens(
pub(crate) fn map_code_lens(
doc_text: &Rope,
cl: &lsp::CodeLens,
offset_enc: OffsetEncoding,
Expand All @@ -1688,7 +1688,6 @@ fn map_code_lens(

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

let language_server =
language_server_with_feature!(cx.editor, doc, LanguageServerFeature::CodeLens);
Expand All @@ -1701,57 +1700,56 @@ pub fn code_lens_under_cursor(cx: &mut Context) {
return;
};

if let Some(lenses) = cx.editor.code_lenses.get(&url.unwrap()) {
let lenses: Vec<CodeLens> = lenses
.iter()
.filter(|cl| {
// TODO: fix the check
cl.range.start.line == pos.line
})
.map(|cl| map_code_lens(doc_text, cl, offset_encoding, language_server.id()))
.collect();
let current_line_lenses: Vec<CodeLens> = doc
.code_lens()
.iter()
.filter(|cl| {
// TODO: fix the check
cl.line == pos.line as usize
})
.cloned()
.collect();

if current_line_lenses.is_empty() {
cx.editor.set_status("No code lens available");
return;
}

if lenses.is_empty() {
cx.editor.set_status("No code lens available");
let mut picker = ui::Menu::new(current_line_lenses, (), move |editor, code_lens, event| {
if event != PromptEvent::Validate {
return;
}

let mut picker = ui::Menu::new(lenses, (), move |editor, code_lens, event| {
if event != PromptEvent::Validate {
return;
}

let code_lens = code_lens.unwrap();
let Some(language_server) = editor.language_server_by_id(code_lens.language_server_id)
let code_lens = code_lens.unwrap();
let Some(language_server) = editor.language_server_by_id(code_lens.language_server_id)
else {
editor.set_error("Language Server disappeared");
return;
};

let lens = code_lens.clone();
if let Some(cmd) = lens.command {
let future = match language_server.command(cmd) {
Some(future) => future,
None => {
editor.set_error("Language server does not support executing commands");
return;
}
};
let lens = code_lens.clone();
if let Some(cmd) = lens.command {
let future = match language_server.command(cmd) {
Some(future) => future,
None => {
editor.set_error("Language server does not support executing commands");
return;
}
};

tokio::spawn(async move {
let res = future.await;
tokio::spawn(async move {
let res = future.await;

if let Err(e) = res {
log::error!("execute LSP command: {}", e);
}
});
}
});
picker.move_down(); // pre-select the first item
if let Err(e) = res {
log::error!("execute LSP command: {}", e);
}
});
}
});
picker.move_down(); // pre-select the first item

let popup = Popup::new("code-lens", picker).with_scrollbar(false);
cx.push_layer(Box::new(popup));
};
let popup = Popup::new("code-lens", picker).with_scrollbar(false);
cx.push_layer(Box::new(popup));
}

// TODO: should be run the same way as diagnostic - shouldn't require manual
Expand All @@ -1777,15 +1775,12 @@ pub fn request_code_lenses(cx: &mut Context) {

cx.callback(
request,
move |editor, compositor, lenses: Option<Vec<lsp::CodeLens>>| {
move |editor, _compositor, lenses: Option<Vec<lsp::CodeLens>>| {
if let Some(lenses) = lenses {
log::error!("lenses got: {:?}", lenses);

let doc = doc_mut!(editor, &doc_id);
let doc_text = doc.text();
if let Some(current_url) = doc.url() {
editor.code_lenses.insert(current_url, lenses.clone());
};

let lenses: Vec<CodeLens> = lenses
.iter()
Expand Down
2 changes: 0 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,6 @@ pub struct Editor {
pub macro_replaying: Vec<char>,
pub language_servers: helix_lsp::Registry,
pub diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize)>>,
pub code_lenses: BTreeMap<lsp::Url, Vec<lsp::CodeLens>>,
pub diff_providers: DiffProviderRegistry,

pub debugger: Option<dap::Client>,
Expand Down Expand Up @@ -1039,7 +1038,6 @@ impl Editor {
theme: theme_loader.default(),
language_servers,
diagnostics: BTreeMap::new(),
code_lenses: BTreeMap::new(),
diff_providers: DiffProviderRegistry::default(),
debugger: None,
debugger_events: SelectAll::new(),
Expand Down

0 comments on commit aafe875

Please sign in to comment.