Skip to content

Commit 2cb0c36

Browse files
authored
Merge pull request #488 from a-kenji/ke-fix-paren-outputs-panic
outputs: unwrap parantheses
2 parents 814527f + b7a4c7e commit 2cb0c36

File tree

5 files changed

+116
-12
lines changed

5 files changed

+116
-12
lines changed

src/walk/outputs.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ use crate::edit::{OutputChange, Outputs};
55
use super::error::WalkerError;
66
use super::node::parse_node;
77

8+
/// Unwrap parentheses around a node, returning the inner node.
9+
/// If the node is not NODE_PAREN, returns a clone of the original.
10+
fn unwrap_parens(node: &SyntaxNode) -> SyntaxNode {
11+
if node.kind() == SyntaxKind::NODE_PAREN {
12+
if let Some(inner) = node.children().find(|c| {
13+
c.kind() != SyntaxKind::TOKEN_L_PAREN && c.kind() != SyntaxKind::TOKEN_R_PAREN
14+
}) {
15+
return inner;
16+
}
17+
}
18+
node.clone()
19+
}
20+
821
/// List the outputs from a flake.nix root node.
922
pub fn list_outputs(root: &SyntaxNode) -> Result<Outputs, WalkerError> {
1023
let mut outputs: Vec<String> = vec![];
@@ -22,8 +35,11 @@ pub fn list_outputs(root: &SyntaxNode) -> Result<Outputs, WalkerError> {
2235
{
2336
assert!(outputs_node.kind() == SyntaxKind::NODE_ATTRPATH);
2437

25-
if let Some(outputs_lambda) = outputs_node.next_sibling() {
26-
assert!(outputs_lambda.kind() == SyntaxKind::NODE_LAMBDA);
38+
if let Some(next_sibling) = outputs_node.next_sibling() {
39+
let outputs_lambda = unwrap_parens(&next_sibling);
40+
if outputs_lambda.kind() != SyntaxKind::NODE_LAMBDA {
41+
continue;
42+
}
2743
if let Some(output) = outputs_lambda
2844
.children()
2945
.find(|n| n.kind() == SyntaxKind::NODE_PATTERN)
@@ -75,8 +91,11 @@ pub fn change_outputs(
7591
{
7692
assert!(outputs_node.kind() == SyntaxKind::NODE_ATTRPATH);
7793

78-
if let Some(outputs_lambda) = outputs_node.next_sibling() {
79-
assert!(outputs_lambda.kind() == SyntaxKind::NODE_LAMBDA);
94+
if let Some(next_sibling) = outputs_node.next_sibling() {
95+
let outputs_lambda = unwrap_parens(&next_sibling);
96+
if outputs_lambda.kind() != SyntaxKind::NODE_LAMBDA {
97+
continue;
98+
}
8099
for output in outputs_lambda.children() {
81100
if SyntaxKind::NODE_PATTERN == output.kind() {
82101
if let OutputChange::Add(ref add) = change {
@@ -191,10 +210,21 @@ pub fn change_outputs(
191210
let changed_outputs_lambda = outputs_lambda
192211
.green()
193212
.replace_child(output.index(), green.into());
194-
let changed_toplevel = toplevel.green().replace_child(
195-
outputs_lambda.index(),
196-
changed_outputs_lambda.into(),
197-
);
213+
let changed_toplevel = if next_sibling.kind() == SyntaxKind::NODE_PAREN
214+
{
215+
let changed_paren = next_sibling.green().replace_child(
216+
outputs_lambda.index(),
217+
changed_outputs_lambda.into(),
218+
);
219+
toplevel
220+
.green()
221+
.replace_child(next_sibling.index(), changed_paren.into())
222+
} else {
223+
toplevel.green().replace_child(
224+
outputs_lambda.index(),
225+
changed_outputs_lambda.into(),
226+
)
227+
};
198228
let changed_attr_set = attr_set
199229
.green()
200230
.replace_child(toplevel.index(), changed_toplevel.into());
@@ -291,10 +321,22 @@ pub fn change_outputs(
291321
let changed_outputs_lambda = outputs_lambda
292322
.green()
293323
.replace_child(output.index(), green.into());
294-
let changed_toplevel = toplevel.green().replace_child(
295-
outputs_lambda.index(),
296-
changed_outputs_lambda.into(),
297-
);
324+
let changed_toplevel = if next_sibling.kind()
325+
== SyntaxKind::NODE_PAREN
326+
{
327+
let changed_paren = next_sibling.green().replace_child(
328+
outputs_lambda.index(),
329+
changed_outputs_lambda.into(),
330+
);
331+
toplevel
332+
.green()
333+
.replace_child(next_sibling.index(), changed_paren.into())
334+
} else {
335+
toplevel.green().replace_child(
336+
outputs_lambda.index(),
337+
changed_outputs_lambda.into(),
338+
)
339+
};
298340
let changed_attr_set = attr_set
299341
.green()
300342
.replace_child(toplevel.index(), changed_toplevel.into());

tests/edit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn test_flake_edit_list(#[case] fixture: &str) {
5454
#[case("leading_comma_trailing_comma_outputs", true, "github:mic92/vmsh")]
5555
#[case("empty_inputs", true, "github:mic92/vmsh")]
5656
#[case("empty_inputs", false, "github:a-kenji/not_a_flake")]
57+
#[case("outputs_paren", true, "github:mic92/vmsh")]
5758
fn test_add_input(#[case] fixture: &str, #[case] is_flake: bool, #[case] uri: &str) {
5859
let content = load_flake(fixture);
5960
let mut flake_edit = FlakeEdit::from_text(&content).unwrap();
@@ -139,6 +140,7 @@ fn test_first_nested_node_add_with_list(#[case] is_flake: bool) {
139140
#[case("outputs_at_remove_multiline", "nixpkgs-lib")]
140141
#[case("outputs_at_leading_comma", "fenix")]
141142
#[case("leading_comma_outputs", "fenix")]
143+
#[case("outputs_paren", "flake-parts")]
142144
#[case("quoted_input_with_dots", "\"hls-1.10\"")]
143145
fn test_remove_input(#[case] fixture: &str, #[case] input_id: &str) {
144146
let content = load_flake(fixture);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
inputs = {
3+
flake-parts.url = "github:hercules-ci/flake-parts";
4+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
5+
};
6+
7+
outputs = (
8+
inputs@{ flake-parts, self, ... }:
9+
flake-parts.lib.mkFlake { inherit inputs; } {
10+
systems = [ "x86_64-linux" ];
11+
}
12+
);
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: tests/edit.rs
3+
expression: result
4+
info:
5+
flake_nix: ""
6+
changes:
7+
- Add:
8+
id: vmsh
9+
uri: "github:mic92/vmsh"
10+
flake: true
11+
---
12+
{
13+
inputs = {
14+
flake-parts.url = "github:hercules-ci/flake-parts";
15+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
16+
vmsh.url = "github:mic92/vmsh";
17+
};
18+
19+
outputs = (
20+
inputs@{ flake-parts, self, ... }:
21+
flake-parts.lib.mkFlake { inherit inputs; } {
22+
systems = [ "x86_64-linux" ];
23+
}
24+
);
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: tests/edit.rs
3+
expression: result
4+
info:
5+
flake_nix: ""
6+
changes:
7+
- Remove:
8+
ids:
9+
- flake-parts
10+
---
11+
{
12+
inputs = {
13+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
14+
};
15+
16+
outputs = (
17+
inputs@{ self, ... }:
18+
flake-parts.lib.mkFlake { inherit inputs; } {
19+
systems = [ "x86_64-linux" ];
20+
}
21+
);
22+
}

0 commit comments

Comments
 (0)