You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.1 KiB

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