Explorar el Código

HUE-4386 [editor] Make it possible to turn autocomplete on or off

This adds the user (cookie based) settings to control the autocompleter. It can be disabled completely and the live autocompletion as you type can also be disabled.
Johan Ahlen hace 9 años
padre
commit
d1a3a98

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/ace/ext-settings_menu.js


+ 64 - 21
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -2637,9 +2637,7 @@
       editor.setTheme($.totalStorage("hue.ace.theme") || "ace/theme/hue");
 
       var editorOptions = {
-        enableBasicAutocompletion: true,
         enableSnippets: true,
-        enableLiveAutocompletion: true,
         showGutter: false,
         showLineNumbers: false,
         showPrintMargin: false,
@@ -2648,8 +2646,44 @@
         maxLines: 25
       };
 
+      editor.enabledMenuOptions = {
+        setShowInvisibles: true,
+        setTabSize: true,
+        setShowGutter: true
+      };
+
+      editor.customMenuOptions = {
+        setEnableAutocompleter: function (enabled) {
+          editor.setOption('enableBasicAutocompletion', enabled);
+          snippet.getApiHelper().setInTotalStorage('hue.ace', 'enableBasicAutocompletion', enabled);
+          if (enabled && $('#setEnableLiveAutocompletion:checked').length === 0) {
+            $('#setEnableLiveAutocompletion').trigger('click');
+          } else if (!enabled && $('#setEnableLiveAutocompletion:checked').length !== 0) {
+            $('#setEnableLiveAutocompletion').trigger('click');
+          }
+        },
+        getEnableAutocompleter: function () {
+          return editor.getOption('enableBasicAutocompletion');
+        },
+        setEnableLiveAutocompletion: function (enabled) {
+          editor.setOption('enableLiveAutocompletion', enabled);
+          snippet.getApiHelper().setInTotalStorage('hue.ace', 'enableLiveAutocompletion', enabled);
+          if (enabled && $('#setEnableAutocompleter:checked').length === 0) {
+            $('#setEnableAutocompleter').trigger('click');
+          }
+        },
+        getEnableLiveAutocompletion: function () {
+          return editor.getOption('enableLiveAutocompletion');
+        }
+      };
+
       $.extend(editorOptions, aceOptions);
 
+      editorOptions['enableBasicAutocompletion'] = snippet.getApiHelper().getFromTotalStorage('hue.ace', 'enableBasicAutocompletion', true);
+      if (editorOptions['enableBasicAutocompletion']) {
+        editorOptions['enableLiveAutocompletion'] = snippet.getApiHelper().getFromTotalStorage('hue.ace', 'enableLiveAutocompletion', true);
+      }
+
       editor.setOptions(editorOptions);
 
       var AceAutocomplete = ace.require("ace/autocomplete").Autocomplete;
@@ -2660,13 +2694,15 @@
       editor.completer.exactMatch = ! snippet.isSqlDialect();
 
       var initAutocompleters = function () {
-        editor.completers.length = 0;
-        if(! options.useNewAutocompleter) {
-          editor.completers.push(langTools.snippetCompleter);
-          editor.completers.push(langTools.textCompleter);
-          editor.completers.push(langTools.keyWordCompleter);
+        if (editor.completers) {
+          editor.completers.length = 0;
+          if(! options.useNewAutocompleter) {
+            editor.completers.push(langTools.snippetCompleter);
+            editor.completers.push(langTools.textCompleter);
+            editor.completers.push(langTools.keyWordCompleter);
+          }
+          editor.completers.push(snippet.autocompleter);
         }
-        editor.completers.push(snippet.autocompleter);
       };
 
       var langTools = ace.require("ace/ext/language_tools");
@@ -3138,17 +3174,13 @@
         }
       });
 
