RiFi2k / format-html-in-php

Basically this vscode extension uses all your standard configurations for html formatting, and your standard configurations for format on save, etc. It more or less works exactly how vscode should already work as it pertains to HTML in PHP files.
The Unlicense
33 stars 6 forks source link

Support for blade files? #12

Open eduardoarandah opened 5 years ago

eduardoarandah commented 5 years ago

Any chance to support Laravel Blade?

(Also used in sage wordpress theme, not only laravel)

Laravel Blade files are the same as php blocks

@if ($var)
@endif

equals

<?php if ($var): ?>
<?php endif; ?>
RiFi2k commented 5 years ago

@eduardoarandah I would for sure be interested in exploring it, I'm a big fan of the roots ecosystem as well so it will motivate me to support their theme.

If I can get around the problem I know we are going to run into which is that JSbeautify is going to basically murder their positioning in the file by default.

@extends('layouts.app')

@section('content')
  @include('partials.page-header')

  @if (!have_posts())
    <div class="alert alert-warning">
      {{ __('Sorry, no results were found.', 'sage') }}
    </div>
    {!! get_search_form(false) !!}
  @endif

  @while (have_posts()) @php the_post() @endphp
    @include('partials.content-'.get_post_type())
  @endwhile

  {!! get_the_posts_navigation() !!}
@endsection

https://beautifier.io/

@extends('layouts.app')

@section('content')
@include('partials.page-header')

@if (!have_posts())
<div class="alert alert-warning">
    {{ __('Sorry, but the page you were trying to view does not exist.', 'sage') }}
</div>
{!! get_search_form(false) !!}
@endif
@endsection

Now I know of the way I would like to handle this which is being able to identify the tags kind of like you showed above and actually string replace @if ($var) to <?php @if ($var) ?> format the doc and then the positions will be retained much like they are in a PHP doc, then remove all the PHP open and close tags and the user would never know it happened. Only problem is I think it may be hard to identify the end of the blade tags, unless they will always be on a line completely by themselves starting with an @ symbol.

What would help is if you could give me some blade template examples that are indicative to what you think I should expect from the common user after they have got their hands on them.

RiFi2k commented 5 years ago

We just got pretty lucky, things are looking super promising now.

https://gist.github.com/mpryvkin/0c46e2493b450f92492e8e9a46ad5d97#file-beautify-html-js

eduardoarandah commented 5 years ago

That sounds clever! What about string-replace into pseudo tags?

@if
@endif 
<if>
</if>

There’s little tags and the rules are simple, for example, the @if ends on the ) Of course that might not work with a simple string-replace because there could be @if(isset(some()))

RiFi2k commented 5 years ago

Are blade files still worth exploring?

eduardoarandah commented 5 years ago

Well...

Wordpress transitioned to gutenberg editor.

ACF pro is making wordpress gutenberg blocks creation super easy using PHP https://www.advancedcustomfields.com/blog/acf-5-8-introducing-acf-blocks-for-gutenberg/

And blade is the easiest way of doing PHP in html.

Gutenberg + ACF + Blade example:

... 
(same code than the example)
...
// render blade template
    echo \App\template("blocks.section-events", compact(
        'id',
        'class',
        'title',
        'subtitle'
    ));

Blade template:

<div class="section section-events {{ $class }} bg-cover" id="{{ $id }}">
  <div class="container">
    <h1 class="title">{{ $title }}</h1>
    <h4 class="subtitle">{{ $subtitle }}</h4>
  </div>
</div>

so... until wordpress theme ecosystem transition to pure JS frontends, PHP and blade will still be used. That's my bet

RiFi2k commented 5 years ago

So the example you just showed me with the {{ }} that should be pretty simple if it doesn't already work. It's more or less handlebars which is already supported.

RiFi2k commented 5 years ago

Yeah that code formatted fine for me, but let me know, or share some more advanced template examples from WP / ACF / Blocks

"html.format.indentHandlebars": false

gustavocastrov commented 5 years ago

I am glad you are already working on adding Laravel Blade templates to "Format HTML in PHP Extension" Nothing there really works well. I am using your extension by manually switching the language mode between blade and php. Please be aware that the actual names for blade templates used two extension "nnnn.blade.php" i.e. view.blade.php Although blade.php files are intended to have only html and blade markup, in reality it end up having html, php, css styles, js, vue whatever the developer wants like in a php file. Beautify managed to add the language "blade" on vscode. That's great but the format is not great. If I use beautify then I have to manually select block by block for each type of code on the file then on the command palette do Beautify Selection and select between html, js, css. With yours it does the entire file smoothly. So I can assure a great success within the Laravel VSCode community. Thank You for your extension.

RiFi2k commented 5 years ago

@gustavocastrov is blade a registered language already in VSCode or are you addingin the language with another plugin. Because I can bind to blade as well as PHP and then you could configure it the same way as PHP, either language scope the format on save for "[blade]" or turn it on always. Plus then you could get the context menu and right click menu options as well.

