Code Folding - single-line vs multi-line "Type".

Discuss and announce AkelPad plugins
Post Reply
  • Author
  • Message
Offline
Posts: 32
Joined: Thu Oct 15, 2015 2:20 am

Code Folding - single-line vs multi-line "Type".

Post by CBruce »

A problem with handling certain statements in Code-Folding when the same initial
keyword has both *single-line* and *multi-line-block* variants.

:
===========================
_____ !!! SOLVED !!! _____
===========================
I had to make use of the regex (?-m) option to turn off multi-line search for the single-line "Type" variations.

Code: Select all

;These rules go in the "Folds:" section of the freebasic.coder file.
;
;Type (Alias): *Single-Line*
	;With "Deny Fold" - (Used in Production):
	;The folds will *not* display... (but neither will "Color text", so you cannot see which rules were used to capture the text).
	;3409920=2048+262144+1048576+2097152
3409920	0	${REG}	0	"^(?-m)(?i)\s*(type)\b([^\n]*?)(as)\b"	"$"	" 	"		0	0
	;
	;With-out "Deny Fold" - (Used when Troubleshooting):
	;The folds *will* display... (and you can use different "Color text" per rule to see which rules were used to capture the text).
	;3407872=262144+1048576+2097152
	;3407872	0	${REG}	0	"^(?-m)(?i)\s*(type)\b([^\n]*?)(as)\b"	"$"	" 	"		0	0
	;
;Type (Block): *Multi-Line*
	;40=8+32
40	0	0	0	"type"		"end type"	" 	"	0	0
===========================
===========================
:

FreeBASIC has two (2) "Type" keywords:

Code: Select all

  A. *multi-line* "Type ... End Type" block.
	Type some__TYPE
	  ' comment #1
	  ' comment #2 ... something with the word AS in it
	  m As ulongint = 2762781
	  s As ulongint = 0
	  i As ulongint = 0
	  r As ubyte    = 13
	End Type
  B. *single-line* "Type Alias" with three (3) variations.
     1. Type some_alias_name As Function (As Integer, As Integer) As Integer
     2. Type As Function (As Integer, As Integer) As Integer some_ALIAS_name
     3. Type some__TYPE As some_ALIAS_name
I can handle the #1 and #2 "Type ... As Function" aliases with this basic "Folds:" rule:

Code: Select all

	;Type (Alias): *Single-Line*
	;262152=8+262144
	262152	0	0	0	"type"		"as function"	" 	"	0	0
- Catches the #1 and #2 Aliases and successfully eliminates them as fold points.

And I can handle the standard "Type ... End Type" block with this basic "Folds:" rule:

Code: Select all

	;Type (Block): *Multi-Line*
	;40=8+32
	40	0	0	0	"type"		"end type"	" 	"	0	0
But I cannot handle the folding for "Type ... End Type" block and eliminate folding
the #3 "Type mytype As mytype_alias" at the same time.

I'm sure it is going to need regex(s) to work, but so far... just failures.

The #3 "Type mytype As mytype_alias" only has two (2) keywords to work with...
"TYPE" and "AS". So I think a regex is going to have to function something like this
pseudo-regex.

Code: Select all

	^(type)(any characters except a \n)(\s)(as)(\s)(any characters)$
I have an AkelPad search regex that successfully finds the "type ... as " occurrences,
but that rule does not seem to work correctly as a "Folds:" regex.

Code: Select all

	"^\s*(type)\s+[^\n]+? (as) "
Here is most of the garbage I have been trying:

Note:
(While testing, I usually set the fold captures to different colors so that it
is easy to see what is getting captured).

Code: Select all

;
;Type (Alias): *Single-Line*
	;262152=8+262144
262152	0	${ERR}	0	"type"		"as function"	" 	"	0	0
262152	0	${REG}	0	"type"		"as"	" 	\n"	0	0
	;2099208=8+2048+2097152
	;2097160=8+2097152
	;2097160	0	${ERR}	0	"type"	"^[ \t]*[a-z0-9_]*? as "   " 	"		0	0
	;2099208	0	${ERR}	0	"type"	"(^[ \t]*?type [a-z0-9_]*?)? as "   " 	"		0	0
	;2099208	0	${ERR}	0	"type"	"(^[ \t]*type [a-z0-9_]*)? as "   " 	"		0	0
	;1050632=8+2048+1048576
	;3147784=8+2048+1048576+2097152
	;3409928=8+2048+262144+1048576+2097152
	;1310720=262144+1048576
	;2359304=8+262144+2097152
	;3407872=262144+1048576+2097152
	;3409920=2048+262144+1048576+2097152
	;
