From 907af7f8f9c0b86514992b541c6b07c870cb3687 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 19 May 2021 14:38:47 +0200 Subject: [PATCH 1/8] bump lezer-promql to v0.19.0 Signed-off-by: Augustin Husson --- package-lock.json | 14 +++++++------- package.json | 2 +- src/lang-promql/complete/hybrid.ts | 19 ++++++++++--------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7eddd36..a1c7d66 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "version": "0.15.0", "license": "MIT", "dependencies": { - "lezer-promql": "^0.18.0", + "lezer-promql": "^0.19.0", "lru-cache": "^6.0.0" }, "devDependencies": { @@ -6338,9 +6338,9 @@ } }, "node_modules/lezer-promql": { - "version": "0.18.0", - "resolved": "https://registry.npmjs.org/lezer-promql/-/lezer-promql-0.18.0.tgz", - "integrity": "sha512-4ZQGyiU4JoL14rhtuAEmlSKHhu0dcBiLsqjF+RyouZNojUiLh6vyBFwrtPgAdD58s4j8+J21dOO/yUnaJvoGkw==", + "version": "0.19.0", + "resolved": "https://registry.npmjs.org/lezer-promql/-/lezer-promql-0.19.0.tgz", + "integrity": "sha512-divgYjuKw4ESDWVCXg7FipH2dF4vq0aWTb0QCyIGz5NHTLx6H+tVC7IlMkQSqsK8t/6qhgxh6A9s6XrE4ZFFJQ==", "peerDependencies": { "lezer": "^0.13.0" } @@ -17374,9 +17374,9 @@ } }, "lezer-promql": { - "version": "0.18.0", - "resolved": "https://registry.npmjs.org/lezer-promql/-/lezer-promql-0.18.0.tgz", - "integrity": "sha512-4ZQGyiU4JoL14rhtuAEmlSKHhu0dcBiLsqjF+RyouZNojUiLh6vyBFwrtPgAdD58s4j8+J21dOO/yUnaJvoGkw==", + "version": "0.19.0", + "resolved": "https://registry.npmjs.org/lezer-promql/-/lezer-promql-0.19.0.tgz", + "integrity": "sha512-divgYjuKw4ESDWVCXg7FipH2dF4vq0aWTb0QCyIGz5NHTLx6H+tVC7IlMkQSqsK8t/6qhgxh6A9s6XrE4ZFFJQ==", "requires": {} }, "lezer-tree": { diff --git a/package.json b/package.json index 500ea49..f0a5f3a 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "homepage": "https://github.com/prometheus-community/codemirror-promql/blob/master/README.md", "dependencies": { - "lezer-promql": "^0.18.0", + "lezer-promql": "^0.19.0", "lru-cache": "^6.0.0" }, "devDependencies": { diff --git a/src/lang-promql/complete/hybrid.ts b/src/lang-promql/complete/hybrid.ts index 1cec6c2..235d335 100644 --- a/src/lang-promql/complete/hybrid.ts +++ b/src/lang-promql/complete/hybrid.ts @@ -220,13 +220,6 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode): Context result.push({ kind: ContextKind.Duration }); break; } - if (node.parent?.type.id === StepInvariantExpr) { - // we are likely in the given situation: - // `expr @ s` - // we can autocomplete start / end - result.push({ kind: ContextKind.AtModifiers }); - break; - } if (node.parent?.type.id === SubqueryExpr && containsAtLeastOneChild(node.parent, Duration)) { // we are likely in the given situation: // `rate(foo[5d:5])` @@ -246,14 +239,22 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode): Context case Identifier: // sometimes an Identifier has an error has parent. This should be treated in priority if (node.parent?.type.id === 0) { - if (node.parent.parent?.type.id === AggregateExpr) { + const parent = node.parent; + if (parent.parent?.type.id === StepInvariantExpr) { + // we are likely in the given situation: + // `expr @ s` + // we can autocomplete start / end + result.push({ kind: ContextKind.AtModifiers }); + break; + } + if (parent.parent?.type.id === AggregateExpr) { // it matches 'sum() b'. So here we can autocomplete: // - the aggregate operation modifier // - the binary operation (since it's not mandatory to have an aggregate operation modifier) result.push({ kind: ContextKind.AggregateOpModifier }, { kind: ContextKind.BinOp }); break; } - if (node.parent.parent?.type.id === VectorSelector) { + if (parent.parent?.type.id === VectorSelector) { // it matches 'sum b'. So here we also have to autocomplete the aggregate operation modifier only // if the associated metricIdentifier is matching an aggregation operation. // Note: here is the corresponding tree in order to understand the situation: From 58582664a7458dd63f630af88f9e0f5522e49380 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 19 May 2021 15:52:23 +0200 Subject: [PATCH 2/8] autocomplete only metrics when the top is not PromQL Signed-off-by: Augustin Husson --- src/app/app.html | 6 +++ src/app/app.ts | 8 +++- src/lang-promql/complete/hybrid.ts | 35 +++++++++------ src/lang-promql/index.ts | 2 +- src/lang-promql/promql.ts | 69 ++++++++++++++++-------------- 5 files changed, 74 insertions(+), 46 deletions(-) diff --git a/src/app/app.html b/src/app/app.html index 6ef9f50..0576ae0 100644 --- a/src/app/app.html +++ b/src/app/app.html @@ -34,6 +34,12 @@

CodeMirror Mode PromQL

+
+ + diff --git a/src/app/app.ts b/src/app/app.ts index 36dbc19..d59a697 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -29,6 +29,12 @@ import { customTheme, promQLHighlightMaterialTheme } from './theme'; const promqlExtension = new PromQLExtension(); let editor: EditorView; +function getFullLanguage(): boolean { + const completionSelect = document.getElementById('fullLanguage') as HTMLSelectElement; + const completionValue = completionSelect.options[completionSelect.selectedIndex].value; + return completionValue === 'yes'; +} + function setCompletion() { const completionSelect = document.getElementById('completion') as HTMLSelectElement; const completionValue = completionSelect.options[completionSelect.selectedIndex].value; @@ -59,7 +65,7 @@ function createEditor() { } editor = new EditorView({ state: EditorState.create({ - extensions: [basicSetup, promqlExtension.asExtension(), promQLHighlightMaterialTheme, customTheme], + extensions: [basicSetup, promqlExtension.asExtension(getFullLanguage()), promQLHighlightMaterialTheme, customTheme], doc: doc, }), // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/src/lang-promql/complete/hybrid.ts b/src/lang-promql/complete/hybrid.ts index 235d335..7c840e3 100644 --- a/src/lang-promql/complete/hybrid.ts +++ b/src/lang-promql/complete/hybrid.ts @@ -70,7 +70,14 @@ import { } from 'lezer-promql'; import { Completion, CompletionContext, CompletionResult } from '@codemirror/autocomplete'; import { EditorState } from '@codemirror/state'; -import { containsAtLeastOneChild, containsChild, retrieveAllRecursiveNodes, walkBackward, walkThrough, buildLabelMatchers } from '../parser'; +import { + buildLabelMatchers, + containsAtLeastOneChild, + containsChild, + retrieveAllRecursiveNodes, + walkBackward, + walkThrough +} from '../parser'; import { aggregateOpModifierTerms, aggregateOpTerms, @@ -297,14 +304,9 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode): Context const parent = node.parent?.parent?.parent?.parent; if (!parent) { - // this case is normally impossible since by definition, the identifier has 3 parents, - // and in Lexer, there is always a default parent in top of everything. - result.push( - { kind: ContextKind.MetricName, metricName: state.sliceDoc(node.from, node.to) }, - { kind: ContextKind.Function }, - { kind: ContextKind.Aggregation }, - { kind: ContextKind.Number } - ); + // this case can be possible if the topNode is not anymore PromQL but MetricName. + // In this particular case, then we just want to autocomplete the metric + result.push({ kind: ContextKind.MetricName, metricName: state.sliceDoc(node.from, node.to) }); break; } // now we have to know if we have two Expr in the direct children of the `parent` @@ -397,7 +399,12 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode): Context const metricName = getMetricNameInVectorSelector(node, state); // finally get the full matcher available const labelMatchers = buildLabelMatchers(retrieveAllRecursiveNodes(walkBackward(node, LabelMatchList), LabelMatchList, LabelMatcher), state); - result.push({ kind: ContextKind.LabelValue, metricName: metricName, labelName: labelName, matchers: labelMatchers }); + result.push({ + kind: ContextKind.LabelValue, + metricName: metricName, + labelName: labelName, + matchers: labelMatchers + }); } break; case NumberLiteral: @@ -423,7 +430,10 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode): Context // In this case we are in the given situation: // sum() or in rate() // with the cursor between the bracket. So we can autocomplete the metric, the function and the aggregation. - result.push({ kind: ContextKind.MetricName, metricName: '' }, { kind: ContextKind.Function }, { kind: ContextKind.Aggregation }); + result.push({ + kind: ContextKind.MetricName, + metricName: '' + }, { kind: ContextKind.Function }, { kind: ContextKind.Aggregation }); break; case Neq: if (node.parent?.type.id === MatchOp) { @@ -483,11 +493,13 @@ export class HybridComplete implements CompleteStrategy { for (const context of contexts) { switch (context.kind) { case ContextKind.Aggregation: + completeSnippet = true; asyncResult = asyncResult.then((result) => { return result.concat(autocompleteNodes.aggregateOp); }); break; case ContextKind.Function: + completeSnippet = true; asyncResult = asyncResult.then((result) => { return result.concat(autocompleteNodes.functionIdentifier); }); @@ -540,7 +552,6 @@ export class HybridComplete implements CompleteStrategy { break; case ContextKind.MetricName: asyncResult = asyncResult.then((result) => { - completeSnippet = true; return this.autocompleteMetricName(result, context); }); break; diff --git a/src/lang-promql/index.ts b/src/lang-promql/index.ts index ae6625e..7a0a83e 100644 --- a/src/lang-promql/index.ts +++ b/src/lang-promql/index.ts @@ -23,4 +23,4 @@ export { PrometheusClient } from './client'; export { CompleteConfiguration, CompleteStrategy } from './complete'; export { LintStrategy } from './lint'; -export { PromQLExtension, promQLLanguage } from './promql'; +export { PromQLExtension } from './promql'; diff --git a/src/lang-promql/promql.ts b/src/lang-promql/promql.ts index 01117c6..4e45ccd 100644 --- a/src/lang-promql/promql.ts +++ b/src/lang-promql/promql.ts @@ -28,35 +28,38 @@ import { LintStrategy, newLintStrategy, promQLLinter } from './lint'; import { CompletionContext } from '@codemirror/autocomplete'; import { LezerLanguage } from '@codemirror/language'; -export const promQLLanguage = LezerLanguage.define({ - parser: parser.configure({ - props: [ - styleTags({ - LineComment: tags.comment, - LabelName: tags.labelName, - StringLiteral: tags.string, - NumberLiteral: tags.number, - Duration: tags.number, - 'Abs Absent AbsentOverTime AvgOverTime Ceil Changes Clamp ClampMax ClampMin CountOverTime DaysInMonth DayOfMonth DayOfWeek Delta Deriv Exp Floor HistogramQuantile HoltWinters Hour Idelta Increase Irate LabelReplace LabelJoin LastOverTime Ln Log10 Log2 MaxOverTime MinOverTime Minute Month PredictLinear QuantileOverTime Rate Resets Round Scalar Sgn Sort SortDesc Sqrt StddevOverTime StdvarOverTime SumOverTime Time Timestamp Vector Year': tags.function( - tags.variableName - ), - 'Avg Bottomk Count Count_values Group Max Min Quantile Stddev Stdvar Sum Topk': tags.operatorKeyword, - 'By Without Bool On Ignoring GroupLeft GroupRight Offset Start End': tags.modifier, - 'And Unless Or': tags.logicOperator, - 'Sub Add Mul Mod Div Eql Neq Lte Lss Gte Gtr EqlRegex EqlSingle NeqRegex Pow At': tags.operator, - UnaryOp: tags.arithmeticOperator, - '( )': tags.paren, - '[ ]': tags.squareBracket, - '{ }': tags.brace, - '⚠': tags.invalid, - }), - ], - }), - languageData: { - closeBrackets: { brackets: ['(', '[', '{', "'", '"', '`'] }, - commentTokens: { line: '#' }, - }, -}); +function promQLLanguage(top: string) { + return LezerLanguage.define({ + parser: parser.configure({ + top: top, + props: [ + styleTags({ + LineComment: tags.comment, + LabelName: tags.labelName, + StringLiteral: tags.string, + NumberLiteral: tags.number, + Duration: tags.number, + 'Abs Absent AbsentOverTime AvgOverTime Ceil Changes Clamp ClampMax ClampMin CountOverTime DaysInMonth DayOfMonth DayOfWeek Delta Deriv Exp Floor HistogramQuantile HoltWinters Hour Idelta Increase Irate LabelReplace LabelJoin LastOverTime Ln Log10 Log2 MaxOverTime MinOverTime Minute Month PredictLinear QuantileOverTime Rate Resets Round Scalar Sgn Sort SortDesc Sqrt StddevOverTime StdvarOverTime SumOverTime Time Timestamp Vector Year': tags.function( + tags.variableName + ), + 'Avg Bottomk Count Count_values Group Max Min Quantile Stddev Stdvar Sum Topk': tags.operatorKeyword, + 'By Without Bool On Ignoring GroupLeft GroupRight Offset Start End': tags.modifier, + 'And Unless Or': tags.logicOperator, + 'Sub Add Mul Mod Div Eql Neq Lte Lss Gte Gtr EqlRegex EqlSingle NeqRegex Pow At': tags.operator, + UnaryOp: tags.arithmeticOperator, + '( )': tags.paren, + '[ ]': tags.squareBracket, + '{ }': tags.brace, + '⚠': tags.invalid, + }), + ], + }), + languageData: { + closeBrackets: { brackets: ['(', '[', '{', "'", '"', '`'] }, + commentTokens: { line: '#' }, + }, + }); +} /** * This class holds the state of the completion extension for CodeMirror and allow hot-swapping the complete strategy. @@ -102,10 +105,12 @@ export class PromQLExtension { return this; } - asExtension(): Extension { - let extension: Extension = [promQLLanguage]; + asExtension(fullPromQLLanguage = true): Extension { + const top = fullPromQLLanguage ? 'PromQL' : 'MetricName'; + const language = promQLLanguage(top); + let extension: Extension = [language]; if (this.enableCompletion) { - const completion = promQLLanguage.data.of({ + const completion = language.data.of({ autocomplete: (context: CompletionContext) => { return this.complete.promQL(context); }, From 2c0f4b037d54235aa33b493aec3d6df2b0439ab2 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 19 May 2021 15:55:03 +0200 Subject: [PATCH 3/8] fix security issue Signed-off-by: Augustin Husson --- package-lock.json | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index a1c7d66..02ccafd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4948,9 +4948,9 @@ } }, "node_modules/hosted-git-info": { - "version": "2.8.8", - "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz", - "integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==", + "version": "2.8.9", + "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.9.tgz", + "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==", "dev": true }, "node_modules/hpack.js": { @@ -6411,9 +6411,9 @@ } }, "node_modules/lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, "node_modules/lodash.flattendeep": { @@ -10467,9 +10467,9 @@ } }, "node_modules/url-parse": { - "version": "1.4.7", - "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.4.7.tgz", - "integrity": "sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg==", + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.1.tgz", + "integrity": "sha512-HOfCOUJt7iSYzEx/UqgtwKRMC6EU91NFhsCHMv9oM03VJcVo2Qrp8T8kI9D7amFf1cu+/3CEhgb3rF9zL7k85Q==", "dev": true, "dependencies": { "querystringify": "^2.1.1", @@ -16255,9 +16255,9 @@ } }, "hosted-git-info": { - "version": "2.8.8", - "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz", - "integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==", + "version": "2.8.9", + "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.9.tgz", + "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==", "dev": true }, "hpack.js": { @@ -17432,9 +17432,9 @@ } }, "lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, "lodash.flattendeep": { @@ -20830,9 +20830,9 @@ } }, "url-parse": { - "version": "1.4.7", - "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.4.7.tgz", - "integrity": "sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg==", + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.1.tgz", + "integrity": "sha512-HOfCOUJt7iSYzEx/UqgtwKRMC6EU91NFhsCHMv9oM03VJcVo2Qrp8T8kI9D7amFf1cu+/3CEhgb3rF9zL7k85Q==", "dev": true, "requires": { "querystringify": "^2.1.1", From 6d7640925a901954eb3ad138107eb595ba5c9de1 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 19 May 2021 15:58:51 +0200 Subject: [PATCH 4/8] fix linter issue Signed-off-by: Augustin Husson --- src/lang-promql/complete/hybrid.ts | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/lang-promql/complete/hybrid.ts b/src/lang-promql/complete/hybrid.ts index 7c840e3..06f685d 100644 --- a/src/lang-promql/complete/hybrid.ts +++ b/src/lang-promql/complete/hybrid.ts @@ -70,14 +70,7 @@ import { } from 'lezer-promql'; import { Completion, CompletionContext, CompletionResult } from '@codemirror/autocomplete'; import { EditorState } from '@codemirror/state'; -import { - buildLabelMatchers, - containsAtLeastOneChild, - containsChild, - retrieveAllRecursiveNodes, - walkBackward, - walkThrough -} from '../parser'; +import { buildLabelMatchers, containsAtLeastOneChild, containsChild, retrieveAllRecursiveNodes, walkBackward, walkThrough } from '../parser'; import { aggregateOpModifierTerms, aggregateOpTerms, @@ -403,7 +396,7 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode): Context kind: ContextKind.LabelValue, metricName: metricName, labelName: labelName, - matchers: labelMatchers + matchers: labelMatchers, }); } break; @@ -430,10 +423,7 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode): Context // In this case we are in the given situation: // sum() or in rate() // with the cursor between the bracket. So we can autocomplete the metric, the function and the aggregation. - result.push({ - kind: ContextKind.MetricName, - metricName: '' - }, { kind: ContextKind.Function }, { kind: ContextKind.Aggregation }); + result.push({ kind: ContextKind.MetricName, metricName: '' }, { kind: ContextKind.Function }, { kind: ContextKind.Aggregation }); break; case Neq: if (node.parent?.type.id === MatchOp) { From 963c77473438ace9b6040c53512005425d3c22bf Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 19 May 2021 16:33:35 +0200 Subject: [PATCH 5/8] rename the choice that affects the top language Signed-off-by: Augustin Husson --- src/app/app.html | 6 +++--- src/app/app.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/app.html b/src/app/app.html index 0576ae0..50293c6 100644 --- a/src/app/app.html +++ b/src/app/app.html @@ -35,10 +35,10 @@

CodeMirror Mode PromQL


- + diff --git a/src/app/app.ts b/src/app/app.ts index d59a697..ff0ebf8 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -32,7 +32,7 @@ let editor: EditorView; function getFullLanguage(): boolean { const completionSelect = document.getElementById('fullLanguage') as HTMLSelectElement; const completionValue = completionSelect.options[completionSelect.selectedIndex].value; - return completionValue === 'yes'; + return completionValue === 'promql'; } function setCompletion() { From 5804c31a96b965dcf7059cf043f3de524ac43a25 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Thu, 20 May 2021 09:00:51 +0200 Subject: [PATCH 6/8] define an enum for different language supported Signed-off-by: Augustin Husson --- src/app/app.ts | 13 ++++++++++--- src/lang-promql/index.ts | 2 +- src/lang-promql/promql.ts | 12 ++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/app/app.ts b/src/app/app.ts index ff0ebf8..a9b8b8f 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -23,16 +23,23 @@ import { basicSetup } from '@codemirror/basic-setup'; import { EditorState } from '@codemirror/state'; import { EditorView } from '@codemirror/view'; -import { PromQLExtension } from '../lang-promql'; +import { LanguageType, PromQLExtension } from '../lang-promql'; import { customTheme, promQLHighlightMaterialTheme } from './theme'; const promqlExtension = new PromQLExtension(); let editor: EditorView; -function getFullLanguage(): boolean { +function getFullLanguage(): LanguageType { const completionSelect = document.getElementById('fullLanguage') as HTMLSelectElement; const completionValue = completionSelect.options[completionSelect.selectedIndex].value; - return completionValue === 'promql'; + switch (completionValue) { + case 'promql': + return LanguageType.PromQL; + case 'metricName': + return LanguageType.MetricName; + default: + return LanguageType.PromQL; + } } function setCompletion() { diff --git a/src/lang-promql/index.ts b/src/lang-promql/index.ts index 7a0a83e..e21d3d7 100644 --- a/src/lang-promql/index.ts +++ b/src/lang-promql/index.ts @@ -23,4 +23,4 @@ export { PrometheusClient } from './client'; export { CompleteConfiguration, CompleteStrategy } from './complete'; export { LintStrategy } from './lint'; -export { PromQLExtension } from './promql'; +export { PromQLExtension, LanguageType } from './promql'; diff --git a/src/lang-promql/promql.ts b/src/lang-promql/promql.ts index 4e45ccd..27560a1 100644 --- a/src/lang-promql/promql.ts +++ b/src/lang-promql/promql.ts @@ -28,7 +28,12 @@ import { LintStrategy, newLintStrategy, promQLLinter } from './lint'; import { CompletionContext } from '@codemirror/autocomplete'; import { LezerLanguage } from '@codemirror/language'; -function promQLLanguage(top: string) { +export enum LanguageType { + PromQL = 'PromQL', + MetricName = 'MetricName', +} + +function promQLLanguage(top: LanguageType) { return LezerLanguage.define({ parser: parser.configure({ top: top, @@ -105,9 +110,8 @@ export class PromQLExtension { return this; } - asExtension(fullPromQLLanguage = true): Extension { - const top = fullPromQLLanguage ? 'PromQL' : 'MetricName'; - const language = promQLLanguage(top); + asExtension(languageType = LanguageType.PromQL): Extension { + const language = promQLLanguage(languageType); let extension: Extension = [language]; if (this.enableCompletion) { const completion = language.data.of({ From b1a0cf566f8c4819315f3d79c89a6bb5ab297b65 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Thu, 20 May 2021 09:08:44 +0200 Subject: [PATCH 7/8] re-export the promQLLanguage Signed-off-by: Augustin Husson --- src/lang-promql/index.ts | 2 +- src/lang-promql/promql.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lang-promql/index.ts b/src/lang-promql/index.ts index e21d3d7..a14e5ae 100644 --- a/src/lang-promql/index.ts +++ b/src/lang-promql/index.ts @@ -23,4 +23,4 @@ export { PrometheusClient } from './client'; export { CompleteConfiguration, CompleteStrategy } from './complete'; export { LintStrategy } from './lint'; -export { PromQLExtension, LanguageType } from './promql'; +export { PromQLExtension, LanguageType, promQLLanguage } from './promql'; diff --git a/src/lang-promql/promql.ts b/src/lang-promql/promql.ts index 27560a1..ec720d3 100644 --- a/src/lang-promql/promql.ts +++ b/src/lang-promql/promql.ts @@ -33,7 +33,7 @@ export enum LanguageType { MetricName = 'MetricName', } -function promQLLanguage(top: LanguageType) { +export function promQLLanguage(top: LanguageType) { return LezerLanguage.define({ parser: parser.configure({ top: top, From 9fac3ddc146246da518d86cadd7c7f5e8fe6bc8d Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Thu, 20 May 2021 14:49:13 +0200 Subject: [PATCH 8/8] rename fullLanguage by languageType in app Signed-off-by: Augustin Husson --- src/app/app.html | 4 ++-- src/app/app.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app/app.html b/src/app/app.html index 50293c6..b8e9d38 100644 --- a/src/app/app.html +++ b/src/app/app.html @@ -35,8 +35,8 @@

CodeMirror Mode PromQL


- - diff --git a/src/app/app.ts b/src/app/app.ts index a9b8b8f..75983bd 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -29,8 +29,8 @@ import { customTheme, promQLHighlightMaterialTheme } from './theme'; const promqlExtension = new PromQLExtension(); let editor: EditorView; -function getFullLanguage(): LanguageType { - const completionSelect = document.getElementById('fullLanguage') as HTMLSelectElement; +function getLanguageType(): LanguageType { + const completionSelect = document.getElementById('languageType') as HTMLSelectElement; const completionValue = completionSelect.options[completionSelect.selectedIndex].value; switch (completionValue) { case 'promql': @@ -72,7 +72,7 @@ function createEditor() { } editor = new EditorView({ state: EditorState.create({ - extensions: [basicSetup, promqlExtension.asExtension(getFullLanguage()), promQLHighlightMaterialTheme, customTheme], + extensions: [basicSetup, promqlExtension.asExtension(getLanguageType()), promQLHighlightMaterialTheme, customTheme], doc: doc, }), // eslint-disable-next-line @typescript-eslint/no-non-null-assertion