diff --git a/src/languageservice/parser/recursivelyBuildAst.ts b/src/languageservice/parser/recursivelyBuildAst.ts index 42765835..a6f2a600 100644 --- a/src/languageservice/parser/recursivelyBuildAst.ts +++ b/src/languageservice/parser/recursivelyBuildAst.ts @@ -13,6 +13,10 @@ import { parseYamlBoolean } from './scalar-type'; const maxRefCount = 1000; let refDepth = 0; + +//This is a patch for redirecting values with these strings to be boolean nodes because its not supported in the parser. +const possibleBooleanValues = ['y', 'Y', 'yes', 'Yes', 'YES', 'n', 'N', 'no', 'No', 'NO', 'on', 'On', 'ON', 'off', 'Off', 'OFF']; + export default function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode { if (!node) { return; @@ -55,7 +59,7 @@ export default function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode keyNode.value = key.value; const valueNode = instance.value - ? recursivelyBuildAst(result, instance.value) + ? recursivelyBuildAst(result, instance.value) ?? new NullASTNodeImpl(parent, instance.endPosition, 0) : new NullASTNodeImpl(parent, instance.endPosition, 0); valueNode.location = key.value; @@ -90,26 +94,6 @@ export default function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode const type = Yaml.determineScalarType(instance); const value = instance.value; - - //This is a patch for redirecting values with these strings to be boolean nodes because its not supported in the parser. - const possibleBooleanValues = [ - 'y', - 'Y', - 'yes', - 'Yes', - 'YES', - 'n', - 'N', - 'no', - 'No', - 'NO', - 'on', - 'On', - 'ON', - 'off', - 'Off', - 'OFF', - ]; if (instance.plainScalar && possibleBooleanValues.indexOf(value.toString()) !== -1) { return new BooleanASTNodeImpl(parent, parseYamlBoolean(value), node.startPosition, node.endPosition - node.startPosition); } @@ -144,7 +128,6 @@ export default function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode return result; } } - break; } case Yaml.Kind.ANCHOR_REF: { diff --git a/src/languageservice/services/yamlCodeLens.ts b/src/languageservice/services/yamlCodeLens.ts index 63023f2e..d1f96bca 100644 --- a/src/languageservice/services/yamlCodeLens.ts +++ b/src/languageservice/services/yamlCodeLens.ts @@ -20,9 +20,9 @@ export class YamlCodeLens { // eslint-disable-next-line @typescript-eslint/no-unused-vars async getCodeLens(document: TextDocument, params: CodeLensParams): Promise { - const yamlDocument = yamlDocumentsCache.getYamlDocument(document); const result = []; try { + const yamlDocument = yamlDocumentsCache.getYamlDocument(document); for (const currentYAMLDoc of yamlDocument.documents) { const schema = await this.schemaService.getSchemaForResource(document.uri, currentYAMLDoc); if (schema?.schema) { diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index e609caa1..1efc72cf 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -17,12 +17,13 @@ import { getNodeValue } from '../parser/jsonParser07'; import { JSONSchema } from '../jsonSchema'; import { URI } from 'vscode-uri'; import * as path from 'path'; +import { Telemetry } from '../../languageserver/telemetry'; export class YAMLHover { private shouldHover: boolean; private schemaService: YAMLSchemaService; - constructor(schemaService: YAMLSchemaService) { + constructor(schemaService: YAMLSchemaService, private readonly telemetry: Telemetry) { this.shouldHover = true; this.schemaService = schemaService; } @@ -34,20 +35,24 @@ export class YAMLHover { } doHover(document: TextDocument, position: Position, isKubernetes = false): Promise { - if (!this.shouldHover || !document) { - return Promise.resolve(undefined); - } - const doc = yamlDocumentsCache.getYamlDocument(document); - const offset = document.offsetAt(position); - const currentDoc = matchOffsetToDocument(offset, doc); - if (currentDoc === null) { - return Promise.resolve(undefined); - } + try { + if (!this.shouldHover || !document) { + return Promise.resolve(undefined); + } + const doc = yamlDocumentsCache.getYamlDocument(document); + const offset = document.offsetAt(position); + const currentDoc = matchOffsetToDocument(offset, doc); + if (currentDoc === null) { + return Promise.resolve(undefined); + } - setKubernetesParserOption(doc.documents, isKubernetes); - const currentDocIndex = doc.documents.indexOf(currentDoc); - currentDoc.currentDocIndex = currentDocIndex; - return this.getHover(document, position, currentDoc); + setKubernetesParserOption(doc.documents, isKubernetes); + const currentDocIndex = doc.documents.indexOf(currentDoc); + currentDoc.currentDocIndex = currentDocIndex; + return this.getHover(document, position, currentDoc); + } catch (error) { + this.telemetry.sendError('yaml.hover.error', { error, documentUri: document.uri }); + } } // method copied from https://github.com/microsoft/vscode-json-languageservice/blob/2ea5ad3d2ffbbe40dea11cfe764a502becf113ce/src/services/jsonHover.ts#L23 diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index a25b25a9..07438ba5 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -151,7 +151,7 @@ export function getLanguageService( ): LanguageService { const schemaService = new YAMLSchemaService(schemaRequestService, workspaceContext); const completer = new YAMLCompletion(schemaService, clientCapabilities, telemetry); - const hover = new YAMLHover(schemaService); + const hover = new YAMLHover(schemaService, telemetry); const yamlDocumentSymbols = new YAMLDocumentSymbols(schemaService, telemetry); const yamlValidation = new YAMLValidation(schemaService); const formatter = new YAMLFormatter();