Skip to content

Commit

Permalink
[v9.3.x] Tempo: Fix TraceQL autocomplete issues (grafana#60058) (graf…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamas Shafiq authored and GuaYounesPW committed Feb 8, 2023
1 parent 1bc6e48 commit 4b01f8f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
4 changes: 1 addition & 3 deletions .betterer.results
Expand Up @@ -6818,9 +6818,7 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "15"],
[0, 0, 0, "Unexpected any. Specify a different type.", "16"],
[0, 0, 0, "Unexpected any. Specify a different type.", "17"],
[0, 0, 0, "Unexpected any. Specify a different type.", "18"],
[0, 0, 0, "Unexpected any. Specify a different type.", "19"],
[0, 0, 0, "Unexpected any. Specify a different type.", "20"]
[0, 0, 0, "Unexpected any. Specify a different type.", "18"]
],
"public/app/plugins/datasource/tempo/traceql/autocomplete.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
Expand Down
23 changes: 18 additions & 5 deletions public/app/plugins/datasource/tempo/traceql/autocomplete.test.ts
Expand Up @@ -82,6 +82,17 @@ describe('CompletionProvider', () => {
]);
});

it('only suggests tags after typing the global attribute scope', async () => {
const { provider, model } = setup('{.}', 2, defaultTags);
const result = await provider.provideCompletionItems(
model as unknown as monacoTypes.editor.ITextModel,
{} as monacoTypes.Position
);
expect((result! as monacoTypes.languages.CompletionList).suggestions).toEqual(
defaultTags.map((s) => expect.objectContaining({ label: s, insertText: s }))
);
});

it('suggests operators after a space after the tag name', async () => {
const { provider, model } = setup('{ foo }', 6, defaultTags);
const result = await provider.provideCompletionItems(model as any, {} as any);
Expand All @@ -92,11 +103,13 @@ describe('CompletionProvider', () => {

it('suggests tags after a scope', async () => {
const { provider, model } = setup('{ resource. }', 11, defaultTags);
const result = await provider.provideCompletionItems(model as any, {} as any);
expect((result! as monacoTypes.languages.CompletionList).suggestions).toEqual([
...CompletionProvider.intrinsics.map((s) => expect.objectContaining({ label: s, insertText: s })),
...defaultTags.map((s) => expect.objectContaining({ label: s, insertText: s })),
]);
const result = await provider.provideCompletionItems(
model as unknown as monacoTypes.editor.ITextModel,
{} as monacoTypes.Position
);
expect((result! as monacoTypes.languages.CompletionList).suggestions).toEqual(
defaultTags.map((s) => expect.objectContaining({ label: s, insertText: s }))
);
});

it('suggests logical operators and close bracket after the value', async () => {
Expand Down
14 changes: 13 additions & 1 deletion public/app/plugins/datasource/tempo/traceql/autocomplete.ts
Expand Up @@ -120,10 +120,13 @@ export class CompletionProvider implements monacoTypes.languages.CompletionItemP
}
case 'SPANSET_EMPTY':
return this.getScopesCompletions().concat(this.getIntrinsicsCompletions()).concat(this.getTagsCompletions('.'));
case 'SPANSET_ONLY_DOT': {
return this.getTagsCompletions();
}
case 'SPANSET_IN_NAME':
return this.getScopesCompletions().concat(this.getIntrinsicsCompletions()).concat(this.getTagsCompletions());
case 'SPANSET_IN_NAME_SCOPE':
return this.getIntrinsicsCompletions().concat(this.getTagsCompletions());
return this.getTagsCompletions();
case 'SPANSET_AFTER_NAME':
return CompletionProvider.operators.map((key) => ({
label: key,
Expand Down Expand Up @@ -223,6 +226,12 @@ export class CompletionProvider implements monacoTypes.languages.CompletionItemP
};
}

if (nameFull === '.') {
return {
type: 'SPANSET_ONLY_DOT',
};
}

const nameMatched = nameFull.match(/^(?<pre_dot>\.)?(?<word>\w[\w./-]*\w)(?<post_dot>\.)?$/);

// We already have a (potentially partial) tag name so let's check if there's an operator declared
Expand Down Expand Up @@ -344,6 +353,9 @@ export type Situation =
| {
type: 'SPANSET_EMPTY';
}
| {
type: 'SPANSET_ONLY_DOT';
}
| {
type: 'SPANSET_AFTER_NAME';
}
Expand Down

0 comments on commit 4b01f8f

Please sign in to comment.