diff --git a/.gitignore b/.gitignore index 3aad76d03d..6fad6eca6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ logs *.log +**/*.log npm-debug.log* yarn-debug.log* yarn-error.log* @@ -12,6 +13,7 @@ coverage .lock-wscript build/Release node_modules/ +**/node_modules/ .npm .eslintcache .node_repl_history @@ -20,4 +22,5 @@ node_modules/ .env .idea .DS_Store -*test-plugins \ No newline at end of file +*test-plugins +.vscode \ No newline at end of file diff --git a/packages/insomnia-app/app/ui/components/codemirror/extensions/autocomplete.js b/packages/insomnia-app/app/ui/components/codemirror/extensions/autocomplete.js index 13f5e3acbb..fb6fa92634 100644 --- a/packages/insomnia-app/app/ui/components/codemirror/extensions/autocomplete.js +++ b/packages/insomnia-app/app/ui/components/codemirror/extensions/autocomplete.js @@ -206,12 +206,19 @@ function hint (cm, options) { const lowPriorityMatches = []; const highPriorityMatches = []; + let mode; + try { + mode = cm.getOption('mode').baseMode; + } catch (e) { + mode = 'unknown'; + } + // Match variables if (allowMatchingVariables) { matchSegments(variablesToMatch, nameSegment, TYPE_VARIABLE, MAX_VARIABLES) - .map(m => lowPriorityMatches.push(m)); + .forEach(m => lowPriorityMatches.push(m)); matchSegments(variablesToMatch, nameSegmentLong, TYPE_VARIABLE, MAX_VARIABLES) - .map(m => highPriorityMatches.push(m)); + .forEach(m => highPriorityMatches.push(m)); } // Match constants @@ -219,23 +226,36 @@ function hint (cm, options) { const cur = cm.getCursor(); const token = cm.getTokenAt(cur); - if (token.type === 'variable') { - // For GraphQL to autocomplete constants (variables) in JSON keys - matchSegments(constantsToMatch, nameSegment, TYPE_CONSTANT, MAX_CONSTANTS) - .map(m => highPriorityMatches.push(m)); + if (mode === 'graphql-variables') { + const segment = token.string.trim() + .replace(/^{?"?/, '') // Remove leading '{' and spaces + .replace(/"?}?$/, ''); // Remove trailing quotes and spaces + + if (token.type === 'variable') { + // We're inside a JSON key + matchSegments(constantsToMatch, segment, TYPE_CONSTANT, MAX_CONSTANTS) + .forEach(m => highPriorityMatches.push(m)); + } else if (token.type === 'invalidchar' + || token.type === 'ws' + || (token.type === 'punctuation' && token.string === '{') + ) { + // We're outside of a JSON key + matchSegments(constantsToMatch, segment, TYPE_CONSTANT, MAX_CONSTANTS) + .forEach(m => highPriorityMatches.push({...m, text: '"' + m.text + '": '})); + } } else { // Otherwise match full segments matchSegments(constantsToMatch, nameSegmentFull, TYPE_CONSTANT, MAX_CONSTANTS) - .map(m => highPriorityMatches.push(m)); + .forEach(m => highPriorityMatches.push(m)); } } // Match tags if (allowMatchingTags) { matchSegments(tagsToMatch, nameSegment, TYPE_TAG, MAX_TAGS) - .map(m => lowPriorityMatches.push(m)); + .forEach(m => lowPriorityMatches.push(m)); matchSegments(tagsToMatch, nameSegmentLong, TYPE_TAG, MAX_TAGS) - .map(m => highPriorityMatches.push(m)); + .forEach(m => highPriorityMatches.push(m)); } const matches = [...highPriorityMatches, ...lowPriorityMatches];