Is it possible to auto format PHP in Sublime Text 2? - php

In Sublime Text 2, I am trying to type the code and have auto format command to rearrange and auto indent source code. If it is not possible to have a automatic formatter, would be nice to have a shortcut command to do it.
I am searching for something else besides the built-in "Reindent" option and the hard-to-make-work "PHP Beautifier" plugin.
I would like to know if someone found any nice auto formatter for PHP: any formatter that won't mess our PHP code. JSFormat seems to be not useful.
Is there any easy to "install and use" plugin?

PHPTidy is definitely good choice for PHP code formatting in SublimeText2.
You need to install package control package to enable you to install packaged in sublime text: link.
ctrl+shift+p -> type -> Package Control: Install Package
search for phpTidy -> hit enter
that is it - now you can format your code in WordPress coding convention.
How to format you code:
ctrl+shift+p -> type -> tidy -> Hit Enter
ctrl+alt+t
Hope this help.

There is a PHP code formatter named PhpBeautifier. Here you can find a Sublime Text plugin for this program and you can also install it via package control.

I have this set up as a keybind:
[
{
"keys": ["alt+shift+f"], "command": "reindent",
"args": {"single_line": false}
}
]
If you're looking for a bit more than that there is Tidy PHP under package management.
Update: Been some time, I'm now using PHP Coding Standards along with PHP CS Fixer which automates a lot of the cleanup done by autoformatting. This takes a fair bit of work / understanding of php cs to setup but its not too hard, and well worth it. Link

I've been searching this before also, but with no avail. Sublime text 2 probably doesn't support formatting PHP code natively. I have looked into ST2 packages - http://wbond.net/sublime_packages/community, but there is also nothing. Probably you'll have to do this in another editor.
Of course it would be nice, if someone would write a package for this, 'cause packages for formatting JS, JSON, HTML.. already exist.

This is an old subject, but as I found it via Google, here's an answer :
You can use the phpfmt plugin for Sublime Text 3, and configure it to respect code conventions (Such as PSR-0, PSR-1 or PSR-2).
The files will be beautified on save.

I'm using sublimetext-codeformatter
It supports PHP, JavaScript/JSON, HTML, CSS, SCSS, Python, Visual Basic/VBScript and maybe more in the feature. For PHP formatting it uses a fork of the phpfmt which works well. All languages-specific formatting can be set in one config file.

Not sure if this is what you are looking for, but are you aware that when you save your file as .php, Sublime Text 2 automatically highlights and indents according to its built in PHP rules. You can also select this from going to View, and then Syntax, and selecting PHP. But again, this is automatically done when you save the file as .php.

Related

Is there a way to make Visual Studio Code recognize HTML syntax in PHP files

I am using Visual Studio Code on a Mac to work on WordPress applications.
Is there a way to make Visual Studio Code recognize HTML and use it's features (mainly autocompletion) inside PHP files?
I've searched for this answer before but just get told to Go to Visual Studio Code Settings: File >> Preferences >> User Settings
// Place your settings in this file to overwrite the default settings
{
// Configure file associations to languages (e.g. "*.extension": "html"). These have precedence over the default associations of the languages installed.
"files.associations": {"*.php": "html"}
}
But doing this makes you lose all PHP support and autocompletion.
I wish Microsoft would realize that PHP developers commonly use PHP and HTML together when creating applications/websites. I would love to use VS Code instead of the magnificent but expensive PHP storm if possible.
You can install this plugin:
PHP Intelephense
As far as I can tell, it does. Take a look at this php file I have created in VS Code (no extensions), with HTML inside the file with syntax highlighting for both languages.
There is a button (It should say the word PHP) in the bottom right corner of the screen that allows you to switch the syntax highlighting and autocompletion to another language, and while this does temporarily turn off PHP syntax, it will allow you to code in HTML a lot easier.
You can enable html support in any language by adding the following to your settings.json file:
"emmet.includeLanguages": {
"your-language": "html",
"rust": "html",
"php": "html",
"javascript": "html",
}
Very easy solution
Search emmet in settings and remove php form Emmet: Exclude Languages list
This will definitely work for everyone
I also faced this genuine concern while learning PHP, and the only solution I came up with at the moment is clicking on PHP button on bottom right corner of VS Code screen.
I am attaching the screenshot below with that PHP button highlighted so you would be able to locate it easily. Once clicked it will ask for a language you want autocomplete to work on, select HTML and it would start autocompleting html, click again to enable PHP autocomplete.
I also happened to come across GitHub Pilot Extension, you need to be a student for availing this service for free or you'll have to buy membership, It is a good tool for auto complete although that is not the gist of it's use, It is something great invented for learners and also professional coders.
I used to use PHPEd and it has content-aware autocomplete. It knows the content that your cursor is in: php, html, css, js, etc and suggests tags/attributes/functions/etc based on the language where the cursor is. The entire problem with VSCode is its decidedly unintelligent “intellisense” that relies wholly on what language the document is defined as. Any IDE worth its salt is going to have content aware auto-completion. The only satisfactory answer to your question is make your own snippets and turn off emmet and php auto-completion.

