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