开发者

How do you select the entire PHP function definition?

开发者 https://www.devze.com 2022-12-14 17:09 出处:网络
In PHP, if I have a function such as: function test($b) { var $a = 0; while ($a < b) { $a += 3; } return $a;

In PHP, if I have a function such as:

function test($b) {

  var $a = 0;

  while ($a < b) {
    $a += 3;
  }

  return $a;
}

and the cursor is on the $a += 3 line, is it possible to quickly select the entire function?

"v2aB" would select everything including the function braces but not the declaration function test($b开发者_开发问答)


Press V after the selection command you post, to convert the selection to line selection, and it will select the function declaration:

v2aBV


It's been a long time since this question was asked and answered, but I will add my own answer because it's the one I was looking for and none of the others work exactly like this one:

nnoremap vaf ?func.*\n*\s*{<cr>ma/{<cr>%mb`av`b
vmap af o<esc>kvaf

The first mapping, "Visual around function" or vaf, will jump back to the start of the function definition, regardless that the { is in the same line or the next one, and even if it's a lambda function, and visually select it characterwise to it's ending bracket. This works in PHP, Javascript and Go.

The user can then press V to turn to linewise select mode if she wants to.

The only problem that I found is that when I am in the body of a big function, but below a line that uses a lambda (let's say "small") function, this will stop searching at the beginning of the small function and select it's body instead of reaching the start of the big function and select all of its body.

function show_video_server(v_server) {
  // this whole function should get selected
  var something = function(){ /* this function gets selected */ };
  //   |   the cursor is here when I type "vaf"
}

As a workaround I use the second mapping: vmap af o<esc>kvaf. It feels like a repetition or expansion of the selection. What it really does is abandon the selection and go to the line before it, and then try it agan. If the "big" function uses several lambda functions the user has to repeat the af several times to reach the big one.

Usually, vaf es enough. Sometimes vaf af or vaf af af is needed. Anyway, it's the closest I could get to what I wanted, so this is the version I'm using.


Here's a mapping that seems to work very well, no matter the nesting level.

:map t ? function <CR>f{vaBV


Here's another method that will work if you have function-level folding turned on: z c v

That closes the current fold and selects it, but it leaves it closed. If you want it to remain open: z c v $

If you have block-level folding turned on, you would have to close twice, since you're inside the while loop, so: 2 z c v

To enable PHP class/function folding: let php_folding = 1


simple way

nmap vaf va}V

I like this

nmap vaf [{?function<CR>:nohl<CR>vf{]}

if ‘{’ is in new line

nmap vaF [{?function<CR>:nohl<CR>v/{<CR>]}


Yet another way. This should select the entire function definition regardless of your cursor position within the definition, not just when you're at the $a += 3 line.

Use this in normal mode (<CR> means press enter)

?func<CR>V/{%

Explanation of each part:

  • ?func search backward for the word "func" (the idea is to get to the first line of the function definition)
  • V go to visual line mode
  • /{ search forward for the opening brace (I didn't use f{ because the opening brace might be on a separate line)
  • % go to the matching brace


If you are using OOP programming this works (it looks for extra words before function[public, private, protected])

nmap vaf [{?\S* function<CR>:nohl<CR>v/{<CR>]}

As a bonus here is a wrapper around if

nmap vai [{?if<CR>:nohl<CR>v/{<CR>]}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号