Bläddra i källkod

[editor] Smart SQL formatter indentation improvements with sql-formatter

Next step would be to publish/pull the one from https://github.com/romainr/sql-formatter
which contains a prettier semi colon indentation.
Romain Rigaux 4 år sedan
förälder
incheckning
44ca62f4fe
3 ändrade filer med 19 tillägg och 28525 borttagningar
  1. 17 24
      desktop/core/src/desktop/js/apps/editor/components/ko.snippetEditorActions.js
  2. 1 28501
      package-lock.json
  3. 1 0
      package.json

+ 17 - 24
desktop/core/src/desktop/js/apps/editor/components/ko.snippetEditorActions.js

@@ -17,6 +17,7 @@
 import * as ko from 'knockout';
 
 import 'ko/bindings/ko.publish';
+import { format } from 'sql-formatter';
 
 import apiHelper from 'api/apiHelper';
 import componentUtils from 'ko/components/componentUtils';
@@ -165,30 +166,22 @@ class SnippetEditorActions {
     }
 
     hueAnalytics.log('notebook', 'format');
-    apiHelper
-      .formatSql({
-        statements:
-          this.snippet.ace().getSelectedText() !== ''
-            ? this.snippet.ace().getSelectedText()
-            : this.snippet.statement_raw()
-      })
-      .done(data => {
-        if (data.status === 0) {
-          if (this.snippet.ace().getSelectedText() !== '') {
-            this.snippet
-              .ace()
-              .session.replace(
-                this.snippet.ace().session.selection.getRange(),
-                data.formatted_statements
-              );
-          } else {
-            this.snippet.statement_raw(data.formatted_statements);
-            this.snippet.ace().setValue(this.snippet.statement_raw(), 1);
-          }
-        } else {
-          this.snippet.handleAjaxError(data);
-        }
-      });
+
+    const formatted_statements = format(
+      this.snippet.ace().getSelectedText() !== ''
+        ? this.snippet.ace().getSelectedText()
+        : this.snippet.statement_raw(),
+      { uppercase: true, linesBetweenQueries: 2, indentQuerySeparator: true }
+    );
+
+    if (this.snippet.ace().getSelectedText() !== '') {
+      this.snippet
+        .ace()
+        .session.replace(this.snippet.ace().session.selection.getRange(), formatted_statements);
+    } else {
+      this.snippet.statement_raw(formatted_statements);
+      this.snippet.ace().setValue(this.snippet.statement_raw(), 1);
+    }
   }
 
   dispose() {}

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 1 - 28501
package-lock.json


+ 1 - 0
package.json

@@ -79,6 +79,7 @@
     "selectize": "0.12.6",
     "selectize-plugin-clear": "0.0.3",
     "sprintf-js": "1.1.2",
+    "sql-formatter": "^4.0.2",
     "vue": "^3.0.5",
     "vue-custom-element": "3.2.14",
     "vue3-datepicker": "^0.2.4"

Vissa filer visades inte eftersom för många filer har ändrats