Edit: just checked you have to be using an extension to add the language support, if you can let me know what the lowercase key for the language is, it shows to the right of it in the Command Palette view like for example C# (csharp)

gustavocastrov commented 5 years ago

Blade is not a registered language in VSCode. User's can't add blade as language because VSCode can't not manage the two extension on blade i.e. nnnn.blade.php. BUT! this extension Laravel Blade Snippets by Winnie Lin managed to do it. Blade Syntax

My suggestions.

gustavocastrov commented 5 years ago

Following previous comment. I found the following insert on the file user on VSCode configuration files /home/gustavo/.config/Code/CachedExtensions/user

Excerpt: "contributes":{"languages":[{"id":"blade","aliases":["Laravel Blade","Blade","blade"],"extensions":[".blade.php"],"configuration":"./blade.configuration.json"}],"grammars":

I intentionally replaced here the owner's personal data by the word DELETED

,{"name":"laravel-blade","displayName":"Laravel Blade Snippets","description":"Laravel blade snippets and syntax highlight support","version":"1.18.0","publisher":"onecentlin","author":{"name":"DELETED DELETED","email":"DELETED@gmail.com","url":"https://devmanna.blogspot.com"},"homepage":"https://github.com/onecentlin/laravel-blade-snippets-vscode","repository":{"type":"git","url":"https://github.com/onecentlin/laravel-blade-snippets-vscode"},"bugs":{"url":"https://github.com/onecentlin/laravel-blade-snippets-vscode/issues"},"engines":{"vscode":"^1.16.0"},"keywords":["laravel","blade","template","snippet","formatter"],"icon":"images/icon.png","galleryBanner":{"color":"#f66f62","theme":"dark"},"categories":["Languages","Snippets","Formatters"],"main":"/home/gustavo/.vscode/extensions/onecentlin.laravel-blade-1.18.0/out/src/extension","scripts":{"build-srv":"cd ./server && npm install && tsc -p ./","vscode:prepublish":"tsc -p ./ && pushd \"./\" && npm run build-srv && popd","compile":"tsc -watch -p ./","postinstall":"node ./node_modules/vscode/bin/install","test":"node ./node_modules/vscode/bin/test"},"contributes":{"languages":[{"id":"blade","aliases":["Laravel Blade","Blade","blade"],"extensions":[".blade.php"],"configuration":"./blade.configuration.json"}],"grammars":[{"language":"blade","scopeName":"text.html.php.blade","path":"./syntaxes/blade.tmLanguage.json","embeddedLanguages":{"source.php":"php","source.css":"css","source.js":"javascript"}}],"snippets":[{"language":"blade","path":"./snippets/snippets.json"},{"language":"blade","path":"./snippets/helpers.json"}],"configuration":{"title":"Blade Configuration","properties":{"blade.format.enable":{"type":"boolean","default":false,"description":"Enable format blade file"}}}},"activationEvents":["onLanguage:blade"],"devDependencies":{"@types/node":"^8.0.17","vscode":"^1.1.6"},"dependencies":{"vscode-css-languageservice":"^3.0.8","vscode-html-languageservice":"^2.1.2","vscode-languageclient":"^3.5.0","vscode-languageserver-types":"^3.5.0"},"uuid":"3b58a227-618a-4b57-a06b-6984a2a8d1ba","isBuiltin":false,"isUnderDevelopment":false,"id":"onecentlin.laravel-blade","identifier":{"value":"onecentlin.laravel-blade","_lower":"onecentlin.laravel-blade"},"extensionLocation":{"$mid":1,"fsPath":"/home/gustavo/.vscode/extensions/onecentlin.laravel-blade-1.18.0","path":"/home/gustavo/.vscode/extensions/onecentlin.laravel-blade-1.18.0","scheme":"file"}},

RiFi2k commented 5 years ago

Ok, that helps alot, I can see now they are going onLanguage: blade.

So something in there must be registering a new language type. I think for sure initally it would be easier to just have everyone install that extension as well to get it to work.

Now I should just be able to activate mine as well on "blade" and "php"

gustavocastrov commented 5 years ago

It will be great if the extension returns different indentation per language. Blade indented at column 0, the rest php, html and at column 4. This will allow users to be able to collapse and extend code by language easily.

RiFi2k commented 5 years ago

@gustavocastrov Can you share a few full random files with me. Ones with blade, php and html all mixed in. Like a complex version of your day to day usecase.

You can strip all the actual code out and replace with dummy none working code, just need the structure and syntax but from something that someone actually uses, not just defaults with no real user love haha

eduardoarandah commented 5 years ago

hey! @RiFi2k

I feel that basically all instructions go in a new line, except those that have no closing tag like @json($array)

Blade files are supposed to look nice and simple, no php inside.

Here are some ugly ones, with all the complexities:

https://gist.github.com/eduardoarandah/08b3a43d5e6d4621037768f9feca3a0c

ctf0 commented 5 years ago

any news on this ?