Skip to content

Commit

Permalink
[ES|QL] Make getActions more resilient to lack of callbacks (elasti…
Browse files Browse the repository at this point in the history
…c#180260)

## Summary

Now `getActions` provides some more fixes in case of lack of callabacks,
like with unquoted fields.
The feature is still experimental and applies only to unquoted fields
(disabling the existence check on quoted fields).

### Checklist

- [x]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
dej611 authored Apr 8, 2024
1 parent 069d814 commit c2adb13
Show file tree
Hide file tree
Showing 6 changed files with 475 additions and 163 deletions.
4 changes: 2 additions & 2 deletions packages/kbn-esql-ast/src/antlr_error_listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class ESQLErrorListener extends ErrorListener<any> {
const textMessage = `SyntaxError: ${message}`;

const tokenPosition = getPosition(offendingSymbol);
const startColumn = tokenPosition?.min + 1 || column;
const endColumn = tokenPosition?.max + 1 || column + 1;
const startColumn = offendingSymbol && tokenPosition ? tokenPosition.min + 1 : column + 1;
const endColumn = offendingSymbol && tokenPosition ? tokenPosition.max + 1 : column + 2;

this.errors.push({
startLineNumber: line,
Expand Down
28 changes: 28 additions & 0 deletions packages/kbn-esql-validation-autocomplete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const {title, edits} = await getActions(
queryString,
errors,
getAstAndSyntaxErrors,
undefined,
myCallbacks
);

Expand All @@ -124,6 +125,33 @@ const {title, edits} = await getActions(
console.log({ title, edits });
```

Like with validation also `getActions` can 'relax' its internal checks when no callbacks, either all or specific ones, are passed.

```js
import { getAstAndSyntaxErrors } from '@kbn/esql-ast';
import { validateQuery, getActions } from '@kbn/esql-validation-autocomplete';

const queryString = "from index2 | keep unquoted-field"

const myCallbacks = {
getSources: async () => [{name: 'index', hidden: false}],
...
};
const { errors, warnings } = await validateQuery(queryString, getAstAndSyntaxErrors, undefined, myCallbacks);

const {title, edits} = await getActions(
queryString,
errors,
getAstAndSyntaxErrors,
{ relaxOnMissingCallbacks: true },
myCallbacks
);

console.log(edits[0].text); // => `unquoted-field`
```

**Note**: this behaviour is still experimental, and applied for few error types, like the unquoted fields case.

### getAstContext

This is an important function in order to build more features on top of the existing one.
Expand Down
Loading

0 comments on commit c2adb13

Please sign in to comment.