Python code collapsing doesn't exist?

Syntax highlighting, code folding, brace matching, code indenting, and function list

Python code collapsing doesn't exist?

Postby JohnJSal » Wed Jun 14, 2006 6:53 pm

Hi everyone. I'm asking specifically about Python, if anyone is familiar with it.

Anyway, it seems that code collapsing uses the indent/unindent characters from the syntax file, and Python doesn't have an unindent character because it doesn't use braces to enclose code blocks. Is there still a way to use code folding with this type of language?

Thanks.
User avatar
JohnJSal
Advanced User
Advanced User
 
Posts: 60
Joined: Sat Mar 05, 2005 12:00 am

Re: Python code collapsing doesn't exist?

Postby Mofi » Thu Jun 15, 2006 1:05 pm

The code folding engine uses the

/Open Fold Strings = "xxx" "yyy"
/Close Fold Strings = "zzz" "sss"

specifications in the wordfile. If your language definition for Pyhton does not have such definitions, you have to wait for an other UE/UES user writing Pyhton which posts his definitions or you create it by yourself. See help of UE/UES about "Syntax Highlighting" or Syntax.txt in the program directory of UE/UES for more infos about the code folding definitions in the wordfile. Since v12 of UE and v5.50 of UES more code folding definitions in the wordfile are supported.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Python code collapsing doesn't exist?

Postby JohnJSal » Thu Jun 15, 2006 1:14 pm

Thanks. I did a little reading about it but I'm still stuck. I guess the general question I'm asking is, is it possible to implement code folding with a language that does not use explicit delimiter characters like braces?

Example in Python:

Code: Select all
def func(param):
    do_something()
    do_something_else()

So that's the function block, essentially delimited by whitespace (indentation). It seems the "opening" of the block could be the 'def' keyword, but there is no closing.

Thanks.
User avatar
JohnJSal
Advanced User
Advanced User
 
Posts: 60
Joined: Sat Mar 05, 2005 12:00 am

Re: Python code collapsing doesn't exist?

Postby Mofi » Thu Jun 15, 2006 2:44 pm

Yes, you are on the right way. Use

/Open Fold Strings = "def"
/Close Fold Strings = "def"

Identical open and close fold strings are possible. Only the last function in the file is not recognized for code folding because there is no close fold string. I use this method also for some of my files where the definition of a new block is also the marker for the end of the block above. Another example for identical open and close fold strings is my syntax highlighting definition for INI files.

Note: tab in line starting with /Delimiters must be replaced by a horizontal tab character.

/L20"Settings Files" Nocase Line Comment = ; Line Comment Preceding Chars = [~0-9A-Za-z_] String Chars = " DisableMLS File Extensions = INF INI PRJ
/Delimiters = ! "tab%&'()*+,-./:;<=>?@[\]^{|}~
/Function String = "%^[^(*^)^]"
/Marker Characters = "[]%%"
/Open Fold Strings = "["
/Close Fold Strings = "["
/C1"Group name"
[]
/C2"Separators"
,
-
=
/C3"Variables"
%%
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Python code collapsing doesn't exist?

Postby JohnJSal » Thu Jun 15, 2006 2:53 pm

Mofi wrote:Identical open and close fold strings are possible. Only the last function in the file is not recognized for code folding because there is no close fold string.

Hmm, so does this mean that everything between one 'def' and another will get folded, even if it's not in the block? Example:

Code: Select all
def func():
    something()
    more()


class MyClass:

    def func2():
        yet_more()

So there I have a function definition followed by a new class definition. Would the line 'class MyClass:' get folded up too? I guess there's no way to avoid this?
User avatar
JohnJSal
Advanced User
Advanced User
 
Posts: 60
Joined: Sat Mar 05, 2005 12:00 am

Re: Python code collapsing doesn't exist?

Postby Mofi » Thu Jun 15, 2006 3:28 pm

You have to play a little with the fold strings as I'm forced also to do. The problem is, I don't know anything about Pyhton. For your example above following works:

/Open Fold Strings = "def" "class"
/Close Fold Strings = "class" "def"

or

/Open Fold Strings = "def"
/Close Fold Strings = "class" "def"

It is not necessary to restart UE/UES after editing the wordfile or close the wordfile after every save. The changes become automatically active after save of the wordfile. So open a Pyhton file and the wordfile and play with the definitions.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Python code collapsing doesn't exist?

Postby JohnJSal » Thu Jun 15, 2006 3:33 pm

Ok, thanks for the help. If I see that there aren't too many variations, then I can do what you suggest. But it seems like there could many different ways of writing the code, so I'll just have to keep an eye on how it goes and test it.

As it turns out, Python *does* have specific tokens for delimiting blocks, called INDENT and DEDENT. Can UE accept anything other than strings when specifying the code folding characters?
User avatar
JohnJSal
Advanced User
Advanced User
 
Posts: 60
Joined: Sat Mar 05, 2005 12:00 am

Re: Python code collapsing doesn't exist?

Postby Mofi » Thu Jun 15, 2006 4:59 pm

JohnJSal wrote:Can UE accept anything other than strings when specifying the code folding characters?

No! Only strings can be specified. Tabs or line terminations (unfortunately) cannot be specified.

Python has really a crazy syntax. I never heard about a computer language where the level of indentation determines to which code level the current line belongs to, if a have understood the explanation at http://docs.python.org/ref/indentation.html correct. Python programmers must be really carefully working people because 1 tab or space too much or less and the code is not working anymore as expected or produces errors while parsing.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Python code collapsing doesn't exist?

Postby JohnJSal » Thu Jun 15, 2006 5:02 pm

Heh heh, yeah, it's definitely different, but it's also nice because it allows the code to be more concise without the enclosing braces, etc. It also forces code to look basically the same, which helps readability.

And as for being careful, it actually doesn't happen often that you mess up the identation, unless you're the type of programmer who is used to putting bits of code everywhere in no special order! :)
User avatar
JohnJSal
Advanced User
Advanced User
 
Posts: 60
Joined: Sat Mar 05, 2005 12:00 am

Re: Python code collapsing doesn't exist?

Postby Mofi » Thu Jun 07, 2012 6:55 am

Starting with UltraEdit v18.10 code folding by indent level is supported also by UltraEdit which might make happy many Python code writers.

Code folding by indent level is automatically used for *.py files. Being more precise for any wordfile containing file extension PY the language marker PYTHON_LANG is set internally in UltraEdit even if this language marker is missing in the wordfile. Code folding by indent is enabled therefore automatically for all files with a file extension listed in a wordfile containing also either file extension PY or the language marker PYTHON_LANG.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Syntax Highlighting