suy / vim-context-commentstring

Vim plugin that sets the value of ‘commentstring’ to a different value depending on the region of the file you are in.
105 stars 15 forks source link

Support For PHP Files #2

Open hallzy opened 6 years ago

hallzy commented 6 years ago

Thought I would bring this up, since a PHP file can have php, html, and anything that an HTML file can have.

In case you are not familiar with PHP, you have a PHP file (.php), but everything in it is actually HTML unless you have the PHP tags. For example:

<h1>Heading</h1>
<p>
    <?php
        $x = 5;
        echo $x;
    ?>
</p>

So basically, to make a PHP file work here, you would need something like this:

let g:context#commentstring#table.php = {
            \ 'javaScript'  : '// %s',
            \ 'phpRegion'   : '// %s',
            \ 'cssStyle'    : '/*%s*/',
            \}

However, because the contents of a .php file are html unless stated otherwise, we also need to override vim's default php commentstring (which is something like '/*%s*/'), to be the html commentstring.

So we also need something like this:

augroup phpCommentOverride
  autocmd!
  " Override PHP Comments
  autocmd FileType php setlocal commentstring=<!--\ %s\ -->
augroup END

If I add the above autocmd to my vimrc, and that new dictionary entry to your plugin it works for me... In my quick testing I couldn't build in the autocmd into your plugin for some reason though. I don't know why, but I could not get it to work.

robertjk commented 5 years ago
let g:context#commentstring#table.php = {
          \ 'javaScript'  : '// %s',
          \ 'phpRegion'   : '// %s',
          \ 'cssStyle'    : '/*%s*/',
          \}

I added that to my vimrc, but for some reasonsg:context#commentstring#table.php remains unmodified (doesn't contain php key) after startup. It seems this variable is set after vimrc runs. I need to manually invoke :let g:context#commentstring#table.php = { 'javaScript' : '// %s', 'phpRegion' : '// %s', 'cssStyle' : '/*%s*/' } so that the variable gets updated.

Does anybody know how to fix that?

EDIT:

Curiously adding the following line above previous let fixes that. Now I'm even more confused.

let g:context#commentstring#table = g:context#commentstring#table
hallzy commented 5 years ago

ya, to be clear (because reading back my comment I wasn't clear) adding that let g:context#commentstring#table.php to your vimrc doesn't work. I also don't know why.

I forked this project and added that to the plugin itself. It was meant as a temporary fork until this issue was addressed.

I didn't make a PR for it because it required a custom autocmd to get working as well (see my above comment), and for some reason that autocmd didn't work when I tried putting it in the plugin. Unsatisfied that I had to add something to the plugin and vimrc, I opened the issue hoping someone might have a solution.

Completely forgot about this until now though.

So for now to solve the problem I would say either make your own fork and make the changes, use my fork here, or continue to use your workaround.

I too have no idea how your workaround works. Makes no sense to me.

robertjk commented 5 years ago

@hallzy @suy Best if the author explains, where and how you should modify g:context#commentstring#table. The documentation says it's possible, but doesn't say where to do that:

This variable is of type |Dictionary|, and you can modify it to your will. You can add values or overwrite existing ones.

robertjk commented 5 years ago

However, because the contents of a .php file are html unless stated otherwise, we also need to override vim's default php commentstring (which is something like '/%s/'), to be the html commentstring.

So we also need something like this:

augroup phpCommentOverride
  autocmd!
  " Override PHP Comments
  autocmd FileType php setlocal commentstring=<!--\ %s\ -->
augroup END

If I add the above autocmd to my vimrc, and that new dictionary entry to your plugin it works for me... In my quick testing I couldn't build in the autocmd into your plugin for some reason though. I don't know why, but I could not get it to work.

You need to set it after ftlplugin loads (reference). It works then you put the following into vim-context-commentstring/after/ftplugin/php.vim instead of invoking autocmd:

setlocal commentstring=<!--\ %s\ -->

I commited that fix into your plugin repository but cannot push it. If you give me a push access, I can push the fix and then you can submit pull request to this plugin main repository. Or you can commit it by yourself instead of giving me access - it's just one line.

Anyway I think we should commit this into this main repo, as PHP is quite widely used language and unfortunately embedding HTML/CSS/JS is still a common practice...