How to make CKeditor format code indenting elements and adding line breaks?
Finally I found a solution, thanks to the post CKEditor with well-formatted html source code.

I just added to a custom module the following code:

function mymodule_editor_js_settings_alter(array &amp;$settings) {
  foreach (array_keys($settings['editor']['formats']) as $text_format_id) {
    if ($settings['editor']['formats'][$text_format_id]['editor'] === 'ckeditor') {          
      $settings['editor']['formats'][$text_format_id]['editorSettings']['customConfig'] = base_path() . drupal_get_path('module', 'mymodule') . '/ckeditor_config.js';           
    }
  }        
}


to include a configuration a file called ckeditor_config.js that contains the code:

CKEDITOR.on('instanceReady', function(ev) {
    // Sets indentation of source code
    ev.editor.dataProcessor.writer.setRules( 'div', {
                indent: true,
                breakBeforeOpen: true,
                breakAfterOpen: true,
                breakBeforeClose: true,
                breakAfterClose: true
            });
});


Doing this, CKEditor will keep the indentations in the source code, and a well formatted code easy to read.