Open jcote-tc opened 1 year ago
It's hard for such level of style-preserving. The parser couldn't know what table the comments should adhere to.
Would it be an option to add the comment inside the [[routes]]
sections/entries?
Would it be an option to add the comment inside the
[[routes]]
sections/entries?
I am not in control of the original file - but all comments are on top of the sections so it makes it easier to handle that use case. I wrote a python function to handle that scenario. Basically I find the index/location where I need to insert my new Table, check if the line above is a Comment and if so I do -1 to the index, which preserves the location of the commented line:
def search_for_insertion_point_index(parsed_toml):
key_counter = 0
stored_keys = []
for k in parsed_toml.body:
stored_keys.append(k)
key_counter += 1
if isinstance(k[1], tomlkit.items.Table):
# insert new table before the first found tomlkit.items.Table
# check if the last line is a comment
if isinstance(stored_keys[parsed_toml.body.index(k) - 1][1], tomlkit.items.Comment):
#print("Comment found on the line before. Return index minus 1!")
return parsed_toml.body.index(k) - 1
#print(parsed_toml.body.index(k))
return parsed_toml.body.index(k)
if isinstance(k[1], tomlkit.items.AoT):
# insert new table before the first found tomlkit.items.AoT
# check if the line before is a comment
if isinstance(stored_keys[parsed_toml.body.index(k) - 1][1], tomlkit.items.Comment):
#print("Comment found on the line before. Return index minus 1!")
return parsed_toml.body.index(k) - 1
#print(parsed_toml.body.index(k))
return parsed_toml.body.index(k)
Hello,
I am using the tomlkit Python library to modify an existing TOML file.
The original file looks like this:
I add two tables to that original array of tables "[[routes]]", with method = 'POST' and path = '/my/post/path'. When I do that, the resulting file becomes:
The comment ends up in the wrong spot. What can I do to make sure it stays on top of my second "[[routes]]" such as:
This is an extract of the code I use to add my 2 tables:
Thank you for your assistance.