Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/languages.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import { extname } from '../vs/base/common/resources';
6
import { URI } from '../vs/base/common/uri';
7
8
/**
9
* Interface for writing single-line comments in a given language.
10
* Does not include the terminal new-line character (i.e. for many languages,
11
* `end` will just be the empty string).
12
*/
13
interface CommentMarker {
14
readonly start: string;
15
readonly end?: string;
16
}
17
18
/**
19
* A tuple of two characters, like a pair of
20
* opening and closing brackets.
21
*/
22
export type CharacterPair = [string, string];
23
24
export interface ILanguageInfo {
25
readonly aliases?: string[];
26
readonly extensions?: string[];
27
readonly lineComment: CommentMarker;
28
/**
29
* The block comment character pair, like `/* block comment */`
30
*/
31
readonly blockComment?: CharacterPair;
32
readonly alternativeLineComments?: CommentMarker[];
33
readonly markdownLanguageIds?: string[]; // if not set, defaults to the language id
34
}
35
36
export interface ILanguage extends ILanguageInfo {
37
readonly languageId: string;
38
}
39
40
/**
41
* Well known language [from VSCode](https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers)
42
* Markdown ids from https://raw.githubusercontent.com/highlightjs/highlight.js/refs/heads/main/SUPPORTED_LANGUAGES.md
43
*/
44
const languages = Object.freeze({
45
'abap': {
46
lineComment: { start: '\'' },
47
markdownLanguageIds: ['abap', 'sap-abap']
48
},
49
'bat': {
50
lineComment: { start: 'REM' },
51
alternativeLineComments: [{ start: '::' }],
52
aliases: [
53
'Batch',
54
'bat'
55
],
56
extensions: [
57
'.bat',
58
'.cmd'
59
],
60
},
61
'bibtex': {
62
lineComment: { start: '%' },
63
aliases: [
64
'BibTeX',
65
'bibtex'
66
],
67
extensions: [
68
'.bib'
69
]
70
},
71
'blade': {
72
lineComment: { start: '#' }
73
},
74
'c': {
75
lineComment: { start: '//' },
76
aliases: [
77
'C',
78
'c'
79
],
80
extensions: [
81
'.c',
82
'.i'
83
],
84
markdownLanguageIds: ['c', 'h']
85
},
86
'clojure': {
87
lineComment: { start: ';' },
88
aliases: [
89
'Clojure',
90
'clojure'
91
],
92
extensions: [
93
'.clj',
94
'.cljs',
95
'.cljc',
96
'.cljx',
97
'.clojure',
98
'.edn'
99
],
100
markdownLanguageIds: ['clojure', 'clj']
101
},
102
'coffeescript': {
103
lineComment: { start: '//' },
104
aliases: [
105
'CoffeeScript',
106
'coffeescript',
107
'coffee'
108
],
109
extensions: [
110
'.coffee',
111
'.cson',
112
'.iced'
113
],
114
markdownLanguageIds: ['coffeescript', 'coffee', 'cson', 'iced'],
115
blockComment: ['###', '###']
116
},
117
'cpp': {
118
lineComment: { start: '//' },
119
aliases: [
120
'C++',
121
'Cpp',
122
'cpp'
123
],
124
extensions: [
125
'.cpp',
126
'.cc',
127
'.cxx',
128
'.c++',
129
'.hpp',
130
'.hh',
131
'.hxx',
132
'.h++',
133
'.h',
134
'.ii',
135
'.ino',
136
'.inl',
137
'.ipp',
138
'.ixx',
139
'.tpp',
140
'.txx',
141
'.hpp.in',
142
'.h.in'
143
],
144
markdownLanguageIds: ['cpp', 'hpp', 'cc', 'hh', 'c++', 'h++', 'cxx', 'hxx'],
145
blockComment: ['/*', '*/']
146
},
147
'csharp': {
148
lineComment: { start: '//' },
149
aliases: [
150
'C#',
151
'csharp'
152
],
153
extensions: [
154
'.cs',
155
'.csx',
156
'.cake'
157
],
158
markdownLanguageIds: ['csharp', 'cs'],
159
blockComment: ['/*', '*/']
160
},
161
'css': {
162
lineComment: { start: '/*', end: '*/' },
163
aliases: [
164
'CSS',
165
'css'
166
],
167
extensions: [
168
'.css'
169
],
170
blockComment: ['/*', '*/']
171
},
172
'dart': {
173
lineComment: { start: '//' },
174
aliases: [
175
'Dart'
176
],
177
extensions: [
178
'.dart'
179
],
180
blockComment: ['/*', '*/']
181
},
182
'dockerfile': {
183
lineComment: { start: '#' },
184
aliases: [
185
'Docker',
186
'Dockerfile',
187
'Containerfile'
188
],
189
extensions: [
190
'.dockerfile',
191
'.containerfile'
192
],
193
markdownLanguageIds: ['dockerfile', 'docker']
194
},
195
'elixir': {
196
lineComment: { start: '#' },
197
},
198
'erb': {
199
lineComment: { start: '<%#', end: '%>' }
200
},
201
'erlang': {
202
lineComment: { start: '%' },
203
markdownLanguageIds: ['erlang', 'erl']
204
},
205
'fsharp': {
206
lineComment: { start: '//' },
207
aliases: [
208
'F#',
209
'FSharp',
210
'fsharp'
211
],
212
extensions: [
213
'.fs',
214
'.fsi',
215
'.fsx',
216
'.fsscript'
217
],
218
markdownLanguageIds: ['fsharp', 'fs', 'fsx', 'fsi', 'fsscript'],
219
blockComment: ['(*', '*)']
220
},
221
'go': {
222
lineComment: { start: '//' },
223
aliases: [
224
'Go'
225
],
226
extensions: [
227
'.go'
228
],
229
markdownLanguageIds: ['go', 'golang'],
230
blockComment: ['/*', '*/']
231
},
232
'groovy': {
233
lineComment: { start: '//' },
234
aliases: [
235
'Groovy',
236
'groovy'
237
],
238
extensions: [
239
'.groovy',
240
'.gvy',
241
'.gradle',
242
'.jenkinsfile',
243
'.nf'
244
],
245
blockComment: [
246
'/*',
247
'*/'
248
]
249
},
250
'haml': {
251
lineComment: { start: '-#' }
252
},
253
'handlebars': {
254
lineComment: { start: '{{!', end: '}}' },
255
extensions: [
256
'.hbs',
257
'.handlebars'
258
],
259
markdownLanguageIds: ['handlebars', 'hbs', 'html.hbs', 'html.handlebars'],
260
blockComment: [
261
'{{!--',
262
'--}}'
263
]
264
},
265
'haskell': {
266
lineComment: { start: '--' },
267
markdownLanguageIds: ['haskell', 'hs']
268
},
269
'html': {
270
lineComment: { start: '<!--', end: '-->' },
271
aliases: [
272
'HTML',
273
'htm',
274
'html',
275
'xhtml'
276
],
277
extensions: [
278
'.html',
279
'.htm',
280
'.shtml',
281
'.xhtml',
282
'.xht',
283
'.mdoc',
284
'.jsp',
285
'.asp',
286
'.aspx',
287
'.jshtm',
288
'.volt',
289
'.ejs',
290
'.rhtml'
291
],
292
markdownLanguageIds: ['html', 'xhtml'],
293
blockComment: [
294
'<!--',
295
'-->'
296
]
297
},
298
'ini': {
299
lineComment: { start: ';' },
300
blockComment: [
301
';',
302
' '
303
]
304
},
305
'java': {
306
lineComment: { start: '//' },
307
extensions: [
308
'.java',
309
'.class'
310
],
311
markdownLanguageIds: ['java', 'jsp'],
312
blockComment: [
313
'/*',
314
'*/'
315
]
316
},
317
'javascript': {
318
lineComment: { start: '//' },
319
aliases: [
320
'JavaScript',
321
'javascript',
322
'js'
323
],
324
extensions: [
325
'.js',
326
'.es6',
327
'.mjs',
328
'.cjs',
329
'.pac'
330
],
331
markdownLanguageIds: ['javascript', 'js'],
332
blockComment: [
333
'/*',
334
'*/'
335
]
336
},
337
'javascriptreact': {
338
lineComment: { start: '//' },
339
aliases: [
340
'JavaScript JSX',
341
'JavaScript React',
342
'jsx'
343
],
344
extensions: [
345
'.jsx'
346
],
347
markdownLanguageIds: ['jsx']
348
},
349
'json': {
350
extensions: [
351
'.json',
352
],
353
lineComment: { start: '//' },
354
blockComment: [
355
'/*',
356
'*/'
357
]
358
},
359
'jsonc': {
360
lineComment: { start: '//' }
361
},
362
'jsx': {
363
lineComment: { start: '//' },
364
markdownLanguageIds: ['jsx']
365
},
366
'julia': {
367
lineComment: { start: '#' },
368
aliases: [
369
'Julia',
370
'julia'
371
],
372
extensions: [
373
'.jl'
374
],
375
markdownLanguageIds: ['julia', 'jl'],
376
blockComment: [
377
'#=',
378
'=#'
379
]
380
},
381
'kotlin': {
382
lineComment: { start: '//' },
383
markdownLanguageIds: ['kotlin', 'kt']
384
},
385
'latex': {
386
lineComment: { start: '%' },
387
aliases: [
388
'LaTeX',
389
'latex'
390
],
391
extensions: [
392
'.tex',
393
'.ltx',
394
'.ctx'
395
],
396
markdownLanguageIds: ['tex']
397
},
398
'less': {
399
lineComment: { start: '//' },
400
aliases: [
401
'Less',
402
'less'
403
],
404
extensions: [
405
'.less'
406
],
407
blockComment: [
408
'/*',
409
'*/'
410
]
411
},
412
'lua': {
413
lineComment: { start: '--' },
414
aliases: [
415
'Lua',
416
'lua'
417
],
418
extensions: [
419
'.lua'
420
],
421
markdownLanguageIds: ['lua', 'pluto'],
422
blockComment: [
423
'--[[',
424
']]'
425
]
426
},
427
'makefile': {
428
lineComment: { start: '#' },
429
aliases: [
430
'Makefile',
431
'makefile'
432
],
433
extensions: [
434
'.mak',
435
'.mk'
436
],
437
markdownLanguageIds: ['makefile', 'mk', 'mak', 'make']
438
},
439
'markdown': {
440
lineComment: { start: '<!--', end: '-->' },
441
alternativeLineComments: [
442
{ start: '[]: #' }
443
],
444
aliases: [
445
'Markdown',
446
'markdown'
447
],
448
extensions: [
449
'.md',
450
'.mkd',
451
'.mdwn',
452
'.mdown',
453
'.markdown',
454
'.markdn',
455
'.mdtxt',
456
'.mdtext',
457
'.workbook'
458
],
459
markdownLanguageIds: ['markdown', 'md', 'mkdown', 'mkd']
460
},
461
'objective-c': {
462
lineComment: { start: '//' },
463
aliases: [
464
'Objective-C'
465
],
466
extensions: [
467
'.m'
468
],
469
markdownLanguageIds: ['objectivec', 'mm', 'objc', 'obj-c'],
470
blockComment: [
471
'/*',
472
'*/'
473
]
474
},
475
'objective-cpp': {
476
lineComment: { start: '//' },
477
aliases: [
478
'Objective-C++'
479
],
480
extensions: [
481
'.mm'
482
],
483
markdownLanguageIds: ['objectivec++', 'objc+']
484
},
485
'perl': {
486
lineComment: { start: '#' },
487
aliases: [
488
'Perl',
489
'perl'
490
],
491
extensions: [
492
'.pl',
493
'.pm',
494
'.pod',
495
'.t',
496
'.PL',
497
'.psgi'
498
],
499
markdownLanguageIds: ['perl', 'pl', 'pm']
500
},
501
'php': {
502
lineComment: { start: '//' },
503
aliases: [
504
'PHP',
505
'php'
506
],
507
extensions: [
508
'.php',
509
'.php4',
510
'.php5',
511
'.phtml',
512
'.ctp'
513
],
514
blockComment: [
515
'/*',
516
'*/'
517
]
518
},
519
'powershell': {
520
lineComment: { start: '#' },
521
aliases: [
522
'PowerShell',
523
'powershell',
524
'ps',
525
'ps1'
526
],
527
extensions: [
528
'.ps1',
529
'.psm1',
530
'.psd1',
531
'.pssc',
532
'.psrc'
533
],
534
markdownLanguageIds: ['powershell', 'ps', 'ps1'],
535
blockComment: [
536
'<#',
537
'#>'
538
]
539
},
540
'pug': {
541
lineComment: { start: '//' }
542
},
543
'python': {
544
lineComment: { start: '#' },
545
aliases: [
546
'Python',
547
'py'
548
],
549
extensions: [
550
'.py',
551
'.rpy',
552
'.pyw',
553
'.cpy',
554
'.gyp',
555
'.gypi',
556
'.pyi',
557
'.ipy',
558
'.pyt'
559
],
560
markdownLanguageIds: ['python', 'py', 'gyp'],
561
blockComment: [
562
'"""',
563
'"""'
564
]
565
},
566
'ql': {
567
lineComment: { start: '//' }
568
},
569
'r': {
570
lineComment: { start: '#' },
571
aliases: [
572
'R',
573
'r'
574
],
575
extensions: [
576
'.r',
577
'.rhistory',
578
'.rprofile',
579
'.rt'
580
]
581
},
582
'razor': {
583
lineComment: { start: '<!--', end: '-->' },
584
aliases: [
585
'Razor',
586
'razor'
587
],
588
extensions: [
589
'.cshtml',
590
'.razor'
591
],
592
markdownLanguageIds: ['cshtml', 'razor', 'razor-cshtml'],
593
blockComment: [
594
'<!--',
595
'-->'
596
]
597
},
598
'ruby': {
599
lineComment: { start: '#' },
600
aliases: [
601
'Ruby',
602
'rb'
603
],
604
extensions: [
605
'.rb',
606
'.rbx',
607
'.rjs',
608
'.gemspec',
609
'.rake',
610
'.ru',
611
'.erb',
612
'.podspec',
613
'.rbi'
614
],
615
markdownLanguageIds: ['ruby', 'rb', 'gemspec', 'podspec', 'thor', 'irb'],
616
blockComment: [
617
'=begin',
618
'=end'
619
]
620
},
621
'rust': {
622
lineComment: { start: '//' },
623
aliases: [
624
'Rust',
625
'rust'
626
],
627
extensions: [
628
'.rs'
629
],
630
markdownLanguageIds: ['rust', 'rs'],
631
blockComment: [
632
'/*',
633
'*/'
634
]
635
},
636
'sass': {
637
lineComment: { start: '//' }
638
},
639
'scala': {
640
lineComment: { start: '//' }
641
},
642
'scss': {
643
lineComment: { start: '//' },
644
aliases: [
645
'SCSS',
646
'scss'
647
],
648
extensions: [
649
'.scss'
650
],
651
blockComment: [
652
'/*',
653
'*/'
654
]
655
},
656
'shellscript': {
657
lineComment: { start: '#' },
658
aliases: [
659
'Shell Script',
660
'shellscript',
661
'bash',
662
'fish',
663
'sh',
664
'zsh',
665
'ksh',
666
'csh'
667
],
668
extensions: [
669
'.sh',
670
'.bash',
671
'.bashrc',
672
'.bash_aliases',
673
'.bash_profile',
674
'.bash_login',
675
'.ebuild',
676
'.profile',
677
'.bash_logout',
678
'.xprofile',
679
'.xsession',
680
'.xsessionrc',
681
'.Xsession',
682
'.zsh',
683
'.zshrc',
684
'.zprofile',
685
'.zlogin',
686
'.zlogout',
687
'.zshenv',
688
'.zsh-theme',
689
'.fish',
690
'.ksh',
691
'.csh',
692
'.cshrc',
693
'.tcshrc',
694
'.yashrc',
695
'.yash_profile'
696
],
697
markdownLanguageIds: ['bash', 'sh', 'zsh']
698
},
699
'slim': {
700
lineComment: { start: '/' }
701
},
702
'solidity': {
703
lineComment: { start: '//' },
704
markdownLanguageIds: ['solidity', 'sol']
705
},
706
'sql': {
707
lineComment: { start: '--' },
708
aliases: [
709
'SQL'
710
],
711
extensions: [
712
'.sql',
713
'.dsql'
714
],
715
blockComment: [
716
'/*',
717
'*/'
718
]
719
},
720
'stylus': {
721
lineComment: { start: '//' }
722
},
723
'svelte': {
724
lineComment: { start: '<!--', end: '-->' }
725
},
726
'swift': {
727
lineComment: { start: '//' },
728
aliases: [
729
'Swift',
730
'swift'
731
],
732
extensions: [
733
'.swift'
734
],
735
blockComment: [
736
'/*',
737
'*/'
738
]
739
},
740
'terraform': {
741
lineComment: { start: '#' }
742
},
743
'tex': {
744
lineComment: { start: '%' },
745
aliases: [
746
'TeX',
747
'tex'
748
],
749
extensions: [
750
'.sty',
751
'.cls',
752
'.bbx',
753
'.cbx'
754
]
755
},
756
'typescript': {
757
lineComment: { start: '//' },
758
aliases: [
759
'TypeScript',
760
'ts',
761
'typescript'
762
],
763
extensions: [
764
'.ts',
765
'.cts',
766
'.mts'
767
],
768
markdownLanguageIds: ['typescript', 'ts'],
769
blockComment: [
770
'/*',
771
'*/'
772
]
773
},
774
'typescriptreact': {
775
lineComment: { start: '//' },
776
aliases: [
777
'TypeScript JSX',
778
'TypeScript React',
779
'tsx'
780
],
781
extensions: [
782
'.tsx'
783
],
784
markdownLanguageIds: ['tsx'],
785
blockComment: [
786
'/*',
787
'*/'
788
]
789
},
790
'vb': {
791
lineComment: { start: '\'' },
792
aliases: [
793
'Visual Basic',
794
'vb'
795
],
796
extensions: [
797
'.vb',
798
'.brs',
799
'.vbs',
800
'.bas',
801
'.vba'
802
],
803
markdownLanguageIds: ['vb', 'vbscript']
804
},
805
'verilog': {
806
lineComment: { start: '//' }
807
},
808
'vue-html': {
809
lineComment: { start: '<!--', end: '-->' }
810
},
811
'vue': {
812
lineComment: { start: '//' },
813
extensions: [
814
'.vue'
815
]
816
},
817
'xml': {
818
lineComment: { start: '<!--', end: '-->' },
819
aliases: [
820
'XML',
821
'xml'
822
],
823
extensions: [
824
'.xml',
825
'.xsd',
826
'.ascx',
827
'.atom',
828
'.axml',
829
'.axaml',
830
'.bpmn',
831
'.cpt',
832
'.csl',
833
'.csproj',
834
'.csproj.user',
835
'.dita',
836
'.ditamap',
837
'.dtd',
838
'.ent',
839
'.mod',
840
'.dtml',
841
'.fsproj',
842
'.fxml',
843
'.iml',
844
'.isml',
845
'.jmx',
846
'.launch',
847
'.menu',
848
'.mxml',
849
'.nuspec',
850
'.opml',
851
'.owl',
852
'.proj',
853
'.props',
854
'.pt',
855
'.publishsettings',
856
'.pubxml',
857
'.pubxml.user',
858
'.rbxlx',
859
'.rbxmx',
860
'.rdf',
861
'.rng',
862
'.rss',
863
'.shproj',
864
'.storyboard',
865
'.svg',
866
'.targets',
867
'.tld',
868
'.tmx',
869
'.vbproj',
870
'.vbproj.user',
871
'.vcxproj',
872
'.vcxproj.filters',
873
'.wsdl',
874
'.wxi',
875
'.wxl',
876
'.wxs',
877
'.xaml',
878
'.xbl',
879
'.xib',
880
'.xlf',
881
'.xliff',
882
'.xpdl',
883
'.xul',
884
'.xoml'
885
],
886
blockComment: [
887
'<!--',
888
'-->'
889
]
890
},
891
'xsl': {
892
lineComment: { start: '<!--', end: '-->' },
893
aliases: [
894
'XSL',
895
'xsl'
896
],
897
extensions: [
898
'.xsl',
899
'.xslt'
900
]
901
},
902
'yaml': {
903
lineComment: { start: '#' },
904
markdownLanguageIds: ['yaml', 'yml']
905
}
906
} satisfies Record<string, ILanguageInfo>);
907
908
export type WellKnownLanguageId = keyof typeof languages;
909
910
export const wellKnownLanguages = new Map<string, ILanguage>(
911
Object.entries(languages).map(([languageId, info]) => [languageId, { languageId, ...info }]));
912
913
export function getLanguage(languageId: string | undefined): ILanguage;
914
export function getLanguage(document: { languageId: string } | undefined): ILanguage;
915
export function getLanguage(v: string | { languageId: string } | undefined): ILanguage {
916
if (typeof v === 'string') {
917
return _getLanguage(v);
918
}
919
if (typeof v === 'undefined') {
920
return _getLanguage('plaintext');
921
}
922
return _getLanguage(v.languageId);
923
}
924
925
function _getLanguage(languageId: string): ILanguage {
926
return (
927
wellKnownLanguages.get(languageId.toLowerCase())
928
?? { languageId, lineComment: { start: '//' } }
929
);
930
}
931
932
export function getLanguageForResource(uri: URI): ILanguage {
933
const ext = extname(uri).toLowerCase();
934
for (const info of wellKnownLanguages.values()) {
935
if (info.extensions?.includes(ext)) {
936
return info;
937
}
938
}
939
return getLanguage('plaintext');
940
}
941
942