@@ -5,6 +5,19 @@ use crate::edit::{OutputChange, Outputs};
55use super :: error:: WalkerError ;
66use 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.
922pub 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 ( ) ) ;
0 commit comments