Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP code folding #3561

Open
hngts opened this issue Sep 24, 2023 · 8 comments
Open

PHP code folding #3561

hngts opened this issue Sep 24, 2023 · 8 comments

Comments

@hngts
Copy link

hngts commented Sep 24, 2023

Why PHP files do have code folding available only for curly braces ? If I inherit direct C style rendering into custom/user filetypes.php, it works, but then, I need to add all the keywords manually, which is not a problem. It works. But then, again, ^$ (variables) are not painted ... unless I put that as well into primary keywords line ... but, then - again - I cannot change the color of the $variable, the same as other words/keywords and language constructs. In C style, no other keywords than primary is taken into account. Is there any workaround for this instead of using/importing C style filedef ?

@elextr
Copy link
Member

elextr commented Sep 25, 2023

PHP folding on {} works for me.

Not surprised C lexer does not work for PHP, its a different language.

@elextr elextr added the can't reproduce A developer couldn't reproduce the issue label Sep 25, 2023
@hngts
Copy link
Author

hngts commented Sep 25, 2023

First things first: Thanks for reply. :)

PHP folding on {} works for me.

Not surprised C lexer does not work for PHP, its a different language.

Yes. Works for me too, and that's default. But the thing is that [] and () are not working with default php lexer, and these should provide fold/unfold markers within gutter as well.

PHP is C-style but is using HTML definitions for styling and keywords (which is, to be sincere, nowadays - wrong), therefore, when I add

[settings]
lexer_filetype=C
tag_parser=C

.. or Python instead of C - into ".config/geany/filedefsfiletypes.php" .. fold/unfold markers do appear for all "pairs", but no color/syntax highlighting.

When I modify [keywords=HTML] into [keywords] and place all php related reserved words into primary index/key, colors are there .. but not $variables .. and when I add ^$ as last primary (as nothing else but primary works) .. I get the color for variables nicely, but it's the same color (expected, "primary") as the rest of the PHP keywords.

On top of that .. with any other C or C-alike lexer that provides working fold points for all "pairs" ( {},() and [] ) .. shell style comments are not reckognized, as well as line comments like this one /// ... or block comments like this one /*: ... */.

Standard // .. and /* ... */ - do work.

So, to summarize .. when I modify original PHP lexer in order to enable fold points just like for lets say .. Javascript files, the best I can get is one color for all PHP related keywords (including $variables) and "limited" coloring for comments.

If You have some spare time, simply open any php file with some populated array/hashmap defined inside. You won't see folding point for that array. Change to Javascript or C or D filetype, and You'll get folding points.

The thing is that PHP in Geany shares HTML lexer and it is very hard to "hack" that in order to behave like it is supposed to. Arrays as well as function parameters that span accross multiple lines. Those are very common thing in PHP and scrolling or navigating via symbols is not that much elegant sometimes, especially when particular source file is not tiny, but has 1000+ lines of code.

@hngts
Copy link
Author

hngts commented Sep 25, 2023

Here are two crops.

The default PHP document fietype.
php_geany_php

The same file/area with manual javascript document type set from the Document menu.
php_geany_javascript

The difference is notable and I believe this should be corrected somehow, but - HOW ?

@elextr
Copy link
Member

elextr commented Sep 25, 2023

To be clear, identification of lexical elements for highlighting and folding is done by pieces of C++ code specific to the source language being analysed. The C lexer is different code to the PHP lexer to the shell lexer etc. The features and facilities that one lexer has is irrelevant to another lexer, so the fact that the C lexer (also used for Javascript) folds on most bracket like constructs has no implication on what the PHP lexer folds on. There is little or no documentation on what the lexers do, but from the code PHP (and embedded JS) only folds on {} and optionally comments, not any other constructs.

The lexers are from the Lexilla project, to change or add features they should be added there and they will be imported into Geany when it is updated to the relevant version.

@elextr elextr added enhancement Lexilla and removed can't reproduce A developer couldn't reproduce the issue labels Sep 25, 2023
@hngts
Copy link
Author

hngts commented Sep 25, 2023

So, there's nothing else that can be done but to use custom filetypes.php and get the partially working results until some Geany version comes out .. with improved/updated syntax for particular file types .. that depends on another .. project.

"Ok". :/

@elextr
Copy link
Member

elextr commented Sep 25, 2023

As a volunteer project with people doing things in their own time Geany would not support more than 40 filetypes as it does without being able to leverage the excellent work of other projects like Lexilla and its associated project Scintilla, and Universal CTAGS.

@hngts
Copy link
Author

hngts commented Sep 25, 2023

Yes .. I know a lots about Geany. There's no better free IDE if You ask me. Been using it since Windows 7. Last Windows I used as my daily driver. Last windows I used in general. Since early announcments of windows 8.0 .. I switched to linux - 100%. Then, I discovered Crunchbang Linux .. and Geany was there. As default editor for everything. I was looking for decent replacement for Notepad++ in those days .. and as soon as I realized how small, fast and configurable Geany is .. I forgot everything about Np++. That is .. Geany is my best co-worker .. for approx. 10yrs. And I don't want to change it. Nuff' said. This issue I have with PHP files .. I am aware of that for a long time already .. noticed .. a long time ago, but I am also aware how things work (partially, to be honnest) and how many smart people are involved in the project .. so I never bragged and complained until yesterday. If this gets fixed, it will get fixed and I am 101% satisfied. Now, I am 99..99% satistfied and .. well, I can (continue to) live like this. ;) Symbols tree is there (much better than the one from Kate or VSCode for that matter), can compensate lack of code folding in a need of jumping and hopping trough code to a particular spot.

@eechen
Copy link

eechen commented May 25, 2024

You need to modify lexilla's code, to get the () and [] code folding support for PHP:
geany-2.0/scintilla/lexilla/lexers/LexHTML.cxx

case eScriptPHP:
	if (
		(state != SCE_HPHP_COMMENT) && 
		(state != SCE_HPHP_COMMENTLINE) && 
		(state != SCE_HJ_COMMENT) && 
		(state != SCE_HJ_COMMENTLINE) && 
		(state != SCE_HJ_COMMENTDOC) && 
		(!isStringState(state))
	) {
		if (ch == '#') {
			Sci_Position j = i + 1;
			while ((j < lengthDoc) && IsASpaceOrTab(styler.SafeGetCharAt(j))) {
				j++;
			}
			if (styler.Match(j, "region") || styler.Match(j, "if")) {
				levelCurrent++;
			} else if (styler.Match(j, "end")) {
				levelCurrent--;
			}
		}
		// Modify here
		//else if ((ch == '{') || (ch == '}') || (foldComment && (ch == '/') && (chNext == '*'))) {
			//levelCurrent += (((ch == '{') || (ch == '/')) ? 1 : -1);
		//}
		else if ((ch == '{') || (ch == '}') || (ch == '[') || (ch == ']') || (ch == '(') || (ch == ')') || (foldComment && (ch == '/') && (chNext == '*'))) {
			levelCurrent += (((ch == '{') || (ch == '[') || (ch == '(') || (ch == '/')) ? 1 : -1);
		}
	} 
	else if (((state == SCE_HPHP_COMMENT) || (state == SCE_HJ_COMMENT || state == SCE_HJ_COMMENTDOC)) && foldComment && (ch == '*') && (chNext == '/')) {
		levelCurrent--;
	}
	break;

You should give a feedback to Scintilla project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants