fix(chat): add missing write flag attribute when editing files #966
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Previously, the return values of
gsubwere passed top:write. This is incorrect becausegsubreturns two values: the changed data and the number of substitutions made. Since the method signature forwriteiswrite(txt, flag, mode), the number of substitutions returned bygsubwas incorrectly passed as the flag attribute. This could lead to incorrect behavior.Passing
"w"explicitly means that the file will be opened for writing, and if it has any content, it will be truncated.I also modified the test so that it would fail with the previous implementation. The reason for the failure is that when invoking
writewith the changed text and a flag of 1 (1 is the number of substitutions returned by gsub, and by accident it also corresponds to the O_WRONLY flag), the file is not truncated. Since the substitution text is shorter than the original file content, some leftovers remain in the file.P.S. I've read the contributing guide but decided not to start a discussion since it seems like an obvious oversight. I hope you don't mind. :)
Checklist
make docsto update the vimdoc pages