I made a slight change to the on_modified this morning that should handle multiple cursors:
def on_modified (self, view):
#get all of the cursors (this only works with one)
updatedRegions = view.sel()
#get the current size of the text inside the active buffer
newBufferSize = view.size()
#get the id of the buffer for a multiple file project
bufferId = view.buffer_id();
#if we are tracking this view
if bufferId in self.allBufferSizes and bufferId in self.allInsertEventsByFile:
#holds the number of characters that were either inserted or deleted
numCharsChanged = abs(newBufferSize - self.allBufferSizes[bufferId])
#MM- change here!!
#if all of the changes are exactly the same length we can handle
#them all
numCharsChanged = numCharsChanged / len(updatedRegions)
#go through each of the regions (usually only one- multiple regions if
#there are multiple cursors)
for region in updatedRegions:
#if the current buffer size is exactly the same as the last edit
if(newBufferSize == self.allBufferSizes[bufferId]):
#I'm not sure when this happens!!!
print("Size the same-- When does this happen????")
#new buffer size is smaller than the old buffer size, this must be a delete
elif(newBufferSize < self.allBufferSizes[bufferId]):
#remove some data from the shadow container
self.removeEvent(numCharsChanged, region.a, self.allInsertEventsByFile[bufferId], view)
#buffer is larger, must be an insert
else:
self.insertEvent(numCharsChanged, region.a, self.allInsertEventsByFile[bufferId], view)
#store the new buffer size for next time
self.allBufferSizes[bufferId] = newBufferSize
This works only if all of the changes being added are exactly the same length (like in a multi-cursor situation). I take the difference in buffer size and divide by the number of cursors.Three instances of inserting the word ‘cat’ would have a difference of 9 characters. Divide by the three cursors and each cursor contributes 3 characters. Send the position of each cursor to the insert/delete function and it seems to work.
This will not work if unequal sized chunks of text are ever entered at multiple points by the tool. I’m not sure if that ever happens in sublime. Some other editors are more transparent about which changes are occurring.
I also checked and found that find and replace text of exactly the same length does not work… still thinking about how I might solve that problem.
Yes, if I end up going with sublime I will put it on github soon. I am still checking out a few other editors.