Friday, November 27, 2009

Re: Detect when a return follow an if instruction without bracket

I would like just to refined my original goal.
In fact my goal is to add surround bracket that are missing around a
return which follow if while etc...

Actual PB :
-------------------

Case 1 :
if ( cd1
&& cd2 )
return;

Case 2 :
if ( cd1
&& cd2 )
// COMMENT
/****/* another comment*/
return;

Case 3 :
while ( cd1
&& cd2 )
// COMMENT
/****/* another comment*/
return;


Goal to reach:
-------------------
All that have to become :
while(cd1 && cd2)
{
return;
}


My temporary solution :
----------------------------------
I have tried another way by writing a func which returns 1 or 0 to say
return is following an if , while etc..

1/ search for if
2/ delete // COMMENT
3/ delete /* comment by % and visual selection
4/ delete blank lines
5/ analysis if the return line is just after my if line

function! RectifieMultiLignesIf()

norm 1G

" recherche tous les if (extensible aux elseif while for etc... ) qui
ont un bloc conditionnel sur plusieurs lignes et recode sur une seule
ligne le bloc conditionnel
let flags = "w"
let instruction = "if"

while search("if", flags, line("$")) > 0

if CheckIfCursorIsCommented() == 0

if search("(", flags) > 0
let beginLine = line(".")
endif
norm %
let endLine = line(".")
norm %

" cas ou le bloc est codé sur plusieurs lignes
let deltaLines = 0
if endLine > beginLine
let deltaLines = endLine - beginLine + 1
endif

" remonter sur une seule ligne les lignes du bloc conditionnel
if deltaLines != 0
exec 'norm ' . deltaLines . 'J'
endif

let lineOfInstruction = line('.')
" une fois codé sur une ligne verifie qu'il s'agit d'une ligne ave
un return et
" qu'il y a les accolades du bloc d'instructions sinon on les
ajoute
"
" if ( cond1 &&
" cond2 ) return
"
"
if ( matchstr(getline(line(".")), g:patternReturn ) != "" )
" s il n y a pas de bloc d'instruction sur cette ligne là où un
return a été detecté
if ( search("{", 'w', line(".")) == 0 )
exec "norm ^/if"
exec "norm f(%a
{
"
exec "norm $a
}"
endif

else

" le return n'est pas sur la meme ligne que le bloc conditionnel
"
" if ( cond1 && cond2 )
" // COMM
" /*
" AUTRE COM
" */
" return
"
"
if DetectReturnAfterInstruct() == 1
endif

endif " match un return sur la meme ligne que le bloc conditionnel

endif

endwhile

endfunction


But I am opened to the most optimized solution (time performance) if I
can search with a regexp to identify that the return is following the
if and it is fast,
I will prefer to take the regexp.

NB : I will try your work this journey. Say what are you thinking
about my func if you have time.
Thanks
Best Regards

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: