Skip to content

Commit

Permalink
Add console scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bleskes committed Dec 9, 2016
1 parent deec2eb commit c645975
Show file tree
Hide file tree
Showing 20 changed files with 597 additions and 140 deletions.
11 changes: 10 additions & 1 deletion src/core_plugins/console/api_server/es_5_0/query.js
Expand Up @@ -612,7 +612,16 @@ module.exports = function (api) {
min_score: 1.0
},
SCORING_FUNCS
)
),
script: {
__template: {
"script": "_score * doc['f'].value"
},
script: {
//populated by a global rule
}
},


});
};
8 changes: 8 additions & 0 deletions src/core_plugins/console/public/css/sense.light.less
Expand Up @@ -18,6 +18,14 @@
border-left: 1px solid #CCC;
}

.ace_multi_string {
color: #166555;
}

.ace_start_triple_quote, .ace_end_triple_quote {
color: #40B0D3;
}

.ace_snippet-marker {
background: rgba(194, 193, 208, 0.20);
border-top: dotted 1px rgba(194, 193, 208, 0.80);
Expand Down
36 changes: 23 additions & 13 deletions src/core_plugins/console/public/src/autocomplete.js
Expand Up @@ -8,7 +8,6 @@ let url_pattern_matcher = require('./autocomplete/url_pattern_matcher');
let _ = require('lodash');
let ext_lang_tools = require('ace/ext-language_tools');


var AceRange = ace.require('ace/range').Range;

var LAST_EVALUATED_TOKEN = null;
Expand Down Expand Up @@ -226,7 +225,6 @@ module.exports = function (editor) {
return null;
}


if (!context.autoCompleteSet) {
return null; // nothing to do..
}
Expand Down Expand Up @@ -445,7 +443,6 @@ module.exports = function (editor) {
break; // for now play safe and do nothing. May be made smarter.
}


// go back to see whether we have one of ( : { & [ do not require a comma. All the rest do.
tokenIter = editor.iterForCurrentLoc();
nonEmptyToken = tokenIter.getCurrentToken();
Expand All @@ -471,7 +468,6 @@ module.exports = function (editor) {
nonEmptyToken = editor.parser.prevNonEmptyToken(tokenIter);
}


switch (nonEmptyToken ? nonEmptyToken.type : "NOTOKEN") {
case "NOTOKEN":
case "paren.lparen":
Expand Down Expand Up @@ -581,7 +577,6 @@ module.exports = function (editor) {
return context;
}


// needed for scope linking + global term resolving
context.endpointComponentResolver = kb.getEndpointBodyCompleteComponents;
context.globalComponentResolver = kb.getGlobalAutocompleteComponents;
Expand All @@ -597,7 +592,6 @@ module.exports = function (editor) {
return context;
}


function getCurrentMethodAndTokenPaths(pos) {
var tokenIter = editor.iterForPosition(pos.row, pos.column);
var startPos = pos;
Expand Down Expand Up @@ -645,7 +639,6 @@ module.exports = function (editor) {
state = STATES.looking_for_scope_start; // skip everything until the beginning of this scope
break;


case "paren.lparen":
bodyTokenPath.unshift(t.value);
if (state == STATES.looking_for_scope_start) {
Expand Down Expand Up @@ -677,6 +670,29 @@ module.exports = function (editor) {
return {};
}
continue;
case "punctuation.end_triple_quote":
// reset the search for key
state = STATES.looking_for_scope_start;
for (t = tokenIter.stepBackward(); t; t = tokenIter.stepBackward()) {
if (t.type === "punctuation.start_tripple_qoute") {
t = tokenIter.stepBackward();
break;
}
}
if (!t) // oops we run out.. we don't know what's up return null;
{
return {};
}
continue;
case "punctuation.start_triple_quote":
if (state == STATES.start) {
state = STATES.looking_for_key;
}
else if (state == STATES.looking_for_key) {
state = STATES.looking_for_scope_start;
}
bodyTokenPath.unshift('"""');
continue;
case "string":
case "constant.numeric":
case "constant.language.boolean":
Expand Down Expand Up @@ -761,7 +777,6 @@ module.exports = function (editor) {
t = tokenIter.stepBackward();
}


curUrlPart = null;
while (t && t.type.indexOf("url") != -1) {
switch (t.type) {
Expand Down Expand Up @@ -814,7 +829,6 @@ module.exports = function (editor) {
return ret;
}


var evaluateCurrentTokenAfterAChange = _.debounce(function evaluateCurrentTokenAfterAChange(pos) {
var session = editor.getSession();
var currentToken = session.getTokenAt(pos.row, pos.column);
Expand Down Expand Up @@ -843,7 +857,6 @@ module.exports = function (editor) {
return;
}


if (!LAST_EVALUATED_TOKEN) {
LAST_EVALUATED_TOKEN = currentToken;
return; // wait for the next typing.
Expand Down Expand Up @@ -886,7 +899,6 @@ module.exports = function (editor) {
editor.off("changeSelection", editorChangeListener)
}


function getCompletions(aceEditor, session, pos, prefix, callback) {
try {

Expand Down Expand Up @@ -949,7 +961,6 @@ module.exports = function (editor) {
}
}


addChangeListener();

// Hook into Ace
Expand Down Expand Up @@ -997,7 +1008,6 @@ module.exports = function (editor) {
prefix = getAutoCompleteValueFromToken(token);
}


var matches = [];
aceUtils.parForEach(ace_editor.completers, function (completer, next) {
completer.getCompletions(ace_editor, session, pos, prefix, function (err, results) {
Expand Down
8 changes: 6 additions & 2 deletions src/core_plugins/console/public/src/input.js
Expand Up @@ -16,7 +16,11 @@ let input;
export function initializeInput($el, $actionsEl, $copyAsCurlEl, output) {
input = new SenseEditor($el);

uiModules.get('app/sense').setupResizeCheckerForRootEditors($el, input, output);
// this may not exist if running from tests
let appSense = uiModules.get('app/sense');
if (appSense.setupResizeCheckerForRootEditors) {
appSense.setupResizeCheckerForRootEditors($el, input, output);
}

input.autocomplete = new Autocomplete(input);

Expand Down Expand Up @@ -167,7 +171,7 @@ export function initializeInput($el, $actionsEl, $copyAsCurlEl, output) {
if (mode === null || mode === "application/json") {
// assume json - auto pretty
try {
value = JSON.stringify(JSON.parse(value), null, 2);
value = utils.expandScriptsToLiterals(JSON.stringify(JSON.parse(value), null, 2));
}
catch (e) {

Expand Down
12 changes: 7 additions & 5 deletions src/core_plugins/console/public/src/sense_editor/editor.js
Expand Up @@ -136,11 +136,12 @@ function SenseEditor($el) {
var formatted_data = utils.reformatData(parsed_req.data, indent);
if (!formatted_data.changed) {
// toggle.
formatted_data = utils.reformatData(parsed_req.data, !indent);
indent = !indent;
formatted_data = utils.reformatData(parsed_req.data, indent);
}
parsed_req.data = formatted_data.data;

editor.replaceRequestRange(parsed_req, req_range);
editor.replaceRequestRange(parsed_req, req_range, indent);
}
});
});
Expand All @@ -164,8 +165,8 @@ function SenseEditor($el) {

};

editor.replaceRequestRange = function (newRequest, requestRange) {
var text = utils.textFromRequest(newRequest);
editor.replaceRequestRange = function (newRequest, requestRange, autoExpandScripts) {
var text = utils.textFromRequest(newRequest, autoExpandScripts);
if (requestRange) {
var pos = editor.getCursorPosition();
editor.getSession().replace(requestRange, text);
Expand Down Expand Up @@ -317,7 +318,8 @@ function SenseEditor($el) {
dataEndPos.row, dataEndPos.column
);
var data = editor.getSession().getTextRange(bodyRange);
request.data.push(data.trim());
data = utils.collapseLiteralStrings(data.trim());
request.data.push(data);
bodyStartRow = dataEndPos.row + 1;
}

Expand Down
Expand Up @@ -4,6 +4,7 @@ let mode_json = require('ace/mode-json');

var oop = acequire("ace/lib/oop");
var TextMode = acequire("ace/mode/text").Mode;
var ScriptMode = require("./script").ScriptMode;
var MatchingBraceOutdent = acequire("ace/mode/matching_brace_outdent").MatchingBraceOutdent;
var CstyleBehaviour = acequire("ace/mode/behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = acequire("ace/mode/folding/cstyle").FoldMode;
Expand All @@ -20,6 +21,9 @@ var Mode = function () {
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.foldingRules = new CStyleFoldMode();
this.createModeDelegates({
"script-": ScriptMode
});
};
oop.inherits(Mode, TextMode);

Expand All @@ -32,7 +36,9 @@ oop.inherits(Mode, TextMode);
this.getNextLineIndent = function (state, line, tab) {
var indent = this.$getIndent(line);

if (state != "double_q_string") {
if (state == "string_literal") {
// noop
} else {
var match = line.match(/^.*[\{\(\[]\s*$/);
if (match) {
indent += tab;
Expand Down
@@ -1,4 +1,6 @@
let ace = require('ace');
let x_json = require('./x_json_highlight_rules');
let _ = require('lodash');

var oop = ace.require("ace/lib/oop");
var TextHighlightRules = ace.require("ace/mode/text_highlight_rules").TextHighlightRules;
Expand Down Expand Up @@ -55,101 +57,18 @@ var InputHighlightRules = function () {
addEOL(["url.param", "url.equal", "url.value"], /([^&=]+)(=)([^&]*)/, "start"),
addEOL(["url.param"], /([^&=]+)/, "start"),
addEOL(["url.amp"], /(&)/, "start")
),


"json": [
{
token: "variable", // single line
regex: '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]\\s*(?=:)'
},
{
token: "string", // single line
regex: '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
},
{
token: "constant.numeric", // hex
regex: "0[xX][0-9a-fA-F]+\\b"
},
{
token: "constant.numeric", // float
regex: "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
},
{
token: "constant.language.boolean",
regex: "(?:true|false)\\b"
},
{
token: "invalid.illegal", // single quoted strings are not allowed
regex: "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
},
{
token: "invalid.illegal", // comments are not allowed
regex: "\\/\\/.*$"
},
{
token: "paren.lparen",
merge: false,
regex: "{",
next: "json",
push: true
},
{
token: "paren.lparen",
merge: false,
regex: "[[(]"
},
{
token: "paren.rparen",
merge: false,
regex: "[\\])]"
},
{
token: "paren.rparen",
regex: "}",
merge: false,
next: "pop"
},
{
token: "punctuation.comma",
regex: ","
},
{
token: "punctuation.colon",
regex: ":"
},
{
token: "whitespace",
regex: "\\s+"
},
{
token: "text",
regex: ".+?"
}
],
"double_q_string": [
{
token: "string",
regex: '[^"]+'
},
{
token: "punctuation.end_quote",
regex: '"',
next: "json"
},
{
token: "string",
regex: "",
next: "json"
}
]
)
};

x_json.addToRules(this);

if (this.constructor === InputHighlightRules) {
this.normalizeRules();
}

};

oop.inherits(InputHighlightRules, TextHighlightRules);


module.exports.InputHighlightRules = InputHighlightRules;
@@ -1,14 +1,15 @@
let ace = require('ace');
let ace_mode_json = require('ace/mode-json');
let x_json = require('./x_json_highlight_rules');

var oop = ace.require("ace/lib/oop");
var JsonHighlightRules = ace.require("ace/mode/json_highlight_rules").JsonHighlightRules;

var OutputJsonHighlightRules = function () {

// regexp must not have capturing parentheses. Use (?:) instead.
// regexps are ordered -> the first match is used
this.$rules = new JsonHighlightRules().getRules();
this.$rules = {};

x_json.addToRules(this, 'start');

this.$rules.start.unshift(
{
Expand All @@ -17,6 +18,10 @@ var OutputJsonHighlightRules = function () {
}
);

if (this.constructor === OutputJsonHighlightRules) {
this.normalizeRules();
}

};

oop.inherits(OutputJsonHighlightRules, JsonHighlightRules);
Expand Down

0 comments on commit c645975

Please sign in to comment.