test.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function () {
  4. var mode = CodeMirror.getMode({ indentUnit: 2 }, 'swift')
  5. function MT(name) {
  6. test.mode(name, mode, Array.prototype.slice.call(arguments, 1))
  7. }
  8. // Ensure all number types are properly represented.
  9. MT(
  10. 'numbers',
  11. '[keyword var] [def a] [operator =] [number 17]',
  12. '[keyword var] [def b] [operator =] [number -0.5]',
  13. '[keyword var] [def c] [operator =] [number 0.3456e-4]',
  14. '[keyword var] [def d] [operator =] [number 345e2]',
  15. '[keyword var] [def e] [operator =] [number 0o7324]',
  16. '[keyword var] [def f] [operator =] [number 0b10010]',
  17. '[keyword var] [def g] [operator =] [number -0x35ade]',
  18. '[keyword var] [def h] [operator =] [number 0xaea.ep-13]',
  19. '[keyword var] [def i] [operator =] [number 0x13ep6]'
  20. )
  21. // Variable/class/etc definition.
  22. MT(
  23. 'definition',
  24. '[keyword var] [def a] [operator =] [number 5]',
  25. '[keyword let] [def b][punctuation :] [variable-2 Int] [operator =] [number 10]',
  26. '[keyword class] [def C] [punctuation {] [punctuation }]',
  27. '[keyword struct] [def D] [punctuation {] [punctuation }]',
  28. '[keyword enum] [def E] [punctuation {] [punctuation }]',
  29. '[keyword extension] [def F] [punctuation {] [punctuation }]',
  30. '[keyword protocol] [def G] [punctuation {] [punctuation }]',
  31. '[keyword func] [def h][punctuation ()] [punctuation {] [punctuation }]',
  32. '[keyword import] [def Foundation]',
  33. '[keyword typealias] [def NewString] [operator =] [variable-2 String]',
  34. '[keyword associatedtype] [def I]',
  35. '[keyword for] [def j] [keyword in] [number 0][punctuation ..][operator <][number 3] [punctuation {] [punctuation }]'
  36. )
  37. // Strings and string interpolation.
  38. MT(
  39. 'strings',
  40. '[keyword var] [def a][punctuation :] [variable-2 String] [operator =] [string "test"]',
  41. '[keyword var] [def b][punctuation :] [variable-2 String] [operator =] [string "\\(][variable a][string )"]',
  42. '[keyword var] [def c] [operator =] [string """]',
  43. '[string multi]',
  44. '[string line]',
  45. '[string "test"]',
  46. '[string """]',
  47. '[variable print][punctuation (][string ""][punctuation )]'
  48. )
  49. // Comments.
  50. MT('comments', '[comment // This is a comment]', '[comment /* This is another comment */]', '[keyword var] [def a] [operator =] [number 5] [comment // Third comment]')
  51. // Atoms.
  52. MT(
  53. 'atoms',
  54. '[keyword class] [def FooClass] [punctuation {]',
  55. ' [keyword let] [def fooBool][punctuation :] [variable-2 Bool][operator ?]',
  56. ' [keyword let] [def fooInt][punctuation :] [variable-2 Int][operator ?]',
  57. ' [keyword func] [keyword init][punctuation (][variable fooBool][punctuation :] [variable-2 Bool][punctuation ,] [variable barBool][punctuation :] [variable-2 Bool][punctuation )] [punctuation {]',
  58. ' [atom super][property .init][punctuation ()]',
  59. ' [atom self][property .fooBool] [operator =] [variable fooBool]',
  60. ' [variable fooInt] [operator =] [atom nil]',
  61. ' [keyword if] [variable barBool] [operator ==] [atom true] [punctuation {]',
  62. ' [variable print][punctuation (][string "True!"][punctuation )]',
  63. ' [punctuation }] [keyword else] [keyword if] [variable barBool] [operator ==] [atom false] [punctuation {]',
  64. ' [keyword for] [atom _] [keyword in] [number 0][punctuation ...][number 5] [punctuation {]',
  65. ' [variable print][punctuation (][string "False!"][punctuation )]',
  66. ' [punctuation }]',
  67. ' [punctuation }]',
  68. ' [punctuation }]',
  69. '[punctuation }]'
  70. )
  71. // Types.
  72. MT(
  73. 'types',
  74. '[keyword var] [def a] [operator =] [variable-2 Array][operator <][variable-2 Int][operator >]',
  75. '[keyword var] [def b] [operator =] [variable-2 Set][operator <][variable-2 Bool][operator >]',
  76. '[keyword var] [def c] [operator =] [variable-2 Dictionary][operator <][variable-2 String][punctuation ,][variable-2 Character][operator >]',
  77. '[keyword var] [def d][punctuation :] [variable-2 Int64][operator ?] [operator =] [variable-2 Optional][punctuation (][number 8][punctuation )]',
  78. '[keyword func] [def e][punctuation ()] [operator ->] [variable-2 Void] [punctuation {]',
  79. ' [keyword var] [def e1][punctuation :] [variable-2 Float] [operator =] [number 1.2]',
  80. '[punctuation }]',
  81. '[keyword func] [def f][punctuation ()] [operator ->] [variable-2 Never] [punctuation {]',
  82. ' [keyword var] [def f1][punctuation :] [variable-2 Double] [operator =] [number 2.4]',
  83. '[punctuation }]'
  84. )
  85. // Operators.
  86. MT(
  87. 'operators',
  88. '[keyword var] [def a] [operator =] [number 1] [operator +] [number 2]',
  89. '[keyword var] [def b] [operator =] [number 1] [operator -] [number 2]',
  90. '[keyword var] [def c] [operator =] [number 1] [operator *] [number 2]',
  91. '[keyword var] [def d] [operator =] [number 1] [operator /] [number 2]',
  92. '[keyword var] [def e] [operator =] [number 1] [operator %] [number 2]',
  93. '[keyword var] [def f] [operator =] [number 1] [operator |] [number 2]',
  94. '[keyword var] [def g] [operator =] [number 1] [operator &] [number 2]',
  95. '[keyword var] [def h] [operator =] [number 1] [operator <<] [number 2]',
  96. '[keyword var] [def i] [operator =] [number 1] [operator >>] [number 2]',
  97. '[keyword var] [def j] [operator =] [number 1] [operator ^] [number 2]',
  98. '[keyword var] [def k] [operator =] [operator ~][number 1]',
  99. '[keyword var] [def l] [operator =] [variable foo] [operator ?] [number 1] [punctuation :] [number 2]',
  100. '[keyword var] [def m][punctuation :] [variable-2 Int] [operator =] [variable-2 Optional][punctuation (][number 8][punctuation )][operator !]'
  101. )
  102. // Punctuation.
  103. MT(
  104. 'punctuation',
  105. '[keyword let] [def a] [operator =] [number 1][punctuation ;] [keyword let] [def b] [operator =] [number 2]',
  106. '[keyword let] [def testArr][punctuation :] [punctuation [[][variable-2 Int][punctuation ]]] [operator =] [punctuation [[][variable a][punctuation ,] [variable b][punctuation ]]]',
  107. '[keyword for] [def i] [keyword in] [number 0][punctuation ..][operator <][variable testArr][property .count] [punctuation {]',
  108. ' [variable print][punctuation (][variable testArr][punctuation [[][variable i][punctuation ]])]',
  109. '[punctuation }]'
  110. )
  111. // Identifiers.
  112. MT(
  113. 'identifiers',
  114. '[keyword let] [def abc] [operator =] [number 1]',
  115. '[keyword let] [def ABC] [operator =] [number 2]',
  116. '[keyword let] [def _123] [operator =] [number 3]',
  117. '[keyword let] [def _$1$2$3] [operator =] [number 4]',
  118. '[keyword let] [def A1$_c32_$_] [operator =] [number 5]',
  119. '[keyword let] [def `var`] [operator =] [punctuation [[][number 1][punctuation ,] [number 2][punctuation ,] [number 3][punctuation ]]]',
  120. '[keyword let] [def square$] [operator =] [variable `var`][property .map] [punctuation {][variable $0] [operator *] [variable $0][punctuation }]',
  121. '$$ [number 1][variable a] $[atom _] [variable _$] [variable __] `[variable a] [variable b]`'
  122. )
  123. // Properties.
  124. MT(
  125. 'properties',
  126. '[variable print][punctuation (][variable foo][property .abc][punctuation )]',
  127. '[variable print][punctuation (][variable foo][property .ABC][punctuation )]',
  128. '[variable print][punctuation (][variable foo][property ._123][punctuation )]',
  129. '[variable print][punctuation (][variable foo][property ._$1$2$3][punctuation )]',
  130. '[variable print][punctuation (][variable foo][property .A1$_c32_$_][punctuation )]',
  131. '[variable print][punctuation (][variable foo][property .`var`][punctuation )]',
  132. '[variable print][punctuation (][variable foo][property .__][punctuation )]'
  133. )
  134. // Instructions or other things that start with #.
  135. MT(
  136. 'instructions',
  137. '[keyword if] [builtin #available][punctuation (][variable iOS] [number 9][punctuation ,] [operator *][punctuation )] [punctuation {}]',
  138. '[variable print][punctuation (][builtin #file][punctuation ,] [builtin #function][punctuation )]',
  139. '[variable print][punctuation (][builtin #line][punctuation ,] [builtin #column][punctuation )]',
  140. '[builtin #if] [atom true]',
  141. '[keyword import] [def A]',
  142. '[builtin #elseif] [atom false]',
  143. '[keyword import] [def B]',
  144. '[builtin #endif]',
  145. '[builtin #sourceLocation][punctuation (][variable file][punctuation :] [string "file.swift"][punctuation ,] [variable line][punctuation :] [number 2][punctuation )]'
  146. )
  147. // Attributes; things that start with @.
  148. MT('attributes', '[attribute @objc][punctuation (][variable objcFoo][punctuation :)]', '[attribute @available][punctuation (][variable iOS][punctuation )]')
  149. // Property/number edge case.
  150. MT('property_number', '[variable print][punctuation (][variable foo][property ._123][punctuation )]', '[variable print][punctuation (]')
  151. MT(
  152. 'nested_comments',
  153. '[comment /*]',
  154. '[comment But wait /* this is a nested comment */ for real]',
  155. '[comment /**** let * me * show * you ****/]',
  156. '[comment ///// let / me / show / you /////]',
  157. '[comment */]'
  158. )
  159. // TODO: correctly identify when multiple variables are being declared
  160. // by use of a comma-separated list.
  161. // TODO: correctly identify when variables are being declared in a tuple.
  162. // TODO: identify protocols as types when used before an extension?
  163. })()