2023-02-02 14:36:40 -05:00
|
|
|
const jsKeywords = new Set([
|
|
|
|
"abstract", "arguments", "await*", "boolean", "break", "byte", "case",
|
|
|
|
"catch", "char", "class*", "const", "continue", "debugger", "default",
|
|
|
|
"delete", "do", "double", "else", "enum*", "eval", "export*", "extends*",
|
|
|
|
"false", "final", "finally", "float", "for", "function", "goto", "if",
|
|
|
|
"implements", "import*", "in", "instanceof", "int", "interface", "let*",
|
|
|
|
"long", "native", "new", "null", "package", "private", "protected", "public",
|
|
|
|
"return", "short", "static", "super*", "switch", "synchronized", "this",
|
|
|
|
"throw", "throws", "transient", "true", "try", "typeof", "var", "void",
|
|
|
|
"volatile", "while", "with", "yield"
|
|
|
|
])
|
|
|
|
|
|
|
|
const csKeywords = new Set([
|
|
|
|
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char",
|
|
|
|
"checked", "class", "const", "continue", "decimal", "default", "delegate",
|
|
|
|
"do", "double", "else", "enum", "event", "explicit", "extern", "false",
|
|
|
|
"finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit",
|
|
|
|
"in", "int", "interface", "internal", "is", "lock", "long", "namespace",
|
|
|
|
"new", "null", "object", "operator", "out", "override", "params", "private",
|
|
|
|
"protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
|
|
|
|
"short", "sizeof", "stackalloc", "static", "string", "struct", "switch",
|
|
|
|
"this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked",
|
|
|
|
"unsafe", "ushort", "using", "virtual", "void", "volatile", "while",
|
|
|
|
])
|
|
|
|
|
|
|
|
const javaKeywords = new Set([
|
|
|
|
"abstract", "continue", "for", "new", "switch", "assert", "default",
|
|
|
|
"goto", "package", "synchronized", "boolean", "do", "if", "private",
|
|
|
|
"this", "break", "double", "implements", "protected", "throw", "byte",
|
|
|
|
"else", "import", "public", "throws", "case", "enum", "instanceof",
|
|
|
|
"return", "transient", "catch", "extends", "int", "short", "try", "char",
|
|
|
|
"final", "interface", "static", "void", "class", "finally", "long",
|
|
|
|
"strictfp", "volatile", "const", "float", "native", "super", "while",
|
|
|
|
])
|
|
|
|
|
|
|
|
const keywordMap = {
|
|
|
|
js: jsKeywords,
|
|
|
|
mjs: jsKeywords,
|
|
|
|
java: javaKeywords,
|
|
|
|
cs: csKeywords,
|
|
|
|
none: new Set()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bookendedWith = (text, bookend) => text.startsWith(bookend) && text.endsWith(bookend)
|
|
|
|
|
2023-02-07 14:01:47 -05:00
|
|
|
const getClass = (text, prefix, keywords) => {
|
2023-02-02 14:36:40 -05:00
|
|
|
if (keywords.has(text)) {
|
2023-02-07 14:01:47 -05:00
|
|
|
return prefix + 'keyword'
|
2023-02-02 14:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bookendedWith(text, '"')) {
|
2023-02-07 14:01:47 -05:00
|
|
|
return prefix + 'string'
|
2023-02-02 14:36:40 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:01:47 -05:00
|
|
|
return prefix + 'variable'
|
2023-02-02 14:36:40 -05:00
|
|
|
}
|
|
|
|
|
2023-02-02 15:57:09 -05:00
|
|
|
const getKeywords = () => keywordMap[getFileName()?.replace(/.*\./g, '')] || keywordMap.none
|
2023-02-02 14:36:40 -05:00
|
|
|
|
2023-02-07 14:01:47 -05:00
|
|
|
const commentSpan = (innerText, prefix) => {
|
2023-02-02 14:36:40 -05:00
|
|
|
const span = document.createElement('span')
|
|
|
|
span.innerText = innerText
|
2023-02-07 14:01:47 -05:00
|
|
|
span.classList.add(prefix + 'comment')
|
2023-02-02 14:36:40 -05:00
|
|
|
return span
|
|
|
|
}
|
|
|
|
|
|
|
|
const colorCodeInComment = (codeStart = '`', codeEnd) => {
|
|
|
|
codeEnd ??= codeStart
|
|
|
|
const processed = new Set()
|
|
|
|
const keywords = getKeywords()
|
2023-02-07 14:01:47 -05:00
|
|
|
const comments = [...getClassNameElementsArray('hl-comment'), ...getClassNameElementsArray('cm-comment')]
|
|
|
|
comments.forEach(element => {
|
|
|
|
const codeStartIndex = element.innerText?.indexOf(codeStart)
|
|
|
|
if (codeStartIndex === -1 || processed.has(element)) {
|
2023-02-02 14:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
2023-02-07 14:01:47 -05:00
|
|
|
const prefix = element.classList.contains('hl-comment') ? 'hl-' : 'cm-'
|
|
|
|
const endOfCodeStart = codeStartIndex + codeStart.length
|
|
|
|
const codeStartText = element.innerText.substring(0, endOfCodeStart)
|
|
|
|
const codeStartElement = commentSpan(codeStartText, prefix)
|
|
|
|
element.innerText = element.innerText.substring(endOfCodeStart, element.innerText.length)
|
2023-02-02 14:36:40 -05:00
|
|
|
element.replaceWith(codeStartElement, element)
|
|
|
|
while (element) {
|
2023-02-07 14:01:47 -05:00
|
|
|
if (!element.classList.contains('hl-comment') && !element.classList.contains('cm-comment')) {
|
2023-02-02 14:36:40 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
processed.add(element)
|
2023-02-07 14:01:47 -05:00
|
|
|
element.classList.remove(`${prefix}comment`)
|
2023-02-02 14:36:40 -05:00
|
|
|
const text = element.innerText.replaceAll(codeStart, '').replaceAll(codeEnd, '')
|
2023-02-07 14:01:47 -05:00
|
|
|
element.classList.add(getClass(text, prefix, keywords))
|
2023-02-02 14:36:40 -05:00
|
|
|
const codeEndIndex = element.innerText.indexOf(codeEnd)
|
|
|
|
if (codeEndIndex > -1) {
|
2023-02-07 14:01:47 -05:00
|
|
|
const codeEndElement = commentSpan(element.innerText.substring(codeEndIndex), prefix)
|
2023-02-02 14:36:40 -05:00
|
|
|
element.innerText = element.innerText.substring(0, codeEndIndex)
|
|
|
|
element.replaceWith(element, codeEndElement)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
element = element.nextElementSibling
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const commentCodeFix = () => {
|
|
|
|
colorCodeInComment('`', '`')
|
|
|
|
colorCodeInComment('<code>', '</code>')
|
|
|
|
colorCodeInComment('<pre>', '</pre>')
|
|
|
|
colorCodeInComment('<tt>', '</tt>')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addFix(commentCodeFix)
|