Skip to content

Commit 30ac3ea

Browse files
committed
not so working lenses
maybe a rust analyzer or lsp_types bug, not sure
1 parent ee9556f commit 30ac3ea

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/main.rs

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ impl std::fmt::Display for WDProgress {
103103
enum Action {
104104
Command(CodeActionOrCommand),
105105
Completion(CompletionItem),
106+
CodeLens(CodeLens),
106107
}
107108

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

395400
hover.body.clear();
396401
if let Some(text) = &hover.hover {
@@ -431,6 +436,22 @@ impl Server {
431436
write!(&mut hover.body, " {}", d).unwrap();
432437
}
433438
}
439+
// TODO: extract out the range text and append it to the command title to
440+
// distinguish between lenses.
441+
Action::CodeLens(_lens) => {
442+
/*
443+
write!(
444+
&mut hover.body,
445+
"{}[{}]",
446+
newline,
447+
lens.command
448+
.as_ref()
449+
.map(|c| c.title.clone())
450+
.unwrap_or("unknown command".into())
451+
)
452+
.unwrap();
453+
*/
454+
}
434455
}
435456
}
436457
hover.action_addrs.push((hover.body.len(), 100000));
@@ -494,12 +515,6 @@ impl Server {
494515
if caps.implementation_provider.is_some() {
495516
body.push_str("[impl] ");
496517
}
497-
#[cfg(debug_assertions)]
498-
{
499-
if caps.code_lens_provider.is_some() {
500-
body.push_str("[lens] ");
501-
}
502-
}
503518
if caps.references_provider.is_some() {
504519
body.push_str("[references] ");
505520
}
@@ -819,17 +834,9 @@ impl Server {
819834
CodeLensRequest::METHOD => {
820835
let msg = serde_json::from_str::<Option<Vec<CodeLens>>>(result.get())?;
821836
if let Some(msg) = msg {
822-
let mut o: Vec<String> = vec![];
823-
for lens in msg {
824-
let loc = Location {
825-
uri: url.clone(),
826-
range: lens.range,
827-
};
828-
o.push(format!("{}", location_to_plumb(&loc)));
829-
}
830-
if o.len() > 0 {
831-
self.output = o.join("\n");
832-
}
837+
self.set_hover(&url, |hover| {
838+
hover.lens = msg;
839+
});
833840
}
834841
}
835842
CodeActionRequest::METHOD => {
@@ -1158,6 +1165,7 @@ impl Server {
11581165
line,
11591166
token: None,
11601167
signature: None,
1168+
lens: vec![],
11611169
completion: vec![],
11621170
code_actions: vec![],
11631171
actions: vec![],
@@ -1212,13 +1220,22 @@ impl Server {
12121220
)?;
12131221
self.send_request::<SignatureHelpRequest>(
12141222
client_name,
1215-
url,
1223+
url.clone(),
12161224
SignatureHelpParams {
12171225
context: None,
12181226
text_document_position_params: text_document_position_params.clone(),
12191227
work_done_progress_params,
12201228
},
12211229
)?;
1230+
self.send_request::<CodeLensRequest>(
1231+
client_name,
1232+
url.clone(),
1233+
CodeLensParams {
1234+
text_document,
1235+
work_done_progress_params,
1236+
partial_result_params,
1237+
},
1238+
)?;
12221239
Ok(())
12231240
}
12241241
fn run_event(&mut self, ev: Event, wid: usize) -> Result<()> {
@@ -1267,17 +1284,6 @@ impl Server {
12671284
},
12681285
)?;
12691286
}
1270-
"lens" => {
1271-
self.send_request::<CodeLensRequest>(
1272-
client_name,
1273-
url,
1274-
CodeLensParams {
1275-
text_document,
1276-
work_done_progress_params,
1277-
partial_result_params,
1278-
},
1279-
)?;
1280-
}
12811287
"impl" => {
12821288
self.send_request::<GotoImplementation>(
12831289
client_name,
@@ -1372,6 +1378,11 @@ impl Server {
13721378
}
13731379
panic!("unsupported");
13741380
}
1381+
Action::CodeLens(lens) => {
1382+
// TODO: rust-analyzer complains about "code lens without data" here. If I set
1383+
// lens.data to Value::Null, it complains with some resolution error.
1384+
let _id = self.send_request::<CodeLensResolve>(client_name.into(), url, lens)?;
1385+
}
13751386
}
13761387
Ok(())
13771388
}

0 commit comments

Comments
 (0)