Skip to content

Allow extension to match a specialized file with an existing extension match #10915

Closed

Description

@rbuckton
  • VSCode Version: 1.5.0-insider
  • OS Version: Windows 10

I'm trying to create a language extension for ecmarkup, which uses the .html file extension. I don't want my language to completely override the existing HTML syntax, just ones with a <!doctype ecmarkup> first line.

However, #2883, plus the general way languages are matched makes this impossible. If I add ".html" to the "extensions" entry for my language, I end up overriding all HTML files, but if I only add a "firstLine" match for something like "^<!doctype\\s+ecmarkup\\b", the HTML syntax is used instead.

What I feel I really need is a more specific set of file matching rules, something like:

{
  "id": "ecmarkup",
  "aliases": ["ecmarkup", "emu"],
  "rules": [
    { "extensions": [".html"], "firstLine": "^<!(doctype|DOCTYPE)\\s+ecmarkup\b" }
  ],
  "configuration": "./ecmarkup.configuration.json"
}

Proposal

With more specific rules, I can specify multiple requirements for a specific match, where precedence is based on the combined weight of each of the requirements.

Effectively, each of the "filenames", "filenamePatterns", "mimeTypes", "extensions", and "firstLine" patterns would internally be transposed into the "rules" collection as:

{
  "rules": [
    { "filenames": [] },
    { "filenamePatterns": [] },
    { "mimeTypes": [] },
    { "extensions": [] },
    { "firstLine": "" }
  ]
}

The following is an example of the proposed interface for these rules (borrowed from the types defined in modeService.ts):

export interface ILanguageServiceExtensionPoint {
    id: string;
    extensions?: string[];
    filenames?: string[];
    filenamePatterns?: string[];
    firstLine?: string;
    aliases?: string[];
    mimetypes?: string[];
    configuration?: string;
    rules?: ILanguageServiceExtensionPointRule[];
}
export interface ILanguageServiceExtensionPointRule {
    filenames?: string[];
    filenamePatterns?: string[];
    extensions?: string[];
    firstLine?: string;
    ignore?: boolean; // If this rule is matched, *do not* use this extension
}
export interface IValidLanguageExtensionPoint {
    id: string;
    aliases: string[];
    mimetypes: string[];
    configuration: string;
    rules: IVerifiedLanguageServiceExtensionPointRule[];
}
export interface IVerifiedLanguageServiceExtensionPointRule {
    filenames: string[];
    filenamePatterns: string[];
    extensions: string[];
    firstLine: string;
    ignore: boolean; // If this rule is matched, *do not* use this extension
}

The approximate weights for each match would be something like:

const enum TextMimeAssociationWeights {
  UserConfiguredMime = 1 << 4, // highest priority
  Filenames = 1 << 3,
  FilenamePatterns = 1 << 2,
  Extensions = 1 << 1,
  FirstLine = 1 << 0, // lowest priority
}

Given how first-line matches are treated in mime.ts, a user-configured rule that contains only "firstLine" would not have the UserConfiguredMime flag added.

The following illustrates this weighting scheme:

User Rule Weight
yes { "extensions": [".html"], "firstLine": "^<!doctype custom" } 21
no { "extensions": [".html"] } 2
yes { "firstLine": "^#!/usr/bin/env node" } 1

Activity

  1. added
    feature-request
    on Aug 25, 2016
  2. removed
    feature-request
    on Aug 25, 2016
  3. bpasero commented on Aug 25, 2016

    @bpasero
    Contributor

    Fair request, this is an area where we do not support this nicely. I think we new about this corner case when we fixed all the other issues in this area.

  4. added
    extensions
    feature-request
    on Aug 25, 2016
  5. changed the title [-]No way for language extension to match a specialized file with an existing extension match[/-] [+]Allow extension to match a specialized file with an existing extension match[/+] on Aug 30, 2016
  6. 10 remaining items

  7. PranayAgarwal commented on Nov 20, 2017

    @PranayAgarwal

    This can be closed now, I think? VS Code started successfully recognizing .php files as Hack without any overrides after a recent update.

  8. bpasero commented on Apr 25, 2019

    @bpasero
    Contributor

    Duplicate of #53350

  9. locked and limited conversation to collaborators on Jun 11, 2019
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

    apiextensionsfeature-request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions