Based on the provided repository code, language ID resolution from file names is primarily handled in the languageDetection.ts file through multiple layers. Let me break down how it works:
The main detection happens in the FilenameAndExensionLanguageDetection class which implements the core logic. Here's the flow:
The language detection process follows these steps:
a. First, it extracts the base filename and extension from the URI
b. It handles template languages specially (like .ejs, .erb, etc.) by potentially stripping the template extension
c. The actual language detection happens in detectLanguageId() method which:
Checks if the exact filename matches known filenames
If not, checks if the extension matches known extensions
If still not found, tries stripping extensions one by one to match known filenames
Returns 'unknown' as a last resort
The known languages and their mappings are defined in generatedLanguages.ts, which contains a comprehensive mapping of file extensions and filenames to language IDs. For example:
typescript
export const knownLanguages = {
javascript: {
extensions: ['.js', '.mjs', '.cjs', ...],
filenames: ['Jakefile']
},
typescript: {
extensions: ['.ts', '.cts', '.mts']
},
// ... many more languages
};
The system also handles special cases:
Template languages (like .ejs, .erb) through knownTemplateLanguageExtensions
Language grouping (e.g., grouping 'c' and 'cpp' into 'cpp') through GroupingLanguageDetection
Client-provided language IDs for untitled or notebook files through ClientProvidedLanguageDetection
The confidence of the language detection is indicated through the isGuess property, which is:
false when there's an exact filename match
true when there are multiple possible languages for an extension
true when falling back to 'unknown'
The entire system is designed to be extensible and handles edge cases like template languages while providing a way to indicate the confidence of the detection.