Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions crates/ide/src/ide/assists/add_to_inherit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use super::{AssistKind, AssistsCtx};
use crate::def::AstPtr;
use crate::TextEdit;
use syntax::ast::{AstNode, Binding, HasBindings};
use syntax::{ast, TextRange};

// Add unknown symbols to inherit clauses
pub(super) fn add_to_inherit(ctx: &mut AssistsCtx<'_>) -> Option<()> {
let file_id = ctx.frange.file_id;
let unbound = ctx.covering_node::<ast::Ref>()?;
let unbound_text = unbound.syntax().text();
let name_res = ctx.db.name_resolution(file_id);
let source_map = ctx.db.source_map(file_id);

let ptr = AstPtr::new(unbound.syntax());
let expr_id = source_map.expr_for_node(ptr)?;

// The reference is defined, do nothing
if name_res.get(expr_id).is_some() {
return None;
}

// We walk upwards from the current node, adding suggestion for each parent let in that has an
// inherit construct.
for node in unbound.syntax().ancestors() {
if let Some(let_in) = ast::LetIn::cast(node) {
let inherits = let_in
.bindings()
.filter_map(|binding| match binding {
Binding::Inherit(x) => Some(x),
_ => None,
})
.collect::<Vec<_>>();

// Start from the last one (closest one) to have a consistent order
for inherit in inherits.iter().rev() {
if let Some(inherit_from) = inherit.from_expr() {
let add_loc = inherit
.attrs()
.last()
.map(|last| last.syntax().text_range())
.unwrap_or(inherit_from.syntax().text_range());

ctx.add(
"add_to_inherit",
format!(
"Inherit \"{}\" from {}",
unbound_text,
inherit_from.syntax().text()
),
AssistKind::RefactorRewrite,
vec![TextEdit {
delete: TextRange::new(add_loc.end(), add_loc.end()),
insert: format!(" {unbound_text}").into(),
}],
);
}
}
}
}

Some(())
}

#[cfg(test)]
mod tests {
use expect_test::expect;
define_check_assist!(super::add_to_inherit);

#[test]
fn simple() {
// We go from closest to furthest, the first edit would be the farthest
check(
"let inherit (lib) a; inherit (lib.types) b; in $0foo",
expect!["let inherit (lib) a foo; inherit (lib.types) b; in foo"],
);
check(
"let inherit (lib.types) a; in $0foo",
expect!["let inherit (lib.types) a foo; in foo"],
);
}
}
2 changes: 2 additions & 0 deletions crates/ide/src/ide/assists/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ macro_rules! define_check_assist {
};
}

mod add_to_inherit;
mod add_to_top_level_lambda_param;
mod convert_to_inherit;
mod flatten_attrset;
Expand Down Expand Up @@ -59,6 +60,7 @@ pub(crate) fn assists(db: &dyn DefDatabase, frange: FileRange) -> Vec<Assist> {
rewrite_string::rewrite_uri_to_string,
rewrite_string::unquote_attr,
inline::inline,
add_to_inherit::add_to_inherit,
];

let mut ctx = AssistsCtx::new(db, frange);
Expand Down
14 changes: 14 additions & 0 deletions docs/code_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,17 @@ let id = x: x; in a 1
```nix
let id = x: x; in (x: x) 1
```


### `add_to_inherit`

Append an unbound variable to a let-inherit-in clause in scope.
```nix
let inherit (lib) mkMerge;
in mkForce
```
=>
```nix
let inherit (lib) mkMerge mkForce;
in mkForce
```
Loading