Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling in hover and codelens #534

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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