From 1faf6572b7c0f2bf3ae2040ac08ad928f5993bf8 Mon Sep 17 00:00:00 2001 From: Christopher Schleiden Date: Thu, 2 Feb 2023 13:14:02 -0800 Subject: [PATCH] Rename helper --- actions-languageservice/src/complete.ts | 7 ++----- actions-languageservice/src/hover.ts | 15 ++++++--------- .../{type-guards.ts => expression-detection.ts} | 6 +++--- 3 files changed, 11 insertions(+), 17 deletions(-) rename actions-languageservice/src/utils/{type-guards.ts => expression-detection.ts} (80%) diff --git a/actions-languageservice/src/complete.ts b/actions-languageservice/src/complete.ts index 5ba7678..a028bd1 100644 --- a/actions-languageservice/src/complete.ts +++ b/actions-languageservice/src/complete.ts @@ -21,12 +21,12 @@ import {getWorkflowContext, WorkflowContext} from "./context/workflow-context"; import {validatorFunctions} from "./expression-validation/functions"; import {error} from "./log"; import {nullTrace} from "./nulltrace"; +import {isPotentiallyExpression} from "./utils/expression-detection"; import {findToken} from "./utils/find-token"; import {guessIndentation} from "./utils/indentation-guesser"; import {mapRange} from "./utils/range"; import {getRelCharOffset} from "./utils/rel-char-pos"; import {transform} from "./utils/transform"; -import {isStringExpression} from "./utils/type-guards"; import {Value, ValueProviderConfig} from "./value-providers/config"; import {defaultValueProviders} from "./value-providers/default"; import {definitionValues} from "./value-providers/definition"; @@ -77,10 +77,7 @@ export async function complete( // If we are inside an expression, take a different code-path. The workflow parser does not correctly create // expression nodes for invalid expressions and during editing expressions are invalid most of the time. if (token) { - const isStringExpressionToken = isStringExpression(token); - const isBasicExpressionToken = isBasicExpression(token); - - if (isStringExpressionToken || isBasicExpressionToken) { + if (isBasicExpression(token) || isPotentiallyExpression(token)) { const allowedContext = token.definitionInfo?.allowedContext || []; const context = await getContext(allowedContext, contextProviderConfig, workflowContext, Mode.Completion); diff --git a/actions-languageservice/src/hover.ts b/actions-languageservice/src/hover.ts index 9db065c..2c86c0f 100644 --- a/actions-languageservice/src/hover.ts +++ b/actions-languageservice/src/hover.ts @@ -19,9 +19,9 @@ import {HoverVisitor} from "./expression-hover/visitor"; import {validatorFunctions} from "./expression-validation/functions"; import {info} from "./log"; import {nullTrace} from "./nulltrace"; +import {isPotentiallyExpression} from "./utils/expression-detection"; import {findToken, TokenResult} from "./utils/find-token"; import {mapRange} from "./utils/range"; -import {isStringExpression} from "./utils/type-guards"; export type HoverConfig = { descriptionProvider?: DescriptionProvider; @@ -45,15 +45,12 @@ export async function hover(document: TextDocument, position: Position, config?: const tokenResult = findToken(position, result.value); const {token, keyToken, parent} = tokenResult; - const hoverToken = keyToken || parent || token; - if (config?.contextProviderConfig && token && hoverToken?.definition) { - const isStringExpressionToken = isStringExpression(token); - const isBasicExpressionToken = isBasicExpression(token); + const tokenDefinitionInfo = (keyToken || parent || token)?.definitionInfo; + if (config?.contextProviderConfig && token && tokenDefinitionInfo) { + if (isBasicExpression(token) || isPotentiallyExpression(token)) { + info(`Calculating expression hover for token with definition ${tokenDefinitionInfo.definition.key}`); - if (isStringExpressionToken || isBasicExpressionToken) { - info(`Calculating expression hover for token with definition ${hoverToken.definition.key}`); - - const allowedContext = hoverToken.definitionInfo?.allowedContext || []; + const allowedContext = tokenDefinitionInfo.allowedContext || []; const {namedContexts, functions} = splitAllowedContext(allowedContext); const template = convertWorkflowTemplate(result.context, result.value, ErrorPolicy.TryConversion); diff --git a/actions-languageservice/src/utils/type-guards.ts b/actions-languageservice/src/utils/expression-detection.ts similarity index 80% rename from actions-languageservice/src/utils/type-guards.ts rename to actions-languageservice/src/utils/expression-detection.ts index 62fbe2e..2242d8a 100644 --- a/actions-languageservice/src/utils/type-guards.ts +++ b/actions-languageservice/src/utils/expression-detection.ts @@ -4,9 +4,9 @@ import {StringDefinition} from "@github/actions-workflow-parser/templates/schema import {OPEN_EXPRESSION} from "@github/actions-workflow-parser/templates/template-constants"; import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/index"; -export function isStringExpression(token: TemplateToken): boolean { - const isExpression = +export function isPotentiallyExpression(token: TemplateToken): boolean { + const isAlwaysExpression = token.definition?.definitionType === DefinitionType.String && (token.definition as StringDefinition).isExpression; const containsExpression = isString(token) && token.value.indexOf(OPEN_EXPRESSION) >= 0; - return isExpression || containsExpression; + return isAlwaysExpression || containsExpression; }