+      var autocompleteTemporarilyDisabled = false;
       editor.commands.on("afterExec", function (e) {
-        if (e.command.name === "insertstring") {
-          var triggerAutocomplete = ((editor.session.getMode().$id == "ace/mode/hive" || editor.session.getMode().$id == "ace/mode/impala") && (e.args == "." || e.args == " ")) || /["']\/[^\/]*/.test(editor.getTextBeforeCursor());
+        if (editor.getOption('enableLiveAutocompletion') && e.command.name === "insertstring") {
           var questionMarkMatch = editor.getTextBeforeCursor().match(/select \? from \S+[^.]$/i);
           if (questionMarkMatch) {
             editor.moveCursorTo(editor.getCursorPosition().row, editor.getCursorPosition().column - questionMarkMatch[0].length + 8);
             editor.removeTextBeforeCursor(1);
-            triggerAutocomplete = true;
-          }
-
-          if (triggerAutocomplete) {
             window.setTimeout(function () {
               editor.execCommand("startAutocomplete");
             }, 1);
@@ -3156,11 +3188,14 @@
         }
         editor.session.getMode().$id = snippet.getAceMode(); // forces the id again because of Ace command internals
         // if it's pig and before it's LOAD ' we disable the autocomplete and show a filechooser btn
-        if (editor.session.getMode().$id = "ace/mode/pig" && e.args) {
+        if (editor.session.getMode().$id === "ace/mode/pig" && e.args) {
           var textBefore = editor.getTextBeforeCursor();
           if ((e.args == "'" && textBefore.toUpperCase().indexOf("LOAD ") > -1 && textBefore.toUpperCase().indexOf("LOAD ") == textBefore.toUpperCase().length - 5)
               || textBefore.toUpperCase().indexOf("LOAD '") > -1 && textBefore.toUpperCase().indexOf("LOAD '") == textBefore.toUpperCase().length - 6) {
-            editor.disableAutocomplete();
+            if (editor.getOption('enableBasicAutocompletion')) {
+              editor.disableAutocomplete();
+              autocompleteTemporarilyDisabled = true;
+            }
             var btn = editor.showFileButton();
             btn.on("click", function (ie) {
               ie.preventDefault();
@@ -3174,7 +3209,10 @@
                 onFileChoose: function (filePath) {
                   editor.session.insert(editor.getCursorPosition(), filePath + "'");
                   editor.hideFileButton();
-                  editor.enableAutocomplete();
+                  if (autocompleteTemporarilyDisabled) {
+                    editor.enableAutocomplete();
+                    autocompleteTemporarilyDisabled = false;
+                  }
                   $(".ace-filechooser").hide();
                 },
                 selectFolder: false,
@@ -3182,14 +3220,19 @@
               });
               $(".ace-filechooser").css({ "top": $(ie.currentTarget).position().top, "left": $(ie.currentTarget).position().left}).show();
             });
-          }
-          else {
+          } else {
             editor.hideFileButton();
-            editor.enableAutocomplete();
+            if (autocompleteTemporarilyDisabled) {
+              editor.enableAutocomplete();
+              autocompleteTemporarilyDisabled = false;
+            }
           }
           if (e.args != "'" && textBefore.toUpperCase().indexOf("LOAD '") > -1 && textBefore.toUpperCase().indexOf("LOAD '") == textBefore.toUpperCase().length - 6) {
             editor.hideFileButton();
-            editor.enableAutocomplete();
+            if (autocompleteTemporarilyDisabled) {
+              editor.enableAutocomplete();
+              autocompleteTemporarilyDisabled = false;
+            }
           }
         }
       });

+ 23 - 9
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -371,6 +371,7 @@ ${ hueIcons.symbols() }
         <li><a href="#helpFindReplace" data-toggle="tab">${ _('Find/Replace')}</a></li>
         <li><a href="#helpFolding" data-toggle="tab">${ _('Folding')}</a></li>
         <li><a href="#helpOther" data-toggle="tab">${ _('Other')}</a></li>
+        <li><a href="#helpSettings" data-toggle="tab">${ _('Settings')}</a></li>
       </ul>
 
       <div class="tab-content">
@@ -761,9 +762,9 @@ ${ hueIcons.symbols() }
         </tr>
         </tbody>
       </table>
-        </div>
-        <div class="tab-pane" id="helpOther">
-          <table class="table">
+     </div>
+     <div class="tab-pane" id="helpOther">
+      <table class="table">
         <thead>
         <tr>
           <th>Windows/Linux</th>
@@ -792,11 +793,6 @@ ${ hueIcons.symbols() }
           <td>Command-Shift-Z, Command-Y</td>
           <td>${ _('Redo')}</td>
         </tr>
-        <tr>
-          <td>Ctrl-,</td>
-          <td>Command-,</td>
-          <td>${ _('Show the settings menu')}</td>
-        </tr>
         <tr>
           <td>Ctrl-/</td>
           <td>Command-/</td>
@@ -844,7 +840,25 @@ ${ hueIcons.symbols() }
         </tr>
         </tbody>
       </table>
-        </div>
+     </div>
+     <div class="tab-pane" id="helpSettings">
+      <table class="table">
+        <thead>
+        <tr>
+          <th>Windows/Linux</th>
+          <th>Mac</th>
+          <th>${ _('Action')}</th>
+        </tr>
+        </thead>
+        <tbody>
+        <tr>
+          <td>Ctrl - ,</td>
+          <td>Command - ,</td>
+          <td>${ _('Show the settings menu')}</td>
+        </tr>
+        </tbody>
+      </table>
+    </div>
       </div>
   </div>
   <div class="modal-footer">

+ 10 - 6
tools/ace-editor/lib/ace/ext/menu_tools/generate_settings_menu.js

@@ -92,11 +92,6 @@ module.exports.generateSettingsMenu = function generateSettingsMenu (editor) {
             topmenu.appendChild(element);
         });
         
-        var el = topmenu.appendChild(document.createElement('div'));
-        var version = require("../../ace").version;
-        el.style.padding = "1em";
-        el.textContent = "Ace version " + version;
-        
         return topmenu;
     }
     /**
@@ -252,8 +247,17 @@ module.exports.generateSettingsMenu = function generateSettingsMenu (editor) {
     // gather the set functions
     getSetFunctions(editor).forEach(function(setObj) {
         // populate the elements array with good stuff.
-        handleSet(setObj);
+        if (!editor.enabledMenuOptions || editor.enabledMenuOptions[setObj.functionName]) {
+            handleSet(setObj);
+        }
     });
+
+    if (editor.customMenuOptions) {
+        getSetFunctions(editor.customMenuOptions).forEach(function(setObj) {
+            // populate the elements array with good stuff.
+            handleSet(setObj);
+        });
+    }
     // sort the menu entries in the elements list so people can find
     // the settings in alphabetical order.
     cleanupElementsList();

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio