2017-03-12 00:31:23 +00:00
|
|
|
import CodeMirror from 'codemirror';
|
|
|
|
import 'codemirror/addon/mode/overlay';
|
|
|
|
|
2017-03-12 22:44:46 +00:00
|
|
|
const NAME_MATCH_FLEXIBLE = /[\w.\][\-/]+$/;
|
|
|
|
const NAME_MATCH = /[\w.\][]+$/;
|
2017-03-12 00:31:23 +00:00
|
|
|
const AFTER_VARIABLE_MATCH = /{{\s*[\w.\][]*$/;
|
|
|
|
const AFTER_TAG_MATCH = /{%\s*[\w.\][]*$/;
|
|
|
|
const COMPLETE_AFTER_VARIABLE_NAME = /[\w.\][]+/;
|
|
|
|
const COMPLETE_AFTER_CURLIES = /[^{]*\{[{%]\s*/;
|
|
|
|
const COMPLETION_CLOSE_KEYS = /[}|]/;
|
2017-03-12 09:03:57 +00:00
|
|
|
const ESCAPE_FORE_REGEX_MATCH = /[-[\]/{}()*+?.\\^$|]/g;
|
2017-03-12 00:31:23 +00:00
|
|
|
const MAX_HINT_LOOK_BACK = 100;
|
|
|
|
const HINT_DELAY_MILLIS = 100;
|
|
|
|
const TYPE_VARIABLE = 'variable';
|
|
|
|
const TYPE_TAG = 'tag';
|
|
|
|
const TYPE_CONSTANT = 'constant';
|
2017-03-12 05:38:44 +00:00
|
|
|
const MAX_CONSTANTS = -1;
|
|
|
|
const MAX_VARIABLES = -1;
|
|
|
|
const MAX_TAGS = -1;
|
2017-03-12 00:31:23 +00:00
|
|
|
|
|
|
|
const ICONS = {
|
2017-03-12 05:38:44 +00:00
|
|
|
[TYPE_CONSTANT]: '𝒄',
|
|
|
|
[TYPE_VARIABLE]: '𝑥',
|
|
|
|
[TYPE_TAG]: 'ƒ'
|
2017-03-12 00:31:23 +00:00
|
|
|
};
|
|
|
|
|
2017-03-12 05:38:44 +00:00
|
|
|
const TAGS = [
|
|
|
|
'uuid',
|
|
|
|
'now',
|
|
|
|
'response'
|
|
|
|
];
|
2017-03-12 00:31:23 +00:00
|
|
|
|
|
|
|
CodeMirror.defineExtension('isHintDropdownActive', function () {
|
|
|
|
return (
|
|
|
|
this.state.completionActive &&
|
|
|
|
this.state.completionActive.data &&
|
|
|
|
this.state.completionActive.data.list &&
|
|
|
|
this.state.completionActive.data.list.length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
CodeMirror.defineOption('environmentAutocomplete', null, (cm, options) => {
|
|
|
|
if (!options) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeAfter (cm, fn, showAllOnNoMatch = false) {
|
|
|
|
// Bail early if didn't match the callback test
|
|
|
|
if (fn && !fn()) {
|
|
|
|
return CodeMirror.Pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail early if completions are showing already
|
|
|
|
if (cm.isHintDropdownActive()) {
|
|
|
|
return CodeMirror.Pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the hints in a container with class "dropdown__menu" (for themes)
|
|
|
|
let hintsContainer = document.querySelector('#hints-container');
|
|
|
|
if (!hintsContainer) {
|
|
|
|
const el = document.createElement('div');
|
|
|
|
el.id = 'hints-container';
|
|
|
|
el.className = 'dropdown__menu';
|
|
|
|
document.body.appendChild(el);
|
|
|
|
hintsContainer = el;
|
|
|
|
}
|
|
|
|
|
|
|
|
const constants = options.getConstants && options.getConstants();
|
|
|
|
|
|
|
|
// Actually show the hint
|
|
|
|
cm.showHint({
|
|
|
|
// Insomnia-specific options
|
|
|
|
extraConstants: constants || [],
|
|
|
|
|
|
|
|
// Codemirror native options
|
|
|
|
hint,
|
|
|
|
getContext: options.getContext,
|
|
|
|
showAllOnNoMatch,
|
|
|
|
container: hintsContainer,
|
|
|
|
closeCharacters: COMPLETION_CLOSE_KEYS,
|
|
|
|
completeSingle: false,
|
|
|
|
// closeOnUnfocus: false, // Good for debugging (inspector)
|
|
|
|
extraKeys: {
|
|
|
|
'Tab': (cm, widget) => {
|
|
|
|
// Override default behavior and don't select hint on Tab
|
|
|
|
widget.close();
|
|
|
|
return CodeMirror.Pass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return CodeMirror.Pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeIfInVariableName (cm) {
|
|
|
|
return completeAfter(cm, () => {
|
|
|
|
const cur = cm.getCursor();
|
|
|
|
const pos = CodeMirror.Pos(cur.line, cur.ch - MAX_HINT_LOOK_BACK);
|
|
|
|
const range = cm.getRange(pos, cur);
|
|
|
|
return range.match(COMPLETE_AFTER_VARIABLE_NAME);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeIfAfterTagOrVarOpen (cm) {
|
|
|
|
return completeAfter(cm, () => {
|
|
|
|
const cur = cm.getCursor();
|
|
|
|
const pos = CodeMirror.Pos(cur.line, cur.ch - MAX_HINT_LOOK_BACK);
|
|
|
|
const range = cm.getRange(pos, cur);
|
|
|
|
return range.match(COMPLETE_AFTER_CURLIES);
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeForce (cm) {
|
|
|
|
return completeAfter(cm, null, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.on('keydown', (cm, e) => {
|
|
|
|
// Only operate on one-letter keys. This will filter out
|
|
|
|
// any special keys (Backspace, Enter, etc)
|
|
|
|
if (e.key.length > 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In a timeout so it gives the editor chance to update first
|
2017-03-12 09:03:57 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
completeIfInVariableName(cm);
|
|
|
|
}, HINT_DELAY_MILLIS);
|
2017-03-12 00:31:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add hot key triggers
|
|
|
|
cm.addKeyMap({
|
|
|
|
'Ctrl-Space': completeForce, // Force autocomplete on hotkey
|
|
|
|
"' '": completeIfAfterTagOrVarOpen
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to retrieve the list items
|
|
|
|
* @param cm
|
|
|
|
* @param options
|
|
|
|
* @returns {Promise.<{list: Array, from, to}>}
|
|
|
|
*/
|
|
|
|
async function hint (cm, options) {
|
|
|
|
// Get the text from the cursor back
|
|
|
|
const cur = cm.getCursor();
|
|
|
|
const pos = CodeMirror.Pos(cur.line, cur.ch - MAX_HINT_LOOK_BACK);
|
|
|
|
const previousText = cm.getRange(pos, cur);
|
|
|
|
|
|
|
|
// See if we're allowed matching tags, vars, or both
|
|
|
|
const isInVariable = previousText.match(AFTER_VARIABLE_MATCH);
|
|
|
|
const isInTag = previousText.match(AFTER_TAG_MATCH);
|
|
|
|
const isInNothing = !isInVariable && !isInTag;
|
|
|
|
const allowMatchingVariables = isInNothing || isInVariable;
|
|
|
|
const allowMatchingTags = isInNothing || isInTag;
|
|
|
|
const allowMatchingConstants = isInNothing;
|
|
|
|
|
|
|
|
// Define fallback segment to match everything or nothing
|
|
|
|
const fallbackSegment = options.showAllOnNoMatch ? '' : '__will_not_match_anything__';
|
|
|
|
|
|
|
|
// See if we're completing a variable name
|
|
|
|
const nameMatch = previousText.match(NAME_MATCH);
|
2017-03-12 22:44:46 +00:00
|
|
|
const nameMatchLong = previousText.match(NAME_MATCH_FLEXIBLE);
|
2017-03-12 00:31:23 +00:00
|
|
|
const nameSegment = nameMatch ? nameMatch[0] : fallbackSegment;
|
2017-03-12 22:44:46 +00:00
|
|
|
const nameSegmentLong = nameMatchLong ? nameMatchLong[0] : fallbackSegment;
|
2017-03-12 00:31:23 +00:00
|
|
|
|
|
|
|
// Actually try to match the list of things
|
|
|
|
const context = await options.getContext();
|
|
|
|
const allMatches = [];
|
|
|
|
|
|
|
|
if (allowMatchingConstants) {
|
2017-03-12 22:44:46 +00:00
|
|
|
matchSegments(
|
2017-03-12 00:31:23 +00:00
|
|
|
options.extraConstants,
|
2017-03-12 22:44:46 +00:00
|
|
|
[nameSegmentLong, nameSegment],
|
2017-03-12 00:31:23 +00:00
|
|
|
TYPE_CONSTANT,
|
|
|
|
MAX_CONSTANTS
|
2017-03-12 22:44:46 +00:00
|
|
|
).map(m => allMatches.push(m));
|
2017-03-12 00:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (allowMatchingVariables) {
|
2017-03-12 22:44:46 +00:00
|
|
|
matchSegments(
|
2017-03-12 00:31:23 +00:00
|
|
|
context.keys,
|
2017-03-12 22:44:46 +00:00
|
|
|
[nameSegmentLong, nameSegment],
|
2017-03-12 00:31:23 +00:00
|
|
|
TYPE_VARIABLE,
|
|
|
|
MAX_VARIABLES
|
2017-03-12 22:44:46 +00:00
|
|
|
).map(m => allMatches.push(m));
|
2017-03-12 00:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (allowMatchingTags) {
|
2017-03-12 22:44:46 +00:00
|
|
|
matchSegments(
|
2017-03-12 00:31:23 +00:00
|
|
|
TAGS,
|
2017-03-12 22:44:46 +00:00
|
|
|
[nameSegmentLong, nameSegment],
|
2017-03-12 00:31:23 +00:00
|
|
|
TYPE_TAG,
|
|
|
|
MAX_TAGS
|
2017-03-12 22:44:46 +00:00
|
|
|
).map(m => allMatches.push(m));
|
2017-03-12 00:31:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 22:44:46 +00:00
|
|
|
return {
|
|
|
|
list: allMatches,
|
|
|
|
from: CodeMirror.Pos(cur.line, cur.ch - nameSegment.length),
|
|
|
|
to: CodeMirror.Pos(cur.line, cur.ch)
|
|
|
|
};
|
2017-03-12 00:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the text in the editor when a hint is selected.
|
|
|
|
* This also makes sure there is whitespace surrounding it
|
|
|
|
* @param cm
|
|
|
|
* @param self
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
function replaceHintMatch (cm, self, data) {
|
2017-03-12 22:44:46 +00:00
|
|
|
const cur = cm.getCursor();
|
|
|
|
const from = CodeMirror.Pos(cur.line, cur.ch - data.segment.length);
|
|
|
|
const to = CodeMirror.Pos(cur.line, cur.ch);
|
|
|
|
|
|
|
|
const prevStart = CodeMirror.Pos(from.line, from.ch - 10);
|
|
|
|
const prevChars = cm.getRange(prevStart, from);
|
|
|
|
|
|
|
|
const nextEnd = CodeMirror.Pos(to.line, to.ch + 10);
|
|
|
|
const nextChars = cm.getRange(to, nextEnd);
|
2017-03-12 00:31:23 +00:00
|
|
|
|
|
|
|
let prefix = '';
|
|
|
|
let suffix = '';
|
|
|
|
|
|
|
|
if (data.type === TYPE_VARIABLE && !prevChars.match(/{{\s*$/)) {
|
|
|
|
prefix = '{{ '; // If no closer before
|
|
|
|
} else if (data.type === TYPE_VARIABLE && prevChars.match(/{{$/)) {
|
|
|
|
prefix = ' '; // If no space after opener
|
|
|
|
} else if (data.type === TYPE_TAG && !prevChars.match(/{%\s*$/)) {
|
|
|
|
prefix = '{% '; // If no closer before
|
|
|
|
} else if (data.type === TYPE_TAG && prevChars.match(/{%$/)) {
|
|
|
|
prefix = ' '; // If no space after opener
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.type === TYPE_VARIABLE && !nextChars.match(/^\s*}}/)) {
|
|
|
|
suffix = ' }}'; // If no closer after
|
|
|
|
} else if (data.type === TYPE_VARIABLE && nextChars.match(/^}}/)) {
|
|
|
|
suffix = ' '; // If no space before closer
|
|
|
|
} else if (data.type === TYPE_TAG && !nextChars.match(/^\s*%}/)) {
|
|
|
|
suffix = ' %}'; // If no closer after
|
|
|
|
} else if (data.type === TYPE_TAG && nextChars.match(/^%}/)) {
|
|
|
|
suffix = ' '; // If no space before closer
|
|
|
|
} else if (data.type === TYPE_TAG && nextChars.match(/^\s*}/)) {
|
|
|
|
// Edge case because "%" doesn't auto-close tags so sometimes you end
|
|
|
|
// up in the scenario of {% foo}
|
|
|
|
suffix = ' %';
|
|
|
|
}
|
|
|
|
|
2017-03-12 22:44:46 +00:00
|
|
|
cm.replaceRange(`${prefix}${data.text}${suffix}`, from, to);
|
2017-03-12 00:31:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 05:38:44 +00:00
|
|
|
/**
|
|
|
|
* Match against a list of things
|
|
|
|
* @param listOfThings - Can be list of strings or list of {name, value}
|
2017-03-12 22:44:46 +00:00
|
|
|
* @param segments - List of segments to match against
|
2017-03-12 05:38:44 +00:00
|
|
|
* @param type
|
|
|
|
* @param limit
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2017-03-12 22:44:46 +00:00
|
|
|
function matchSegments (listOfThings, segments, type, limit = -1) {
|
|
|
|
const matches = [];
|
|
|
|
|
|
|
|
for (const t of listOfThings) {
|
|
|
|
const name = typeof t === 'string' ? t : t.name;
|
|
|
|
const value = typeof t === 'string' ? '' : t.value;
|
|
|
|
|
|
|
|
for (const segment of segments) {
|
|
|
|
const matchSegment = segment.toLowerCase();
|
|
|
|
const matchName = name.toLowerCase();
|
|
|
|
|
|
|
|
// Throw away exact matches (why would we want to complete those?)
|
|
|
|
if (matchName === matchSegment) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throw away things that don't match
|
|
|
|
if (!matchName.includes(matchSegment)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
matches.push({
|
|
|
|
// Custom Insomnia keys
|
|
|
|
type,
|
|
|
|
segment,
|
|
|
|
comment: value,
|
|
|
|
displayValue: value ? JSON.stringify(value) : '',
|
|
|
|
score: name.length, // In case we want to sort by this
|
|
|
|
|
|
|
|
// CodeMirror
|
|
|
|
text: name,
|
|
|
|
displayText: name,
|
|
|
|
render: renderHintMatch,
|
|
|
|
hint: replaceHintMatch
|
|
|
|
});
|
|
|
|
|
|
|
|
// Only return the first match (2nd would be a duplicate)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit >= 0) {
|
|
|
|
return matches.slice(0, limit);
|
|
|
|
} else {
|
|
|
|
return matches;
|
|
|
|
}
|
2017-03-12 00:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace all occurrences of string
|
|
|
|
* @param text
|
|
|
|
* @param find
|
|
|
|
* @param prefix
|
|
|
|
* @param suffix
|
|
|
|
* @returns string
|
|
|
|
*/
|
|
|
|
function replaceWithSurround (text, find, prefix, suffix) {
|
2017-03-12 09:03:57 +00:00
|
|
|
const escapedString = find.replace(ESCAPE_FORE_REGEX_MATCH, '\\$&');
|
2017-03-12 00:31:23 +00:00
|
|
|
const re = new RegExp(escapedString, 'gi');
|
|
|
|
return text.replace(re, matched => prefix + matched + suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the autocomplete list entry
|
|
|
|
* @param li
|
|
|
|
* @param self
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
function renderHintMatch (li, self, data) {
|
|
|
|
// Bold the matched text
|
|
|
|
const {displayText, segment} = data;
|
|
|
|
const markedName = replaceWithSurround(displayText, segment, '<strong>', '</strong>');
|
|
|
|
|
|
|
|
li.innerHTML = `
|
|
|
|
<label class="label">${ICONS[data.type]}</label>
|
|
|
|
<div class="name">${markedName}</div>
|
|
|
|
<div class="value" title=${data.displayValue}>
|
|
|
|
${data.displayValue}
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
|
|
|
li.className += ` type--${data.type}`;
|
|
|
|
}
|