Skip to content

Commit

Permalink
Fix error handling in hover and codelens
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
  • Loading branch information
evidolob committed Sep 13, 2021
1 parent 6df37d3 commit 40b6fe4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
27 changes: 5 additions & 22 deletions src/languageservice/parser/recursivelyBuildAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -144,7 +128,6 @@ export default function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode
return result;
}
}

break;
}
case Yaml.Kind.ANCHOR_REF: {
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlCodeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export class YamlCodeLens {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getCodeLens(document: TextDocument, params: CodeLensParams): Promise<CodeLens[]> {
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) {
Expand Down
33 changes: 19 additions & 14 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -34,20 +35,24 @@ export class YAMLHover {
}

doHover(document: TextDocument, position: Position, isKubernetes = false): Promise<Hover | null> {
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
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 40b6fe4

Please sign in to comment.