Sublime Forum

Code folding inconsistencies between ST2 and ST3

#1

I just recently got a new laptop and decided to upgrade to ST3. I have found some unusual behavior with how ST3 handles code folding. Many PHP classes that I can successfully code fold in ST2 do not fold properly in ST3. I generally use the command ctrl+k, ctrl+2 to fold all of my methods contained within a class file. I have been using this technique forever in ST2. Now, the same commands on the exact same files in ST3 do not produce the same results. In ST3, ctrl+k, ctrl+2 often behaves as though I chose to fold all (ctrl+k, ctrl+1). Oddly enough, the other fold levels (ctrl+k, ctrl+3…and so on) seem to work as expected in ST3 just like they did in ST2. Note that this is not all classes, just many. There are also many files that still fold just like they did in ST2. Any ideas what could be causing this inconsistent code folding behavior? I suspect some bug that was introduced in ST3 code folding.

0 Likes

#2

Code folding depends on syntax definitions to work and those were rewritten in ST3. You might want to check if that bug is reported on https://github.com/sublimehq/Packages and do that if not. You would need to provide sample code that reproduces.

0 Likes

#3

actually, code folding is based on indentation only - although there is a feature request to have it based on syntax:

I suggest OP to check indentation settings - maybe ST2 detects a different indentation size than ST3?

0 Likes

#4

@rchl I’m currently using the native code folding functionality of ST3 (as opposed to an added code folding package). I’m running Windows 10 Pro 64-bit. There is no issue with using the folding triangles in the gutter or the corresponding keyboard shortcut ctrl+shft+[ and ctrl+shft+]. My issue is specifically with Edit > Code Folding > Fold Level 2 (ctrl+k, ctrl+2). The thing that strikes me is that the same file folds just fine in ST2. I suppose it’s possible that the folding was completely rewritten and just simply does not work the same in ST3. But so much of ST3 seems like it’s basically the same as ST2, so I sort of think the code folding should be expected work the same out of the box (at least, I think that’s the intended behavior). Here are two classes for examples. Both fold as expected in ST2. Class ‘MY_Email’ (see below) is one that is NOT folding correctly in ST3. Class ‘Image’ (see below) IS folding as expected in ST3. They are both PHP classes (saved as individual files) with tab size 4 and UTF-8 encoding, and ST3 is properly identifying these traits. Any ideas are greatly appreciated as I use this functionality all day everyday and really need it to work. If it matters, I have a registered version of ST3.

This class folds as expected in ST2, but incorrectly in ST3:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Copyright (c) 2015, Eye Scream Design
 *
 * Custom Methods for use with Everjewel project.
 */


class MY_Email extends CI_Email {

    /**
     * The CodeIgniter object variable
     * @access public
     * @var object
     */
    public $CI;

    /**
     * Variable for email template slug
     * @access public
     * @var string
     */
    public $email_template_slug;
    
    /**
     * Additional data required to populate selected template
     * @access protected
     * @var array
     */
    protected $additional_template_data = array();

    /**
     * Array of data containing values for user-entered keys
     * @access protected
     * @var array
     */
    protected $inline_variable_data;

    /**
     * Constructor
     *
     * @param array $config
     * @return void
     */
    public function __construct($config = array())
    {
        parent::__construct($config);
        
        $this->inline_variable_data = array();
        $this->CI =& get_instance();
        
        $this->CI->load->config('email');
        
        $this->from($this->CI->config->item('email_from_address'),$this->CI->config->item('email_from_name'));
    }
    
    /**
     * set email template for use when sending emails
     *
     * @param string $slug template slug name
     * @return bool success indicator
     */
    public function template_name($slug) {
        if(isset($slug) && is_string($slug)) {
            $this->email_template_slug = $slug;
        }
        return false;
    }
    
    /**
     * Set email template variables
     *
     * @param array $tempate_vars
     * @return null
     */
    public function template_vars($template_vars = array()) {
        if(is_array($template_vars)) {
            $this->additional_template_data = $template_vars;
        }
    }
    
    /**
     * Set inline variables
     *
     * @param array $inline_vars
     * @return null
     */
    public function inline_vars($inline_vars = array()) {
        if($this->CI->aauth->is_loggedin()) {
            $user = $this->CI->aauth->get_user(false,true);
            $this->inline_variable_data = array(
                'first_name' => $user->first_name,
                'last_name' => $user->last_name,
                'email' => $user->email
            );
        }
        if(is_array($inline_vars)) {
            $this->inline_variable_data = array_merge($this->inline_variable_data, $inline_vars);
        }
    }
    
    // --------------------------------------------------------------------

