Repository navigation
Expand file tree
/
Copy pathlanguage_specs.ts
More file actions
231 lines (211 loc) · 21.7 KB
/
Copy pathlanguage_specs.ts
File metadata and controls
231 lines (211 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/** The one table of languages token-goat indexes. Every per-language list elsewhere is derived from it: the extension and basename maps detectLanguage reads (parser_types.ts), the read, grep and bash hook gates, the pack fence names, and the display labels. The {@link Language} union itself is derived from the `id` column, so a new row that has no extractor in languages/registry.ts's ADAPTER_EXTRACTORS fails the type check rather than indexing nothing. Adding a language is one row here plus its extractor: see CLAUDE.arch.md "Adding a New Language". Leaf module: no imports, so parser_types.ts and every hook can depend on it without a cycle. */
/** Column meanings, one row per language. */
interface LanguageSpecShape {
readonly id: string
/** How symbols are extracted: a tree-sitter grammar, a regex adapter in languages/registry.ts's ADAPTER_EXTRACTORS, or a branch in parser.ts's extractNoTreeSitter that returns symbols and refs together. */
readonly extraction: 'tree-sitter' | 'regex' | 'own-result'
/** Lowercase extensions with the leading dot. */
readonly extensions: readonly string[]
/** Basenames compared case-insensitively. */
readonly basenames?: readonly string[]
/** Basenames compared exactly, for names too common in lowercase to claim (`BUILD`). */
readonly exactBasenames?: readonly string[]
/** Display spelling for a message a person reads; the id when absent. */
readonly label?: string
/** `symbol`/`read "file::Symbol"` resolve named definitions, so the bash hook upgrades a line-range read to a symbol read. */
readonly symbolBearing: boolean
/** skeleton/outline are the intended re-read tool: the read hook's count-based deny and post-read structure hint. */
readonly sourceHints: boolean
/** A single-file Grep for a `def`/`class`/`function` pattern gets the skeleton hint. */
readonly grepSource: boolean
/** Eligible for diff-on-reread when `hints.serve_diff_on_reread` is on. */
readonly diffable: boolean
/** Code-fence language `token-goat pack` writes; no fence when absent. */
readonly fence?: string
/** Per-extension fence overrides (`.tsx` fences as `tsx`, not `typescript`). */
readonly fenceByExtension?: Readonly<Record<string, string>>
/** The extension `imports` parses a basename-matched file as (a bare `Makefile` reads as `.mk`). */
readonly basenameImportsExtension?: string
/** For a language whose refs are recorded but which `dead` skips: why an empty `refs` answer is still not evidence of an unused name. */
readonly partialRefsReason?: string
}
// Program-language defaults: every hint on.
const CODE = { symbolBearing: true, sourceHints: true, grepSource: true, diffable: true } as const
// Data and markup defaults: every hint off.
const DATA = { symbolBearing: false, sourceHints: false, grepSource: false, diffable: false } as const
export const LANGUAGE_SPECS = [
{ id: 'typescript', extraction: 'tree-sitter', extensions: ['.ts', '.tsx', '.mts', '.cts'], ...CODE, fence: 'typescript', fenceByExtension: { '.tsx': 'tsx' } },
{ id: 'javascript', extraction: 'tree-sitter', extensions: ['.js', '.jsx', '.mjs', '.cjs'], ...CODE, fence: 'javascript', fenceByExtension: { '.jsx': 'jsx' } },
// Bazel and Starlark (`.bzl`, `.star`, BUILD/WORKSPACE/MODULE.bazel) are a Python dialect: `def` and top-level calls such as `load(...)` parse with the Python grammar (https://github.com/bazelbuild/starlark/blob/master/spec.md).
{ id: 'python', extraction: 'tree-sitter', extensions: ['.py', '.pyi', '.bzl', '.star'], basenames: ['build.bazel', 'workspace.bazel', 'module.bazel'], exactBasenames: ['BUILD', 'WORKSPACE'], ...CODE, fence: 'python' },
{ id: 'go', extraction: 'tree-sitter', extensions: ['.go'], ...CODE, fence: 'go' },
{ id: 'rust', extraction: 'tree-sitter', extensions: ['.rs'], ...CODE, fence: 'rust' },
// Rake task files, Gemfile, Rakefile and the other extensionless Ruby DSL files are plain Ruby syntax.
{ id: 'ruby', extraction: 'tree-sitter', extensions: ['.rb', '.ruby', '.rake'], basenames: ['gemfile', 'rakefile', 'vagrantfile', 'guardfile', 'podfile', 'capfile', 'fastfile', 'brewfile'], ...CODE, fence: 'ruby' },
{ id: 'java', extraction: 'tree-sitter', extensions: ['.java'], ...CODE, fence: 'java' },
// `.h` is C unless it declares an Objective-C `@interface` or `@protocol` (refineLanguageByContent); a C++ header parses with the C grammar.
{ id: 'c', extraction: 'tree-sitter', extensions: ['.c', '.h'], label: 'C', ...CODE, fence: 'c' },
{ id: 'cpp', extraction: 'tree-sitter', extensions: ['.cpp', '.cc', '.cxx', '.hpp', '.hxx'], label: 'C++', ...CODE, fence: 'cpp' },
// zsh, ksh and bats scripts share the POSIX `name() {` and `function name {` function forms the bash adapter reads.
{ id: 'bash', extraction: 'regex', extensions: ['.sh', '.bash', '.zsh', '.ksh', '.bats'], ...DATA, symbolBearing: true, fence: 'bash' },
// MDX headings are plain ATX; `.rst` needs an underline heading parser this adapter lacks, so it stays unmapped.
{ id: 'markdown', extraction: 'regex', extensions: ['.md', '.markdown', '.mdx'], ...DATA, fence: 'markdown' },
{ id: 'toml', extraction: 'regex', extensions: ['.toml'], basenames: ['cargo.toml', 'pyproject.toml'], label: 'TOML', ...DATA, diffable: true, fence: 'toml' },
// JSON with comments (`.jsonc`) and Avro schemas (`.avsc`) read with the JSON adapter, which skips `//` and `/* */` comments.
{ id: 'json', extraction: 'regex', extensions: ['.json', '.jsonc', '.avsc'], basenames: ['package.json', 'tsconfig.json'], label: 'JSON', ...DATA, diffable: true, fence: 'json' },
{ id: 'yaml', extraction: 'regex', extensions: ['.yaml', '.yml'], label: 'YAML', ...DATA, diffable: true, fence: 'yaml' },
{ id: 'css', extraction: 'regex', extensions: ['.css', '.scss', '.sass', '.less'], label: 'CSS', ...DATA, grepSource: true, diffable: true, fence: 'css', fenceByExtension: { '.scss': 'scss', '.sass': 'sass', '.less': 'less' } },
{ id: 'dockerfile', extraction: 'regex', extensions: [], basenames: ['dockerfile'], ...DATA, fence: 'dockerfile' },
{ id: 'csharp', extraction: 'regex', extensions: ['.cs'], label: 'C#', ...CODE, fence: 'csharp' },
{ id: 'php', extraction: 'regex', extensions: ['.php', '.inc'], label: 'PHP', ...CODE, fence: 'php' },
{ id: 'html', extraction: 'regex', extensions: ['.html', '.htm'], label: 'HTML', ...DATA, fence: 'html' },
{ id: 'liquid', extraction: 'regex', extensions: ['.liquid'], ...DATA, fence: 'liquid' },
// Six template dialects, each masking its own delimiters out then handing off to the HTML extractor (src/languages/templates_idx.ts) -- markup formats, so DATA defaults like html/liquid.
{ id: 'jinja2', extraction: 'regex', extensions: ['.j2', '.jinja', '.jinja2'], label: 'Jinja2', ...DATA, fence: 'jinja' },
{ id: 'handlebars', extraction: 'regex', extensions: ['.hbs', '.handlebars'], label: 'Handlebars', ...DATA, fence: 'handlebars' },
{ id: 'erb', extraction: 'regex', extensions: ['.erb'], label: 'ERB', ...DATA, fence: 'erb' },
{ id: 'ejs', extraction: 'regex', extensions: ['.ejs'], label: 'EJS', ...DATA, fence: 'ejs' },
{ id: 'nunjucks', extraction: 'regex', extensions: ['.njk'], label: 'Nunjucks', ...DATA, fence: 'html' },
{ id: 'twig', extraction: 'regex', extensions: ['.twig'], label: 'Twig', ...DATA, fence: 'twig' },
{ id: 'kotlin', extraction: 'regex', extensions: ['.kt', '.kts'], ...CODE, fence: 'kotlin' },
{ id: 'swift', extraction: 'regex', extensions: ['.swift'], ...CODE, fence: 'swift' },
{ id: 'scala', extraction: 'regex', extensions: ['.scala', '.sc'], ...CODE, fence: 'scala' },
{ id: 'lua', extraction: 'regex', extensions: ['.lua'], ...CODE, fence: 'lua' },
{ id: 'elixir', extraction: 'regex', extensions: ['.ex', '.exs'], ...CODE, fence: 'elixir' },
{ id: 'dart', extraction: 'regex', extensions: ['.dart'], ...CODE, fence: 'dart' },
{ id: 'zig', extraction: 'regex', extensions: ['.zig'], ...CODE, fence: 'zig' },
{ id: 'r', extraction: 'regex', extensions: ['.r'], ...CODE, fence: 'r' },
{ id: 'graphql', extraction: 'regex', extensions: ['.graphql', '.gql'], label: 'GraphQL', ...DATA, symbolBearing: true, fence: 'graphql' },
// Oracle PL/SQL sources: package spec and body, standalone procedure/function, trigger, and object type spec and body.
{ id: 'sql', extraction: 'regex', extensions: ['.sql', '.pks', '.pkb', '.pls', '.plsql', '.pck', '.prc', '.fnc', '.trg', '.tps', '.tpb'], label: 'SQL', ...DATA, symbolBearing: true, diffable: true, fence: 'sql' },
{ id: 'ini', extraction: 'regex', extensions: ['.ini', '.cfg', '.conf'], label: 'INI', ...DATA, fence: 'ini' },
{ id: 'nginx', extraction: 'regex', extensions: ['.nginx'], basenames: ['nginx.conf'], label: 'Nginx', ...DATA, symbolBearing: true, diffable: true, fence: 'nginx' },
// `basenames` is matched against a lower-cased basename, so `Caddyfile` is spelled in lower case here; the display spelling is the label's job.
{ id: 'caddy', extraction: 'regex', extensions: ['.caddy'], basenames: ['caddyfile', 'caddy.conf'], label: 'Caddyfile', ...DATA, symbolBearing: true, diffable: true, fence: 'caddy' },
{ id: 'apache', extraction: 'regex', extensions: ['.apache', '.apache2'], basenames: ['httpd.conf', 'apache2.conf', '.htaccess'], label: 'Apache Config', ...DATA, symbolBearing: true, diffable: true, fence: 'apache' },
// `.mk` fragments (config.mk, rules.mk) share a bare Makefile's syntax.
{ id: 'makefile', extraction: 'regex', extensions: ['.mk'], basenames: ['makefile', 'gnumakefile', 'bsdmakefile'], ...DATA, fence: 'makefile', basenameImportsExtension: '.mk' },
{ id: 'proto', extraction: 'regex', extensions: ['.proto'], label: 'Protocol Buffers', ...DATA, symbolBearing: true, fence: 'protobuf' },
{ id: 'terraform', extraction: 'regex', extensions: ['.tf', '.tfvars', '.hcl'], ...DATA, symbolBearing: true, fence: 'hcl' },
// `.env` and `.env.<suffix>` are matched by parser_types.ts's DOTENV_VARIANT_RE before this table.
{ id: 'env_file', extraction: 'regex', extensions: ['.env'], basenames: ['.envrc'], label: 'env file', ...DATA },
{ id: 'powershell', extraction: 'regex', extensions: ['.ps1', '.psm1'], label: 'PowerShell', ...CODE, fence: 'powershell' },
// VB.NET, VB6/VBA standard modules, VBScript and VB6 forms. A VB6 class module shares `.cls` with Apex (and LaTeX) and is told apart by content in refineLanguageByContent.
{ id: 'vb', extraction: 'regex', extensions: ['.vb', '.bas', '.vbs', '.frm'], label: 'Visual Basic', ...CODE, fence: 'vb', fenceByExtension: { '.vb': 'vbnet', '.vbs': 'vbscript' } },
{
id: 'cobol', extraction: 'own-result', extensions: ['.cbl', '.cob', '.cobol', '.cpy', '.cop', '.cpb'], label: 'COBOL', ...CODE, fence: 'cobol',
partialRefsReason: "PERFORM, GO TO and CALL 'literal' are recorded as references, but a paragraph also runs by falling through from the one above it and a program can be called through a data item holding its name, so `dead` skips COBOL",
},
// Natural object sources as NaturalONE and SYSOBJH export them. Maps (.nsm) and DDMs (.nsd) are layouts, not code, and stay unmapped.
{
id: 'natural', extraction: 'own-result', extensions: ['.nsp', '.nsn', '.nss', '.nsa', '.nsl', '.nsg', '.nsc', '.nsh'], label: 'Natural', ...CODE, fence: 'natural',
partialRefsReason: "PERFORM, CALLNAT 'literal' and FETCH 'literal' are recorded as references, but an object can also be called through a variable holding its name, so `dead` skips Natural",
},
{ id: 'abap', extraction: 'regex', extensions: ['.abap'], label: 'ABAP', ...CODE, fence: 'abap' },
{ id: 'sas', extraction: 'regex', extensions: ['.sas'], label: 'SAS', ...CODE, fence: 'sas' },
{ id: 'pli', extraction: 'regex', extensions: ['.pli', '.pl1'], label: 'PL/I', ...CODE, fence: 'pli' },
// `.rpg` stays unmapped: RPG II and RPG III sources use it, and their fixed layout predates the ILE RPG forms this adapter reads.
{ id: 'rpg', extraction: 'regex', extensions: ['.rpgle', '.sqlrpgle'], label: 'RPG', ...CODE, fence: 'rpgle' },
{ id: 'jcl', extraction: 'regex', extensions: ['.jcl'], label: 'JCL', ...CODE, fence: 'jcl' },
// `.mm` is always Objective-C++. A `.m` (MATLAB uses it too) is Objective-C only on an `#import`, `@interface`, `@implementation` or `@protocol` line, and a `.h` only on `@interface` or `@protocol`: refineLanguageByContent in parser_types.ts decides.
{ id: 'objc', extraction: 'regex', extensions: ['.mm'], label: 'Objective-C', ...CODE, fence: 'objectivec' },
// Gradle build scripts and Jenkinsfiles are Groovy.
{ id: 'groovy', extraction: 'regex', extensions: ['.groovy', '.gvy', '.gradle'], basenames: ['jenkinsfile'], label: 'Groovy', ...CODE, fence: 'groovy', basenameImportsExtension: '.groovy' },
// A Prolog `.pl` stays unknown (refineLanguageByContent), and a `.t` is Perl only on a Perl marker line.
{ id: 'perl', extraction: 'regex', extensions: ['.pl', '.pm'], label: 'Perl', ...CODE, fence: 'perl' },
{ id: 'solidity', extraction: 'regex', extensions: ['.sol'], label: 'Solidity', ...CODE, fence: 'solidity' },
{ id: 'thrift', extraction: 'regex', extensions: ['.thrift'], label: 'Thrift', ...DATA, symbolBearing: true, fence: 'thrift' },
{ id: 'glsl', extraction: 'regex', extensions: ['.glsl', '.vert', '.frag', '.comp', '.geom', '.tesc', '.tese'], label: 'GLSL', ...CODE, fence: 'glsl' },
// `.fx` stays unmapped: other languages use it too.
{ id: 'hlsl', extraction: 'regex', extensions: ['.hlsl', '.hlsli'], label: 'HLSL', ...CODE, fence: 'hlsl' },
{ id: 'wgsl', extraction: 'regex', extensions: ['.wgsl'], label: 'WGSL', ...CODE, fence: 'wgsl' },
{ id: 'metal', extraction: 'regex', extensions: ['.metal'], label: 'Metal', ...CODE, fence: 'metal' },
// `.f`, `.for` and `.f77` are read as fixed form unless code starts in column 1. `.fpp` stays unmapped: it is used for both forms.
{ id: 'fortran', extraction: 'regex', extensions: ['.f', '.for', '.f77', '.f90', '.f95', '.f03', '.f08'], label: 'Fortran', ...CODE, fence: 'fortran' },
// `.inc` is assigned to PHP for legacy includes, not Pascal; a `.pp` (Puppet uses it too) is Pascal only on a unit, program or library header: refineLanguageByContent in parser_types.ts decides. Text-form `.dfm` forms list their components.
{ id: 'pascal', extraction: 'regex', extensions: ['.pas', '.dpr', '.dpk', '.lpr', '.dfm'], label: 'Pascal', ...CODE, fence: 'pascal' },
// MATLAB has no extension of its own: a `.m` that is not Objective-C is MATLAB only on a `function` or `classdef` header line, which refineLanguageByContent in parser_types.ts checks, so a Mathematica or Mercury `.m` stays unknown.
{ id: 'matlab', extraction: 'regex', extensions: [], label: 'MATLAB', ...CODE, fence: 'matlab' },
{ id: 'cmake', extraction: 'regex', extensions: ['.cmake'], basenames: ['cmakelists.txt'], label: 'CMake', ...CODE, fence: 'cmake', basenameImportsExtension: '.cmake' },
// One adapter for the three assembly dialects that share these extensions: GNU as (`.s`, and `.S` through the lowercase lookup), NASM (`.asm`, `.nasm`) and IBM High Level Assembler (`.asm`), which the adapter tells apart by content. `.inc` belongs to PHP legacy includes, not assembly, despite its use elsewhere.
{ id: 'asm', extraction: 'regex', extensions: ['.s', '.asm', '.nasm'], label: 'Assembly', ...CODE, fence: 'asm' },
{ id: 'batch', extraction: 'regex', extensions: ['.bat', '.cmd'], label: 'Windows batch', ...CODE, fence: 'batch' },
{ id: 'erlang', extraction: 'regex', extensions: ['.erl', '.hrl'], label: 'Erlang', ...CODE, fence: 'erlang' },
{ id: 'vhdl', extraction: 'regex', extensions: ['.vhd', '.vhdl'], label: 'VHDL', ...CODE, fence: 'vhdl' },
// Five Lisp-family dialects, each its own row: none shares an extractor (see common_lisp.ts's module doc for why their lexical rules stay separate rather than a single Lisp masker).
{ id: 'common_lisp', extraction: 'regex', extensions: ['.lisp', '.lsp', '.cl'], label: 'Common Lisp', ...CODE, fence: 'lisp' },
{ id: 'scheme', extraction: 'regex', extensions: ['.scm', '.ss'], label: 'Scheme', ...CODE, fence: 'scheme' },
{ id: 'racket', extraction: 'regex', extensions: ['.rkt', '.rktl'], label: 'Racket', ...CODE, fence: 'racket' },
{ id: 'clojure', extraction: 'regex', extensions: ['.clj', '.cljs', '.cljc'], label: 'Clojure', ...CODE, fence: 'clojure' },
{ id: 'emacs_lisp', extraction: 'regex', extensions: ['.el'], label: 'Emacs Lisp', ...CODE, fence: 'lisp' },
// `.lhs` (literate Haskell) is deliberately not claimed here -- see haskell.ts's module doc.
{ id: 'haskell', extraction: 'regex', extensions: ['.hs'], label: 'Haskell', ...CODE, fence: 'haskell' },
// `.mli` interface files share `.ml`'s lexical rules (comments, strings, quoted strings) and are read with the same extractor -- see ocaml.ts's module doc.
{ id: 'ocaml', extraction: 'regex', extensions: ['.ml', '.mli'], label: 'OCaml', ...CODE, fence: 'ocaml' },
// `.fsi` signature files and `.fsx` scripts share `.fs`'s lexical rules and are read with the same extractor -- see fsharp.ts's module doc.
{ id: 'fsharp', extraction: 'regex', extensions: ['.fs', '.fsi', '.fsx'], label: 'F#', ...CODE, fence: 'fsharp' },
{ id: 'nix', extraction: 'regex', extensions: ['.nix'], label: 'Nix', ...CODE, fence: 'nix' },
// OpenEdge ABL has no extension of its own: a `.p` or `.w` (Pascal and CWEB use them too) or a `.cls` (Apex, VB6, LaTeX) is ABL only when its head carries an ABL marker, which refineLanguageByContent in parser_types.ts checks. The path-only hooks see a `.p` or `.w` as unknown and a `.cls` as Apex.
{ id: 'abl', extraction: 'regex', extensions: [], label: 'OpenEdge ABL', ...CODE, fence: 'abl' },
{ id: 'apex', extraction: 'regex', extensions: ['.cls', '.trigger'], label: 'Apex', ...CODE, fence: 'apex' },
// Matched by the `-meta.xml` suffix in detectLanguage, not by an extension.
{ id: 'salesforce_metadata', extraction: 'regex', extensions: [], label: 'Salesforce metadata', ...DATA, symbolBearing: true, sourceHints: true, grepSource: true, fence: 'xml' },
{ id: 'salesforce_markup', extraction: 'own-result', extensions: ['.cmp', '.app', '.evt', '.intf', '.design', '.auradoc', '.tokens', '.page', '.component', '.email'], label: 'Salesforce markup', ...DATA, symbolBearing: true, sourceHints: true, grepSource: true, fence: 'xml' },
{ id: 'vue', extraction: 'own-result', extensions: ['.vue'], ...DATA, fence: 'vue' },
{ id: 'svelte', extraction: 'own-result', extensions: ['.svelte'], ...DATA, fence: 'svelte' },
{ id: 'astro', extraction: 'own-result', extensions: ['.astro'], ...DATA, fence: 'astro' },
// Notebooks index through their code cells as Python, in parser.ts's ipynb branch.
{ id: 'ipynb', extraction: 'own-result', extensions: ['.ipynb'], label: 'Jupyter notebook', ...DATA, fence: 'json' },
] as const satisfies readonly LanguageSpecShape[]
type Spec = (typeof LANGUAGE_SPECS)[number]
/** Languages token-goat can recognise. `unknown` is the catch-all fallback. */
export type Language = Spec['id'] | 'unknown'
/** Languages indexed through a tree-sitter grammar. */
export type TreeSitterLanguage = Extract<Spec, { extraction: 'tree-sitter' }>['id']
/** Languages whose regex adapter must have an entry in languages/registry.ts's ADAPTER_EXTRACTORS (or, for the structured-document formats, in parser.ts's own half of that table). */
export type RegexLanguage = Extract<Spec, { extraction: 'regex' }>['id']
/** Languages parser.ts's extractNoTreeSitter handles in a branch of their own. */
export type OwnResultLanguage = Extract<Spec, { extraction: 'own-result' }>['id']
/** The per-path boolean columns a hook can ask about. */
export type LanguageFlag = 'symbolBearing' | 'sourceHints' | 'grepSource' | 'diffable'
const SPEC_BY_ID: ReadonlyMap<string, LanguageSpecShape> = new Map(LANGUAGE_SPECS.map((s) => [s.id, s]))
function rows(): readonly LanguageSpecShape[] {
return LANGUAGE_SPECS
}
/** Lowercase extension (with the dot) to language. */
export const EXTENSION_LANGUAGE: ReadonlyMap<string, Language> = new Map(
LANGUAGE_SPECS.flatMap((s) => s.extensions.map((e): [string, Language] => [e, s.id])),
)
/** Lowercase basename to language. */
export const FILENAME_LANGUAGE: ReadonlyMap<string, Language> = new Map(
rows().flatMap((s) => (s.basenames ?? []).map((b): [string, Language] => [b, s.id as Language])),
)
/** Exact-case basename to language. */
export const EXACT_FILENAME_LANGUAGE: ReadonlyMap<string, Language> = new Map(
rows().flatMap((s) => (s.exactBasenames ?? []).map((b): [string, Language] => [b, s.id as Language])),
)
/** The languages indexed through a tree-sitter grammar when the optional `tree-sitter` package loads; without it they fall back to a coarse regex scan with no references. */
export const TREE_SITTER_LANGUAGES: readonly Language[] = LANGUAGE_SPECS.filter((s) => s.extraction === 'tree-sitter').map((s) => s.id)
/** True when `language`'s row sets `flag`. `unknown` has no row and every flag off. */
export function languageHasFlag(language: Language, flag: LanguageFlag): boolean {
return SPEC_BY_ID.get(language)?.[flag] === true
}
/** Human spelling of a language id, for a message a person reads. */
export function languageLabel(language: Language): string {
if (language === 'unknown') return 'this file type'
return SPEC_BY_ID.get(language)?.label ?? language
}
/** The pack code-fence name for a file of `language` with lowercase extension `ext`, or `''` for none. */
export function fenceFor(language: Language, ext: string): string {
const spec = SPEC_BY_ID.get(language)
if (spec === undefined) return ''
return spec.fenceByExtension?.[ext] ?? spec.fence ?? ''
}
/** The extension `imports` parses a basename-matched file of `language` as, or undefined. */
export function basenameImportsExtension(language: Language): string | undefined {
return SPEC_BY_ID.get(language)?.basenameImportsExtension
}
/** Why an empty refs answer is not evidence of dead code in a language whose refs are recorded but which `dead` skips; undefined for every other language. */
export function partialRefsReason(language: Language): string | undefined {
return SPEC_BY_ID.get(language)?.partialRefsReason
}