Repository navigation
Description
- 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
bpasero commentedon Aug 25, 2016
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.
[-]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[/+]10 remaining items
PranayAgarwal commentedon Nov 20, 2017
This can be closed now, I think? VS Code started successfully recognizing
.phpfiles as Hack without any overrides after a recent update.bpasero commentedon Apr 25, 2019
Duplicate of #53350