2
0

index.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeMirror: Groovy mode</title>
  6. <link rel="stylesheet" href="../../lib/codemirror.css">
  7. <script src="../../lib/codemirror.js"></script>
  8. <script src="../../addon/edit/matchbrackets.js"></script>
  9. <script src="groovy.js"></script>
  10. <link rel="stylesheet" href="../../doc/docs.css">
  11. <style>.CodeMirror {border-top: 1px solid #500; border-bottom: 1px solid #500;}</style>
  12. </head>
  13. <body>
  14. <h1>CodeMirror: Groovy mode</h1>
  15. <form><textarea id="code" name="code">
  16. //Pattern for groovy script
  17. def p = ~/.*\.groovy/
  18. new File( 'd:\\scripts' ).eachFileMatch(p) {f ->
  19. // imports list
  20. def imports = []
  21. f.eachLine {
  22. // condition to detect an import instruction
  23. ln -> if ( ln =~ '^import .*' ) {
  24. imports << "${ln - 'import '}"
  25. }
  26. }
  27. // print thmen
  28. if ( ! imports.empty ) {
  29. println f
  30. imports.each{ println " $it" }
  31. }
  32. }
  33. /* Coin changer demo code from http://groovy.codehaus.org */
  34. enum UsCoin {
  35. quarter(25), dime(10), nickel(5), penny(1)
  36. UsCoin(v) { value = v }
  37. final value
  38. }
  39. enum OzzieCoin {
  40. fifty(50), twenty(20), ten(10), five(5)
  41. OzzieCoin(v) { value = v }
  42. final value
  43. }
  44. def plural(word, count) {
  45. if (count == 1) return word
  46. word[-1] == 'y' ? word[0..-2] + "ies" : word + "s"
  47. }
  48. def change(currency, amount) {
  49. currency.values().inject([]){ list, coin ->
  50. int count = amount / coin.value
  51. amount = amount % coin.value
  52. list += "$count ${plural(coin.toString(), count)}"
  53. }
  54. }
  55. </textarea></form>
  56. <script>
  57. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  58. lineNumbers: true,
  59. matchBrackets: true,
  60. mode: "text/x-groovy"
  61. });
  62. </script>
  63. <p><strong>MIME types defined:</strong> <code>text/x-groovy</code></p>
  64. </body>
  65. </html>