1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
use std::{path::Path, fs};
use anyhow::{Result, Context};
use itertools::Itertools;
use rnix::{SyntaxKind, ast::{AttrpathValue, AttrSet, HasEntry, Entry::*}, SyntaxNode};
use rowan::{ast::AstNode, TextSize};
use crate::status_reporter::StatusReport;
fn textsize_at_line(s: &str, line: usize) -> TextSize {
s
.split('\n')
.map(|l|
TextSize::of(l) + TextSize::new(1)
)
.take(line-1)
.sum()
}
fn dig_to_kind(kind: SyntaxKind, node: &SyntaxNode) -> Option<SyntaxNode> {
if node.kind() == kind {
return Some(node.clone());
}
node.descendants()
.filter(|node| node.kind() == kind)
.next()
}
fn add_to_meta_block(rough_pos: SyntaxNode, content: &str, main_program: &str, file: &Path, line: usize) -> Result<(String, usize)> {
let meta_node = dig_to_kind(SyntaxKind::NODE_ATTR_SET, &rough_pos).unwrap();
let meta_set = AttrSet::cast(meta_node.clone()).unwrap();
let description_entry = meta_set.entries()
.filter(|entry| {
match &entry {
Inherit(it) => it.attrs().any(|c| c.to_string() == "description"),
AttrpathValue(it) => it.attrpath().unwrap().to_string() == "description",
}
})
.exactly_one().ok()
.with_context(|| format!("meta node has no description attribute in {:?} at {}", file, line))?;
let description = description_entry.syntax();
let pos = description.text_range();
let indent = content[..pos.start().into()].chars().rev().position(|c| c == '\n').unwrap();
let patch = String::new()
+ "\n"
+ &" ".repeat(indent)
+ "mainProgram = \"" + main_program + "\";";
Ok((patch, pos.end().into()))
}
fn edit_one(file: &Path, line: usize, main_program: &str, p: &StatusReport) -> Result<String> {
let mut content = fs::read_to_string(file)?;
let searchpos = textsize_at_line(&content, line);
p.update_item(format!("doing {:?}", file));
let parse = rnix::Root::parse(&content);
if !parse.errors().is_empty() {
anyhow::bail!("error: {:?}", parse.errors());
}
let tree = parse.tree();
let pos_node = tree.syntax().descendants()
.filter(|node| {
if node.kind() == SyntaxKind::NODE_ATTRPATH_VALUE {
let value = AttrpathValue::cast(node.clone()).unwrap();
node.text_range().contains(searchpos) && value.attrpath().unwrap().to_string() == "meta"
} else { false }
})
.exactly_one().ok();
// do we have a meta attrset already?
let (patch, insert_offset) = match pos_node {
None => {
let version_node = tree
.syntax()
.descendants()
.filter(|node| {
if node.kind() == SyntaxKind::NODE_ATTRPATH_VALUE {
let value = AttrpathValue::cast(node.clone()).unwrap();
let name = value.attrpath().unwrap().to_string();
node.text_range().contains(searchpos + TextSize::new(5))
&& (name == "version" || name == "pname" || name == "name")
} else { false }
})
.exactly_one().ok()
.with_context(|| format!("neither meta nor version node found for {:?} at {}", file, line))?;
let attrset = version_node.parent().unwrap();
if attrset.kind() != SyntaxKind::NODE_ATTR_SET {
anyhow::bail!("name not in an attrset in {:?} at {}", file, line)
}
// does a meta block already exist?
let maybe_meta_block = attrset
.descendants()
.filter(|node| {
if node.kind() == SyntaxKind::NODE_ATTRPATH_VALUE {
let value = AttrpathValue::cast(node.clone()).unwrap();
let name = value.attrpath().unwrap().to_string();
name == "meta"
} else { false }
})
.exactly_one();
if let Ok(meta) = maybe_meta_block {
add_to_meta_block(meta.clone(), &content, main_program, file, line)?
} else {
let before_attrset_end = Into::<usize>::into(attrset.text_range().end())
- 1
- content[..attrset.text_range().end().into()]
.chars().rev().position(|c| c == '\n').unwrap();
let indent = content[..version_node.text_range().start().into()]
.chars().rev().position(|c| c == '\n').unwrap();
// some language specific build systems don't use meta as its own attrset
// there's no good way to recognise these, but this seems to work fine
let weird_nonstandard_meta = attrset
.descendants()
.any(|node| {
if node.kind() == SyntaxKind::NODE_ATTRPATH_VALUE {
let value = AttrpathValue::cast(node.clone()).unwrap();
let name = value.attrpath().unwrap().to_string();
name == "description" || name == "homepage" || name == "license"
} else { false }
});
let patch = String::new()
+ "\n"
+ &" ".repeat(indent)
+ if weird_nonstandard_meta { "mainProgram = \"" } else { "meta.mainProgram = \"" }
+ main_program + "\";";
(patch, before_attrset_end)
}
},
Some(pos) => {
add_to_meta_block(pos.clone(), &content, main_program, file, line)?
}
};
content = String::new()
+ &content[..insert_offset]
+ &patch
+ &content[insert_offset..];
p.changed_item();
Ok(content)
}
|