Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot inject grammar to some scopes in Markdown #221682

Closed
KermanX opened this issue Jul 15, 2024 · 10 comments
Closed

Cannot inject grammar to some scopes in Markdown #221682

KermanX opened this issue Jul 15, 2024 · 10 comments
Assignees
Labels
grammar Syntax highlighting grammar upstream Issue identified as 'upstream' component related (exists outside of VS Code)

Comments

@KermanX
Copy link

KermanX commented Jul 15, 2024

Does this issue occur when all extensions are disabled?: Yes. Except the current extension

  • VS Code Version: 1.19.1
  • OS Version: WinNT x64 10.0.22631

Steps to Reproduce:

  1. Clone https://github.com/KermanX/grammar-injection-repro
  2. Start debugging the extension
  3. See this:

The "Success Injection" is injected correctly, while the "Failed Injection" is not.
5. Open ./synatxes/failed.json and ./synatxes/success.json, they are all the same except the injectionSelector.

After some testing, I think most of the scopes can be injected, while the following can't:

  • fenced_code.block.language.attributes.markdown
  • fenced_code.block.language.markdown
  • punctuation.definition.markdown
@vscodenpa
Copy link

Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.91.1. Please try upgrading to the latest version and checking whether this issue remains.

Happy Coding!

@RedCMD
Copy link
Contributor

RedCMD commented Jul 15, 2024

you can only inject in-between begin/end rules or capture groups that have a recapturing patterns array

entity.name.section.markdown is recapturing
image

fenced_code.block.language.attributes.markdown is not
image

https://github.com/mjbvz/vscode-fenced-code-block-grammar-injection-example

@KermanX
Copy link
Author

KermanX commented Jul 15, 2024

you can only inject into begin/end rules or capture groups that have a recapturing patterns array

Thanks for your reply. Then how can I inject grammars into the fenced_code.block.language.attributes.markdown part without re-defining all the code block grammars for every embedded language?

For example, the following grammar is a modified version of Markdown grammar which works injected to text.html.markdown:

    "fenced_code_block_ts": {
      "begin": "(^|\\G)(\\s*)(`{3,}|~{3,})\\s*(?i:(typescript|ts)((\\s+|:|,|\\{|\\?)[^`]*)?$)",
      "beginCaptures": {
        "3": {
          "name": "punctuation.definition.markdown"
        },
        "4": {
          "name": "fenced_code.block.language.markdown"
        },
        "5": {
          "name": "fenced_code.block.language.attributes.markdown",
          "patterns": [
            {
              "include": "#codeblock-attributes" // <--- My grammar
            }
          ]
        }
      },
      "end": "(^|\\G)(\\2|\\s{0,3})(\\3)\\s*$",
      "endCaptures": {
        "3": {
          "name": "punctuation.definition.markdown"
        }
      },
      "name": "markup.fenced_code.block.markdown",
      "patterns": [
        {
          "begin": "(^|\\G)(\\s*)(.*)",
          "contentName": "meta.embedded.block.typescript",
          "patterns": [
            {
              "include": "source.ts"
            }
          ],
          "while": "(^|\\G)(?!\\s*([`~]{3,})\\s*$)"
        }
      ]
    },

However, this is only for TypeScript, and in this way all embedded languages need a new pattern.

@RedCMD
Copy link
Contributor

RedCMD commented Jul 15, 2024

as far as I'm aware
you can't

you could maybe ask @mjbvz kindly to add an empty patterns array under capture 5
but this would also require every single other extension author to do the same (if you wish to support their injections),
wouldn't work on any old version of VSCode/extensions
and might cause other injections that target markup.fenced_code.block.markdown to also inject into it

@KermanX
Copy link
Author

KermanX commented Jul 15, 2024

Thanks for your advice. In that case I may generate all the code blocks on my grammar file to simplify the task. However, I am wondering why the patterns array is required to be injected? Is it possible to change this behavior?

Thanks a lot!

@mjbvz
Copy link
Collaborator

mjbvz commented Jul 16, 2024

What's the goal with these injections?

@KermanX
Copy link
Author

KermanX commented Jul 17, 2024

What's the goal with these injections?

Slidev.js has some special attributes for code blocks. For example, line highlighting. And the expected highlighting is:

image

which is exactly the fenced_code.block.language.attributes.markdown scope.

And it is common for Markdown-based CMS to add its syntax into this scope, for example, VitePress

Sorry for the confusion, I've fixed my last comment which used to say capture groups instead of patterns

@mjbvz
Copy link
Collaborator

mjbvz commented Jul 17, 2024

Thanks. Can you test if adding the empty patterns element to fenced_code.block.language.attributes.markdown would allow you to do the injection?

We can pretty easily add this for all of our built-in languages

@KermanX
Copy link
Author

KermanX commented Jul 17, 2024

Thanks. Can you test if adding the empty patterns element to fenced_code.block.language.attributes.markdown would allow you to do the injection?

Yes, it works. In fact, I've used some code to generate new grammars with patterns: [] for each built-in languages and everything works as expected. So if you feel this grammar scope is too specific, it's completely fine to not update the official grammar file.

May I ask if is there any way to to support injection to any scope without patterns defined? I am not sure why currently this is not allowed.

@mjbvz
Copy link
Collaborator

mjbvz commented Jul 17, 2024

Thanks. Can you please file an issue against https://github.com/microsoft/vscode-textmate for the injection issue

Also if you want to add the empty patterns element, you can do so over in this repo: https://github.com/microsoft/vscode-markdown-tm-grammar

Should just require changing markdown.tmLanguage.base.yaml. See the contributing instructions for more details

@mjbvz mjbvz closed this as completed Jul 17, 2024
@mjbvz mjbvz added upstream Issue identified as 'upstream' component related (exists outside of VS Code) grammar Syntax highlighting grammar labels Jul 17, 2024
@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators Aug 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
grammar Syntax highlighting grammar upstream Issue identified as 'upstream' component related (exists outside of VS Code)
Projects
None yet
Development

No branches or pull requests

4 participants