Lines with only spaces causes chunks to move [Bug]

I was working on implementing a mergeview into my project that shows the difference between two documents. But when i tried one of my testing documents i found a bug that causes chunks to move which results into the editors not being aligned anymore. After testing around what could have caused the bug i found the following.

  1. If there is a line with only spaces in one of the editors that can be merged. This causes the chunk after it to move.
  2. If there are spaces after a line with text it also sometimes happens depending on the amount of spaces.

In both cases this also causes the whitespacing to either change in size or add another whitespacing which is not needed. Another thing is that when merging the chunks that are causing the bug the alignment and whitespacing goes back to normal.

an example of the first scenario is shown here:

As you can see in the screenshot line 881 contains 5 spaces. This causes the chunk on the right editor to shift 2 lines up (line 1249 to line 1268). This then causes the whitespacing to be doubled. Above and below the mergable line.

an example of the second scenario (which may be a bit extreme) is show here:

Before:

After:

As you can see in the screenshots and this is an extreme case is when adding a lot of spaces to the end of line 21 it causes the chunk below it to increase in size.

Is there a fix for this problem or is it possible for it to be fixed?

Can you give an example of two documents (as text, not screenshots) that I can put into a merge view to see the bug happen?

After testing around a bit more i found out that the double spacing is because of something i implemented. But this is caused by something similair to the second scenario i mentioned in my first post, where the chunks move after something is merged or contains spaces.

For the testing i did i used these 2 example documents.

  1. (left document)
<?php 
     public static function &get_setting_data_for_date($settings = array()) {
     $database   =& Database::getInstance();
     $setting    = null;
     
     if(!is_empty($settings['setting']))
         $setting = $settings['setting'];
     elseif(!is_empty($settings['settings']) && !is_empty($settings['date']))
     {
         foreach($settings['settings'] as $setting_for_date)
             if($setting_for_date['start_date'] <= $settings['date'] && ifempty($setting_for_date['end_date'], $settings['date']) >= $settings['date'])
             {
                 $setting = $setting_for_date;
                 
                 break;
             }
     }
 }
  1. (right document)
<?php 
    public static function &get_setting_data_for_date($settings = array()) {
    $database   =& Database::getInstance();
    $setting    = null;

    if(!is_empty($settings['setting']))
        $setting = $settings['setting'];
    elseif(!is_empty($settings['settings']) && !is_empty($settings['date']))
    {
        foreach($settings['settings'] as $setting_for_date)
            if($setting_for_date['start_date'] <= $settings['date'] && ifempty($setting_for_date['end_date'], $settings['date']) >= $settings['date'])
            {
                $setting = $setting_for_date;
                
                break;
            }
    }
    elseif(!is_empty($settings['settings']) && !is_empty($settings['year_week']))
    {
        foreach($settings['settings'] as $setting_for_date)
        {
            if($settings['log'])
            {
                print_v($setting_for_date);
                print_v(strftime('%G%V', strtotime($setting_for_date['start_date'])));
                print_v(strftime('%G%V', strtotime($setting_for_date['end_date'])));
            }
            
            if(strftime('%G%V', strtotime($setting_for_date['start_date'])) <= $settings['year_week'] && (is_empty($setting_for_date['end_date']) || strftime('%G%V', strtotime($setting_for_date['end_date'])) >= $settings['year_week']))
            {
                $setting = $setting_for_date;
                
                break;
            }
        }
    }
}

Note that this is not the entire document but this also showcases the bug.

When putting this in the mergeview the last chunk move down 1 line after merging the line with only spaces. This then causes my implementation to make double spacing.

I’m seeing only a single chunk for those documents, and thus can’t merge the line with spaces separately. Did something go wrong with indentation when pasting those into discord (the first document has one more space of indentation on all but the first line)?

Edit: Okay I think I managed to reproduce this by removing spaces from the first document.

This seems to be a case where the differ comes up with different diffs depending on previous input—initially, it matches the closing brace on line 16 and the spaces before that to the text at the end of the insertion. After the change, it matches it to the identical text before the insertion. Both of these are valid diffs, so I don’t think this is a bug—the library doesn’t guarantee that chunks are stable.

Thanks for the reply. I will try to see if i can fix the problem im facing with my own implementation.