Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Match comments
#1
hello everyone,
I wanted to use regular expressions to match comments, 

But, lables and comments are very similar. How to distinguish them?

In the following example:
Line 1 was deleted
Line 3 was not deleted
Line 8 was not deleted

Expected results:
___________________________
 lable1
run "notepad"
_i+1
if _i=3
    ret
goto lable1
___________________________

Any suggestions are welcome. Thanks in advance


Macro Match comments
Trigger Aq /QM     Help - how to add the trigger to the macro
Code:
Copy      Help
_s.getsel
_s.replacerx("^\s.+\n|\s;;.+" "")
mes _s
out _s


Macro comment test
Code:
Copy      Help
;lable1
run "notepad" ;;comment
;run "notepad" ;;comment2
_i+1
if _i=3
,ret
goto lable1
;coment3
#2
This is my take on it

str aa=
  lable1
 run "notepad" ;;comment
 ;run "notepad" ;;comment2
 _i+1
 if _i=3 
     ret
 goto lable1
 ;coment3
ARRAY(str) result
str line
foreach line aa
    str pattern= ";\D*."
    line.replacerx(pattern "")
    if(line.len)
        result[]=line

str answer= result
answer.replacerx("$[]" "")
out answer

When im pasting its adding ; 
so here is an image to show


Attached Files Image(s)
   
#3
@redbull2k

Thanks for your help
It doesn't work in the actual test

In the following code, I use the function aa.getsel, select the target code [Macro comment test] and test it

Macro comment test
Code:
Copy      Help
;lable1
run "notepad" ;;comment
;run "notepad" ;;comment2
_i+1
if _i=3
,ret
goto lable1
;coment3

Macro redbull2k_code
Trigger Aq     Help - how to add the trigger to the macro
Code:
Copy      Help
str aa.getsel
ARRAY(str) result
str line
foreach line aa
,str pattern= ";\D*."
,line.replacerx(pattern "")
,if(line.len)
,,result[]=line

str answer= result
answer.replacerx("$[]" "")
mes answer
#4
this works for your example
select the text below the #ret line then run
Code:
Copy      Help
out
_s.getsel
str s ss
int i
ARRAY(str) result
if _s.len
,findrx(_s "goto(\s.+)" 0 8 s 1); s.rtrim()
,foreach ss _s
,,if(StrCompareEx(ss s 1)<>0)
,,,ss.replacerx("^\040.+$|\040\;\;.+" "")
,,if ss.len
,,,result[]=ss
,out result
else
,out "nothing is selected"
#ret 
;lable1
run "notepad" ;;comment
;run "notepad" ;;comment2
_i+1
if _i=3
,ret
goto lable1
;coment3
#5
@kevin thanks a lot
The following comments are not deleted
Line 23
Line 35
Line 51 58

In addition, how to merge multiple blank lines into one blank line
For example:
Line 36-39    
Line 44-45    
Line 47-49    

Macro Macro3
Code:
Copy      Help
out
_s.getsel
str s ss
int i
ARRAY(str) result
if _s.len
,findrx(_s "goto(\s.+)" 0 8 s 1); s.rtrim()
,foreach ss _s
,,if(StrCompareEx(ss s 1)<>0)
,,,ss.replacerx("^\040.+$|\040\;\;.+" "")
,,if ss.len
,,,result[]=ss
,mes result
else
,mes "nothing is selected"
#ret
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,QmRegisterDropTarget(id(3 hDlg) hDlg 0)
,__Font-- f.Create("Courier New" 8 0) ;;comment
,;__Font-- f.Create("Consolas" 8 0) ;;comment
,f.SetDialogFont(hDlg "3")
,case WM_DESTROY
,case WM_COMMAND goto messages2
,case WM_QM_DRAGDROP
,QMDRAGDROPINFO& di=+lParam
,sel wParam
,,case 3 ;;drop
,,str s
,,if(!di.GetText(s 1)) s=di.files
,,s.setsel(0 di.hwndTarget)
,,ret DT_Ret(hDlg 1)
,,;ret DT_Ret(hDlg 1) ;;comment
,,
,,


ret
;messages2
sel wParam
,case IDOK
,
,
,case IDCANCEL
,
,
,
ret 1

;sel wParam
,;case IDOK
,;int hcb3=id(3 hDlg)
,;int cp=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0) ;;currentpos: begin of the line
,;out cp
,;case IDCANCEL
;ret 1
#6
as i said above "this works for your example".
was only meant to work with the example
any thing more requires much more processing.

change
Code:
Copy      Help
ss.replacerx("^\040.+$|\040\;\;.+" "")
 

to
Code:
Copy      Help
ss.replacerx("^\040.+$|\040\;\;.+|\011\040.+" "")



 and this still wont cover every possibility i don't think
