prism-kotlin.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. (function (Prism) {
  2. Prism.languages.kotlin = Prism.languages.extend('clike', {
  3. 'keyword': {
  4. // The lookbehind prevents wrong highlighting of e.g. kotlin.properties.get
  5. pattern: /(^|[^.])\b(?:abstract|actual|annotation|as|break|by|catch|class|companion|const|constructor|continue|crossinline|data|do|dynamic|else|enum|expect|external|final|finally|for|fun|get|if|import|in|infix|init|inline|inner|interface|internal|is|lateinit|noinline|null|object|open|operator|out|override|package|private|protected|public|reified|return|sealed|set|super|suspend|tailrec|this|throw|to|try|typealias|val|var|vararg|when|where|while)\b/,
  6. lookbehind: true
  7. },
  8. 'function': [
  9. {
  10. pattern: /(?:`[^\r\n`]+`|\b\w+)(?=\s*\()/,
  11. greedy: true
  12. },
  13. {
  14. pattern: /(\.)(?:`[^\r\n`]+`|\w+)(?=\s*\{)/,
  15. lookbehind: true,
  16. greedy: true
  17. }
  18. ],
  19. 'number': /\b(?:0[xX][\da-fA-F]+(?:_[\da-fA-F]+)*|0[bB][01]+(?:_[01]+)*|\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?(?:[eE][+-]?\d+(?:_\d+)*)?[fFL]?)\b/,
  20. 'operator': /\+[+=]?|-[-=>]?|==?=?|!(?:!|==?)?|[\/*%<>]=?|[?:]:?|\.\.|&&|\|\||\b(?:and|inv|or|shl|shr|ushr|xor)\b/
  21. });
  22. delete Prism.languages.kotlin['class-name'];
  23. var interpolationInside = {
  24. 'interpolation-punctuation': {
  25. pattern: /^\$\{?|\}$/,
  26. alias: 'punctuation'
  27. },
  28. 'expression': {
  29. pattern: /[\s\S]+/,
  30. inside: Prism.languages.kotlin
  31. }
  32. };
  33. Prism.languages.insertBefore('kotlin', 'string', {
  34. // https://kotlinlang.org/spec/expressions.html#string-interpolation-expressions
  35. 'string-literal': [
  36. {
  37. pattern: /"""(?:[^$]|\$(?:(?!\{)|\{[^{}]*\}))*?"""/,
  38. alias: 'multiline',
  39. inside: {
  40. 'interpolation': {
  41. pattern: /\$(?:[a-z_]\w*|\{[^{}]*\})/i,
  42. inside: interpolationInside
  43. },
  44. 'string': /[\s\S]+/
  45. }
  46. },
  47. {
  48. pattern: /"(?:[^"\\\r\n$]|\\.|\$(?:(?!\{)|\{[^{}]*\}))*"/,
  49. alias: 'singleline',
  50. inside: {
  51. 'interpolation': {
  52. pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:[a-z_]\w*|\{[^{}]*\})/i,
  53. lookbehind: true,
  54. inside: interpolationInside
  55. },
  56. 'string': /[\s\S]+/
  57. }
  58. }
  59. ],
  60. 'char': {
  61. // https://kotlinlang.org/spec/expressions.html#character-literals
  62. pattern: /'(?:[^'\\\r\n]|\\(?:.|u[a-fA-F0-9]{0,4}))'/,
  63. greedy: true
  64. }
  65. });
  66. delete Prism.languages.kotlin['string'];
  67. Prism.languages.insertBefore('kotlin', 'keyword', {
  68. 'annotation': {
  69. pattern: /\B@(?:\w+:)?(?:[A-Z]\w*|\[[^\]]+\])/,
  70. alias: 'builtin'
  71. }
  72. });
  73. Prism.languages.insertBefore('kotlin', 'function', {
  74. 'label': {
  75. pattern: /\b\w+@|@\w+\b/,
  76. alias: 'symbol'
  77. }
  78. });
  79. Prism.languages.kt = Prism.languages.kotlin;
  80. Prism.languages.kts = Prism.languages.kotlin;
  81. }(Prism));