howto set up autocomplete for php in eclipse similar to java

i'm pretty new to php development and want to start using an IDE with syntax checking and autocompletion.
I'm used to programming java with eclipse and thought that the PDT provides similiar functionality.
In java if i type in for example: "new Arr" and press ctrl+space autocomplete suggests a list of possibilities with "new ArrayList()" on top.
but if i try the same in a php project, for example with "new mysq" and ctrl+space i get nothing. I would expect to get "new mysqli()" or similar recommended.
Is the described functionality not included in PDT? Or did i configure something wrong?
I did the "right click on project-> configure-> Add php support" but it didn't change anything.
If you would like to have some basic auto complete feature you can use NotePad++ (download) for a quick and easy starter. This is a really great minimal program that can be used very efficiently to code in many different languages. It provides huge ammount of different functions. It is a must have tool for any developer.
I used Notepad++ to write ten thousands of lines of PHP code, it worked really great for me.
Otherwise PDT also supports autocomplete. Make sure that you have a PHP project added and then your are in a PHP file (.php extension). If you have Java autocomplete working in the same project then I think that you are in a Java project, try adding a PHP project, then a new PHP source file. Then PHP autocomplete should be working well in this source file.
Make sure also that you have PHP perspective selected.
I hope you can get by now starting using PHP! Have luck and good times using this great language!
I found the solution in this question: Why does Eclipse code completion not work on some projects?
I had to manually add this:
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
to the buildpath file of my project.

Why notepad++ autocomplete doesn't work with function arguments?

