-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Fix append() with negative line numbers. #3321
Conversation
Failing some tests, gonna take a look later |
ac7ec36
to
8467dc0
Compare
Mmmmm, ok this works. It's not as pretty as the fix I had in mind but I think this part should be rewritten anyway (remove include_start, include_end, etc...). This is just temporary so python plugins work the same in Vim and neovim. |
@choco Thanks. It doesn't matter to me one way or other, but this feature was added to the API in #2846. So, if it's an actually desirable feature, perhaps the API should have this feature, but the specific behavior of On the other hand if this is a niche feature, the inconsistency may not be worth it. I need @nhynes to comment here. @fwalch @bfredl @tarruda also need your opinion. |
I completely understand, I was actually discussing in the vim-plug issue junegunn/vim-plug#278 what the best way was to get around the problem. In the end I though it was better to let you decide :) Fixing it on python side would require adding a request for the lines number, not that it matters from a performance point of view but maybe it's something to consider. Anyway the second part of the fix is needed because the change I made yesterday breaks append() without arguments in neovim. I didn't notice at first since the append function isn't covered by any test. So if you decide in the end to maintain the current behavior I'll edit the commit and remove the bound checking hack. |
Oh. Someone should write a test for append()... |
@choco Although the command you're trying to issue is semantically valid, there's the underlying problem of not being able to lock a buffer and prevent the line length from changing between the time you fetch it and the time you make the append. Since On the other hand, it would certainly be nice to be consistent with |
8467dc0
to
45db2eb
Compare
@nhynes Hi, thank you so much for your comment, I just have a couple of questions if you don't mind. Why is calling buffer_line_count here prone to race conditions? Aren't we simply getting the line count from the buffer directly here, in the same API request as buffer_insert? |
7879b70
to
7dc61a6
Compare
@choco Consider the following scenario:
To clarify, there's no issue with calling |
@@ -467,7 +467,11 @@ void buffer_insert(Buffer buffer, | |||
ArrayOf(String) lines, | |||
Error *err) | |||
{ | |||
buffer_set_line_slice(buffer, lnum, lnum, true, false, lines, err); | |||
linenr_T nlines = buffer_line_count(buffer, err); | |||
if (lnum == nlines) lnum = -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code standard requires using
if (lnum == nlines) {
lnum = -1
}
: http://neovim.io/develop/style-guide.xml?showone=Conditionals#Conditionals.
@nhynes That makes complete sense, I wasn't thinking about multiple clients for some reason (fell ashamed...) |
7dc61a6
to
c6daa85
Compare
lnum = -1; | ||
} | ||
|
||
bool end_start = lnum < 0 ? true : false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, this is just as clear bool end_start = lnum < 0;
If you disagree that's fine; I don't want to waste time bikeshedding ;)
c6daa85
to
bba8801
Compare
@@ -467,7 +467,13 @@ void buffer_insert(Buffer buffer, | |||
ArrayOf(String) lines, | |||
Error *err) | |||
{ | |||
buffer_set_line_slice(buffer, lnum, lnum, true, false, lines, err); | |||
linenr_T nlines = buffer_line_count(buffer, err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Integer
here, not linenr_T
. Otherwise you will need one (here) or two (here and in condition) casts to pass travis tests. Also note that buffer_set_line_slice
accepts Integer as well, though I think it will not complain because sizeof(Integer) >= sizeof(linenr_T)
always.
And I think that you need to abort if buffer_line_count
returned zero (== aborted with error, but checking for err->set
is rather uncommon for some reason).
938d2d3
to
741ea76
Compare
@nhynes / @choco there's a way to "lock" nvim for multiple operations by one client: Make nvim call the client via msgpack-rpc and only complete the call when all client operations are complete. It is not very polished but something like this should work:
@choco this means it may be possible to implement this 100% in the python side. When making compatibility changes in the python interface, I prefer if they are made in the compatibility layer so it doesn't affect clients in other languages. In this particular case, it is necessary to add the python client infrastructure to send atomic "transactions" to nvim |
We want to switch include_start/end when the index is positive or negative.
741ea76
to
8ab0908
Compare
@tarruda I'm not sure I fully understand, do you mean introducing a new method in the api that allows us to lock vim into executing request from only one client until the client release that lock, i.e.:
or simply implementing this lock and keeping it private and using it in some method to perform multiple atomic operation exposed by the api?
I still think it makes sense to do it, but it doesn't solve the problem completely. I modified the commit to just fix the regression introduced with #3315 and will leave other changes to another pull request when I have more time to work on it. |
Fix append() with negative line numbers.
In Vim bound checking is performed from 0 to buffer.linecount, and
returns error if trying to operate outside these bounds. Neovim allows to
use negative indexes but excludes buffer.linecount making its
implementation incompatible with Vim's one.
To see the issue in action
This fixes the incompatibility also maintaining the ability to index using
negative numbers (that would otherwise break python-client
implementation).
This also fixes junegunn/vim-plug#278