NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Custom code-completion rules
Veteran

Joined: 22 May 2008
Posts: 141
Reply with quote
I'm not a desktop programmer, so I have no idea if this is even doable (although I think it is.)

Here is an explanation of my problem:
I use CodeIgniter which follows a Model-View-Controller approach.
A controller is just a Class with different methods that represent pages on website. So a controller might consist of an index() method, add_user() method, edit_user() method, and so on.
In one of those methods, I can call $this->load->model('UserModel'). UserModel is just another class.
After I load UsersModel, I could access it in my controller like this: $this->UserModel->add_user().

What I think would be awesome is for PHPEd to provide me some Code Insight when I type in $this->UserModel->.
Naturally, its not capable of doing so because it isn't aware of CodeIgniter's (or any other framework's) way of loading classes.

What I thought would be cool is if PHPEd had a user-defined hooks file..
This would allow users of about any framework to write a "plugin" so PHPEd can provide Code Insight for it...
Since I don't know squat about whatever language PHPEd was developed in, I'll post what I was thinking in a "mostly" PHP format.
Code:

// Example PHP file where code insight is used...
class MyClass
{
   var $user = 'Bob';
   function method()
   {
      $this->user  //-->There is a classvar $user, so CodeInsight works correctly as you type this.
      
      // Either way of loading a model below is acceptable in CodeIgniter.
      
      $this->load->model('RoleModel');
      $this->RoleModel->  //-->There is no classvar $RoleModel, so PHPEd needs to run any registered "noinsight" hooks.

      $this->load->model('RoleModel', 'role');  //--> Assigns RoleModel to a classvar named "role"
      $this->role->  //-->There is no classvar $role, so PHPEd needs to run any registered "noinsight" hooks.
   }
}

/////////////////////////////
// code_insight_hooks.cfg
/////////////////////////////

// My custom array to hold pairs of PseudoClassNames (role) to RealClassNames (RoleModel)
$ci_classmap = array();

// Run this on each individual file the PHPEd scans to get its Code Explorer tree.
function my_treegen_hook($ci_classmap)
{
   // Look for calls to $this->load->model() that contain 2 arguments.
   // For each call found, append an element to $ci_classmap
   $ci_classmap[$argument1] = $argument2; //--> $ci_classmap['RoleModel'] = 'role';

   // It is imperative that we return the modified array, because it will be used by my_noinsight_hook.
   return $ci_classmap;
}

// Run this if no CodeInsight is found as the user types.
function my_noinsight_hook($my_ci_classmap)
{
   // As user types out $this->RoleModel->
      // Look for a class in Code Explorer tree named "RoleModel".
      // If found, show user CodeInsight of that class and return true.

      // If "RoleModel" not found in Code Explorer tree...
         // Look for element in $my_ci_classmap whose value (PseudoClassName) == "RoleModel".
         // If element exists, get its key (RealClassName), show user CodeInsight of RealClassName, and return true.

   // As user types out $this->role->
      // Look for a class in Code Explorer tree named "role".
      // If found, show user CodeInsight of that class and return true.

      // If "role" not found in Code Explorer tree...
         // Look for element in $my_ci_classmap whose value (PseudoClassName) == "role".
         // If element exists, get its key (RealClassName), show user CodeInsight of RealClassName, and return true.

   // Still no CodeInsight to show the user.
   return false;
}

/*
Both of the methods below accept multiple arguments.
argument1: Function to call.
argument2+: Variables to pass to function.
*/
PHPEd->add_no_insight_hook('my_noinsight_hook', $my_ci_classmap);
PHPEd->add_treegen_hook('my_treegen_hook', $my_ci_classmap);

Hope you understand it. Smile
View user's profileFind all posts by simshaunSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8335
Reply with quote
Code:
$this->load->model('RoleModel');
$this->RoleModel->

In this case you can define RoleModel class property
either as a real property:


or as a virtual property:

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website
Veteran

Joined: 22 May 2008
Posts: 141
Reply with quote
Ah, the first method seems to work just fine.
I'd prefer to not have to define the var but if I must, I will. Smile

Thanks for the quicker/easier solution dmitri.
View user's profileFind all posts by simshaunSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8335
Reply with quote
Quote:
I'd prefer to not have to define the var


It's property, not var, but still per phpdoc the tag is @var...

Quote:
I'd prefer to not have to define the var but if I must, I will.


As I've suggested, there two ways. The first way explicitly declares the property and it is the most correct way. With the 2nd way, you define only virtual propery. It is more suitable for getter/setter cases when properties can not be explicitly defined.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website
Veteran

Joined: 22 May 2008
Posts: 141
Reply with quote
I may have been doing it incorrectly, but the 2nd method did not work in my situation.
The first method does, however.
View user's profileFind all posts by simshaunSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8335
Reply with quote
Quote:
but the 2nd method did not work in my situation


make sure that the phpdoc follow the following scheme:
/** <- two asterisks, no less, no more
@property CLASSNAME $PROPERTYNAME <- starts with dollar sign
*/
and make sure the property is listed in the Code Explorer (or Code Navigator), like on the screenshot

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 25 Jul 2006
Posts: 70
Reply with quote
Hey dmitri, is this feature documented anywhere? I recently discovered this @property capability and was wondering if there are any other features to it?

Specifically I'd like to type hint an "array of objects", something like this:

Code:
/**
 * @property array[myRegion] $tblRegions
 */
 class xx {
 }
View user's profileFind all posts by cpriestSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8335
Reply with quote
I believe "@property array[myRegion] $tblRegions " would be useless. What may make sense is @property someobject $tblRegions[], otherwise it would be completely unclear of what class the object instances are in the array.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 07 Aug 2008
Posts: 6
Reply with quote
I've the same question regards CodeIgniter & PhpEd!!

now I've Models, Controls & Views each as separate file and there isn't require() or include() , so the code-completion function doesn't work WHICH IS ON OF MOST IMPORTANT features in PhpED Sad !!

is there any solution for this ?
View user's profileFind all posts by m_abdelfattahSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8335
Reply with quote
Why do you think there is any relation between require() or include() and code completion?
There is nothing in common unless you turned on LIMIT SCOPE TO INCLUDES. By default this option is off and Code Completion looks for the classes through all the files in the project (does not limit the scope to only included files).

In short, if the code is correct, Code Completion works fine.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 07 Aug 2008
Posts: 6
Reply with quote
dmitri wrote:
Why do you think there is any relation between require() or include() and code completion?
There is nothing in common unless you turned on LIMIT SCOPE TO INCLUDES. By default this option is off and Code Completion looks for the classes through all the files in the project (does not limit the scope to only included files).

In short, if the code is correct, Code Completion works fine.

Sorry dmitri, I just tried to guess how it works !!
BTW, I'm just starting with CodeIgniter and my code is so simple, only 1 model & 1 controller and it works fine !

here's my categories model code :
Code:

<?php
class Categories extends Model{
    function Categories(){
        parent::Model();
       
        // CONNECTING TO DATABASE
        $this->load->database();
    }
   
    /**
    * Getting all sub categories
    *
    * @param mixed $category_name
    */
    function get_sub_categories($category_name) {
       
        // GETTING CATEGORIES
        $query = $this->db->query('SELECT * FROM `categories` WHERE `parent_url`=\'' . $category_name . '\' ORDER BY `cat_name` ASC') or die(mysql_error());
       
        // CHECK IF ITEM EXIST
        if($query->num_rows() !== 0) {
            $categories = array();
            foreach ($query->result_array() as $row)
            {
                $categories[] = $row;
            }
            return $categories;
            $query->free_result();
        } else {
            return FALSE;
        }   
    }
}
?>


and here's my category controller
Code:

<?php

class Category extends Controller {
    function index() {
        parent::Controller();
    }
   
    function _remap() {
        $category_url = explode('/',$this->uri->uri_string(), 3);
        $category_name = '/' . str_replace("-", " ", $category_url[2]);
       
        // LOADING Categories Model
        $this->load->model('Categories');
       
        // GETTING SUB CATEGORIES
        $sub_categories = $this->Categories->get_sub_categories($category_name);
       
        if($sub_categories !== FALSE) {
            $data = array();
            $data['sub_categories'] = $sub_categories;
        } else {
            //$this->item_not_found();
        }
       
        // GETTING ITEMS
        if(empty($category_name)) {
            // LOADING Categories Model
            $this->load->model('Items');
            $items = $this->Items->get_items($cat_id);
            if($items !== FALSE) {
                $data['items'] = $items;
            }
        }
       
        $this->load->vars($data);
        $this->load->view('category_view');
       
    }

}
?>


So, I think simply, when I write $this->Categories-> ( IT SHOULD appear get_sub_categories(), but it doesn't).

am I right or am that fool ?! Crying or Very sad
View user's profileFind all posts by m_abdelfattahSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8335
Reply with quote
Please try to understand the code below and tell me what methods are supposed to appear after $this->Wood->

Code:
<?php
  class Wood {
    function AMethod() {
    }
  }

  class myClass {
    function myFunc() {
       $this->Wood->
    }
  }

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website
Custom code-completion rules
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 2  

  
  
 Reply to topic