I checked "Settings > Prefences > Backup/Auto-Completion > Enable Auto-completion on each input" and "Function parameters hint on input" options. I have Notepad++ 5.9 (unicode) version. It auto completes function names but not their arguments. Also i tried re-install. I know it has this feature but not working. Any idea why?
I mean it auto completes print_r when i write "pri" but i want it to work like in the image;
http://i51.tinypic.com/3322auc.jpg
Hmm, this is not an answer to your question but I want to share it anyway.
I have used Notepad++ for years. I didn't like Eclipse and NetBeans is just so damn slow.
I never used the auto complete function for anything.
Notepad++ can't really do any code analysis, it can only give you auto-completion on the known PHP functions.
I've since switched to an IDE that does full code analysis and can do auto-completion for your own code as well.
Notepad++ is great if you just want to write something real quick. It's also a great tool if you want to learn about PHP since it doesn't do auto-completion very well ;) but if you really want to be productive when you're working on a large application I recommend you use an IDE that does code analysis. Productivity is the key here. I spend less time looking through files and more time actually writing code, which is great! :D
Good IDEs for PHP: NetBeans, PHPStorm, Eclipse.
For this feature to work, you need to supply an .xml file which contains the appropriate functions and parameters.
An example for Javascript with installation instructions can be found here.
I have not yet found xml files for PHP (or Python, which I'm looking for). But it seems this can work in npp, you just need to locate the right xml file.
Best of luck!
(Oh, and don't listen to the naysayers who are too quick to give up, npp works fine as an IDE.)
Notepad++ it's very good text editor, but it's not IDE.
Try to use IDE - NetBeans or PhpStorm, and you will get much more than just smart autocomplete.
I have a same question about this, function hint is Ok,but param hints not display until I download a new release of notepad++(a not install release), all is ok. before, I tryed many time, and edit the xml files.
If you will edit you own language, Can copy from a exist language xml ,for cpp.xml in plugins/APIs folder. It's easy to understand its rule.
It is very easy to have a error, if you config file is changed.

How can I make visual studio highlight .php files

Hope the title explains it all, but all I'm trying to do is open some .php files in my web-site solution and have Visual studio treat them as if they are html files. I don't need the actual PHJP code highlighted (it's only includes). The HTML syntax should be parse-able, should it not?
Tools -> Options -> Text Editor -> File Extension
in the Extension box, put .php. For the Editor, you can select HTML Editor. Click Add, then OK
You can also install the PHP Tools for Visual Studio add-in. Tools > Extension and Updates and then search for PHP online. Your first result should be this add-in.
If not, you can get it from here:
http://visualstudiogallery.msdn.microsoft.com/6eb51f05-ef01-4513-ac83-4c5f50c95fb5
If you want intellisense, debug, and other advanced functionality, you could get the php ide for VS
If all you need is syntax highlighting, check out this link. It's what I'm currently using, but doesn't have all the goodies of vs.php.
Install Phalanger.
If you're using VS2013 or older, I would recommend this extension for PHP Language support : https://visualstudiogallery.msdn.microsoft.com/2a10ba81-26c5-47d9-939b-6bcc7bbec251
For me Josh's solution didn't work and Miguel's solution needs PHP Tools which is free for 30 days but then you need to activate it.

Formatting PHP Code within Vim

I'm currently using Vim as a lightweight IDE. I have NERDTree, bufexplorer, supertab, and ctags plugins which do almost everything I want. Only big thing missing for me is auto code formatting.
I'm working with some messy PHP code which has inconsistent indenting and code formatting, ideally I could highlight the code I want formatted (whole files would be fine too) and run a command to tidy it.
Does anybody have a nice solution for this working in Vim?
Quick way to fix PHP indentation in vim is to visually select the lines you want to work with using shift-v, and then press equals (=) to trigger auto-formatting.
As for other formatting issues you're probably looking at employing some regex search and replaces, such as :%s/^M/\r/g (that's ctrl-V ctrl-m, not caret-M) to fix line endings
Enter normal mode in vim and then type
1GVG=
Format in PSR-2 style
For the new standard Coding Style Guide PSR-2 use the PHP-CS-Fixer.
There is a Vim plugin: Vim-php-cs-fixer
How to install:
Install PHP-CS-Fixer (globally with Composer):
composer global require friendsofphp/php-cs-fixer
Then add the Vim plugin (Pathogen):
cd ~/.vim/bundle
git clone git#github.com:stephpy/vim-php-cs-fixer.git
Restart Vim.
Default mapping:
<leader>pcd " For directory
<leader>pcf " For flie
There is a vim plugin that enables formatting on your code from within vim. It's called vim-autoformat and you can read about it and download it here:
https://github.com/vim-autoformat/vim-autoformat
It integrates external code-formatting programs into vim. When this plugin is installed, you only have to install an external code formatter to get everything to work out of the box. It supports the php formatter phpCB, which is the best php formatter i've seen so far.
UPDATE: phpCB is not supported anymore, due to code breaking behaviour. However, vim's indentfile is always used as fallback, allowing you to at least indent your code when there's is no formatter available.
The vim website is not the easiest to navigate, but there is a wealth of chewy nougat center there.
For instance I found this php indenting script there. Give it a try.

Categories