Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

netbeans php go to variable assignment for class attribute (rather than declaration)

Tags:

php

netbeans

In netbeans, I can Ctrl+Click a variable name to jump to the declaration of that variable. This works great for normal variables. However, when I use it for a class attribute, it jumps me to the top of the class to a line like

private $myVar;

which is technically correct, but pretty much useless. It would be much more helpful if it jumped me to the line where the variable is first assigned a value, ie

$this->$myVar=7;

Is this possible? If so, how?

Using NetBeans 8.0.2 on Windows 7

like image 244
chiliNUT Avatar asked Apr 06 '15 18:04

chiliNUT


2 Answers

as far as I know and as I have tried, this is not possible.

Because, a variable can be defined once, but can be initialized/assigned at multiple places. How will you say as which one is first?

For Example, I may be initializing the variable in the constructor method or I may be having a setter method to set the variable without the contructor too, or I may be having entirely a different method, not specifically constructor, which I may be calling to set values for variables. So there may be a chance that I have all these in my code.

Hence it's not possible.

like image 126
Vinod Tigadi Avatar answered Oct 10 '22 02:10

Vinod Tigadi


Well, I couldn't figure it out with NetBeans macro language since I don't know how to grab the selection, modify it, and do a regex search for it, which seems like that is needed. I was able to do it with AutoHotKey. The idea is to make a macro which does the following:

  1. Double click at the caret position to highlight the property name. Netbeans wasn't reporting its caret position, so I have to settle for using the mouse position, which is fine.

  2. Create the following regex to locate when propName gets a value assigned:

\$this\s*\->\s*propName\s*=

  1. Then, do a search for that regex, and go to the first instance found.

It's not perfect, but its a start and it seems to be working out for me. It currently won't work for nested properties ($this->someProp->subProp) could locate someProp but not subProp (it would incorrectly search for $this->subProp) but it should be able to handle those too by adjusting the regex.

I have assigned the macro to Alt+Click in the following:

!~LButton Up::
; //save the old clipboard
oldClipboard := Clipboard
; //Sleep a while. Without this, the double click overlaps with the 
; //original click used to trigger the macro, 
; //and the wrong text is highlighted (usually the thole line)
Sleep 500
Click 2 ;
; //Copy the text
Send ^c
searchText := Clipboard
; //prefix it with this regex: "\$this\s*\-\>\s*" and add "\s*=" to the end so varName becomes \$this\s*\-\>\s*varName\s*=
searchText := "\$this\s*\-\>\s*" . searchText . "\s*="
Sleep 50
; //Toggle search dialog
Send ^f
Sleep 50
; //write the text into the form
Send %searchText% 
Loop, 2 {
Sleep 100
Send !g ; //turn regex on or off
; //since the state of whether regex is on or off is not known, cycle thru both

Sleep 100
Send {Enter}
}

; //restore the clipboard
 Clipboard := oldClipboard

return
like image 4
chiliNUT Avatar answered Oct 10 '22 03:10

chiliNUT