PHP indenting string over many lines

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

PHP indenting string over many lines

David Fishburn

I am using the PHP version 1.19 script.

I have been using PHP a lot lately and noticed two things.

1.  Starting typing in this line:
if ( something ) {
        $var = "something" .
            " that will span " .

When  you hit enter after the last ., the indent is placed at column 1
instead of the same indent of the line above.


2.  I notice the script changes the formatoption setting.
        setlocal formatoptions=qrowcb
I think the formatoption setting is usually set by a ftplugin not an indent
script, but hopefully someone on the list can correct me if I am wrong.

Is there a reason your script changes this setting?
I see this line:
" Changes: 1.14 - Added auto-formatting for comments (using the Vim
option formatoptions=qroc).

I had to create a vimfiles\after\ftplugin\php.vim so that I could change
these values back to something I am used to:
setlocal formatoptions-=t
setlocal formatoptions+=l
setlocal formatoptions+=c
setlocal formatoptions+=n

If I recall, I think it was not dealing with comments correctly.
If I was adding to a comment, and hit <enter> the comment character was not
showing up at the beginning of the line.
So I had to add this to my vimfiles\after\ftplugin\php.vim
" Add additional comment characters
setlocal comments+=s1:/*,mb:*,ex:*/,://
setlocal commentstring=//%s

Then I think it was not allowing me to format the comments with gq based on
the formatoptions your indent script was overriding.


The comments option is most appropriately set by the ftplugin\php.vim script
as well (I believe).

So what I am asking is for the indent\php.vim script to be more user
friendly.  Do not specifically set the formatoptions setting, but simply
turn on and off the particular settings that your script needs as I have
done above.


I have emailed Dan Sharp about the issues with the ftplugin\php.vim.

Hopefully this thread can figure out what changes are needed where.

Thanks,
Dave

Reply | Threaded
Open this post in threaded view
|

RE: PHP indenting string over many lines

John Wellesz
Hello,

Thanks for reporting these problems, I've corrected them all:

Version 1.20 changelog:

- Line beginning by a single or double quote followed
  by a space would cause problems... this was related
  to the bug correction of version 1.10 - Thanks to
  David F. for finding this (he was lucky).
- Changed the way this script set the formatoptions
  setting, now it uses '-=' and '+='
- New option: PHP_autoformatcomment (defaults to 1),
  if set to 0 the formatoptions setting will not be
  altered.
- When PHP_autoformatcomment is not 0, the 'comments'
  setting is set to the type of comments that PHP
  supports.

Doenload:

http://www.2072productions.com/vim/indent/php.vim

(Note that I wrote "David F." because I don't know if you want your name
written there)

Try it out and tell me if everything works correctly, then I upload it on
Vim.org.

Also read detailed answers below, especially about the formatoptions
problems.

