delta
delta copied to clipboard
🐛 PHP syntax highlighting not working properly
The syntax highlighting for PHP only works when there's an opening tag <?php
in the hunk.
<?php
namespace App\Models;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
use Illuminate\Database\Eloquent\Builder;
public function restaurants(): BelongsToMany
{
// Adding a comment
return $this->belongsToMany(Restaurant::class);
}
Hi @gpressutto5, thanks for the report! Delta uses the same language syntax definitions as bat, and bat gets them from upstream repositories (sublime syntax definitions).
I think I'm seeing the behavior you're describing in bat as well as delta, do you agree? If so then this is neither a Delta bug nor a Bat bug.
But beyond that, I'm not entirely sure what's going on. I see sublime syntax definitions have a header key called first_line_match
and that is used in the PHP syntax file used by bat.
But doesn't it seem like that should be irrelevant if the file has the .php
extension?
![]() |
- https://github.com/sharkdp/bat/tree/master/assets/syntaxes
- https://github.com/sublimehq/Packages/blob/759d6eed9b4beed87e602a23303a121c3a6c2fb3/PHP/PHP.sublime-syntax#13
- https://www.sublimetext.com/docs/3/syntax.html
@dandavison It makes sense when highlighting an entire file because the code is only interpreted as PHP after the <?php
opening tag, otherwise it's only plain text. But it doesn't make sense when highlighting only parts of the file because after that tag everything is PHP, so it is best to assume there is an opening tag even if you can't see it in the current hunk, like github does.
The code highlighting works currently will rarely work for PHP the way it is currently.
I am also seeing this bug.
I believe this is related to sublime syntax's support for embedding languages:
Sublime Syntax files support the notion of one syntax definition embedding another. For example, HTML can contain embedded JavaScript. Here's an example of a basic syntax defintion for HTML that does so:
The sublime syntax file appears to assume we start in html
scope until it sees a php opening tag: https://github.com/sublimehq/Packages/blob/759d6eed9b4beed87e602a23303a121c3a6c2fb3/PHP/PHP.sublime-syntax#L18
A php file often contains html that embeds php. Example: https://gist.github.com/dasl-/72242668bf2dde3634802d49a23ef7b6
Other times, a php file may contain pure php and no html, as in the example posted here. I am not sure what the best fix for this would be... it seems like a tricky thing to solve.
Perhaps we could assume that php files do not contain any html and just highlight them as php?
As a workaround for this issue, you can:
This will make delta assume that any file with a .php
extension starts in the PHP scope, rather than HTML scope.
credit to @mjec for this fix
UPDATE: if it doesn't work for you, it might be because your versions of delta and bat are incompatible. This might for instance happen if you installed one of them from a package manager that uses outdated versions. Try using the latest versions of both delta and bat. See: https://github.com/dandavison/delta/issues/895
Another improvement: the default Monokai Extended
theme doesn't do the greatest job of coloring PHP code.
Instead, you can use a custom monokai theme to get better coloring.
Before ("Monokai Extended"):
After ("Monokai (SL)"):
Instructions for how to upgrade your monokai:
I've also seen this issue, even though bat
does proper syntax highlighting.
In this example, I've modified two php
files: one is pure PHP, and one is mixed PHP and HTML.
Now, if I use bat to simply print those two files, I get this:
In bat
, you can see that the PHP sections are appropriately highlighted. However, in delta
, the HTML sections are highlighted as HTML, but the PHP sections are unhighlighted.
@dasl-'s workaround fixed delta
for me.
@goodevilgenius That's because in the first example you have the short open tag <=
.
In the second example, when you're using bat you're highlighting the entire file, so it can see the open tag <?php
in the start of the file.
I couldn't test the workaround yet, but I will later.
@gpressutto5 Good observation. I hadn't actually noticed that the code within the <?=
was properly highlighted in the mixed file. I had just noticed that the pure PHP file wasn't highlighted at all.
That's a good catch. That would also explain why bat
worked just fine, since it had the entire file, rather than just the diff, to work with.
Somewhat related: https://github.com/dandavison/delta/issues/117
@dandavison Would there be a way to enable the workaround purely in delta, without having to install bat?
You don't need bat at run-time for this. But currently using bat cache --build
is the only way to build custom versions of the static syntax-highlighting assets that Delta and Bat use. If that code is available in the bat crate then someone could add the asset-building functionality to delta.