Inject regex and printf format strings into PHP strings#131
Open
roxblnfk wants to merge 1 commit into
Open
Conversation
Highlight embedded mini-languages inside PHP string arguments via tree-sitter injections: - the pattern argument of `preg_*` calls as a regular expression; - the format string of `printf`/`sprintf`/`vprintf`/`vsprintf` (first argument) and `fprintf`/`vfprintf`/`sscanf`/`fscanf` (second argument) as printf. Heredoc/nowdoc bodies already inject by tag: Zed resolves the tag against language names and file extensions case-insensitively, so `<<<SQL`, `<<<HTML`, `<<<JSON`, `<<<JS`, ... already work without per-language rules.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Highlight embedded mini-languages inside PHP string arguments via tree-sitter injections.
preg_match,preg_match_all,preg_replace,preg_replace_callback,preg_split,preg_grep) is injected asregex, so character classes, groups, quantifiers and anchors are highlighted.preg_quoteis intentionally excluded — its first argument is a literal string to escape, not a pattern.printf/sprintf/vprintf/vsprintf(first argument) andfprintf/vfprintf/sscanf/fscanf(second argument) is injected asprintf. Zed doesn't currently bundle aprintfgrammar, so this is dormant today (a harmless no-op, same as the java extension'sprintfinjection) and lights up automatically if a compatible grammar is registered.Heredoc/nowdoc bodies are untouched: Zed already resolves the tag against language names and file extensions case-insensitively (
language_for_name_or_extension), so<<<SQL,<<<HTML,<<<JSON,<<<JS,<<<YML, ... already inject without per-language rules.Notes
regexis bundled with Zed, so it highlights out of the box. Injection highlights the source text and does not apply PHP's string-escape unescaping, so a pattern is best written in a single-quoted string with single backslashes ('/\w+/'); a doubled backslash ('/\\w+/') is read by the regex grammar as an escaped backslash, which is correct for the source but not PHP's runtime value.