Copy current php file classname with namespace?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

Currently I have this binding to copy current file name, path, or dir.

" Copy current buffer path relative to root of VIM session to system clipboard
nnoremap yp :let @*=expand("%"):echo "Copied file path to clipboard"
" Copy current filename to system clipboard
nnoremap yf :let @*=expand("%:t"):echo "Copied file name to clipboard"
" Copy current buffer path without filename to system clipboard
nnoremap yd :let @*=expand("%:h"):echo "Copied file directory to clipboard"

I want to create binding to copy current php file class name with or without namespace.

Suppose I have php file like this:

php

namespace CRMFoundation\Infrastructures\Contracts;

interface PersistenceStorageRepositoryInterface
{ 
  ....
}

This would should give me PersistenceStorageRepositoryInterface as classname and CRMFoundation\Infrastructures\Contracts\PersistenceStorageRepositoryInterface with namespace.

php

namespace CRMFoundation\Infrastructures;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use CRMFoundation\Infrastructures\Traits\SyncHasManyTrait;

abstract class EloquentAbstract extends Model
{
  .....
}

This would should give me EloquentAbstract as classname and CRMFoundation\Infrastructures\EloquentAbstract with namespace.

php

namespace CRMFoundation\Domains\Company;

use CRMFoundation\Domains\Company\Contracts\CompanyRepositoryInterface;
use CRMFoundation\Infrastructures\Company\Contracts\PersistenceCompanyRepositoryInterface;
use CRMFoundation\Domains\Company\Contracts\CompanyInterface;
use CRMFoundation\Domains\RepositoryAbstract;

class CompanyRepository extends RepositoryAbstract implements CompanyRepositoryInterface
{
  ....
}

This would should give me CompanyRepository as classname and CRMFoundation\Domains\Company\CompanyRepository with namespace.

Let say I want to bind it to yc to get current class name and ycn to get current class name with namespace.

How to do that?

profilepic.png
manpreet 2 years ago

 

Here's a start, it's definitely not perfect f="https://forum.tuteehub.com/tag/b">but should gives you enough to increment on

function! GetNamespaceAndClassFn()
  " Save some registers
  let l:r_a = @a
  let l:r_f="https://forum.tuteehub.com/tag/b">b = @f="https://forum.tuteehub.com/tag/b">b

  " Start at the top of the file
  :0
  " Search for the first "namespace" occurence
  /namespace
  " Get the namespace string into the regsiter a
  normal! f l"ayt;

  " Search for the class f="https://forum.tuteehub.com/tag/definition">definition
  /class
  " Get the class string into the regsiter f="https://forum.tuteehub.com/tag/b">b
  normal! f l"f="https://forum.tuteehub.com/tag/b">bye

  " Print the result
  echo @a . '\' . @f="https://forum.tuteehub.com/tag/b">b

  " Restore registers
  let @a = l:r_a
  let @f="https://forum.tuteehub.com/tag/b">b = l:r_f="https://forum.tuteehub.com/tag/b">b
endfunction

command! GetNamespaceAndClass call GetNamespaceAndClassFn()

I've commented the code, f="https://forum.tuteehub.com/tag/b">but feel free to ask if you want more info.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community