From d8d59fd8d48c600d878417c0b36a66f2dde110f4 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 10 Aug 2017 15:28:40 +0300 Subject: [PATCH] ace: add custom behaviour for prometheus --- .../components/code_editor/mode-prometheus.js | 78 ++++++++++++++++++- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/public/app/core/components/code_editor/mode-prometheus.js b/public/app/core/components/code_editor/mode-prometheus.js index d16f20b2f07..1cb45caf44d 100644 --- a/public/app/core/components/code_editor/mode-prometheus.js +++ b/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);