Skip to content

Commit 0303982

Browse files
committed
New runnables API
1 parent f137b3a commit 0303982

File tree

8 files changed

+108
-118
lines changed

8 files changed

+108
-118
lines changed

crates/ra_ide/src/display/navigation_target.rs

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,16 @@ impl NavigationTarget {
9292
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
9393
if let Some(src) = module.declaration_source(db) {
9494
let frange = original_range(db, src.as_ref().map(|it| it.syntax()));
95-
return NavigationTarget::from_syntax(
95+
let mut res = NavigationTarget::from_syntax(
9696
frange.file_id,
9797
name,
9898
None,
9999
frange.range,
100100
src.value.syntax().kind(),
101-
src.value.doc_comment_text(),
102-
src.value.short_label(),
103101
);
102+
res.docs = src.value.doc_comment_text();
103+
res.description = src.value.short_label();
104+
return res;
104105
}
105106
module.to_nav(db)
106107
}
@@ -130,11 +131,9 @@ impl NavigationTarget {
130131
}
131132

132133
/// Allows `NavigationTarget` to be created from a `NameOwner`
133-
fn from_named(
134+
pub(crate) fn from_named(
134135
db: &RootDatabase,
135136
node: InFile<&dyn ast::NameOwner>,
136-
docs: Option<String>,
137-
description: Option<String>,
138137
) -> NavigationTarget {
139138
//FIXME: use `_` instead of empty string
140139
let name = node.value.name().map(|it| it.text().clone()).unwrap_or_default();
@@ -148,8 +147,6 @@ impl NavigationTarget {
148147
focus_range,
149148
frange.range,
150149
node.value.syntax().kind(),
151-
docs,
152-
description,
153150
)
154151
}
155152

@@ -159,8 +156,6 @@ impl NavigationTarget {
159156
focus_range: Option<TextRange>,
160157
full_range: TextRange,
161158
kind: SyntaxKind,
162-
docs: Option<String>,
163-
description: Option<String>,
164159
) -> NavigationTarget {
165160
NavigationTarget {
166161
file_id,
@@ -169,8 +164,8 @@ impl NavigationTarget {
169164
full_range,
170165
focus_range,
171166
container_name: None,
172-
description,
173-
docs,
167+
description: None,
168+
docs: None,
174169
}
175170
}
176171
}
@@ -238,12 +233,11 @@ where
238233
{
239234
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
240235
let src = self.source(db);
241-
NavigationTarget::from_named(
242-
db,
243-
src.as_ref().map(|it| it as &dyn ast::NameOwner),
244-
src.value.doc_comment_text(),
245-
src.value.short_label(),
246-
)
236+
let mut res =
237+
NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner));
238+
res.docs = src.value.doc_comment_text();
239+
res.description = src.value.short_label();
240+
res
247241
}
248242
}
249243

@@ -258,15 +252,7 @@ impl ToNav for hir::Module {
258252
}
259253
};
260254
let frange = original_range(db, src.with_value(syntax));
261-
NavigationTarget::from_syntax(
262-
frange.file_id,
263-
name,
264-
focus,
265-
frange.range,
266-
syntax.kind(),
267-
None,
268-
None,
269-
)
255+
NavigationTarget::from_syntax(frange.file_id, name, focus, frange.range, syntax.kind())
270256
}
271257
}
272258

@@ -285,8 +271,6 @@ impl ToNav for hir::ImplDef {
285271
None,
286272
frange.range,
287273
src.value.syntax().kind(),
288-
None,
289-
None,
290274
)
291275
}
292276
}
@@ -296,12 +280,12 @@ impl ToNav for hir::Field {
296280
let src = self.source(db);
297281

298282
match &src.value {
299-
FieldSource::Named(it) => NavigationTarget::from_named(
300-
db,
301-
src.with_value(it),
302-
it.doc_comment_text(),
303-
it.short_label(),
304-
),
283+
FieldSource::Named(it) => {
284+
let mut res = NavigationTarget::from_named(db, src.with_value(it));
285+
res.docs = it.doc_comment_text();
286+
res.description = it.short_label();
287+
res
288+
}
305289
FieldSource::Pos(it) => {
306290
let frange = original_range(db, src.with_value(it.syntax()));
307291
NavigationTarget::from_syntax(
@@ -310,8 +294,6 @@ impl ToNav for hir::Field {
310294
None,
311295
frange.range,
312296
it.syntax().kind(),
313-
None,
314-
None,
315297
)
316298
}
317299
}
@@ -322,12 +304,10 @@ impl ToNav for hir::MacroDef {
322304
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
323305
let src = self.source(db);
324306
log::debug!("nav target {:#?}", src.value.syntax());
325-
NavigationTarget::from_named(
326-
db,
327-
src.as_ref().map(|it| it as &dyn ast::NameOwner),
328-
src.value.doc_comment_text(),
329-
None,
330-
)
307+
let mut res =
308+
NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner));
309+
res.docs = src.value.doc_comment_text();
310+
res
331311
}
332312
}
333313

