#435 Cannot fold comments in scripts embedded in HTML (PHP)

open
nobody
General (289)
5
2014-11-05
2010-02-23
Andy Brown
No

Version: 0.18.1, git
OS: Ubuntu Karmic

In PHP and other scripts embedded in HTML, multi-line comments cannot be folded.

To reproduce, open a new PHP file and type a multi-line comment inside a PHP block (or just use the one created from the template.) There will be no way to fold the comment, even though there would be with other curly-brace languages like C or Java. Intriguingly, the comment is colored correctly...

Apparently the root of the issue is actually with the Scintilla HTML lexer defined in "scintilla/LexHTML.cxx". As can be seen in the "ColouriseHyperTextDoc" function, the "fold.hypertext.comment" property defaults to 0 so comments are not folded. In fact, the SciTE documentation at http://www.scintilla.org/SciTEDoc.html under "Defined variables in properties files" confirms the intended behavior. While this may be appropriate for SciTE, it doesn't seem appropriate for Geany since the latter is aimed at more serious developers.

Of course, being able to modify the Scintilla source, a simple fix is to just change the default to 1, and doing so does fix the problem. However, that's obviously not a very good fix since new changes to Scintilla would quickly wipe that out.

For that reason, it would be better to set the property within Geany. The Scintilla documentation shows that can be done with the SET_PROPERTY message, which takes the property name and value as arguments. In this case the property name is "fold.hypertext.comment" and the value is "1". (Note that even though the value is a number, it should be passed as a character string.) It seems like the property is then stored in a hash table defined in "PropSet.cxx", which is later referenced by the lexer through the "GetPropertyInt" method in the "include/Accessor.h" interface.

While I'm not an expert at the innards of Geany, I was able to create a working patch. Basically, it defines a new function in "sciwrappers.c" called "sci_set_fold_hypertext_comments" that is then called in the "create_new_sci" function in "editor.c". It may be better to call the function somewhere else only if the lexer is HTML, but this way it is only called once per ScintillaObject. Plus, similar functions are called there.

Also, it may be beneficial to abstract this out further by creating a "sci_set_property" or "sci_set_lexer_property" function that takes two character strings and sends the message appropriately. (It would at least take care of casting the character strings to pointers.) I'm not sure if the developers would really want to advertise that feature so much though, since I could see it possibly conflicting with other functions at some point. (A lot of properties listed in the table duplicate functionality already in Geany not implemented directly through properties.)

Discussion

  • One can (at least nowadays) set the property in the filetypes.php file like this (see the docs for details):

    [lexer_properties]
    fold.hypertext.comment=1
    

    Maybe we should have it on by default? @codebrainz @elextr, others?