Просмотр исходного кода

ace: add custom behaviour for prometheus

Alexander Zobnin 8 лет назад
Родитель
Сommit
d8d59fd8d4
1 измененных файлов с 75 добавлено и 3 удалено
  1. 75 3
      public/app/core/components/code_editor/mode-prometheus.js

+ 75 - 3
public/app/core/components/code_editor/mode-prometheus.js

@@ -49,10 +49,10 @@ var PrometheusHighlightRules = function() {
       regex : "\\+|\\-|\\*|\\/|%|\\^|=|==|!=|<=|>=|<|>|=\\~|!\\~"
     }, {
       token : "paren.lparen",
-      regex : "[\\(]"
+      regex : "[[({]"
     }, {
       token : "paren.rparen",
-      regex : "[\\)]"
+      regex : "[\\])}]"
     }, {
       token : "text",
       regex : "\\s+"
@@ -65,16 +65,88 @@ oop.inherits(PrometheusHighlightRules, TextHighlightRules);
 exports.PrometheusHighlightRules = PrometheusHighlightRules;
 });
 
+ace.define("ace/mode/behaviour/prometheus",["require","exports","module","ace/lib/oop","ace/mode/behaviour","ace/mode/behaviour/cstyle","ace/token_iterator"], function(require, exports, module) {
+"use strict";
+
+var oop = require("../../lib/oop");
+var Behaviour = require("../behaviour").Behaviour;
+var CstyleBehaviour = require("./cstyle").CstyleBehaviour;
+var TokenIterator = require("../../token_iterator").TokenIterator;
+
+function getWrapped(selection, selected, opening, closing) {
+  var rowDiff = selection.end.row - selection.start.row;
+  return {
+    text: opening + selected + closing,
+    selection: [
+      0,
+      selection.start.column + 1,
+      rowDiff,
+      selection.end.column + (rowDiff ? 0 : 1)
+    ]
+  };
+};
+
+var PrometheusBehaviour = function () {
+  this.inherit(CstyleBehaviour);
+
+  // Rewrite default CstyleBehaviour for {} braces
+  this.add("braces", "insertion", function(state, action, editor, session, text) {
+    if (text == '{') {
+      var selection = editor.getSelectionRange();
+      var selected = session.doc.getTextRange(selection);
+      if (selected !== "" && editor.getWrapBehavioursEnabled()) {
+        return getWrapped(selection, selected, '{', '}');
+      } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
+        return {
+          text: '{}',
+          selection: [1, 1]
+        };
+      }
+    } else if (text == '}') {
+      var cursor = editor.getCursorPosition();
+      var line = session.doc.getLine(cursor.row);
+      var rightChar = line.substring(cursor.column, cursor.column + 1);
+      if (rightChar == '}') {
+        var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
+        if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
+          return {
+            text: '',
+            selection: [1, 1]
+          };
+        }
+      }
+    }
+  });
+
+  this.add("braces", "deletion", function(state, action, editor, session, range) {
+    var selected = session.doc.getTextRange(range);
+    if (!range.isMultiLine() && selected == '{') {
+      var line = session.doc.getLine(range.start.row);
+      var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
+      if (rightChar == '}') {
+        range.end.column++;
+        return range;
+      }
+    }
+  });
+
+}
+oop.inherits(PrometheusBehaviour, CstyleBehaviour);
+
+exports.PrometheusBehaviour = PrometheusBehaviour;
+});
+
 ace.define("ace/mode/prometheus",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/prometheus_highlight_rules"], function(require, exports, module) {
 "use strict";
 
 var oop = require("../lib/oop");
 var TextMode = require("./text").Mode;
 var PrometheusHighlightRules = require("./prometheus_highlight_rules").PrometheusHighlightRules;
+var PrometheusBehaviour = require("./behaviour/prometheus").PrometheusBehaviour;
 
 var Mode = function() {
   this.HighlightRules = PrometheusHighlightRules;
-  this.$behaviour = this.$defaultBehaviour;
+  this.$behaviour = new PrometheusBehaviour();
 };
 oop.inherits(Mode, TextMode);