浏览代码

HUE-7745 [dashboard] Create Solr keywords highlighter for Ace

Enrico Berti 8 年之前
父节点
当前提交
9f398bef4a

+ 1 - 0
desktop/core/src/desktop/static/desktop/js/ace/mode-solr.js

@@ -0,0 +1 @@
+ace.define("ace/mode/solr_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_highlight_rules"],function(e,t,n){"use strict";var r=e("../lib/oop"),i=e("./text_highlight_rules").TextHighlightRules,s=function(){var e="AND|OR|NOT|TO|NOW|HOUR|HOURS|DAY|DAYS|MONTH|MONTHS|YEAR|YEARS",t="ABS|AVG|CHILDFIELD|DEF|DIST|DIV|DOCFREQ|EQ|EXISTS|FIELD|GT|GTE|HLL|HSIN|IDF|IF|LINEAR|LOG|LT|LTE|MAP|MAX|MAX|MAXDOC|MIN|MIN|MS|MUL|NORM|NUMDOCS|ORD|PAYLOAD|PERCENTILE|POW|PRODUCT|QUERY|RECIP|RORD|SCALE|SQEDIST|SQRT|STDDEV|STRDIST|SUB|SUM|SUM|SUMSQ|SUMTOTALTERMFREQ|TERMFREQ|TF|TOP|TOTALTERMFREQ|UNIQUE|VARIANCE|XOR",n=this.createKeywordMapper({"support.function":t,keyword:e},"identifier",!0);this.$rules={start:[{token:"string",regex:'".*?"'},{token:"string",regex:"'.*?'"},{token:"constant.numeric",regex:"[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"},{token:n,regex:"[a-zA-Z_$][a-zA-Z0-9_$]*\\b"},{token:"keyword.operator",regex:'\\+|\\-|\\/|\\?|&|\\^|~|:|\\*|\\||!|"'},{token:"paren.lparen",regex:"[\\(]"},{token:"paren.rparen",regex:"[\\)]"},{token:"text",regex:"\\s+"}]},this.normalizeRules()};s.metaData={fileTypes:["solr"],name:"Solr",scopeName:"source.solr"},r.inherits(s,i),t.SolrHighlightRules=s}),ace.define("ace/mode/solr",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/solr_highlight_rules"],function(e,t,n){"use strict";var r=e("../lib/oop"),i=e("./text").Mode,s=e("./solr_highlight_rules").SolrHighlightRules,o=function(){this.HighlightRules=s};r.inherits(o,i),function(){this.$id="ace/mode/solr",this.getCompletions=function(e,t,n,r){var i=this.$keywordList||this.$createKeywordList();return i.map(function(e){return{ignoreCase:!1,name:e,value:e,upperCaseValue:e.toUpperCase(),score:1,meta:"keyword"}})}}.call(o.prototype),t.Mode=o})

+ 1 - 0
desktop/core/src/desktop/static/desktop/js/ace/snippets/solr.js

@@ -0,0 +1 @@
+ace.define("ace/snippets/solr",["require","exports","module"],function(e,t,n){"use strict";t.snippetText=undefined,t.scope="solr"})

+ 3 - 0
desktop/core/src/desktop/templates/ko_components/ko_simple_ace_editor.mako

@@ -779,6 +779,9 @@ from desktop.views import _ko
         }
         }
 
 
         var editor = ace.edit($element.find('.ace-editor')[0]);
         var editor = ace.edit($element.find('.ace-editor')[0]);
+        if (params.mode) {
+          editor.getSession().setMode('ace/mode/' + ko.unwrap(params.mode));
+        }
         editor.$blockScrolling = Infinity;
         editor.$blockScrolling = Infinity;
         editor.setTheme($.totalStorage('hue.ace.theme') || 'ace/theme/hue');
         editor.setTheme($.totalStorage('hue.ace.theme') || 'ace/theme/hue');
         self.ace(editor);
         self.ace(editor);

+ 2 - 1
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -109,6 +109,7 @@ from dashboard.conf import USE_GRIDSTER, HAS_REPORT_ENABLED
               onExec: $parent.searchBtn,
               onExec: $parent.searchBtn,
               placeHolder: $root.collection.engine() === 'solr' ? '${ _ko('Example: field:value, or press CTRL + space') }' : '${ _ko('Example: col = value, or press CTRL + space') }',
               placeHolder: $root.collection.engine() === 'solr' ? '${ _ko('Example: field:value, or press CTRL + space') }' : '${ _ko('Example: col = value, or press CTRL + space') }',
               autocomplete: { type: $root.collection.engine() + 'Query', support: { collection: $root.collection } },
               autocomplete: { type: $root.collection.engine() + 'Query', support: { collection: $root.collection } },
+              mode: $root.collection.engine(),
               singleLine: true }
               singleLine: true }
             }"></div>
             }"></div>
 ##             <input data-bind="clearable: q, valueUpdate:'afterkeydown', typeahead: { target: q, nonBindableSource: queryTypeahead, multipleValues: true, multipleValuesSeparator: ':', extraKeywords: 'AND OR TO', completeSolrRanges: true }, css: {'input-small': $root.query.qs().length > 1, 'flat-left': $index() === 0, 'input-xlarge': $root.collection.supportAnalytics()}" maxlength="4096" type="text" class="search-query">
 ##             <input data-bind="clearable: q, valueUpdate:'afterkeydown', typeahead: { target: q, nonBindableSource: queryTypeahead, multipleValues: true, multipleValuesSeparator: ':', extraKeywords: 'AND OR TO', completeSolrRanges: true }, css: {'input-small': $root.query.qs().length > 1, 'flat-left': $index() === 0, 'input-xlarge': $root.collection.supportAnalytics()}" maxlength="4096" type="text" class="search-query">