> David Fishburn [mailto:[hidden email]]
>
> 1.  Starting typing in this line:
> if ( something ) {
>         $var = "something" .
>             " that will span " .
>
> When  you hit enter after the last ., the indent is placed at column 1
> instead of the same indent of the line above.

This problem must exist since version 1.17, and comes from a correction in
version 1.10...
I used the pattern
 
"^\\s*[\"']*\s*\\zs\\S"

Instead of

 "^\\s*[\"']*\\s*\\zs\\S"

To detect the first character of a line that is not a blank or quote
character...

If you had written "that will span " . instead of " that will span " .
You'd never had noticed this bug!


> 2.  I notice the script changes the formatoption setting.
> setlocal formatoptions=qrowcb
> I think the formatoption setting is usually set by a ftplugin not an
> indent
> script, but hopefully someone on the list can correct me if I am wrong.
>
> Is there a reason your script changes this setting?

I added this setting so comments would be formatted correctly. Else they are
not by default.

> I had to create a vimfiles\after\ftplugin\php.vim so that I could change
> these values back to something I am used to:
> setlocal formatoptions-=t
> setlocal formatoptions+=l
> setlocal formatoptions+=c
> setlocal formatoptions+=n

Note that the option +=n is useless because it only works when formatting
text; which is disabled (-=t)

> If I recall, I think it was not dealing with comments correctly.
> If I was adding to a comment, and hit <enter> the comment character was
> not
> showing up at the beginning of the line.

On my side it was working as expected, comment leaders where added
automatically when hitting <Enter> or using o in normal mode.

> Then I think it was not allowing me to format the comments with gq based
> on
> the formatoptions your indent script was overriding.

My indent script set the flag q of formatoptions which does enable the
command "gq". So your problems must come from somewhere else.

If you run the command ":setlocal formatoptions" when editing a php file,
what does it say?

> The comments option is most appropriately set by the ftplugin\php.vim
> script
> as well (I believe).

I don't know this script; can you give me a link?

> So what I am asking is for the indent\php.vim script to be more user
> friendly.  Do not specifically set the formatoptions setting, but simply
> turn on and off the particular settings that your script needs as I have
> done above.

I corrected that and I'm using += and -= now:

        if b:PHP_autoformatcomment

            " Set the comment setting to something correct for PHP
            setlocal comments=s1:/*,mb:*,ex:*/,://,:#
           
            " disable Auto-wrap of text
            setlocal formatoptions-=t
            " Allow formatting of comments with "gq".
            setlocal formatoptions-=q
            setlocal formatoptions+=q
            " Insert comment leader after hitting <Enter>
            setlocal formatoptions-=r
            setlocal formatoptions+=r
            " Insert comment leader after hitting o or O in normal mode
            setlocal formatoptions-=o
            setlocal formatoptions+=o
            " Uses trailing white space to detect paragraphs
            setlocal formatoptions-=w
            setlocal formatoptions+=w
            " Autowrap comments using textwidth
            setlocal formatoptions-=c
            setlocal formatoptions+=c
            " Do not wrap if you modify a line after textwidth
            setlocal formatoptions-=b
            setlocal formatoptions+=b
        endif


If someone sees important options for him that are not here, please tell me.
 
>
> I have emailed Dan Sharp about the issues with the ftplugin\php.vim.
>
> Hopefully this thread can figure out what changes are needed where.
>
> Thanks,
> Dave


Reply | Threaded
Open this post in threaded view
|

RE: PHP indenting string over many lines

David Fishburn
 

> -----Original Message-----
> From: John Wellesz [mailto:[hidden email]]
> Sent: Saturday, November 19, 2005 7:43 PM
> To: 'David Fishburn'; 'Dan Sharp'
> Cc: [hidden email]
> Subject: RE: PHP indenting string over many lines
>
...
> (Note that I wrote "David F." because I don't know if you
> want your name written there)

Fine with anything.

> Try it out and tell me if everything works correctly, then I
> upload it on Vim.org.

From my limited but usual tests all seems to work as expected.
Thank you.

...
> > The comments option is most appropriately set by the
> ftplugin\php.vim
> > script as well (I believe).
>
> I don't know this script; can you give me a link?

It is the default script that is included in the Vim distribution:
vim63\ftplugin\php.vim
vim64\ftplugin\php.vim
vim70aa\ftplugin\php.vim


Dave

Reply | Threaded
Open this post in threaded view
|

RE: PHP indenting string over many lines

John Wellesz
> ...
> > > The comments option is most appropriately set by the
> > ftplugin\php.vim
> > > script as well (I believe).
> >
> > I don't know this script; can you give me a link?
>
> It is the default script that is included in the Vim distribution:
> vim63\ftplugin\php.vim
> vim64\ftplugin\php.vim
> vim70aa\ftplugin\php.vim


Hmm... Ok, I didn't know there were such files! Moreover I was reading
ftplugin as "File Transfer Protocol plugin" instead of "File Type plugin"
- no comment -
I never find the time to learn to use vim completely; I think I only know
the last layer of snow covering the top of the iceberg that is Vim!

Anyway you're right the options I set in my indent script should have been
defined there... But sometimes it's difficult to contact maintainers of some
scripts and even more difficult to make them accept what you think is best.
So it will remain in the indent script for now, until it's added to the
ftplugin file.

Btw, in my previous post I wrote:

setlocal formatoptions-=q
setlocal formatoptions+=q

but the lines with -= are useless since flags cannot be added several
times...

John


Reply | Threaded
Open this post in threaded view
|

Re: PHP indenting string over many lines

A.J.Mechelynck
John Wellesz wrote:

>> ...
>>>> The comments option is most appropriately set by the
>>> ftplugin\php.vim
>>>> script as well (I believe).
>>> I don't know this script; can you give me a link?
>> It is the default script that is included in the Vim distribution:
>> vim63\ftplugin\php.vim
>> vim64\ftplugin\php.vim
>> vim70aa\ftplugin\php.vim
>
>
> Hmm... Ok, I didn't know there were such files! Moreover I was reading
> ftplugin as "File Transfer Protocol plugin" instead of "File Type plugin"
> - no comment -
> I never find the time to learn to use vim completely; I think I only know
> the last layer of snow covering the top of the iceberg that is Vim!
>
> Anyway you're right the options I set in my indent script should have been
> defined there... But sometimes it's difficult to contact maintainers of some
> scripts and even more difficult to make them accept what you think is best.
> So it will remain in the indent script for now, until it's added to the
> ftplugin file.
>
> Btw, in my previous post I wrote:
>
> setlocal formatoptions-=q
> setlocal formatoptions+=q
>
> but the lines with -= are useless since flags cannot be added several
> times...
>
> John
>
>
>
>
>

You can always -- when you come around to it -- write your own "filetype
plugin tweaks" and put them in a file named <filetype>.vim (where
<filetype> is the concerned filetype) and placed in one of the following
directories:

        $VIM/vimfiles/after/ftplugin/
                system-wide, all platforms
        $HOME/vimfiles/after/ftplugin/
                user-private, Dos/Windows
        $HOME/.vim/after/ftplugin/
                user-private, Unix and similar

i.e., in the ftplugin/ subdirectory of some directory mentioned after
$VIMRUNTIME in the 'runtimepath' option. This way, your ft-plugin will
be read immediately after the "standard" ft-plugin of the same name,
which will allow it to tweak filetype-dependent settings.

Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|

PHP indent script version 1.21 released

John Wellesz
In reply to this post by John Wellesz
Changes in version 1.21:

- 'try' and 'catch' were not registered as block starters so the '{' after a
try' or 'catch' could be wrongly indented...
(Thanks to Gert Muller for finding this issue)

You can view its capabilities and download it there:

http://www.vim.org/scripts/script.php?script_id=1120

or there:

http://www.2072productions.com/?to=phpindent.txt


John Wellesz

> -----Message d'origine-----
> De : John Wellesz [mailto:[hidden email]]
> Envoyé : dimanche 20 novembre 2005 01:43
> À : 'David Fishburn'; 'Dan Sharp'
> Cc : [hidden email]
> Objet : RE: PHP indenting string over many lines
>
> Hello,
>
> Thanks for reporting these problems, I've corrected them all:
>
> Version 1.20 changelog:
>
> - Line beginning by a single or double quote followed
>   by a space would cause problems... this was related
>   to the bug correction of version 1.10 - Thanks to
>   David F. for finding this (he was lucky).
> - Changed the way this script set the formatoptions
>   setting, now it uses '-=' and '+='
> - New option: PHP_autoformatcomment (defaults to 1),
>   if set to 0 the formatoptions setting will not be
>   altered.
> - When PHP_autoformatcomment is not 0, the 'comments'
>   setting is set to the type of comments that PHP
>   supports.
>
> Doenload:
>
> http://www.2072productions.com/vim/indent/php.vim
>
> (Note that I wrote "David F." because I don't know if you want your name
> written there)
>
> Try it out and tell me if everything works correctly, then I upload it on
> Vim.org.
>
> Also read detailed answers below, especially about the formatoptions
> problems.
>
> > David Fishburn [mailto:[hidden email]]
> >
> > 1.  Starting typing in this line:
> > if ( something ) {
> >         $var = "something" .
> >             " that will span " .
> >
> > When  you hit enter after the last ., the indent is placed at column 1
> > instead of the same indent of the line above.
>
> This problem must exist since version 1.17, and comes from a correction in
> version 1.10...
> I used the pattern
>
> "^\\s*[\"']*\s*\\zs\\S"
>
> Instead of
>
>  "^\\s*[\"']*\\s*\\zs\\S"
>
> To detect the first character of a line that is not a blank or quote
> character...
>
> If you had written "that will span " . instead of " that will span " .
> You'd never had noticed this bug!
>
>
> > 2.  I notice the script changes the formatoption setting.
> > setlocal formatoptions=qrowcb
> > I think the formatoption setting is usually set by a ftplugin not an
> > indent
> > script, but hopefully someone on the list can correct me if I am wrong.
> >
> > Is there a reason your script changes this setting?
>
> I added this setting so comments would be formatted correctly. Else they
> are
> not by default.
>
> > I had to create a vimfiles\after\ftplugin\php.vim so that I could change
> > these values back to something I am used to:
> > setlocal formatoptions-=t
> > setlocal formatoptions+=l
> > setlocal formatoptions+=c
> > setlocal formatoptions+=n
>
> Note that the option +=n is useless because it only works when formatting
> text; which is disabled (-=t)
>
> > If I recall, I think it was not dealing with comments correctly.
> > If I was adding to a comment, and hit <enter> the comment character was
> > not
> > showing up at the beginning of the line.
>
> On my side it was working as expected, comment leaders where added
> automatically when hitting <Enter> or using o in normal mode.
>
> > Then I think it was not allowing me to format the comments with gq based
> > on
> > the formatoptions your indent script was overriding.
>
> My indent script set the flag q of formatoptions which does enable the
> command "gq". So your problems must come from somewhere else.
>
> If you run the command ":setlocal formatoptions" when editing a php file,
> what does it say?
>
> > The comments option is most appropriately set by the ftplugin\php.vim
> > script
> > as well (I believe).
>
> I don't know this script; can you give me a link?
>
> > So what I am asking is for the indent\php.vim script to be more user
> > friendly.  Do not specifically set the formatoptions setting, but simply
> > turn on and off the particular settings that your script needs as I have
> > done above.
>
> I corrected that and I'm using += and -= now:
>
> if b:PHP_autoformatcomment
>
>    " Set the comment setting to something correct for PHP
>    setlocal comments=s1:/*,mb:*,ex:*/,://,:#
>
>    " disable Auto-wrap of text
>    setlocal formatoptions-=t
>    " Allow formatting of comments with "gq".
>    setlocal formatoptions-=q
>    setlocal formatoptions+=q
>    " Insert comment leader after hitting <Enter>
>    setlocal formatoptions-=r
>    setlocal formatoptions+=r
>    " Insert comment leader after hitting o or O in normal mode
>    setlocal formatoptions-=o
>    setlocal formatoptions+=o
>    " Uses trailing white space to detect paragraphs
>    setlocal formatoptions-=w
>    setlocal formatoptions+=w
>    " Autowrap comments using textwidth
>    setlocal formatoptions-=c
>    setlocal formatoptions+=c
>    " Do not wrap if you modify a line after textwidth
>    setlocal formatoptions-=b
>    setlocal formatoptions+=b
> endif
>
>
> If someone sees important options for him that are not here, please tell
> me.
>
> >
> > I have emailed Dan Sharp about the issues with the ftplugin\php.vim.
> >
> > Hopefully this thread can figure out what changes are needed where.
> >
> > Thanks,
> > Dave
>


Reply | Threaded
Open this post in threaded view
|

PHP indent script version 1.23 released

John Wellesz
In reply to this post by John Wellesz
Hello,

Here is a new version of my indent script for PHP with some improvements and
some bug fixes :)

Additionally, there is now a newsletter where you can subscribe to be
alerted when a new version is released: (No other kind of message will be
broadcasted)

http://www.2072productions.com/?to=php-indent-for-vim-newsletter.php


Changes in version 1.23:

- PHPDoc comments are now indented according to the surrounding code.
- This is also true for '/* */' multi-line comments when the second line
  begins by a '*'.
- Single line '/* */' comments are also indented.
- <script> html tags are now correctly indented the same way their content
  is.
- <?.*?> (on a single line) PHP declarations are now always considered as
  non-PHP code and let untouched.


You can view its capabilities and download it there:

http://www.vim.org/scripts/script.php?script_id=1120

Or there (colorful version):

http://www.2072productions.com/?to=phpindent.txt

Enjoy :-)

John Wellesz