Skip to content

Commit

Permalink
✨ Improved checks for sources and policies
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Jan 11, 2024
1 parent 58e902e commit afecef9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ const fields: Array<{ name: string; type: string; suggestedAs?: string }> = [
const indexes = (
[] as Array<{ name: string; hidden: boolean; suggestedAs: string | undefined }>
).concat(
['a', 'index', 'otherIndex', '.secretIndex'].map((name) => ({
['a', 'index', 'otherIndex', '.secretIndex', 'my-index'].map((name) => ({
name,
hidden: name.startsWith('.'),
suggestedAs: undefined,
})),
[{ name: 'my-index', hidden: false, suggestedAs: '`my-index`' }]
['my-index[quoted]', 'my-index$', 'my_index{}'].map((name) => ({
name,
hidden: false,
suggestedAs: `\`${name}\``,
}))
);
const policies = [
{
Expand All @@ -50,13 +54,13 @@ const policies = [
matchField: 'otherStringField',
enrichFields: ['otherField', 'yetAnotherField', 'yet-special-field'],
},
{
name: 'my-policy',
...['my-policy[quoted]', 'my-policy$', 'my_policy{}'].map((name) => ({
name,
sourceIndices: ['enrichIndex1'],
matchField: 'otherStringField',
enrichFields: ['otherField', 'yetAnotherField', 'yet-special-field'],
suggestedAs: '`my-policy`',
},
suggestedAs: `\`${name}\``,
})),
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export const TRIGGER_SUGGESTION_COMMAND = {
id: 'editor.action.triggerSuggest',
};

function getSafeInsertText(text: string) {
function getSafeInsertText(text: string, { dashSupported }: { dashSupported?: boolean } = {}) {
if (dashSupported) {
return /[^a-zA-Z\d_\.@-]/.test(text) ? `\`${text}\`` : text;
}
return /[^a-zA-Z\d_\.@]/.test(text) ? `\`${text}\`` : text;
}

Expand Down Expand Up @@ -130,7 +133,7 @@ export const buildVariablesDefinitions = (variables: string[]): AutocompleteComm
export const buildSourcesDefinitions = (sources: string[]): AutocompleteCommandDefinition[] =>
sources.map((label) => ({
label,
insertText: getSafeInsertText(label),
insertText: getSafeInsertText(label, { dashSupported: true }),
kind: 21,
detail: i18n.translate('monaco.esql.autocomplete.sourceDefinition', {
defaultMessage: `Input table`,
Expand Down Expand Up @@ -171,7 +174,7 @@ export const buildPoliciesDefinitions = (
): AutocompleteCommandDefinition[] =>
policies.map(({ name: label, sourceIndices }) => ({
label,
insertText: getSafeInsertText(label),
insertText: getSafeInsertText(label, { dashSupported: true }),
kind: 5,
detail: i18n.translate('monaco.esql.autocomplete.policyDefinition', {
defaultMessage: `Policy defined on {count, plural, one {index} other {indices}}: {indices}`,
Expand Down

0 comments on commit afecef9

Please sign in to comment.