__ini_value: add space detection on insert

This detection tires to somehow inteligent places the new entry at the
end of the section, but tires to not infer with existing comments for
the section etc.

This detection is not perfect but will work in some cases. Hope it helps
(if someone whats to look at these files after they got edited).

This currently only applies to the `present` state, but not to the
`commented` state. This will be changed somehow when both scripts will
be unifed cause of there great similarities.
This commit is contained in:
matze 2021-03-06 22:09:59 +01:00
parent 72c6306ba2
commit 3e67cdbf9b
2 changed files with 66 additions and 11 deletions

View file

@ -1,7 +1,7 @@
BEGIN { BEGIN {
bufindex = -1 bufindex = -1
buflen = 0 buflen = 0
maxbuflen = 2 maxbuflen = 10
# no section means the start to the first section # no section means the start to the first section
if(section == "") { if(section == "") {
@ -15,9 +15,13 @@ function flush_buffer() {
while(buflen > 0) while(buflen > 0)
_pop_line() _pop_line()
} }
function flush_lines(n) {
while(buflen > 0 && n-- > 0)
_pop_line()
}
function push_line() { function push_line() {
linebuf[++bufindex] = $0 linebuf[++bufindex] = $0
buflen++; buflen++
while(buflen > maxbuflen) _pop_line() while(buflen > maxbuflen) _pop_line()
} }
function revert_line() { function revert_line() {
@ -41,8 +45,8 @@ function _pop_line() {
function is_comment(line) { function is_comment(line) {
# get character and check # get character and check
line_sign = substr(line, 1, 1) line_sign = substr(line, 1, 1)
for(i in comment_signs) for(c in comment_signs)
if(line_sign == comment_signs[i]) if(line_sign == comment_signs[c])
return 1 return 1
# nothing found # nothing found
@ -85,6 +89,10 @@ found {print; next}
found=1 found=1
# %codeblock_insert% # %codeblock_insert%
# print line as it would else only be populated below
print
next
} }
is_curr_section = 0 is_curr_section = 0
} }
@ -101,7 +109,7 @@ found {print; next}
# check for a delimiter and a key (must be at first position due to trimming) # check for a delimiter and a key (must be at first position due to trimming)
if((idel = index(line, delimiter)) && (ikey = index(line, key)) == 1) { if((idel = index(line, delimiter)) && (ikey = index(line, key)) == 1) {
# check there are only spaces between the delimiter # check there are only spaces between the key and delimiter
if(check_spaces(substr(line, ikey + length(key), idel - (length(key) + 1)))) { if(check_spaces(substr(line, ikey + length(key), idel - (length(key) + 1)))) {
found = 1 found = 1

View file

@ -1,9 +1,56 @@
was_com_there = was_comment(lastline(), comment) # check if there is a comment block before the section
firstline_index = bufindex - (buflen - 1)
insertpoint = -1 # the insertpoint marks the point before the insert
lastfreespace = -1
for(i = bufindex; i >= firstline_index; i--) {
_line = trim(linebuf[i])
if(_line == "") {
lastfreespace = i
continue
}
if(comment && was_comment(_line, comment)) {
insertpoint = i + 1
no_insert_comment = 1
if(lastfreespace != insertpoint)
insert_line_after = 1
break
}
if(!is_comment(_line) || index(_line, delimiter) > 0) {
insertpoint = i + 1
# only insert a line before if we do not have a space around
if(lastfreespace == insertpoint)
insertpoint++
else
insert_line_before = 1
# check for empty line after the insert point
# use absolute boundary cause the insertpoint can be changed
if(trim(linebuf[i + 2]) != "")
insert_line_after = 1
break
}
}
# insert into the last free space
if(insertpoint == -1) {
if(lastfreespace != -1) {
insertpoint = lastfreespace
insert_line_before = 1
}
else {
insertpoint = firstline_index
insert_line_after = 1
}
}
# print lines before
flush_lines(insertpoint - firstline_index)
# print before and comment # print before and comment
flush_buffer() if(insert_line_before) print ""
if(comment && !was_com_there) c_print() if(comment && !no_insert_comment) c_print()
# print value + seperator line at the end
v_print() v_print()
print "" if(insert_line_after) print ""
flush_buffer()