From 2a12283da89cd865789be2868e786edd41849fb4 Mon Sep 17 00:00:00 2001 From: Matias Chomicki Date: Thu, 13 Apr 2023 14:11:14 +0200 Subject: [PATCH] Loki Autocomplete: Update Lezer LogQL and update labels autocomplete after the upgrade (#66289) * Dependencies: update lezer * Loki Autocomplete: Add support for selectors containing error nodes * Update tests --- package.json | 2 +- .../situation.test.ts | 7 ++++ .../monaco-completion-provider/situation.ts | 35 ++++++++++++------- yarn.lock | 10 +++--- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index d266f25c225..6d395b02e71 100644 --- a/package.json +++ b/package.json @@ -262,7 +262,7 @@ "@grafana/faro-core": "1.0.2", "@grafana/faro-web-sdk": "1.0.2", "@grafana/google-sdk": "0.1.1", - "@grafana/lezer-logql": "0.1.2", + "@grafana/lezer-logql": "0.1.3", "@grafana/monaco-logql": "^0.0.7", "@grafana/runtime": "workspace:*", "@grafana/scenes": "^0.3.0", diff --git a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/situation.test.ts b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/situation.test.ts index 84ae6d606e6..3c24635f12f 100644 --- a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/situation.test.ts +++ b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/situation.test.ts @@ -115,6 +115,13 @@ describe('situation', () => { type: 'IN_LABEL_SELECTOR_NO_LABEL_NAME', otherLabels: [], }); + + ['sum({label="value",^})', '{label="value",^}', '{label="value", ^}'].forEach((query) => { + assertSituation(query, { + type: 'IN_LABEL_SELECTOR_NO_LABEL_NAME', + otherLabels: [{ name: 'label', value: 'value', op: '=' }], + }); + }); }); it('identifies labels from queries', () => { diff --git a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/situation.ts b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/situation.ts index 891dc320966..7bfa9b7fb70 100644 --- a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/situation.ts +++ b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/situation.ts @@ -143,6 +143,10 @@ const RESOLVERS: Resolver[] = [ path: [Selector], fun: resolveSelector, }, + { + path: [ERROR_NODE_ID, Matchers, Selector], + fun: resolveSelector, + }, { path: [LogQL], fun: resolveTopLevel, @@ -244,14 +248,11 @@ function getLabels(selectorNode: SyntaxNode, text: string): Label[] { while (listNode !== null) { const matcherNode = walk(listNode, [['lastChild', Matcher]]); - if (matcherNode === null) { - // unexpected, we stop - return []; - } - - const label = getLabel(matcherNode, text); - if (label !== null) { - labels.push(label); + if (matcherNode !== null) { + const label = getLabel(matcherNode, text); + if (label !== null) { + labels.push(label); + } } // there might be more labels @@ -469,14 +470,24 @@ function resolveSelector(node: SyntaxNode, text: string, pos: number): Situation // to be able to suggest adding the next label. // the area between the end-of-the-child-node and the cursor-pos // must contain a `,` in this case. - const textToCheck = text.slice(child.to, pos); - - if (!textToCheck.includes(',')) { + const textToCheck = text.slice(child.from, pos); + if (!textToCheck.trim().endsWith(',')) { return null; } } - const otherLabels = getLabels(node, text); + const selectorNode = + node.type.id === ERROR_NODE_ID + ? walk(node, [ + ['parent', Matchers], + ['parent', Selector], + ]) + : node; + if (!selectorNode) { + return null; + } + + const otherLabels = getLabels(selectorNode, text); return { type: 'IN_LABEL_SELECTOR_NO_LABEL_NAME', diff --git a/yarn.lock b/yarn.lock index 1b202d1a912..7100713f754 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3262,12 +3262,12 @@ __metadata: languageName: node linkType: hard -"@grafana/lezer-logql@npm:0.1.2": - version: 0.1.2 - resolution: "@grafana/lezer-logql@npm:0.1.2" +"@grafana/lezer-logql@npm:0.1.3": + version: 0.1.3 + resolution: "@grafana/lezer-logql@npm:0.1.3" peerDependencies: "@lezer/lr": ^1.0.0 - checksum: 4b62fda9c2c7e29c48f7485df19573b96bfd34539529283980f24e9931f81b44c86b1e655bae5f50730d9d7b37bae87e6f7f7773dafaf4f6acef90b2c2cb776f + checksum: 160e4039ce7dd0cd304d37608c0764c24d61030a84636161161bb7cd5cd2d6f064cd3156e8814ed4cb5b8ddebeb4dbae77120f9a3eaea6afe889d99f6dd23543 languageName: node linkType: hard @@ -20134,7 +20134,7 @@ __metadata: "@grafana/faro-core": 1.0.2 "@grafana/faro-web-sdk": 1.0.2 "@grafana/google-sdk": 0.1.1 - "@grafana/lezer-logql": 0.1.2 + "@grafana/lezer-logql": 0.1.3 "@grafana/monaco-logql": ^0.0.7 "@grafana/runtime": "workspace:*" "@grafana/scenes": ^0.3.0