@@ -2016,7 +2017,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
       <div class="clearfix"></div>
       <div class="clearfix"></div>
       <br/>
       <br/>
 
 
-      <div data-bind="component: { name: 'hue-simple-ace-editor', params: { value: plain_formula, parsedValue: formula, autocomplete: { type: 'solrFormula', support: { fields: $root.collection.template.fieldsAttributes } }, singleLine: true } }, visible: $parent.field() == 'formula'"></div>
+      <div data-bind="component: { name: 'hue-simple-ace-editor', params: { value: plain_formula, parsedValue: formula, autocomplete: { type: 'solrFormula', support: { fields: $root.collection.template.fieldsAttributes } }, singleLine: true, mode: $root.collection.engine() } }, visible: $parent.field() == 'formula'"></div>
 
 
       <!-- ko if: $parents[1].widgetType() != 'hit-widget' -->
       <!-- ko if: $parents[1].widgetType() != 'hit-widget' -->
         <div class="facet-field-cnt">
         <div class="facet-field-cnt">

+ 48 - 0
tools/ace-editor/lib/ace/mode/solr.js

@@ -0,0 +1,48 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+define(function(require, exports, module) {
+"use strict";
+
+var oop = require("../lib/oop");
+var TextMode = require("./text").Mode;
+var SolrHighlightRules = require("./solr_highlight_rules").SolrHighlightRules;
+
+var Mode = function() {
+    this.HighlightRules = SolrHighlightRules;
+};
+oop.inherits(Mode, TextMode);
+
+(function() {
+    this.$id = "ace/mode/solr";
+
+    this.getCompletions = function(state, session, pos, prefix) {
+        var keywords = this.$keywordList || this.$createKeywordList();
+        return keywords.map(function (word) {
+            return {
+                ignoreCase: false,
+                name: word,
+                value: word,
+                upperCaseValue: word.toUpperCase(),
+                score: 1,
+                meta: "keyword"
+            };
+        });
+    };
+}).call(Mode.prototype);
+
+exports.Mode = Mode;
+});

+ 80 - 0
tools/ace-editor/lib/ace/mode/solr_highlight_rules.js

@@ -0,0 +1,80 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+define(function (require, exports, module) {
+  "use strict";
+
+  var oop = require("../lib/oop");
+  var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
+
+  var SolrHighlightRules = function () {
+
+    var keywords = (
+        "AND|OR|NOT|TO|NOW|HOUR|HOURS|DAY|DAYS|MONTH|MONTHS|YEAR|YEARS"
+    );
+
+    var builtinFunctions = (
+        "ABS|AVG|CHILDFIELD|DEF|DIST|DIV|DOCFREQ|EQ|EXISTS|FIELD|GT|GTE|HLL|HSIN|IDF|IF|LINEAR|LOG|LT|LTE|MAP|MAX|MAX|MAXDOC|MIN|MIN|MS|MUL|NORM|NUMDOCS|ORD|PAYLOAD|PERCENTILE|POW|PRODUCT|QUERY|RECIP|RORD|SCALE|SQEDIST|SQRT|STDDEV|STRDIST|SUB|SUM|SUM|SUMSQ|SUMTOTALTERMFREQ|TERMFREQ|TF|TOP|TOTALTERMFREQ|UNIQUE|VARIANCE|XOR"
+    );
+
+    var keywordMapper = this.createKeywordMapper({
+        "support.function": builtinFunctions,
+        "keyword": keywords
+    }, "identifier", true);
+
+    this.$rules = {
+      start: [
+        {
+            token : "string",           // " string
+            regex : '".*?"'
+        }, {
+            token : "string",           // ' string
+            regex : "'.*?'"
+        }, {
+            token : "constant.numeric", // float
+            regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
+        }, {
+            token : keywordMapper,
+            regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
+        }, {
+            token : "keyword.operator",
+            regex : '\\+|\\-|\\/|\\?|&|\\^|~|:|\\*|\\||!|"'
+        }, {
+            token : "paren.lparen",
+            regex : "[\\(]"
+        }, {
+            token : "paren.rparen",
+            regex : "[\\)]"
+        }, {
+            token : "text",
+            regex : "\\s+"
+        }
+      ]
+    };
+
+    this.normalizeRules();
+  };
+
+  SolrHighlightRules.metaData = {
+    fileTypes: ["solr"],
+    name: "Solr",
+    scopeName: "source.solr"
+  };
+
+  oop.inherits(SolrHighlightRules, TextHighlightRules);
+
+  exports.SolrHighlightRules = SolrHighlightRules;
+});

+ 7 - 0
tools/ace-editor/lib/ace/snippets/solr.js

@@ -0,0 +1,7 @@
+define(function(require, exports, module) {
+"use strict";
+
+exports.snippetText = require("../requirejs/text!./solr.snippets");
+exports.scope = "solr";
+
+});

+ 0 - 0
tools/ace-editor/lib/ace/snippets/solr.snippets