Skip to content

Support file language detection with more than the first line #69868

Closed

Description

We need a way to associate ARM template files with a language ID separate from the current JSON language. Unfortunately, the existing firstLine filter for languages doesn't suffice for ARM templates (or for any other sub-JSON support). ARM template files are .json files and generally look like this:

{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {

The $schema value is the key to knowing if it’s an ARM template (technically it could be anywhere in the file although it’s usually at the top like this). So we need a way to check further into the file than the first line. Thanks!

Activity

  1. added
    json
    on Mar 5, 2019
  2. added
    feature-request
    and removed
    json
    on Mar 14, 2019
  3. GordonSmith commented on Mar 24, 2019

    @GordonSmith

    Quick "me too" and not limited to JSON - think about XML at the same time...

  4. ak1394 commented on Apr 23, 2019

    @ak1394

    I would like to see this resolved as well. I'm looking at implementing better support for editing Swagger/OpenAPI JSON files. However, there are two major versions of the standard with different JSON Schemas, and the recommended way to tell one from another is to check values of the top level "swagger" or "openapi" fields.

  5. bpasero commented on Apr 25, 2019

    @bpasero
    Contributor

    This will not work (today) even if we had a property to match on the first 3 lines because of #53350. Unless the file extension is different and no registration exists for the file extension, we would not pick up such a contribution. The contributions based on content currently only win if no other registration was picked based on filename, path or extension but in this case our own JSON extension would always be prioritized.

  6. bpasero commented on May 6, 2019

    @bpasero
    Contributor

    After talking to Martin Aeschlimann (@aeschli) and Erich Gamma (@egamma) here is a proposal also based on #10915 and #53350

    Introduce a new preamble matcher

    Today we have the possibility to contribute languages based on firstLine. This is not enough for Azure resources that have marker text spanning multiple lines. We should have a new contribution mechanism preamble that is defined as the first lines of a file (e.g. 3-5). We still need to decide if we require the target text to be fully included in these lines or if we allow glob matching too as we do with firstLine.

    Allow language extensions to override built in extensions by more than one rule

    Even if we allow this new preamble matcher, an extension will not be able to overrule our built in JSON extension because we always first try to match on file extensions and only ever consider textual content if no extension matches based on the file name and extension.

    To enable this, we would need a new way for extensions to contribute a language based on multiple conditions, not just one. For example:

    "contributes": {
      "languages": [
        {
          "id": "azure-json",
          "rules": {
              "extensions": [".json"],
              "preamble": "<some text in Azure JSON resources>"
           }
        }
      ]
    }

    (this format is just an idea taken from #10915 and is up for debate)

    The rule would be that extensions with multiple conditions that match are prioritized over others that only define single conditions.

    //cc alexandrudima

  7. alexdima commented on May 7, 2019

    @alexdima
    Member

    I think we have reached the limits of what can be implemented with a descriptive approach. I suggest the extension:

    • activates on language:json
    • listens on text document open
    • checks if the opened text document is JSON
    • checks if it has "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#"
    • changes the language from JSON to some new language via languages.setTextDocumentLanguage
    • this will trigger a document close and a document open event, but that's OK
  8. ak1394 commented on May 7, 2019

    @ak1394

    alexandrudima How would this approach play with built in 'json-language-features' extension? Currently it supports two languages 'json' and 'jsonc'. Will it have to be updated to include new language IDs?

  9. alexdima commented on May 7, 2019

    @alexdima
    Member

    No changes would be needed to the JSON extension.

  10. ak1394 commented on May 7, 2019

    @ak1394

    alexandrudima This proposal looks good to me. However, in order to supply JSON Schemas for the documents which lack "$schema" attribute, I propose to extend 'json-language-features' extension, to expose API to supply schemas. I've opened a PR #73424 which should give an idea to what I'm looking to do. I would appreciate a feedback on it.

  11. 5 remaining items

  12. added
    workbench-editors
    and removed on Oct 10, 2019
  13. removed this from the Backlog milestone on Oct 10, 2019
  14. changed the title [-]file language detection with more the first line[/-] [+]Support file language detection with more the first line[/+] on Oct 24, 2019
  15. added
    languages-guessing
    and removed
    workbench-editors
    on Oct 24, 2019
  16. vscode-triage-bot commented on Nov 6, 2020

    @vscode-triage-bot

    This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.

    Happy Coding!

  17. changed the title [-]Support file language detection with more the first line[/-] [+]Support file language detection with more than the first line[/+] on Nov 6, 2020
  18. vscode-triage-bot commented on Dec 27, 2020

    @vscode-triage-bot

    This feature request has not yet received the 20 community upvotes it takes to make to our backlog. 10 days to go. To learn more about how we handle feature requests, please see our documentation.

    Happy Coding!

  19. vscode-triage-bot commented on Jan 6, 2021

    @vscode-triage-bot

    🙁 In the last 60 days, this feature request has received less than 20 community upvotes and we closed it. Still a big Thank You to you for taking the time to create this issue! To learn more about how we handle feature requests, please see our documentation.

    Happy Coding!

  20. locked and limited conversation to collaborators on Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestlanguages-guessing

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions