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

Commit

Permalink
Fix comments for keys in tables gets moved away (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat authored May 20, 2024
1 parent ee6ab9a commit 24ef03f
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 112 deletions.
14 changes: 14 additions & 0 deletions rust/src/data/ruff-21.expected.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tool.ruff]
lint.select = [
"ALL",
]
lint.ignore = [
# We do not annotate the type of 'self'.
"ANN101",
]
# Do not automatically remove commented out code.
# We comment out code during development, and with VSCode auto-save, this code
# is sometimes annoyingly removed.
lint.unfixable = [
"ERA001",
]
12 changes: 12 additions & 0 deletions rust/src/data/ruff-21.start.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.ruff.lint]
select = ["ALL"]

ignore = [
# We do not annotate the type of 'self'.
"ANN101",
]

# Do not automatically remove commented out code.
# We comment out code during development, and with VSCode auto-save, this code
# is sometimes annoyingly removed.
unfixable = ["ERA001"]
30 changes: 15 additions & 15 deletions rust/src/helpers/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::cell::RefCell;
use std::collections::HashMap;

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

use crate::helpers::create::{make_comma, make_newline};
Expand All @@ -12,9 +13,9 @@ where
F: Fn(&str) -> String,
{
for array in node.children_with_tokens() {
if array.kind() == SyntaxKind::ARRAY {
if array.kind() == ARRAY {
for array_entry in array.as_node().unwrap().children_with_tokens() {
if array_entry.kind() == SyntaxKind::VALUE {
if array_entry.kind() == VALUE {
update_content(array_entry.as_node().unwrap(), transform);
}
}
Expand All @@ -27,7 +28,7 @@ where
F: Fn(&str) -> String,
{
for array in node.children_with_tokens() {
if array.kind() == SyntaxKind::ARRAY {
if array.kind() == ARRAY {
let array_node = array.as_node().unwrap();
let mut value_set = Vec::<Vec<SyntaxElement>>::new();
let entry_set = RefCell::new(Vec::<SyntaxElement>::new());
Expand All @@ -53,13 +54,13 @@ where
if previous_is_value {
// make sure ends with trailing comma
previous_is_value = false;
if entry.kind() != SyntaxKind::COMMA {
if entry.kind() != COMMA {
entry_set.borrow_mut().push(make_comma());
}
}
if previous_is_bracket_open {
// make sure ends with trailing comma
if entry.kind() == SyntaxKind::NEWLINE || entry.kind() == SyntaxKind::WHITESPACE {
if entry.kind() == NEWLINE || entry.kind() == WHITESPACE {
continue;
}
previous_is_bracket_open = false;
Expand All @@ -78,7 +79,7 @@ where
}
entries.push(entry);
}
SyntaxKind::VALUE => {
VALUE => {
if has_value {
entry_set.borrow_mut().push(make_newline());
add_to_value_set(entry_value.clone());
Expand All @@ -88,9 +89,8 @@ where
let mut found_string = false;
for child in value_node.children_with_tokens() {
let kind = child.kind();
if kind == SyntaxKind::STRING {
entry_value =
transform(load_text(child.as_token().unwrap().text(), SyntaxKind::STRING).as_str());
if kind == STRING {
entry_value = transform(load_text(child.as_token().unwrap().text(), STRING).as_str());
found_string = true;
break;
}
Expand All @@ -102,7 +102,7 @@ where
entry_set.borrow_mut().push(entry);
previous_is_value = true;
}
SyntaxKind::NEWLINE => {
NEWLINE => {
entry_set.borrow_mut().push(entry);
if has_value {
add_to_value_set(entry_value.clone());
Expand Down Expand Up @@ -133,7 +133,7 @@ mod tests {
use rstest::rstest;
use taplo::formatter::{format_syntax, Options};
use taplo::parser::parse;
use taplo::syntax::SyntaxKind;
use taplo::syntax::SyntaxKind::{ENTRY, VALUE};

use crate::helpers::array::{sort, transform};
use crate::helpers::pep508::format_requirement;
Expand Down Expand Up @@ -193,9 +193,9 @@ mod tests {
fn test_normalize_requirement(#[case] start: &str, #[case] expected: &str, #[case] keep_full_version: bool) {
let root_ast = parse(start).into_syntax().clone_for_update();
for children in root_ast.children_with_tokens() {
if children.kind() == SyntaxKind::ENTRY {
if children.kind() == ENTRY {
for entry in children.as_node().unwrap().children_with_tokens() {
if entry.kind() == SyntaxKind::VALUE {
if entry.kind() == VALUE {
transform(entry.as_node().unwrap(), &|s| format_requirement(s, keep_full_version));
}
}
Expand Down Expand Up @@ -275,9 +275,9 @@ mod tests {
fn test_order_array(#[case] start: &str, #[case] expected: &str) {
let root_ast = parse(start).into_syntax().clone_for_update();
for children in root_ast.children_with_tokens() {
if children.kind() == SyntaxKind::ENTRY {
if children.kind() == ENTRY {
for entry in children.as_node().unwrap().children_with_tokens() {
if entry.kind() == SyntaxKind::VALUE {
if entry.kind() == VALUE {
sort(entry.as_node().unwrap(), str::to_lowercase);
}
}
Expand Down
35 changes: 18 additions & 17 deletions rust/src/helpers/create.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use taplo::parser::parse;
use taplo::syntax::{SyntaxElement, SyntaxKind};
use taplo::syntax::SyntaxElement;
use taplo::syntax::SyntaxKind::{ARRAY, COMMA, ENTRY, KEY, NEWLINE, STRING, VALUE};

pub fn make_string_node(text: &str) -> SyntaxElement {
let expr = &format!("a = \"{}\"", text.replace('"', "\\\""));
Expand All @@ -10,9 +11,9 @@ pub fn make_string_node(text: &str) -> SyntaxElement {
.unwrap()
.children_with_tokens()
{
if root.kind() == SyntaxKind::VALUE {
if root.kind() == VALUE {
for entries in root.as_node().unwrap().children_with_tokens() {
if entries.kind() == SyntaxKind::STRING {
if entries.kind() == STRING {
return entries;
}
}
Expand All @@ -23,7 +24,7 @@ pub fn make_string_node(text: &str) -> SyntaxElement {

pub fn make_empty_newline() -> SyntaxElement {
for root in parse("\n\n").into_syntax().clone_for_update().children_with_tokens() {
if root.kind() == SyntaxKind::NEWLINE {
if root.kind() == NEWLINE {
return root;
}
}
Expand All @@ -32,7 +33,7 @@ pub fn make_empty_newline() -> SyntaxElement {

pub fn make_newline() -> SyntaxElement {
for root in parse("\n").into_syntax().clone_for_update().children_with_tokens() {
if root.kind() == SyntaxKind::NEWLINE {
if root.kind() == NEWLINE {
return root;
}
}
Expand All @@ -41,13 +42,13 @@ pub fn make_newline() -> SyntaxElement {

pub fn make_comma() -> SyntaxElement {
for root in parse("a=[1,2]").into_syntax().clone_for_update().children_with_tokens() {
if root.kind() == SyntaxKind::ENTRY {
if root.kind() == ENTRY {
for value in root.as_node().unwrap().children_with_tokens() {
if value.kind() == SyntaxKind::VALUE {
if value.kind() == VALUE {
for array in value.as_node().unwrap().children_with_tokens() {
if array.kind() == SyntaxKind::ARRAY {
if array.kind() == ARRAY {
for e in array.as_node().unwrap().children_with_tokens() {
if e.kind() == SyntaxKind::COMMA {
if e.kind() == COMMA {
return e;
}
}
Expand All @@ -66,9 +67,9 @@ pub fn make_key(text: &str) -> SyntaxElement {
.clone_for_update()
.children_with_tokens()
{
if root.kind() == SyntaxKind::ENTRY {
if root.kind() == ENTRY {
for value in root.as_node().unwrap().children_with_tokens() {
if value.kind() == SyntaxKind::KEY {
if value.kind() == KEY {
return value;
}
}
Expand All @@ -84,7 +85,7 @@ pub fn make_array(key: &str) -> SyntaxElement {
.clone_for_update()
.children_with_tokens()
{
if root.kind() == SyntaxKind::ENTRY {
if root.kind() == ENTRY {
return root;
}
}
Expand All @@ -98,13 +99,13 @@ pub fn make_array_entry(key: &str) -> SyntaxElement {
.clone_for_update()
.children_with_tokens()
{
if root.kind() == SyntaxKind::ENTRY {
if root.kind() == ENTRY {
for value in root.as_node().unwrap().children_with_tokens() {
if value.kind() == SyntaxKind::VALUE {
if value.kind() == VALUE {
for array in value.as_node().unwrap().children_with_tokens() {
if array.kind() == SyntaxKind::ARRAY {
if array.kind() == ARRAY {
for e in array.as_node().unwrap().children_with_tokens() {
if e.kind() == SyntaxKind::VALUE {
if e.kind() == VALUE {
return e;
}
}
Expand All @@ -124,7 +125,7 @@ pub fn make_entry_of_string(key: &String, value: &String) -> SyntaxElement {
.clone_for_update()
.children_with_tokens()
{
if root.kind() == SyntaxKind::ENTRY {
if root.kind() == ENTRY {
return root;
}
}
Expand Down
18 changes: 6 additions & 12 deletions rust/src/helpers/string.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use taplo::syntax::SyntaxKind::{IDENT, MULTI_LINE_STRING, MULTI_LINE_STRING_LITERAL, STRING, STRING_LITERAL};
use taplo::syntax::{SyntaxElement, SyntaxKind, SyntaxNode};

use crate::helpers::create::make_string_node;

pub fn load_text(value: &str, kind: SyntaxKind) -> String {
let mut chars = value.chars();
let offset = if [SyntaxKind::STRING, SyntaxKind::STRING_LITERAL].contains(&kind) {
let offset = if [STRING, STRING_LITERAL].contains(&kind) {
1
} else if kind == SyntaxKind::IDENT {
} else if kind == IDENT {
0
} else {
3
Expand All @@ -18,7 +19,7 @@ pub fn load_text(value: &str, kind: SyntaxKind) -> String {
chars.next_back();
}
let mut res = chars.as_str().to_string();
if kind == SyntaxKind::STRING {
if kind == STRING {
res = res.replace("\\\"", "\"");
}
res
Expand All @@ -33,18 +34,11 @@ where
for mut child in entry.children_with_tokens() {
count += 1;
let kind = child.kind();
if [
SyntaxKind::STRING,
SyntaxKind::STRING_LITERAL,
SyntaxKind::MULTI_LINE_STRING,
SyntaxKind::MULTI_LINE_STRING_LITERAL,
]
.contains(&kind)
{
if [STRING, STRING_LITERAL, MULTI_LINE_STRING, MULTI_LINE_STRING_LITERAL].contains(&kind) {
let found_str_value = load_text(child.as_token().unwrap().text(), kind);
let output = transform(found_str_value.as_str());

changed = output != found_str_value || kind != SyntaxKind::STRING;
changed = output != found_str_value || kind != STRING;
if changed {
child = make_string_node(output.as_str());
}
Expand Down
Loading

0 comments on commit 24ef03f

Please sign in to comment.