Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 24ef03f

Browse files
authored
Fix comments for keys in tables gets moved away (#23)
1 parent ee6ab9a commit 24ef03f

File tree

8 files changed

+156
-112
lines changed

8 files changed

+156
-112
lines changed

rust/src/data/ruff-21.expected.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[tool.ruff]
2+
lint.select = [
3+
"ALL",
4+
]
5+
lint.ignore = [
6+
# We do not annotate the type of 'self'.
7+
"ANN101",
8+
]
9+
# Do not automatically remove commented out code.
10+
# We comment out code during development, and with VSCode auto-save, this code
11+
# is sometimes annoyingly removed.
12+
lint.unfixable = [
13+
"ERA001",
14+
]

rust/src/data/ruff-21.start.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[tool.ruff.lint]
2+
select = ["ALL"]
3+
4+
ignore = [
5+
# We do not annotate the type of 'self'.
6+
"ANN101",
7+
]
8+
9+
# Do not automatically remove commented out code.
10+
# We comment out code during development, and with VSCode auto-save, this code
11+
# is sometimes annoyingly removed.
12+
unfixable = ["ERA001"]

rust/src/helpers/array.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cell::RefCell;
22
use std::collections::HashMap;
33

44
use lexical_sort::{natural_lexical_cmp, StringSort};
5+
use taplo::syntax::SyntaxKind::{ARRAY, COMMA, NEWLINE, STRING, VALUE, WHITESPACE};
56
use taplo::syntax::{SyntaxElement, SyntaxKind, SyntaxNode};
67

78
use crate::helpers::create::{make_comma, make_newline};
@@ -12,9 +13,9 @@ where
1213
F: Fn(&str) -> String,
1314
{
1415
for array in node.children_with_tokens() {
15-
if array.kind() == SyntaxKind::ARRAY {
16+
if array.kind() == ARRAY {
1617
for array_entry in array.as_node().unwrap().children_with_tokens() {
17-
if array_entry.kind() == SyntaxKind::VALUE {
18+
if array_entry.kind() == VALUE {
1819
update_content(array_entry.as_node().unwrap(), transform);
1920
}
2021
}
@@ -27,7 +28,7 @@ where
2728
F: Fn(&str) -> String,
2829
{
2930
for array in node.children_with_tokens() {
30-
if array.kind() == SyntaxKind::ARRAY {
31+
if array.kind() == ARRAY {
3132
let array_node = array.as_node().unwrap();
3233
let mut value_set = Vec::<Vec<SyntaxElement>>::new();
3334
let entry_set = RefCell::new(Vec::<SyntaxElement>::new());
@@ -53,13 +54,13 @@ where
5354
if previous_is_value {
5455
// make sure ends with trailing comma
5556
previous_is_value = false;
56-
if entry.kind() != SyntaxKind::COMMA {
57+
if entry.kind() != COMMA {
5758
entry_set.borrow_mut().push(make_comma());
5859
}
5960
}
6061
if previous_is_bracket_open {
6162
// make sure ends with trailing comma
62-
if entry.kind() == SyntaxKind::NEWLINE || entry.kind() == SyntaxKind::WHITESPACE {
63+
if entry.kind() == NEWLINE || entry.kind() == WHITESPACE {
6364
continue;
6465
}
6566
previous_is_bracket_open = false;
@@ -78,7 +79,7 @@ where
7879
}
7980
entries.push(entry);
8081
}
81-
SyntaxKind::VALUE => {
82+
VALUE => {
8283
if has_value {
8384
entry_set.borrow_mut().push(make_newline());
8485
add_to_value_set(entry_value.clone());
@@ -88,9 +89,8 @@ where
8889
let mut found_string = false;
8990
for child in value_node.children_with_tokens() {
9091
let kind = child.kind();
91-
if kind == SyntaxKind::STRING {
92-
entry_value =
93-
transform(load_text(child.as_token().unwrap().text(), SyntaxKind::STRING).as_str());
92+
if kind == STRING {
93+
entry_value = transform(load_text(child.as_token().unwrap().text(), STRING).as_str());
9494
found_string = true;
9595
break;
9696
}
@@ -102,7 +102,7 @@ where
102102
entry_set.borrow_mut().push(entry);
103103
previous_is_value = true;
104104
}
105-
SyntaxKind::NEWLINE => {
105+
NEWLINE => {
106106
entry_set.borrow_mut().push(entry);
107107
if has_value {
108108
add_to_value_set(entry_value.clone());
@@ -133,7 +133,7 @@ mod tests {
133133
use rstest::rstest;
134134
use taplo::formatter::{format_syntax, Options};
135135
use taplo::parser::parse;
136-
use taplo::syntax::SyntaxKind;
136+
use taplo::syntax::SyntaxKind::{ENTRY, VALUE};
137137

138138
use crate::helpers::array::{sort, transform};
139139
use crate::helpers::pep508::format_requirement;
@@ -193,9 +193,9 @@ mod tests {
193193
fn test_normalize_requirement(#[case] start: &str, #[case] expected: &str, #[case] keep_full_version: bool) {
194194
let root_ast = parse(start).into_syntax().clone_for_update();
195195
for children in root_ast.children_with_tokens() {
196-
if children.kind() == SyntaxKind::ENTRY {
196+
if children.kind() == ENTRY {
197197
for entry in children.as_node().unwrap().children_with_tokens() {
198-
if entry.kind() == SyntaxKind::VALUE {
198+
if entry.kind() == VALUE {
199199
transform(entry.as_node().unwrap(), &|s| format_requirement(s, keep_full_version));
200200
}
201201
}
@@ -275,9 +275,9 @@ mod tests {
275275
fn test_order_array(#[case] start: &str, #[case] expected: &str) {
276276
let root_ast = parse(start).into_syntax().clone_for_update();
277277
for children in root_ast.children_with_tokens() {
278-
if children.kind() == SyntaxKind::ENTRY {
278+
if children.kind() == ENTRY {
279279
for entry in children.as_node().unwrap().children_with_tokens() {
280-
if entry.kind() == SyntaxKind::VALUE {
280+
if entry.kind() == VALUE {
281281
sort(entry.as_node().unwrap(), str::to_lowercase);
282282
}
283283
}

rust/src/helpers/create.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use taplo::parser::parse;
2-
use taplo::syntax::{SyntaxElement, SyntaxKind};
2+
use taplo::syntax::SyntaxElement;
3+
use taplo::syntax::SyntaxKind::{ARRAY, COMMA, ENTRY, KEY, NEWLINE, STRING, VALUE};
34

45
pub fn make_string_node(text: &str) -> SyntaxElement {
56
let expr = &format!("a = \"{}\"", text.replace('"', "\\\""));
@@ -10,9 +11,9 @@ pub fn make_string_node(text: &str) -> SyntaxElement {
1011
.unwrap()
1112
.children_with_tokens()
1213
{
13-
if root.kind() == SyntaxKind::VALUE {
14+
if root.kind() == VALUE {
1415
for entries in root.as_node().unwrap().children_with_tokens() {
15-
if entries.kind() == SyntaxKind::STRING {
16+
if entries.kind() == STRING {
1617
return entries;
1718
}
1819
}
@@ -23,7 +24,7 @@ pub fn make_string_node(text: &str) -> SyntaxElement {
2324

2425
pub fn make_empty_newline() -> SyntaxElement {
2526
for root in parse("\n\n").into_syntax().clone_for_update().children_with_tokens() {
26-
if root.kind() == SyntaxKind::NEWLINE {
27+
if root.kind() == NEWLINE {
2728
return root;
2829
}
2930
}
@@ -32,7 +33,7 @@ pub fn make_empty_newline() -> SyntaxElement {
3233

3334
pub fn make_newline() -> SyntaxElement {
3435
for root in parse("\n").into_syntax().clone_for_update().children_with_tokens() {
35-
if root.kind() == SyntaxKind::NEWLINE {
36+
if root.kind() == NEWLINE {
3637
return root;
3738
}
3839
}
@@ -41,13 +42,13 @@ pub fn make_newline() -> SyntaxElement {
4142

4243
pub fn make_comma() -> SyntaxElement {
4344
for root in parse("a=[1,2]").into_syntax().clone_for_update().children_with_tokens() {
44-
if root.kind() == SyntaxKind::ENTRY {
45+
if root.kind() == ENTRY {
4546
for value in root.as_node().unwrap().children_with_tokens() {
46-
if value.kind() == SyntaxKind::VALUE {
47+
if value.kind() == VALUE {
4748
for array in value.as_node().unwrap().children_with_tokens() {
48-
if array.kind() == SyntaxKind::ARRAY {
49+
if array.kind() == ARRAY {
4950
for e in array.as_node().unwrap().children_with_tokens() {
50-
if e.kind() == SyntaxKind::COMMA {
51+
if e.kind() == COMMA {
5152
return e;
5253
}
5354
}
@@ -66,9 +67,9 @@ pub fn make_key(text: &str) -> SyntaxElement {
6667
.clone_for_update()
6768
.children_with_tokens()
6869
{
69-
if root.kind() == SyntaxKind::ENTRY {
70+
if root.kind() == ENTRY {
7071
for value in root.as_node().unwrap().children_with_tokens() {
71-
if value.kind() == SyntaxKind::KEY {
72+
if value.kind() == KEY {
7273
return value;
7374
}
7475
}
@@ -84,7 +85,7 @@ pub fn make_array(key: &str) -> SyntaxElement {
8485
.clone_for_update()
8586
.children_with_tokens()
8687
{
87-
if root.kind() == SyntaxKind::ENTRY {
88+
if root.kind() == ENTRY {
8889
return root;
8990
}
9091
}
@@ -98,13 +99,13 @@ pub fn make_array_entry(key: &str) -> SyntaxElement {
9899
.clone_for_update()
99100
.children_with_tokens()
100101
{
101-
if root.kind() == SyntaxKind::ENTRY {
102+
if root.kind() == ENTRY {
102103
for value in root.as_node().unwrap().children_with_tokens() {
103-
if value.kind() == SyntaxKind::VALUE {
104+
if value.kind() == VALUE {
104105
for array in value.as_node().unwrap().children_with_tokens() {
105-
if array.kind() == SyntaxKind::ARRAY {
106+
if array.kind() == ARRAY {
106107
for e in array.as_node().unwrap().children_with_tokens() {
107-
if e.kind() == SyntaxKind::VALUE {
108+
if e.kind() == VALUE {
108109
return e;
109110
}
110111
}
@@ -124,7 +125,7 @@ pub fn make_entry_of_string(key: &String, value: &String) -> SyntaxElement {
124125
.clone_for_update()
125126
.children_with_tokens()
126127
{
127-
if root.kind() == SyntaxKind::ENTRY {
128+
if root.kind() == ENTRY {
128129
return root;
129130
}
130131
}

rust/src/helpers/string.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use taplo::syntax::SyntaxKind::{IDENT, MULTI_LINE_STRING, MULTI_LINE_STRING_LITERAL, STRING, STRING_LITERAL};
12
use taplo::syntax::{SyntaxElement, SyntaxKind, SyntaxNode};
23

34
use crate::helpers::create::make_string_node;
45

56
pub fn load_text(value: &str, kind: SyntaxKind) -> String {
67
let mut chars = value.chars();
7-
let offset = if [SyntaxKind::STRING, SyntaxKind::STRING_LITERAL].contains(&kind) {
8+
let offset = if [STRING, STRING_LITERAL].contains(&kind) {
89
1
9-
} else if kind == SyntaxKind::IDENT {
10+
} else if kind == IDENT {
1011
0
1112
} else {
1213
3
@@ -18,7 +19,7 @@ pub fn load_text(value: &str, kind: SyntaxKind) -> String {
1819
chars.next_back();
1920
}
2021
let mut res = chars.as_str().to_string();
21-
if kind == SyntaxKind::STRING {
22+
if kind == STRING {
2223
res = res.replace("\\\"", "\"");
2324
}
2425
res
@@ -33,18 +34,11 @@ where
3334
for mut child in entry.children_with_tokens() {
3435
count += 1;
3536
let kind = child.kind();
36-
if [
37-
SyntaxKind::STRING,
38-
SyntaxKind::STRING_LITERAL,
39-
SyntaxKind::MULTI_LINE_STRING,
40-
SyntaxKind::MULTI_LINE_STRING_LITERAL,
41-
]
42-
.contains(&kind)
43-
{
37+
if [STRING, STRING_LITERAL, MULTI_LINE_STRING, MULTI_LINE_STRING_LITERAL].contains(&kind) {
4438
let found_str_value = load_text(child.as_token().unwrap().text(), kind);
4539
let output = transform(found_str_value.as_str());
4640

47-
changed = output != found_str_value || kind != SyntaxKind::STRING;
41+
changed = output != found_str_value || kind != STRING;
4842
if changed {
4943
child = make_string_node(output.as_str());
5044
}

0 commit comments

Comments
 (0)