Menu

#1834 Python Fold Levels

Bug
closed-invalid
nobody
5
2016-09-05
2016-05-28
No

I noticed something different with the Python lexer fold levels. My assumption was that fold levels always increased sequentially. The C++ image and test code show fold levels as I would expect. The Python image and test code show it increasing/decreasing by 4 each time. I've also attached the lua script I used to test with.

If this is intended behavior, is there a programmatic why to handle this?

Tested with SciTE 3.6.6 but also saw weird levels in Scintilla 3.5.6 (which is what Notepad++ is currently using). See here

5 Attachments

Discussion

  • Justin Dailey

    Justin Dailey - 2016-05-28

    Glancing through the lexer it appears that the level is based on how far it is indented...which I guess makes a bit more sense being Python and all.

    I'd still be curious if there is a programmatic way to easily determine when something is the nth level of indentation.

     
  • Neil Hodgson

    Neil Hodgson - 2016-05-29
    • labels: --> scintilla, folding, python
    • status: open --> open-invalid
     
  • Neil Hodgson

    Neil Hodgson - 2016-05-29

    This is behaving as intended.

    To determine the level as you desire would require scanning lines forward while maintaining a stack of open clause indents. This is not needed to implement folding.

     
  • Justin Dailey

    Justin Dailey - 2016-05-29

    Thanks for the info. Are you aware of any other Lexers besides Python that does not have sequentially changing fold levels?

    So assuming a Python file has sane indentation, is it safe to assume something like the following...

    actualLevel = 0
    for each line
        if FoldLevel(currentLine) > FoldLevel(previousLine) actualLevel++;
        else if FoldLevel(currentLine) < FoldLevel(previousLine) actualLevel--;
    

    Thanks

     
    • Neil Hodgson

      Neil Hodgson - 2016-05-29

      Some other folders were copied from Python but you'll have to read the source code to discover which.

      Indentation can decrease by multiple levels between 2 lines. This is why you need to maintain a stack or similar.

      if a:
         if b:
            doit()
      else:
         other()
      
       
      • Justin Dailey

        Justin Dailey - 2016-05-29

        Ah! Yes I see what you are referring to now. Thanks for your help.

         
  • Christophe Meriaux

    Hello @Neil Hodgson , what about this solution :
    On the first indentation found by the lexer, remember the tabulation size (tab, 1 spaces, 2 spaces, 3 spaces ...)
    Then the fold level = the number of space / tab size or the number of tab (in case of tabulation indent)

    Having a true fold level is very convenient to fold automatically evey fold level (feature available in notepad++)

    Thanks

     
    • Neil Hodgson

      Neil Hodgson - 2016-05-30

      That would be inaccurate. Python files may be indented by different amounts than the current tab or indentation size. They may even use different indentation amounts in different parts of the file. If the folding structure does not accurately follow the structure of the code, as it does now, then there is potential for much confusion and bugs.

      This works in Python, although its more common to have all of a class or function that has been pasted in using different indents:

      if a:
              if b:
                doit()
      else:
           other()
      

      The Notepad++ fold level feature could take the tab or indentation size into account if that project considers it safe.

       
  • Neil Hodgson

    Neil Hodgson - 2016-09-05
    • status: open-invalid --> closed-invalid
     

Log in to post a comment.

MongoDB Logo MongoDB