Fix autocomplete and make the code a bit better (#2394)

This commit is contained in:
Gregory Schier 2020-07-10 14:11:23 -07:00 committed by GitHub
parent 5e8f1ee13e
commit 938cb9ad54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -184,10 +184,16 @@ CodeMirror.defineOption('environmentAutocomplete', null, (cm, options) => {
* @returns {Promise.<{list: Array, from, to}>} * @returns {Promise.<{list: Array, from, to}>}
*/ */
function hint(cm, options) { function hint(cm, options) {
const variablesToMatch = options.variables || []; // Add type to all things (except constants, which need to convert to an object)
const constantsToMatch = options.constants || []; const variablesToMatch = (options.variables || []).map(v => ({ ...v, type: TYPE_VARIABLE }));
const snippetsToMatch = options.snippets || []; const snippetsToMatch = (options.snippets || []).map(v => ({ ...v, type: TYPE_SNIPPET }));
const tagsToMatch = options.tags || []; const tagsToMatch = (options.tags || []).map(v => ({ ...v, type: TYPE_TAG }));
const constantsToMatch = (options.constants || []).map(s => ({
name: s,
value: s,
displayValue: '', // No display since name === value
type: TYPE_CONSTANT,
}));
// Get the text from the cursor back // Get the text from the cursor back
const cur = cm.getCursor(); const cur = cm.getCursor();
@ -372,12 +378,19 @@ function matchSegments(listOfThings, segment, type, limit = -1) {
// Generate the value we'll fill it with // Generate the value we'll fill it with
let defaultFill; let defaultFill;
if (t.args) { if (t.type === TYPE_CONSTANT) {
defaultFill = getDefaultFill(t.name, t.args);
} else if (t.value) {
defaultFill = t.value; defaultFill = t.value;
} else if (t.type === TYPE_SNIPPET) {
defaultFill = t.value;
} else if (t.type === TYPE_VARIABLE) {
// Variables fill with variable name, not value (eg. {{ foo }})
// TODO: This is extremely confusing and does not make any sense so let's
// refactor this to a single unified format for all types
defaultFill = t.name;
} else if (t.type === TYPE_TAG) {
defaultFill = getDefaultFill(t.name, t.args);
} else { } else {
defaultFill = name; throw new Error('Unidentified autocomplete type: ' + t.type);
} }
const matchSegment = segment.toLowerCase(); const matchSegment = segment.toLowerCase();