Monaco-editor: PHP: support for code snippets without the starting `<?php` token

Created on 5 Nov 2019  路  3Comments  路  Source: microsoft/monaco-editor


monaco-editor version: 0.18.1

Steps or JS usage snippet reproducing the issue:

The following snippet doesn't get highlighted properly because the starting <?php token is missing, I think it should get highlighted better, like GitHub is doing, even though the current behavior is probably overall more correct, but kind of too strict.

public function index() {
    $projects = Project::all();

    return view('projects.index', compact('projects'));
}
feature-request monaco-languages

Most helpful comment

TM grammars allow to include named rules from a grammar, so in this case I believe the markdown TM grammar includes directly the inner PHP rules.

For the Monarch PHP parser, the inner PHP is here -- https://github.com/microsoft/monaco-languages/blob/0ed9a6c3e90a24375fab54f7205fb76ce992f117/src/php/php.ts#L204 . AFAIK currently Monarch cannot embed a specific state from within a grammar, so I think we would need a separate php language like you suggest, "php-snippet".

All 3 comments

The PHP grammar assumes that everything outside <?/<?php/?> is HTML. If we would assume directly PHP, without entering PHP, it would break the coloring of all existing PHP files... We would almost need a new language "php-direct" or something...

We would almost need a new language "php-direct" or something...

Yeah maybe just a version of the php grammar where the starting <?/<?php token is optional. I'd call it php-snippet 馃.

It looks like you guys have actually solved this already in vscode, the first editor below contains Markdown and the other two contain PHP, it seems like the starting token is already optional there when embedding PHP inside Markdown (and the token is highlighted inconsistently):

Screen Shot 2019-12-11 at 15 01 30

TM grammars allow to include named rules from a grammar, so in this case I believe the markdown TM grammar includes directly the inner PHP rules.

For the Monarch PHP parser, the inner PHP is here -- https://github.com/microsoft/monaco-languages/blob/0ed9a6c3e90a24375fab54f7205fb76ce992f117/src/php/php.ts#L204 . AFAIK currently Monarch cannot embed a specific state from within a grammar, so I think we would need a separate php language like you suggest, "php-snippet".

Was this page helpful?
0 / 5 - 0 ratings