Skip to content

Commit

Permalink
not so working lenses
Browse files Browse the repository at this point in the history
maybe a rust analyzer or lsp_types bug, not sure
  • Loading branch information
maddyblue committed Oct 18, 2021
1 parent ee9556f commit 30ac3ea
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl std::fmt::Display for WDProgress {
enum Action {
Command(CodeActionOrCommand),
Completion(CompletionItem),
CodeLens(CodeLens),
}

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
Expand Down Expand Up @@ -168,6 +169,7 @@ struct WindowHover {
hover: Option<String>,
/// result of signature request
signature: Option<String>,
lens: Vec<CodeLens>,
/// completion response. we need to cache this because we also need the token
/// response to come, and we don't know which will come first.
completion: Vec<CompletionItem>,
Expand Down Expand Up @@ -391,6 +393,9 @@ impl Server {
}
hover.actions.extend(v);
}
hover
.actions
.extend(hover.lens.iter().map(|lens| Action::CodeLens(lens.clone())));

hover.body.clear();
if let Some(text) = &hover.hover {
Expand Down Expand Up @@ -431,6 +436,22 @@ impl Server {
write!(&mut hover.body, " {}", d).unwrap();
}
}
// TODO: extract out the range text and append it to the command title to
// distinguish between lenses.
Action::CodeLens(_lens) => {
/*
write!(
&mut hover.body,
"{}[{}]",
newline,
lens.command
.as_ref()
.map(|c| c.title.clone())
.unwrap_or("unknown command".into())
)
.unwrap();
*/
}
}
}
hover.action_addrs.push((hover.body.len(), 100000));
Expand Down Expand Up @@ -494,12 +515,6 @@ impl Server {
if caps.implementation_provider.is_some() {
body.push_str("[impl] ");
}
#[cfg(debug_assertions)]
{
if caps.code_lens_provider.is_some() {
body.push_str("[lens] ");
}
}
if caps.references_provider.is_some() {
body.push_str("[references] ");
}
Expand Down Expand Up @@ -819,17 +834,9 @@ impl Server {
CodeLensRequest::METHOD => {
let msg = serde_json::from_str::<Option<Vec<CodeLens>>>(result.get())?;
if let Some(msg) = msg {
let mut o: Vec<String> = vec![];
for lens in msg {
let loc = Location {
uri: url.clone(),
range: lens.range,
};
o.push(format!("{}", location_to_plumb(&loc)));
}
if o.len() > 0 {
self.output = o.join("\n");
}
self.set_hover(&url, |hover| {
hover.lens = msg;
});
}
}
CodeActionRequest::METHOD => {
Expand Down Expand Up @@ -1158,6 +1165,7 @@ impl Server {
line,
token: None,
signature: None,
lens: vec![],
completion: vec![],
code_actions: vec![],
actions: vec![],
Expand Down Expand Up @@ -1212,13 +1220,22 @@ impl Server {
)?;
self.send_request::<SignatureHelpRequest>(
client_name,
url,
url.clone(),
SignatureHelpParams {
context: None,
text_document_position_params: text_document_position_params.clone(),
work_done_progress_params,
},
)?;
self.send_request::<CodeLensRequest>(
client_name,
url.clone(),
CodeLensParams {
text_document,
work_done_progress_params,
partial_result_params,
},
)?;
Ok(())
}
fn run_event(&mut self, ev: Event, wid: usize) -> Result<()> {
Expand Down Expand Up @@ -1267,17 +1284,6 @@ impl Server {
},
)?;
}
"lens" => {
self.send_request::<CodeLensRequest>(
client_name,
url,
CodeLensParams {
text_document,
work_done_progress_params,
partial_result_params,
},
)?;
}
"impl" => {
self.send_request::<GotoImplementation>(
client_name,
Expand Down Expand Up @@ -1372,6 +1378,11 @@ impl Server {
}
panic!("unsupported");
}
Action::CodeLens(lens) => {
// TODO: rust-analyzer complains about "code lens without data" here. If I set
// lens.data to Value::Null, it complains with some resolution error.
let _id = self.send_request::<CodeLensResolve>(client_name.into(), url, lens)?;
}
}
Ok(())
}
Expand Down

0 comments on commit 30ac3ea

Please sign in to comment.