;2359304	0	${REG}	0	"type"	"[^\n]*?\b(as)\b"	" 	"		0	0
	;262152		0	${REG}	0	"type"	" as "	" 	"		0	0
	;1310720	0	${REG}	0	"^\s*(type)\s+[^\n]++"	"as"	" 	"		0	0
	;3407872	0	${REG}	0	"^\s*(type)\s+[^\n]+?\b(as)\b"	"\n"	" 	"		0	0
	;;;3407872	0	${REG}	0	"^[ \t]*(type)[ \t]*\b([a-z0-9_]+)\b[ \t]*(as)\b"	"$"	" 	"		0	0
	;3409920	0	${REG}	0	"^[ \t]*(type)[ \t]*\b([a-z0-9_]+)\b[ \t]*(as)\b"	"\n"	" 	"		0	0
	;3407872	0	${REG}	0	"^([ \t]*)(type)([ \t]*)([a-z0-9_]+)([ \t]*)(as\s)"	"\n"	" 	"		0	0
	;2359304	0	${REG}	0	"type"		"([ \t]*)([a-z0-9_\s]+)(as)([ \t]*)\n"	" 	"	0	0
	;
	;3409920	0	${ERR}	0	"^[ \t]*(type)[ \t]*(([a-z0-9_]?)[ \t]*)?(as)\b"	"$"	" 	"		0	0
	;2359304=8+262144+2097152
	;392=8+128+256
	;2359304	0	${ERR}	0	"type"		" as .+?$"	" 	"	0	0
	;
;Type (Block): *Multi-Line*
	;40=8+32
40	0	${NUM}	0	"type"		"end type"	" 	"	0	0
;
And you can test it on the example code I showed when explaining the issue:

Code: Select all

Type some_alias_name As Function (As Integer, As Integer) As Integer

Type As Function (As Integer, As Integer) As Integer some_ALIAS_name

Type some__TYPE As some_ALIAS_name

Type some__TYPE
  ' comment #1
  ' comment #2 ... something with the word AS in it
  m As ulongint = 2762781
  s As ulongint = 0
  i As ulongint = 0
  r As ubyte    = 13
End Type

if x then
end if
if x then      'lkjldjkf
end if
if x then y else z end if
if x then y
Thanks!
CBruce
Last edited by CBruce on Sat Jul 02, 2022 5:23 pm, edited 1 time in total.

Offline
Posts: 13
Joined: Thu May 05, 2022 5:38 am

Post by dothen »

I choose this

Code: Select all

;1048584=8+1048576
1048584	0	0	0	"type(?=[ \t](([^\n]*[ \t]as[ \t])|as[ \t])[^\n]*$)"    "as"    " 	"	0	0
;40=8+32
40	0	0	0	"type"   "end type"   " 	"	0	0
Last edited by dothen on Sun Jul 03, 2022 9:36 am, edited 1 time in total.

Offline
Posts: 32
Joined: Thu Oct 15, 2015 2:20 am

Post by CBruce »

Hi dothen,

Your solution for the single-line "type" works if there are only space characters between "type" and "as", but if there are any tab characters in there, it is the multi-line fold that catches it, instead of the single-line no-fold.

Also, I believe you meant for your flag field to be 1048576 = (regular expression in fold start ("Folds:" section) or skip start ("Skips:" section)).

Offline
Posts: 13
Joined: Thu May 05, 2022 5:38 am

Post by dothen »

Hi CBruce.

I tested on freebasic.coder there "type..." "as" works.

In the freebasic.coder file, the "function" "end function" block has the flag 40

Code: Select all

40	0	0	0	"function"         "end function"    " 	"	0	0
In the vbs.coder file, the "function" "end function" block has the flag 0

Code: Select all

0	0	0	0	"Function"  "End Function" " 	"	0	0
Here the "type..." "as" block that contains Function does not work.
It does not depend on tabulation.

fixed ;1048576=8+1048584 on ;1048584=8+1048576 :D
Post Reply