This question already has an answer here:
How can I get Vim to not break DocBlock comments?
(1 answer)
Closed 9 years ago.
I don't even know how to search for this on google, I tried with the title of this question, but could not find anything useful.
Working with PHP on Vim, if I have something like this:
/**
* <cursor>
*/
If I press enter, I'll get:
/**
*
<cursor>
*/
What I want is this:
/**
*
* <cursor>
*/
Yes, it is just one character, but it is bugging me a little bit.
What is the easiest way to achieve this?
Edit:
My .vimrc file have these commands (and others):
" Enable syntax highlight
syntax on
" Syntax in a plugin-based way
filetype on
" Indentation in a plugin-based way
filetype indent plugin on
set fo+=or
I guess, what you need below lines in your vimrc:
filetype on
filetype indent plugin on
I'm using the configuration of spf13-vim and it works for me. I think that the plugin PHP Integration environment for Vim - PIV makes the trick because have a feature for documentation
conforming documentation blocks for your PHP code.
Well, I'm sorry for this. I found the problem:
autocmd FileType php,phtml :set ft=php.html
I used this line to load HTML snippets for PHP files with the snipMate.vim plugin. Turns out, if I remove this line, the behaviour will be what I expected to be.
Thank you everyone.
Related
PHP has a function called get_meta_tags which can read meta tags of HTML files. However, as far as I know there is no standard way to define meta tags for PHP files. The de facto solution seems to be to add comment to the top of the file like so:
<?php
# Author: Ood
# Description: Hello World
?>
Is there any way to read these "meta tags" with PHP similar to the way get_meta_tags works using the default PHP library? Preferably without parsing the entire file with file_get_contents followed by a regex for best performance. If not, maybe someone knows of a better solution to add meta data capabilities to PHP files. Thanks in advance!
In our project we are happy with the standard JavaDoc that was adopted by PHPDoc using the #field syntax as you might know it from any PHP function or class definition. This is pretty fine readable using the PHPDocumenter.
In our adoption we use the very first multi-line comment, ie anyting between /** and closing tag */, using the JavaDoc style to describe all the details about the current script.
So to adopt your example in our project we would have following syntax:
<?php
/**
* #author Ood
* #desc Hello World
*/
Of course you may end up with your custom function reading the beginning of the php file parsing just the very first multi-line comment to get the script description aka meta tags.
I want to automatically generate block comments for documentation using vim.
Something like:
/**
* comment
*/
are there any plugins for this?
Vim has this functionality built-in. See :help format-comments for details. Basically if you have filetype plugin on in your ~/.vimrc and are using a bracket language (like C, C++, Java, javascript, etc.), you can type /**<cr> and it will expand to:
/**
* _
where _ is the cursor position. When you're done with the comment block just hit / to end it.
A low-budget way of simplifying a Vimmer's life with C- or Java-style block comments is to add the following mapping to .vimrc.
autocmd FileType c,java inoreabbrev <buffer> /** /**<CR>/<Up>
That way, whenever you type /**<Enter> in your C or Java source it will be expanded to the following, with the cursor at _:
/**
* _
*/
Edit: As #Conner mentioned, you need to have filetype plugin indent on in your vimrc to make this work.
Is there any way to show the current PHP function or class name in the VIM command line? I found a plugin for showing C function names in the status line but it does not work for PHP and in any case I prefer the command line be used to save valuable vertical lines.
Thanks.
EDIT
While looking for something completely unrelated in TagList's help I've just found these two functions:
Tlist_Get_Tagname_By_Line()
Tlist_Get_Tag_Prototype_By_Line()
Adding this in my statusbar works beautifully:
%{Tlist_Get_Tagname_By_Line()}
Also, did you read the Vim Wiki? It has a bunch of tips trying to adress the same need. There is also this (untested) plugin.
ENDEDIT
If you are short on vertical space maybe you won't mind using a bit of horizontal space?
TagList and TagBar both show a vertical list of the tags used in the current buffer (and other opened documents in TagList's case) that you can use to navigate your code.
However, I'm not particularly a fan of having all sorts of informations (list of files, VCS status, list of tags, list of buffers/tabs…) displayed at all times: being able to read the name of the function you are in is only useful when you actually need to know it, otherwise it's clutter. Vim's own [{ followed by <C-o> are enough for me.
I don't know anything about PHP, and I'm not trying to step on anyone's toes, but having looked at some PHP code I came up with this function which I think takes a simpler approach than the plugins that have been mentioned.
My assumpmtion is that PHP functions are declared using the syntax function MyFunction(){} and classes declared using class MyClass{} (possibly preceded by public). The following function searches backwards from the cursor position to find the most recently declared class or function (and sets startline). Then we search forward for the first {, and find the matching }, setting endline. If the starting cursor line is inbetween startline and endline, we return the startline text. Otherwise we return an empty string.
function! PHP_Cursor_Position()
let pos = getpos(".")
let curline = pos[1]
let win = winsaveview()
let decl = ""
let startline = search('^\s*\(public\)\=\s*\(function\|class\)\s*\w\+','cbW')
call search('{','cW')
sil exe "normal %"
let endline = line(".")
if curline >= startline && curline <= endline
let decl = getline(startline)
endif
call cursor(pos)
call winrestview(win)
return decl
endfunction
set statusline=%{PHP_Cursor_Position()}
Because it returns nothing when it is outside a function/class, it does not display erroneous code on the statusline, as the suggested plugin does.
Of course, I may well be oversimplifying the problem, in which case ignore me, but this seems like a sensible approach.
I am trying to switch to vim for all my editing and trying out the plugins. I have been using snipmate and ragtag and like them both. Now I am coding PHP with HTML in it and I saw this question to get snipmate to work with snippets for the two file types here: Vim - Activiting HTML snippets on PHP files .
This works well, I can get snippets for both PHP and HTML, but ragtag is no longer introducing PHP <?php ... ?> blocks, it's introducing <% ... %>. Is there a way to fix this?
Thank you.
It's possible that Ragtag doesn't understand multiple filetypes. Does it work when you :set ft=php or :set ft=html.php ?
I've never used ragtag, but since you are using Snipmate too why don't you just type php<Tab>?
It's certainly a lot easier to remember and more "mnemonic" than <C-X>_ (if I read the doc well).
-- EDIT --
It appears a check is made on ft (filetype) at line 71 but it accepts only "php" as value, since you have set ft=php.html ragtag should accept "php.html" as well.
I think changing this line from
if &ft == "php"
to
if &ft == "php" || &ft == "php.html"
should work.
I am about writing own simple syntax highlighter in PHP. I've done basic algorithm based on regular expressions and string replacement, but what I really don't know is way how to disable replacing keywords which are commented.
For example:
/**
* Some class
*
* #property-read $foo
*/
class Test
{
private $foo;
public function __construct()
{
}
}
Now my solution simply highlight defined keywords (like class or variables) but also those which are commented.
Any solution for this problem?
Why not use PHP's tokenizer to do the job for you? That way, your syntax highlighter will parse the PHP code the exact same way the Zend Engine does, which is probably going to give you a lot better results than a regular expression.
Why not borrow lessons from how vi or vim already does this? long back I remember for some custom tag based language we developed, we wanted syntax highlighting in VI and VIM , that is when we changed few .vi sort of configuration files where we mentioned, all the meta data like which color to what kind of tag, what are tags possible etc.
Looking more into how vi or vim or any text editor does this might be more helpful!
You could exclude the commented lines by this logic:
if line starts with /** disable highlight
if next line starts with * do nothing and check next line
if line starts with */ reenable highlight
Just a quick guess and can be defined more precise, but should work as a logic.