    /**
     * Send Email
     *
     * @param    bool    $auto_clear = TRUE
     * @return    bool
     */
    public function send($auto_clear=true) {
        if(isset($this->email_template_slug) && is_string($this->email_template_slug)) {
            // retrieve email template
            $read_db = $this->CI->load->database('read', TRUE);
            
            // grab template
            $query = $read_db->select('subject, content')->where('slug',$this->email_template_slug)->get('email_content');
            $template = $query->row();
            if(isset($template)) {
                $this->subject($template->subject);
                
                // set key parameters
                $template_data = array(
                    'base_url' => base_url(),
                    'template' => $template
                );
                $template_data = array_merge($template_data, $this->additional_template_data);
                $template_data['email_sub_template'] = $this->CI->mustache->view($this->CI->config->item('email_template_base_folder').$this->email_template_slug, $template_data, TRUE);
                $master_template = $this->CI->config->item('email_template_base_folder').$this->CI->config->item('email_template_master');
                
                // merge template with master template
                $whole_email = $this->CI->mustache->view($master_template, $template_data, TRUE);
                
                // replace user variables.
                foreach($this->inline_variable_data as $search => $replace){
                    $whole_email = preg_replace('/\['.$search.'\]/i', $replace, $whole_email);
                }
                
                
                // Set mail type to html
                $this->set_mailtype("html");
                $this->message($whole_email);
            }
        }
        return parent::send($auto_clear);
    }
    
    /**
     * Initialize the Email Data
     *
     * @param    bool
     * @return    CI_Email
     */
    public function clear($clear_attachments = FALSE)
    {
        $this->CI =& get_instance();
        $this->CI->load->config('email');
        
        $this->email_template_slug = null;
        $this->additional_template_data = array();
        $this->inline_variable_data = array();
        $success = parent::clear($clear_attachments);        
        $this->from($this->CI->config->item('email_from_address'),$this->CI->config->item('email_from_name'));
        return $success;
        
    }

}

This class folds as expected in both ST2 and ST3:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Image model
 *
 */

class Image extends CI_Model {


    /**
     * Get Image object
     * 
     * @param Int $image_id DB ID number
     * @return MIXED Image object if successful, FALSE if unsuccessful
     */
    public function get_image($image_id)
    {
        if(isset($image_id) ) {
            $query = $this->db->where('id',$image_id)->limit(1)->get('image');
            $image = $query->row();
            if(isset($image)) {            
                return $image;
            }
            else {
                return FALSE;
            }
        }
        else {
            return FALSE;
        }

    }

    /**
     * Insert Image
     * 
     * @param String $file_name
     * @param String $mime
     * @param Int $size
     * @return MIXED Image ID if successful, FALSE if unsuccessful
     */
    public function insert_image($file_name, $mime, $size)
    {
        $img_data = array(
            'file' => $file_name,
            'mime' => $mime,
            'size' => $size
        );
        if($this->db->insert('image', $img_data)){
            return $this->db->insert_id();
        }
        return false;
    }

}
0 Likes

#5

others have reported the same:

0 Likes

#6

Good news: I have found a work around for this issue. I installed a package called Trimmer and ran the “Collapse lines” process. My problem class now folds as expected when using the various Fold Level options (particularly Fold Level 2). I have run the same process on a couple of other files that ST3 was having code folding issues with and it appears to have worked in those cases as well.

I submit that this should provide some indication as to what’s going on differently with ST2 vs ST3 code folding. Also, I do not feel ST3 is behaving as intended (i.e. I think one could reasonably argue this is a bug in ST3). I would also submit that it should provide a fairly clear path for patching the ST3 code folding behavior (so that it behaves as it did in ST2).

0 Likes

#7

just edit the Packages/Default/fold.py file

0 Likes

#8

Forgive me for my ignorance, but I do not understand your instructions. I do not have a ‘fold.py’ file on my local machine. Keep in mind I’m running Windows if that makes a difference.

0 Likes

#9
  1. install PackageResourceViewer
  2. open Command Palette
  3. Type PRV: O
  4. Select PackageResourceViewer: Open Resource
  5. Default
  6. fold.py
0 Likes

#10

I’ve checked ‘fold.py’ and verified it has the same code as ST2. This appears to be ST3 source code that you’ve directed me to. I certainly could customize that code, but I’m not sure that’s the appropriate thing to do in this case.

The ST website directs me to this Technical Support forum when something is not working right. Obviously some issues might be due to settings or other user addressable items, or perhaps sometimes a user wants some custom behavior. But in this case, it seems to be more of an “under the hood” type issue. Who determines if an issue discussed here gets converted to a bug fix? Really, I’m just wondering if this issue will be seen and/or addressed by the developer(s). It seems like a bug to me.

0 Likes

#11

The devs (@jps and @wbond) do read the forums, but your best bet is to report the defect on the Issue Tracker to make sure that it doesn’t get lost in the shuffle.

After checking that there isn’t already such a report, of course. :wink:

0 Likes

#12

Thanks for all the feedback. I reported this issue:

1 Like