#7
The following rule can merge multiple blank lines into one blank line. Where should I add it?
Code:
Copy      Help
"(?:(?:(?!\n)\s)*\n){2,}"  replace to  "\n"
#8
@Kevin

merge multiple blank lines into one blank line success

Macro Macro4
Code:
Copy      Help
_s.getsel
_s.replacerx("(?:(?:(?!\n)\s)*\n){2,}" "[][]") 
mes _s
out _s

#ret
,,ret DT_Ret(hDlg 1)
,,;ret DT_Ret(hDlg 1) ;;comment
,,
,,


ret
;messages2
sel wParam
,case IDOK 
,
,
,case IDCANCEL
,
,
,
ret 1

At present, there is only one problem

Using the following code, empty lines are removed
Huh :
______________________________________________________
            ss.replacerx("^\040.+$|\040\;\;.+" "")
             ss.replacerx("^\040.+$|\040\;\;.+|\011\040.+" "")

______________________________________________________
[Image: 1.png]
#9
actually it is not the replacerx line at all that is removing the line
it is this line
if ss.len 
because the line len value is 0
I will recode this as there are a lot more issues that haven't come up yet that need to be fixed.
will post in a few minutes.

sorry for the delay got a phone call.
this covers a lot more but still doesn't cover everything.
 
Code:
Copy      Help
out
opt clip 1
_s.getsel
if _s.len
,str s ss result
,int i j fn length1 length2
,ARRAY(str) labels a b
,a=_s
,if(findrx(_s "goto(\s.+)" 0 4 b 1)>0)
,,for i 0 b.len
,,,ss.formata("%s[]" b[0 i].rtrim)
,,ss.rtrim
,,labels=ss
,,ss.fix(0)
,if(findrx(_s "(?s) BEGIN DIALOG(.+?)''*'' '''' '''' ''''" 0 1 s)<>-1)
,,foreach ss s
,,,labels[]=ss
,for(i a.len-1 -1 -1)
,,for(j 0 labels.len)
,,,if(a[i] = labels[j])
,,,,fn=1
,,,,j=labels.len
,,,else
,,,,fn=0
,,if fn<>1
,,,length1=a[i].len
,,,a[i].replacerx("^\040.+|\040\;\;.+|\011\040.+")
,,,length2=a[i].len
,,,if(length1!=0 and length2= 0)
,,,,a.remove(i)
,result=a
,result.rtrim;;remove extra emptyline at end of result that array adds on
,result.replacerx("(?:(?:(?!\n)\s)*\n){2,}" "[][]");;replace multiple blank lines to 1
,ShowText3 "Results" result 0 2;;show the result in qm format
else
,mes "Nothing is Selected!"
#ret
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,QmRegisterDropTarget(id(3 hDlg) hDlg 0)
,__Font-- f.Create("Courier New" 8 0) ;;comment
,;__Font-- f.Create("Consolas" 8 0) ;;comment
,f.SetDialogFont(hDlg "3")
,case WM_DESTROY
,case WM_COMMAND goto messages2
,case WM_QM_DRAGDROP
,QMDRAGDROPINFO& di=+lParam
,sel wParam
,,case 3 ;;drop
,,str s
,,if(!di.GetText(s 1)) s=di.files
,,s.setsel(0 di.hwndTarget)
,,ret DT_Ret(hDlg 1)
,,;ret DT_Ret(hDlg 1) ;;comment
,,
,,


ret

;messages2
sel wParam
,case IDOK
,
,
,case IDCANCEL
,
,
,
ret 1

;sel wParam
,;case IDOK
,;int hcb3=id(3 hDlg)
,;int cp=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0) ;;currentpos: begin of the line
,;out cp
,;case IDCANCEL
;ret 1

forgot to paste this function as well
needs this to show result in qm format
Function ShowText3
Code:
Copy      Help
;/
function# $caption $text [hwndowner] [flags]

;Everything is the same as with <tip "#ShowText">ShowText</tip>, except:
;;;flag 1 (nonmodal) unavailable.


opt waitmsg 1
int i he=id(3 ShowText(caption text hwndowner flags|1))
CHARFORMAT cf.cbSize=sizeof(CHARFORMAT)
cf.dwMask=CFM_FACE|CFM_SIZE
strncpy(&cf.szFaceName "Courier New" 12)
long twips= 12*20
cf.yHeight=twips ;;twips    
SendMessage he EM_SETCHARFORMAT SCF_ALL &cf    
rep
,0.01
,if(!IsWindow(he)) break
ret

if dont want comment out this line in above function
Code:
Copy      Help
ShowText3 "Results" result 0 2;;show the result in qm format

and replace with
Code:
Copy      Help
mes result "Results"
#10
@Kevin
Thanks a lot for your sharing,
I tested more than 30 pieces of code, including dialog code
Perfect job!
Smile


Forum Jump:


Users browsing this thread: 1 Guest(s)