| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,56 @@ | |||
| 1 | + --- | ||
| 2 | + layout: post | ||
| 3 | + title: "Composer knows the path of class file" | ||
| 4 | + date: 2015-06-08 23:00:00 | ||
| 5 | + tags: vim, php, composer, sensorario | ||
| 6 | + category: plugin | ||
| 7 | + author: sensorario | ||
| 8 | + author_link: https://sensorario.github.io | ||
| 9 | + summary: Open class file witouht pass throuw ctag | ||
| 10 | + --- | ||
| 11 | + | ||
| 12 | + <p>I work with vi since late 2014. Starting from that date, I've learned different ways to work with this amazing and customizable editor. Since few weeks ago, I always worked with CtrlP to search a file. But, ... why type everytime class name for CtrlP instead of "grep" directly to composer's auto-generated files? But grep all times is a slow process so, ... why dont write a Vim script that do this stuff for me? Yeah! After this assumtions and after creation of a funciton I also discovered vim-composer plugin. So, ... I decided to share this function with the community. I hope you like that idea.</p> | ||
| 13 | + | ||
| 14 | + <p>And now, ... step by steps all the parts related to the function I've added to vim-composer. If you like, you can simply <a href="https://github.com/vim-php/vim-composer/commit/117279627adf9dfc4bd44c0b01c1f19580de609b">go to the commit</a> and just see the code.</p> | ||
| 15 | + | ||
| 16 | + <h2>Grep composer to find the path</h2> | ||
| 17 | + <p>First of all, I started to grep a class name in composer folder:</p> | ||
| 18 | + {% highlight bash %} | ||
| 19 | + grep ParticularClassName ./vendor/composer -R | awk '{print $6}' | awk -F\\' '{print $2}' | ||
| 20 | + {% endhighlight %} | ||
| 21 | + <p>This kind of instruction returns a path like <em>/src/Path/To/ParticularClassName.php</em>. And this, .. answer to the question, ... where is ParticularClassName located??</p> | ||
| 22 | + | ||
| 23 | + <h2>Keep bash output inside a VimL variable</h2> | ||
| 24 | + <p>Asking to stackoverflow, ... I discovered the way to <strong>capture bash output inside a variable</strong>:</p> | ||
| 25 | + {% highlight bash %} | ||
| 26 | + let l:fileName = system(l:command) | ||
| 27 | + {% endhighlight %} | ||
| 28 | + <p>This is amazing for me: I know bash better than Vim. I can leave to bash a lot of stuffs, and just execute little funcion in VimL.</p> | ||
| 29 | + | ||
| 30 | + <h2>Open new tab form VimL</h2> | ||
| 31 | + <p>Finally, I can open in new tab the result.</p> | ||
| 32 | + {% highlight vim %} | ||
| 33 | + let l:fileName = system(l:command) | ||
| 34 | + let l:openFileCommand = 'tabe .' . l:fileName | ||
| 35 | + exec l:openFileCommand | ||
| 36 | + {% endhighlight %} | ||
| 37 | + <p>My contribute to this <a href="https://github.com/vim-php/vim-composer">vim-composer</a> plugin is a way to open faslty a file, when we want open it. The principle is "when I see a class name, I want to open source file of that class, and I want ask this to composer".</p> | ||
| 38 | + | ||
| 39 | + <hr /> | ||
| 40 | + | ||
| 41 | + <h2>Make plugin useful for custom improvements</h2> | ||
| 42 | + <p>vim-composer is a specific plugin for composer. This must work even if developers has CtrlP, ctags or other plugin. And also, this plugin can help developers and/or others plugins. For example I work with CtrlP. Someone not (I cant believe but is possible). Someone works with ctags. Probably exists others plugin. I dont care. My contribute aims to help every kind of improvements. With my last pull request. I suggest an intresting improvement. The function "g:ComposerKnowWhereCurrentFileIs()" define some global variables if o or more instance of current word are find in composer aut-generated files. With this improvements developers can create, for example, a map that execute ComposerKnowWhereCurrentFileIs. If the function found one file, will open it. If not, .. CtrlP could be executed with this trick:</p> | ||
| 43 | + {% highlight php %} | ||
| 44 | + nnoremap <F6>call s:ComposerOrCtrlP() | ||
| 45 | + | ||
| 46 | + function! s:SendCurrentWorldToCtrlP() | ||
| 47 | + exec "normal \<c-p>" . g:currentWord | ||
| 48 | + endfunction | ||
| 49 | + | ||
| 50 | + function! s:ComposerOrCtrlP() | ||
| 51 | + call g:ComposerKnowWhereCurrentFileIs() | ||
| 52 | + if g:numberOfResults > 1 | ||
| 53 | + call s:SendCurrentWorldToCtrlP() | ||
| 54 | + endif | ||
| 55 | + endfunction | ||
| 56 | + {% endhighlight %} | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments