mode-modeler-sql.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * Licensed Materials - Property of IBM
  3. * IBM Cognos Products: Modeling UI (C) Copyright IBM Corp. 2018, 2019
  4. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  5. */
  6. // this is a model defining the syntax highlighting rules and the code complete list
  7. // for the sql modeler
  8. // it uses the text mode as its base mode/highlighter
  9. ace.define(
  10. "ace/mode/modeler_sql_highlight_rules",
  11. [
  12. "require",
  13. "exports",
  14. "module",
  15. "ace/mode/modeler_resources",
  16. "ace/lib/oop",
  17. "ace/lib/lang",
  18. "ace/mode/text_highlight_rules"
  19. ], function(
  20. require,
  21. exports,
  22. module
  23. ) {
  24. var oop = require("../lib/oop");
  25. var Resources = require("ace/mode/modeler_resources");
  26. require("../lib/lang");
  27. // get the text highlight so we can use it as our base, so we get bracet matching and what not
  28. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  29. // this list comes from RQPSQLKWYWORD.JAVA from XQE
  30. var keyWordsGroup = {
  31. name: Resources.get('keywordsGroup'),
  32. val: ["absolute","action","add","admin","after","alias","all","allocate","alter","and","any","are","array","as","asc","assertion","at","authorization","before","begin","binary","bit","blob","boolean","both","breadth","by","call","cascade","cascaded","catalog","character","check","class","clob","close","collate","collation","column","commit","completion","connect","connection","constraint","constraints","constructor","continue","corresponding","create","cross","cube","current","current_path","current_role","current_user","cursor","cycle","deallocate","declare","deferrable","deferred","delete","depth","deref","desc","describe","descriptor","destroy","destructor","deterministic","dictionary","diagnostics","disconnect","domain","drop","dynamic","each","end-exec","every","exception","exec","execute","external","false","fetch","first","foreign","found","from","free","full","function","general","get","global","go","goto","grant","group","grouping","having","host","identity","ignore","immediate","indicator","initialize","initially","inner","inout","input","into","is","isolation","iterate","join","key","language","large","last","lateral","leading","left","less","limit","local","locator","map","match","modifies","modify","module","names","national","natural","nchar","nclob","new","next","no","none","numeric","object","of","off","old","on","only","open","operation","option","or","order","ordinality","out","outer","output","pad","parameter","parameters","partial","path","postfix","precision","prefix","preorder","prepare","preserve","primary","prior","privileges","procedure","public","read","reads","recursive","ref","references","referencing","relative","restrict","result","return","returns","revoke","right","rollback","rollup","routine","savepoint","scroll","scope","search","section","select","sequence","sets","size","some","specific","specifictype","sql","sqlexception","sqlstate","sqlwarning","start","state","statement","static","structure","table","temporary","terminate","than","timezone_hour","timezone_minute","to","trailing","transaction","translation","treat","trigger","true","under","union","unknown","unnest","update","usage","using","values","variable","varying","view","whenever","where","with","without","work","write","zone"]
  33. };
  34. // build the regex string for the keywords
  35. var keyWordsRegExStr = "\\b(" + keyWordsGroup.val.join("|") + ")\\b";
  36. // these are the actually rules, add more as you see fit, the token is basically the CSS styling class that
  37. // will be used for the matched text, if the token value "foo" it means it will apply one class calls foo, if
  38. // it is "foo.bar" it means it will apply 2 classes foo and bar
  39. var ModelerHighlightRules = function() {
  40. var mapper = function(map, defaultToken, ignoreCase, splitChar) {
  41. var keywords = Object.create(null);
  42. Object.keys(map).forEach(function(className) {
  43. var a = map[className];
  44. if (ignoreCase)
  45. a = a.toLowerCase();
  46. var list = a.split(splitChar || "|");
  47. for (var i = list.length; i--; )
  48. keywords[list[i]] = className;
  49. });
  50. if (Object.getPrototypeOf(keywords)) {
  51. keywords.__proto__ = null;
  52. }
  53. this.$keywordList = Object.keys(keywords);
  54. map = null;
  55. return ignoreCase
  56. ? function(value) {return keywords[value.toLowerCase()] || defaultToken; }
  57. : function(value) {return keywords[value] || defaultToken; };
  58. };
  59. this.setKeywords = function(kwMap) {
  60. this.keywordRule.onMatch = mapper(kwMap, "identifier", true)
  61. }
  62. this.keywordRule = {
  63. regex : "[\\w\\-\\.]+",
  64. onMatch : function() {return "text"}
  65. }
  66. this.$rules = {
  67. start : [
  68. {
  69. token : 'comment',
  70. regex : '--.*$'
  71. },{
  72. token : 'comment-block',
  73. start : '/\\*',
  74. end : '\\*/'
  75. },{
  76. token : 'macro-block',
  77. start : '\\#',
  78. end : '\\#'
  79. },{
  80. token : 'keyword',
  81. regex : keyWordsRegExStr,
  82. caseInsensitive : true
  83. },{
  84. token : 'operator',
  85. regex : '\\+|\\-|\\*|\\/|\\/\\/|%|<@>|@>|<@|&|\\^|~|<|>|<=|=>|==|!=|<>|='
  86. },{
  87. token : 'paren.lparen',
  88. regex : '\\(|\\[|\\{'
  89. },{
  90. token : 'paren.rparen',
  91. regex : '\\)|\\]|\\}'
  92. },{
  93. token : 'string',
  94. regex : /".*?"/
  95. },{
  96. token : 'string',
  97. regex : /'.*?'/
  98. },
  99. this.keywordRule
  100. ]
  101. };
  102. this.normalizeRules()
  103. };
  104. // do the actual inheritance
  105. oop.inherits(ModelerHighlightRules, TextHighlightRules);
  106. ModelerHighlightRules._keyWordsGroup = keyWordsGroup;
  107. exports.ModelerHighlightRules = ModelerHighlightRules;
  108. });
  109. ace.define("ace/mode/folding/modeler_sql_rules", [], function(require, exports, module) {
  110. "use strict";
  111. var oop = require("../../lib/oop");
  112. var Range = require("../../range").Range;
  113. var BaseFoldMode = require("./fold_mode").FoldMode;
  114. var FoldMode = exports.FoldMode = function() {};
  115. oop.inherits(FoldMode, BaseFoldMode);
  116. (function() {
  117. this.foldingStartMarker = /(\()/;
  118. this.foldingEndMarker = /(\))/;
  119. var shouldFold = function(line) {
  120. var lineStartMarkerCount = (line.match(/\(/g) || []).length;
  121. var lineEndMarkerCount = (line.match(/\)/g) || []).length;
  122. return (lineStartMarkerCount > lineEndMarkerCount);
  123. }
  124. this.getFoldWidgetRange = function(session, foldStyle, row) {
  125. var line = session.getLine(row);
  126. if(shouldFold(line)) {
  127. var match = line.match(this.foldingStartMarker);
  128. if (match && match[1]) {
  129. return this.openingBracketBlock(session, match[1], row, match.index);
  130. }
  131. } else {
  132. var range = this.indentationBlock(session, row);
  133. if (range)
  134. return range;
  135. var re = /\S/;
  136. var startLevel = line.search(re);
  137. if (startLevel == -1)
  138. return;
  139. var startColumn = line.length;
  140. var maxRow = session.getLength();
  141. var startRow = row;
  142. var endRow = row;
  143. while (++row < maxRow) {
  144. line = session.getLine(row);
  145. var level = line.search(re);
  146. if (level == -1)
  147. continue;
  148. endRow = row;
  149. }
  150. if (endRow > startRow) {
  151. var endColumn = session.getLine(endRow).length;
  152. return new Range(startRow, startColumn, endRow, endColumn);
  153. }
  154. }
  155. };
  156. // must return "" if there's no fold, to enable caching
  157. this.getFoldWidget = function(session, foldStyle, row) {
  158. var line = session.getLine(row);
  159. var indent = line.search(/\S/);
  160. var next = session.getLine(row + 1);
  161. var prev = session.getLine(row - 1);
  162. var prevIndent = prev.search(/\S/);
  163. var nextIndent = next.search(/\S/);
  164. if(shouldFold(line)) {
  165. return "start";
  166. }
  167. if (indent == -1) {
  168. session.foldWidgets[row - 1] = prevIndent!= -1 && prevIndent < nextIndent ? "start" : "";
  169. return "";
  170. }
  171. if (prevIndent!= -1 && prevIndent < indent && !prev.search(/\(/))
  172. session.foldWidgets[row - 1] = "start";
  173. if (indent < nextIndent)
  174. return "start";
  175. else
  176. return "";
  177. };
  178. }).call(FoldMode.prototype);
  179. });
  180. ace.define("ace/mode/modeler-sql",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/modeler_sql_highlight_rules","ace/ext-language_tools", "ace/mode/folding/rules"], function(require, exports, module) {
  181. var oop = require("../lib/oop");
  182. var Range = require("../range").Range;
  183. // get the base text mode so we can inherit it
  184. var TextMode = require("./text").Mode;
  185. // get the highlight rules we defined
  186. var ModelerHighlightRules = require("./modeler_sql_highlight_rules").ModelerHighlightRules;
  187. var MyFoldMode = require("./folding/modeler_sql_rules").FoldMode;
  188. // prepare the Mode function
  189. var Mode = function() {
  190. this.HighlightRules = ModelerHighlightRules;
  191. this.foldingRules = new MyFoldMode();
  192. };
  193. // do the actual inheritance for the mode
  194. oop.inherits(Mode, TextMode);
  195. (function() {
  196. this.$id = "ace/mode/modeler-sql";
  197. this.lineCommentStart = "--";
  198. }).call(Mode.prototype);
  199. exports.Mode = Mode;
  200. });
  201. ace.define("ace/snippets/text",["require","exports","module"], function(require, exports, module) {
  202. exports.snippetText =undefined;
  203. exports.scope = "text";
  204. });
  205. window.define(function() {
  206. return {
  207. setResources: function(res) {
  208. ace.define("ace/mode/modeler_resources",["require","exports","module"], function(require, exports, module) {
  209. exports.get = function(str) {
  210. return res.getString(str);
  211. };
  212. });
  213. }
  214. };
  215. });