I'm trying to get some "repl-like" feature for PHP, inside vim.
Basically, what I want is to be able to visually select a part of my script, execute it, and see the result in a separate buffer.
But I don't want to execute the whole current file (so :!php % doesn't do the trick ...)
I found the vim-quickrun plugin, which seems to greatly fit that need, but can't make it work and when looking for more documentation, most of the result I get are in japanese (I don't speak japanese :( ... )
For now, I have installed the plugin via Vundle, but have not added any extra configuration to my .vimrc
From inside a file, I can type
...
echo 'hello quickrun sh test'
...
=> visual select the date line, and type
:QuickRun sh
I got my hello world printed, all fine
But if I do
...
echo 'hellow quickrun php'
...
=> visual select ...
:QuickRun php
I just get a buffer with just the same text that I typed, no execution ...
Does someone already achieved something like this ?
Thanks a lot !
EDIT :
PHP is correctly added to my PATH. Added the 2 config lines suggested below ... Sadly, it doesn't change anything :(
You need to put the php flags around your php code, like any php script (it always starts in plain text mode):
...
<?php
echo 'hellow quickrun php';
?>
....
Then you can select only one part with QuickRun, but don't forget to select the flags as well.
I don't use that plugin, but I think you need to configure something like this in your ~/.vimrc:
let g:quickrun_config = {}
let g:quickrun_config.php = {'command' : 'php'}
and have the php executable in your PATH.
The following solution does not use vim-quickrun but allows you to visually select, execute and see the result just as you like. You need vim-slime with phpsh :
First, install the vim-slime plugin. It allows to send lines and visually selected chunks of code from VIM to a screen or tmux session.
Now install screen: On Ubuntu, do sudo apt-get install screen.
Open a terminal and start screen with a session name: screen -S sessionname.
Open a second terminal and start vim. Write some code, visually select it and press <C-c><C-c>, that is two times CTRL+C. You will be asked for the session name, use sessionname as before. The selected lines will be sent to the first terminal just as if you had written them directly there.
To make use of this functionality, you need to start an interactive PHP shell in the first terminal, such as phpsh.
Related
i need a plugin/component/code or anything else that beautifying my data
in my framework terminal (countdown) user can see my commands list with this command:
php countdown help
i wanna show the commands in some style like this:
since the countdown commands are specific and are not changeable, i can create this format in some txt file and when user type the help command print it by getting its content in terminal
but in some cases, such as when user wanna see the routes, i need to put the unknown datas in this text. so i need an function for example that i can put my user routes in its arguments with an array for example and it create this format text for me
something like this:
$headers = array('Command', 'Description', 'Arguments');
$help_command = Text_Beautifer(self::$commands, $headers);
$headers = array('Route', 'Path', 'Middleware');
$routes = Text_Beautifer(self::$routes, $headers);
anyone has idea about it ?
thank you in advance
There are really two questions implied here:
how to beautify console output?
how to manage information related to commands?
how to beautify console output
The easiest way to get started is to use the printf command to format the output for your listing.
There are many examples here in the PHP manual.
And there is a nice answer here on How to make alignment on console in php.
Perhaps the trickiest aspect of printing a table in the terminal is adjusting the output to the width of the terminal and analyzing your data to know how wide to make your columns before you start to print.
Here is a simple table with some ideas to get started:
$width=22;
echo str_repeat("-", $width)."\n";
printf( "| %-10s", "name");
printf( "| %6d", 44);
echo " |\n";
echo str_repeat("-", $width)."\n";
creates this:
----------------------
| name | 44 |
----------------------
you can retrieve the width of the terminal window with:
$width=intval(shell_exec("tput cols");
the rest of the calculations are up to you.
how to manage information related to commands
As my suite of scripts and commands for the command line have grown, I have started to consider a framework for organizing the logic and presentation.
Symfony Console is looking pretty good. It handles the management of command options and their requirements, as well as help text.
I'm trying to set auto-complete for readline to enable a user to navigate through the filesystem directories when running a script through CLI.
I found out PHP has a function called readline_completion_function for this purpose but every time I hit tab to complete a word, it adds a space at the end.
readline_completion_function(function() {
return ['aaa', 'bbb', 'ccc'];
});
// Input a or b or c
readline('Add char and tab: ');
I've create fiddle for you to test this:
https://repl.it/#rmdev/Readline-Completion-Tab-Extra-Space
I saw on Python readline configs that there's a configuration called set_completer_delims to set a character to be apendend after tab is hit but in PHP there are only two configurations and none of them apply to the completer delims.
I also saw some posts referring to this as a known bug but it seems it has been fixed on most of the integrations although I don't see any reference to a fix for the PHP one.
Is there any way to remove this space?
I'm using PHP 7.2.12 on a Mac.
I'm intending to format the selection (indentation) for the PHP code, but it does not work.
I already made sure that there aren't duplicate shortcuts.
I also disabled all extensions.
I changed the keyboard shortcut from Ctrl+K Ctrl+f to Ctrl+k Ctrl+y.
None of these helped.
Is the only language that does not work for me to format selection
Eye. It's not because I'm missing the closing tag (?>).
To see menu bar if not present press
Left Alt
then go:
Preferences > Keyboard Shortcuts
type in the search bar
ctrl+k ctrl+f
you should see
perhaps you have a collision and other command has the same shortcut defined or your shortcut is not defined at all.
You can double click on shortcut to edit it.
Note at the picture When this is when the command works because one shortcut may work only if you are currently editing document and other when you are browsing files so once you set a shortcut make sure your checking it in different places of editor to see if its working or not.
If you use shortcut:
Ctrl+Shift+P
and select command:
You'll see whole bunch of shortcuts and there should be there one you are missing:
{ "key": "ctrl+k ctrl+f", "command": "editor.action.formatSelection",
"when": "editorHasDocumentSelectionFormattingProvider && editorHasDocumentSelectionFormattingProvider && editorTextFocus && !editorReadonly" },
I think that you can just copy the one above, paste to your file if it is not present and save that file, restart your Code and all should be working. Remember that the file is JSON so keep its format - look how other keys are presented there and your pasting should not make JSON invalid.
.vue file doesn't have formate selection.
This function depends on your file type.
I checked that this key binding was indeed still specified, there was no duplicate key binding, etc.; still, Visual Studio Code refused to recognize the key combination. Then I quit visual studio code and restarted it, and the key combination started working again.
Sometimes, the basic quit-and-restart is the answer!
If nothing is working you can create your own "format selection" with multiple commands. You would need a code formatter and a macro extension to run multiple commands from one keybinding,I'm using "prettier" and "multi-command" extension.
You can use this keybinding in your keybindings.json (Click File -> Preferences -> Keyboard shortcuts. Use the tab that opens up to edit and find available key bindings) with the multi-command extension - no need for anything in settings.json:
{
"key": "Shift+Alt+A", // or whatever keybinding you wish
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.formatDocument",
"editor.action.clipboardCopyAction",
"undo",
"editor.action.clipboardPasteAction",
]
},
"when": "editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly"
}
I use it because "format selection" is not working with "*.vue" files.
My problem is I need to fetch FOOBAR2000's title because that including information of playing file, so I create a execute file via Win32 API(GetWindowText(), EnumWindows()) and it's working good.
TCHAR SearchText[MAX_LOADSTRING] = _T("foobar2000");
BOOL CALLBACK WorkerProc(HWND hwnd, LPARAM lParam)
{
TCHAR buffer[MAX_TITLESTRING];
GetWindowText(hwnd, buffer, MAX_TITLESTRING);
if(_tcsstr(buffer, SearchText))
{
// find it output something
}
return TRUE;
}
EnumWindows(WorkerProc, NULL);
Output would look like "album artis title .... [foobar2000 v1.1.5]"
I created a php file like test.php, and use exec() to execute it.
exec("foobar.exe");
then in console(cmd) I use command to execute it
php test.php
It's working good too, same output like before.
Now I use browser(firefox) to call this php file(test.php), strange things happened.
The output only foobar2000 v1.1.5, others information gone ...
I think maybe is exec() problem? priority or some limitation, so I use C# to create a COM Object and register it, and rewrite php code
$mydll = new COM("FOOBAR_COMObject.FOOBAR_Class");
echo $mydll->GetFooBarTitle();
still same result, command line OK, but browser Fail.
My question is
Why have 2 different output between command line and browser. I can't figure it out.
How can I get correct output via browser.
or there is a easy way to fetch FOOBAR2000's title?
Does anyone have experience on this problem?
== 2012/11/28 edited ==
follow Enno's opinion, I modify http_control plug-in to add filename info, original json info is "track title".
modify as following
state.cpp line 380 add 1 line
+pb_helper1 = pfc::string_filename(pb_item_ptr->get_path());
pb_helper1x = xml_friendly_string(pb_helper1);
# 1: when firefox opens the php and it gets executed, it the context depends on the user which runs the php-container (apache), this is quite different from the commandline call which gets executed in your context
# 2 and 3: there seems to be more than one way for getting the title: use the foobar-sdk and create a module which simply reads the current title per api, then write your result in an static-html-document inside your http-root-folder OR use the http-client inside the sdk, with it, you do not need a wabserver, even better use a already implemented module: for instance foo_upnp or foo-httpcontrol
Good luck!
If your webserver runs as a service, in windows you need to enable "allow desktop interaction" for the service. Your php script runs as a child of the webserver process when requested via browser.
I'm trying to get PHP autocompletion right in Vim. Right now when I do a $blog = new Blog(); $blog-> and then hit CTRL+X CTRL+O I'd expect omnicompletion to return all the functions in the class Blog.
Instead, it returns all functions for the entire project. I've built ctags for my project like so: ctags -R *
Is there any way to make the autocompletion context-aware?
catchmeifyoutry's answer points out a work-around by adding a comment such as /* #var $myVar myClass */ immediately before the line on which you use omnicomplete, however this is cumbersome and for the time it takes to write the comment, you may as well have written the function name yourself.
Solution: phpComplete
It is a Vim script: phpComplete
You will still need a tags file generated for your classes, but you can then use omni complete within the file, like so (modified from the description on the script's page);
This patch allows for in-file checking so you don't need the comment.
$blog = new Blog;
...
$blog->Blah(); // <-- complete without comment
It also allows support for singleton instantiations:
$instance = Class::getInstance();
$instance->completeMe(); // sweet completion
" Assuming Vim 7 (full version) is installed,
" adding the following to your ~/.vimrc should work.
filetype plugin on
au FileType php set omnifunc=phpcomplete#CompletePHP
" You might also find this useful
" PHP Generated Code Highlights (HTML & SQL)
let php_sql_query=1
let php_htmlInStrings=1
" Hope this helps!
(via http://www.linuxquestions.org/questions/linux-software-2/vim-omin-completion-for-php-621940/#post3155311)
Omnicompletion will only work if the tag file contains both the class definition, and the variable declaration.
Straightforward solution
In general that means that you will need to save and (re)generate the tags file after the class Blog { ... } and $blog = new Blog(); parts, but before trying $blog-> <C-X><C-O>.
This is because the PHP omni-complete function will look for the class declaration of the $blog variable in the tags file.
(BTW if you have opened the tags file in a buffer, reload it after regenerating it.)
Alternative
The vim documentation (:help ft-php-omni) also defines a way which doesn't require the variable to be indexed in the tags file, but uses instead a specific comment on the preceding line:
/* #var $myVar myClass */
$myVar->
However, the class definition still does need to be in the tag file, and the comment is needed every time you want to use omni-complete. So typing away in a new PHP file still won't give you nice omni-completion :(
Just a thought
Maybe it is possible to generate on-the-fly a temporary tags file (like the taglist plugin) of just the unsaved buffer, and allow omni-complete to use it too?? I'm not a great vim hacker though...
This one works as expected:
https://github.com/shawncplus/phpcomplete.vim
I am just missing the function parameters in the pveview!
The following works better. Taken from http://weierophinney.net/matthew/archives/134-exuberant-ctags-with-PHP-in-Vim.html
ctags \
-f ~/.vim/tags \
-h ".php" -R \
--exclude="\.svn" \
--totals=yes \
--tag-relative=yes \
--PHP-kinds=+ivcf \
--regex-PHP='/(abstract)?\s+class\s+([^ ]+)/\2/c/' \
--regex-PHP='/(static|abstract|public|protected|private)\s+function\s+(\&\s+)?([^ (]+)/\3/f/' \
--regex-PHP='/interface\s+([^ ]+)/\1/i/' \
--regex-PHP='/\$([a-zA-Z_][a-zA-Z0-9_]*)/\1/v/' \
Even with the above, there seems to be some issues. e.g. phpcomplete doesn't seem to support methods of instance vars.
$this->objA = new SomeClass();
$this->objA-><do_autocomplete> #fails
However,
$objA = new SomeClass();
$objA-><do_autocomplete> #works
After trying to get phpcomplete working for the last few hours my advice to anyone also trying, is to STOP. It doesn't work well and is not worth the trouble.
In C++, I run the following to get better context sensitivity:
ctags '--c++-kinds=+p' '--fields=+iaS' '--extra=+q'
It's not perfect, but after ctags adds the extra information to the tags file as specified by the above command vim handles completion better.
You can use a pretty powerful combo:
Phpactor
nvim-completion-manager
I tried a lot of stuff: PHPComplete, Padawan and so on. This is the best I could find.
In case you are interested, I wrote as well an article how to do a PHP IDE with Vim.
I've created a vim plugin for my padawan.php completion server. Checkout this video to see how it works.
try
curl -L -s https://git.io/ide | sh
then relaunch your nvim. You might got code completion, and goto definition features.
*Currently, it's only available for neovim