crates/ra_ide/src/runnables.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1+
use std::fmt;
2+
13
use hir::{AsAssocItem, Attrs, HirFileId, InFile, Semantics};
24
use itertools::Itertools;
5+
use ra_cfg::CfgExpr;
36
use ra_ide_db::RootDatabase;
47
use ra_syntax::{
5-
ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner},
6-
match_ast, SyntaxNode, TextRange,
8+
ast::{self, AstNode, AttrsOwner, DocCommentsOwner, ModuleItemOwner, NameOwner},
9+
match_ast, SyntaxNode,
710
};
811

9-
use crate::FileId;
10-
use ast::DocCommentsOwner;
11-
use ra_cfg::CfgExpr;
12-
use std::fmt::Display;
12+
use crate::{display::ToNav, FileId, NavigationTarget};
1313

1414
#[derive(Debug)]
1515
pub struct Runnable {
16-
pub range: TextRange,
16+
pub nav: NavigationTarget,
1717
pub kind: RunnableKind,
1818
pub cfg_exprs: Vec<CfgExpr>,
1919
}
@@ -24,8 +24,8 @@ pub enum TestId {
2424
Path(String),
2525
}
2626

27-
impl Display for TestId {
28-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
27+
impl fmt::Display for TestId {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2929
match self {
3030
TestId::Name(name) => write!(f, "{}", name),
3131
TestId::Path(path) => write!(f, "{}", path),
@@ -131,7 +131,8 @@ fn runnable_fn(
131131
let cfg_exprs =
132132
attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)).collect();
133133

134-
Some(Runnable { range: fn_def.syntax().text_range(), kind, cfg_exprs })
134+
let nav = NavigationTarget::from_named(sema.db, InFile::new(file_id.into(), &fn_def));
135+
Some(Runnable { nav, kind, cfg_exprs })
135136
}
136137

137138
#[derive(Debug)]
@@ -183,7 +184,6 @@ fn runnable_mod(
183184
if !has_test_function {
184185
return None;
185186
}
186-
let range = module.syntax().text_range();
187187
let module_def = sema.to_def(&module)?;
188188

189189
let path = module_def
@@ -197,7 +197,8 @@ fn runnable_mod(
197197
let cfg_exprs =
198198
attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)).collect();
199199

200-
Some(Runnable { range, kind: RunnableKind::TestMod { path }, cfg_exprs })
200+
let nav = module_def.to_nav(sema.db);
201+
Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg_exprs })
201202
}
202203

203204
#[cfg(test)]

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{collections::HashMap, path::PathBuf};
44

55
use lsp_types::request::Request;
66
use lsp_types::{Position, Range, TextDocumentIdentifier};
7-
use rustc_hash::FxHashMap;
87
use serde::{Deserialize, Serialize};
98

109
pub enum AnalyzerStatus {}
@@ -121,25 +120,30 @@ pub struct RunnablesParams {
121120
pub position: Option<Position>,
122121
}
123122

124-
// Must strictly correspond to the executable name
123+
#[derive(Deserialize, Serialize, Debug)]
124+
#[serde(rename_all = "camelCase")]
125+
pub struct Runnable {
126+
pub label: String,
127+
#[serde(skip_serializing_if = "Option::is_none")]
128+
pub location: Option<lsp_types::LocationLink>,
129+
pub kind: RunnableKind,
130+
pub args: CargoRunnable,
131+
}
132+
125133
#[derive(Serialize, Deserialize, Debug)]
126134
#[serde(rename_all = "lowercase")]
127135
pub enum RunnableKind {
128136
Cargo,
129-
Rustc,
130-
Rustup,
131137
}
132138

133139
#[derive(Deserialize, Serialize, Debug)]
134140
#[serde(rename_all = "camelCase")]
135-
pub struct Runnable {
136-
pub range: Range,
137-
pub label: String,
138-
pub kind: RunnableKind,
139-
pub args: Vec<String>,
140-
pub extra_args: Vec<String>,
141-
pub env: FxHashMap<String, String>,
142-
pub cwd: Option<PathBuf>,
141+
pub struct CargoRunnable {
142+
pub workspace_root: Option<PathBuf>,
143+
// command, --package and --lib stuff
144+
pub cargo_args: Vec<String>,
145+
// stuff after --
146+
pub executable_args: Vec<String>,
143147
}
144148

145149
pub enum InlayHints {}

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use ra_ide::{
2323
use ra_prof::profile;
2424
use ra_project_model::TargetKind;
2525
use ra_syntax::{AstNode, SyntaxKind, TextRange, TextSize};
26-
use rustc_hash::FxHashMap;
2726
use serde::{Deserialize, Serialize};
2827
use serde_json::to_value;
2928
use stdx::format_to;
@@ -401,7 +400,7 @@ pub fn handle_runnables(
401400
let cargo_spec = CargoTargetSpec::for_file(&world, file_id)?;
402401
for runnable in world.analysis().runnables(file_id)? {
403402
if let Some(offset) = offset {
404-
if !runnable.range.contains_inclusive(offset) {
403+
if !runnable.nav.full_range().contains_inclusive(offset) {
405404
continue;
406405
}
407406
}
@@ -422,25 +421,31 @@ pub fn handle_runnables(
422421
Some(spec) => {
423422
for &cmd in ["check", "test"].iter() {
424423
res.push(lsp_ext::Runnable {
425-
range: Default::default(),
426424
label: format!("cargo {} -p {}", cmd, spec.package),
425+
location: None,
427426
kind: lsp_ext::RunnableKind::Cargo,
428-
args: vec![cmd.to_string(), "--package".to_string(), spec.package.clone()],
429-
extra_args: Vec::new(),
430-
env: FxHashMap::default(),
431-
cwd: workspace_root.map(|root| root.to_owned()),
427+
args: lsp_ext::CargoRunnable {
428+
workspace_root: workspace_root.map(|root| root.to_owned()),
429+
cargo_args: vec![
430+
cmd.to_string(),
431+
"--package".to_string(),
432+
spec.package.clone(),
433+
],
434+
executable_args: Vec::new(),
435+
},
432436
})
433437
}
434438
}
435439
None => {
436440
res.push(lsp_ext::Runnable {
437-
range: Default::default(),
438441
label: "cargo check --workspace".to_string(),
442+
location: None,
439443
kind: lsp_ext::RunnableKind::Cargo,
440-
args: vec!["check".to_string(), "--workspace".to_string()],
441-
extra_args: Vec::new(),
442-
env: FxHashMap::default(),
443-
cwd: workspace_root.map(|root| root.to_owned()),
444+
args: lsp_ext::CargoRunnable {
445+
workspace_root: workspace_root.map(|root| root.to_owned()),
446+
cargo_args: vec!["check".to_string(), "--workspace".to_string()],
447+
executable_args: Vec::new(),
448+
},
444449
});
445450
}
446451
}
@@ -782,10 +787,11 @@ pub fn handle_code_lens(
782787
}
783788
};
784789

785-
let mut r = to_proto::runnable(&world, file_id, runnable)?;
790+
let range = to_proto::range(&line_index, runnable.nav.range());
791+
let r = to_proto::runnable(&world, file_id, runnable)?;
786792
if world.config.lens.run {
787793
let lens = CodeLens {
788-
range: r.range,
794+
range,
789795
command: Some(Command {
790796
title: run_title.to_string(),
791797
command: "rust-analyzer.runSingle".into(),
@@ -797,13 +803,8 @@ pub fn handle_code_lens(
797803
}
798804

799805
if debugee && world.config.lens.debug {
800-
if r.args[0] == "run" {
801-
r.args[0] = "build".into();
802-
} else {
803-
r.args.push("--no-run".into());
804-
}
805806
let debug_lens = CodeLens {
806-
range: r.range,
807+
range,
807808
command: Some(Command {
808809
title: "Debug".into(),
809810
command: "rust-analyzer.debugSingle".into(),

crates/rust-analyzer/src/to_proto.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use ra_ide::{
88
};
99
use ra_syntax::{SyntaxKind, TextRange, TextSize};
1010
use ra_vfs::LineEndings;
11-
use rustc_hash::FxHashMap;
1211

1312
use crate::{
1413
cargo_target_spec::CargoTargetSpec, lsp_ext, semantic_tokens, world::WorldSnapshot, Result,
@@ -638,9 +637,8 @@ pub(crate) fn runnable(
638637
) -> Result<lsp_ext::Runnable> {
639638
let spec = CargoTargetSpec::for_file(world, file_id)?;
640639
let target = spec.as_ref().map(|s| s.target.clone());
641-
let (args, extra_args) =
640+
let (cargo_args, executable_args) =
642641
CargoTargetSpec::runnable_args(spec, &runnable.kind, &runnable.cfg_exprs)?;
643-
let line_index = world.analysis().file_line_index(file_id)?;
644642
let label = match &runnable.kind {
645643
RunnableKind::Test { test_id, .. } => format!("test {}", test_id),
646644
RunnableKind::TestMod { path } => format!("test-mod {}", path),
@@ -650,18 +648,16 @@ pub(crate) fn runnable(
650648
target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t))
651649
}
652650
};
651+
let location = location_link(world, None, runnable.nav)?;
653652

654653
Ok(lsp_ext::Runnable {
655-
range: range(&line_index, runnable.range),
656654
label,
655+
location: Some(location),
657656
kind: lsp_ext::RunnableKind::Cargo,
658-
args,
659-
extra_args,
660-
env: {
661-
let mut m = FxHashMap::default();
662-
m.insert("RUST_BACKTRACE".to_string(), "short".to_string());
663-
m
657+
args: lsp_ext::CargoRunnable {
658+
workspace_root: world.workspace_root_for(file_id).map(|root| root.to_owned()),
659+
cargo_args,
660+
executable_args,
664661
},
665-
cwd: world.workspace_root_for(file_id).map(|root| root.to_owned()),
666662
})
667663
}

0 commit comments

Comments
 (0)