|
| 1 | +import type { |
| 2 | + CompletionContext, |
| 3 | + CompletionSource, |
| 4 | +} from '@codemirror/autocomplete'; |
| 5 | +import { syntaxTree } from '@codemirror/language'; |
| 6 | +import { completeAnyWord, ifIn } from '@codemirror/autocomplete'; |
| 7 | +import { completer, wrapField } from '../autocompleter'; |
| 8 | +import { languageName } from '../json-editor'; |
| 9 | + |
| 10 | +const completeWordsInString = ifIn(['String'], completeAnyWord); |
| 11 | + |
| 12 | +function resolveTokenAtCursor(context: CompletionContext) { |
| 13 | + return syntaxTree(context.state).resolveInner(context.pos, -1); |
| 14 | +} |
| 15 | + |
| 16 | +type Token = ReturnType<typeof resolveTokenAtCursor>; |
| 17 | + |
| 18 | +function isJSONPropertyName(token: Token): boolean { |
| 19 | + return ( |
| 20 | + // Cursor is currently on the valid property name in the object |
| 21 | + token.name === 'PropertyName' || |
| 22 | + // Cursor is possibly on the invalid property name as indicated by the |
| 23 | + // previous sibling being a property or an open bracket and not a |
| 24 | + // `PropertyName`, which would be the case for property value |
| 25 | + (token.type.isError && |
| 26 | + ['Property', '{'].includes(token.prevSibling?.name ?? '')) |
| 27 | + ); |
| 28 | +} |
| 29 | +function isJavaScriptPropertyName(token: Token): boolean { |
| 30 | + return ( |
| 31 | + // Cursor is currently inside a property |
| 32 | + token.parent?.name === 'Property' && |
| 33 | + // There is no previous sibling or it's an opening bracket (indicating |
| 34 | + // computed property) |
| 35 | + (!token.prevSibling || token.prevSibling.name === '[') |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Autocompleter for the document object, only autocompletes field names in the |
| 41 | + * appropriate format (either escaped or not) both for javascript and json modes |
| 42 | + */ |
| 43 | +export const createDocumentAutocompleter = ( |
| 44 | + fields: string[] |
| 45 | +): CompletionSource => { |
| 46 | + const completions = completer('', { fields, meta: ['field:identifier'] }); |
| 47 | + |
| 48 | + return (context) => { |
| 49 | + const token = resolveTokenAtCursor(context); |
| 50 | + |
| 51 | + const shouldAlwaysEscapeProperty = |
| 52 | + context.state.facet(languageName)[0] === 'json'; |
| 53 | + |
| 54 | + if (isJSONPropertyName(token) || isJavaScriptPropertyName(token)) { |
| 55 | + const prefix = context.state |
| 56 | + .sliceDoc(token.from, context.pos) |
| 57 | + .replace(/^("|')/, ''); |
| 58 | + |
| 59 | + return { |
| 60 | + from: token.from, |
| 61 | + to: token.to, |
| 62 | + options: completions |
| 63 | + .filter((completion) => { |
| 64 | + return completion.value |
| 65 | + .toLowerCase() |
| 66 | + .startsWith(prefix.toLowerCase()); |
| 67 | + }) |
| 68 | + .map((completion) => { |
| 69 | + return { |
| 70 | + label: wrapField(completion.value, shouldAlwaysEscapeProperty), |
| 71 | + // https://codemirror.net/docs/ref/#autocomplete.Completion.type |
| 72 | + type: 'property', |
| 73 | + detail: 'field', |
| 74 | + }; |
| 75 | + }), |
| 76 | + filter: false, |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + return completeWordsInString(context); |
| 81 | + }; |
| 82 | +}; |
0 commit comments