Bug 473266 - Regex doesn't seem to respect flag state
Summary: Regex doesn't seem to respect flag state
Status: CONFIRMED
Alias: None
Product: kate
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: Microsoft Windows All
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-08-10 22:51 UTC by reptillia39
Modified: 2023-12-29 17:43 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description reptillia39 2023-08-10 22:51:17 UTC
SUMMARY
***
There seem to be a issue highlighting text when the regex contains a flag. (?s) is a flag in regex. That's where the issues comes in.
***

This is the text test.
```
fill "y+x*h" mirror. x

    m "rep_mitchell_concatenation_arguments_for_shapes:
     
        skip $""{1=1},$""{2=0},$""{3=1},$""{4=1}
     
        ratio:=cut($""1,0,1)
        if !$ratio error ratio_arg>0==F fi
        sublevel:=abs($""3)+1
        status $ratio,$""2,$sublevel,$""4
        "

    command "+rep_mitchell_concatenation_create_shape:
        skip $""{1=},$""{9=},$""{10=},$""{11=},$""{12=},$""{13=},$""{14=},$""{15=},$""{16=},$""{17=},$""{18=},$""{19=}
        sid,pixel_size,subpixel_level,ratio,angle,reflect_direction,sublevel,fit_inside=$""{1-8}
      
        if $pixel_size<4 error dim_form>=4=F fi
      
        if $_rep_mitchell_concatenation_use_shape_image
         
            $_rep_mitchell_concatenation_shape_image 
            
            _rep_mitchell_concatenation_use_shape_image,_rep_mitchell_concatenation_shape_image=
         
            if d!=1 error d_form_img==1=F fi
         
            if s<=4&&s>1
                if s==3
                    to_gray.
                else
                    s. c,{if(s==4,-3,if(s==2,-1,-s))}
                    to_gray..
                    *[-2,-1]
                fi
            elif s==5
                s. c,-4 cmyk2rgb.. to_gray.. *[-2,-1]
            fi
        else
            if narg($sid)
                if isnum($sid) if isint($sid)
                    sid=${arg\ $sid+1,australia,barbedwire,circle,crosshair,cupid,diamond,dragon,dragonfly,fern,flip,gear,gumleaf,heart,information,kookaburra,mail,mapleleaf,paint_splat,paw,phone,polygon,rooster,shopping_cart,snowflake,star}
                fi fi
            else 
                sid=diamond
            fi
         
            if narg($""{9--1}) 
                shape_$sid {$pixel_size*$subpixel_level},$""{9--1}
            else 
                shape_$sid {$pixel_size*$subpixel_level}
            fi
         
            resize. {ceil([w,h]/$pixel_size)*$pixel_size},100%,100%,0,0,.5,.5
        fi
      
        if $angle-360*floor($angle/360) rotate. $angle,1 fi
      
        if $reflect_direction==1 mirror. x
        elif $reflect_direction==2 mirror. y
        fi
      
        n. 0,1 autocrop. 0
      
        if $fit_inside
            resize2din. {vector2(round($pixel_size)*$ratio)},5
        else
            resize. {vector2(round($pixel_size)*$ratio)},100%,100%,5
        fi
      
        resize. $pixel_size,$pixel_size,100%,100%,0,0,.5,.5
      
        cut. 0,1 n. 0,1
        "

jp " " "" "" ""
```
This would be the regex to test
```
((?<=m )|(?<=command ))(?s)"{1}(.*?)"(?=\s)
```

STEPS TO REPRODUCE
1. Test the text test on regex101 website, and the regex on KDE. Then do the same on Kate.

OBSERVED RESULT
Kate fails to highlight texts which are highlighted in regex101/

EXPECTED RESULT
Kate should successfully highlight texts.

SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
Comment 1 reptillia39 2023-08-10 23:04:03 UTC
New Information. Even syntax highlighting approach does not work at all:

```
        <contexts>
            
            <context name="Multi-line Comment" attribute="Comment" lineEndContext="#stay">
                <RegExpr attribute="Quotation Mark" context="#stay" String="((?&lt;=m )|(?&lt;=command ))(?s)&quot;{1}(.*?)&quot;(?=\s)" />
            </context>
            
        </contexts>
        
        <itemDatas>
            <itemData name="Quotation Mark" defStyleNum="dsString" />
        </itemDatas>
```
Comment 2 Kåre Särs 2023-08-11 18:09:26 UTC
Thanks for the report!

I think the problem might be that Kate treats every line as a separate string. Only if the regular expression includes "\n", the search will search in a copy of all lines joined into one string with "\n".

It could be interesting to test if this could be fixed  by also treating the (?s) flags as a trigger to join the lines.

This would have to be checked/fixed both in the KTextEditor search and the Kate multi-file search plugin 

(this does go into the more advanced regular expressions :)
Comment 3 Kåre Särs 2023-08-11 18:33:04 UTC
Sorry not the string "\n" but the new-line character '\n' is used to join lines in multi-line searches :)