From e404d18b345e6d08f7d85ab4ff5742a944f022c2 Mon Sep 17 00:00:00 2001 From: Kees Schollaart Date: Wed, 15 May 2019 12:01:49 +0200 Subject: [PATCH] Errors & Windows --- src/server/fileAccessor.ts | 2 +- src/server/haConfig/haConfig.ts | 13 ++++++++++-- src/server/haConfig/haYamlFile.ts | 35 +++++++++++++++++++++++++------ src/server/server.ts | 16 ++++++++++---- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/server/fileAccessor.ts b/src/server/fileAccessor.ts index 173290aea2..2c3e40962c 100644 --- a/src/server/fileAccessor.ts +++ b/src/server/fileAccessor.ts @@ -81,7 +81,7 @@ export class VsCodeFileAccessor implements FileAccessor { let workspaceFolderUri = Uri.parse(this.workspaceFolder); let fileUri = Uri.parse(uri); let local = fileUri.fsPath.replace(workspaceFolderUri.fsPath, ""); - if (local[0] === "/"){ + if (local[0] === "/" || local[0] === "\\"){ local = local.substring(1); } // let joined = path.join(workspaceFolderUri.fsPath, uri); diff --git a/src/server/haConfig/haConfig.ts b/src/server/haConfig/haConfig.ts index 3d36655dd8..765f7c0ac7 100644 --- a/src/server/haConfig/haConfig.ts +++ b/src/server/haConfig/haConfig.ts @@ -36,7 +36,8 @@ export class HomeAssistantConfiguration { var homeAssistantYamlFile = new HomeAssistantYamlFile(this.fileAccessor, filename, ourFile.path); this.files[filename] = homeAssistantYamlFile; - if (!await homeAssistantYamlFile.isValid()) { + var validationResult = await homeAssistantYamlFile.isValid(); + if (!validationResult.isValid) { return { isValidYaml: false, newFilesFound: false @@ -108,7 +109,15 @@ export class HomeAssistantConfiguration { errorMessage += ` Error message: ${err}`; } - if (!await homeAssistantYamlFile.isValid() || error) { + var validationResult = await homeAssistantYamlFile.isValid(); + if (!validationResult.isValid) { + error = true; + if (validationResult.errors && validationResult.errors.length > 0) { + errorMessage += " Error(s): "; + validationResult.errors.forEach(e => errorMessage += `\r\n - ${e}`); + } + } + if (error) { if (filename === path) { // root file has more impact console.warn(errorMessage); diff --git a/src/server/haConfig/haYamlFile.ts b/src/server/haConfig/haYamlFile.ts index 3e27d25c90..5a43eb8720 100644 --- a/src/server/haConfig/haYamlFile.ts +++ b/src/server/haConfig/haYamlFile.ts @@ -30,20 +30,39 @@ export class HomeAssistantYamlFile { await this.parseAstRecursive(this.yaml.contents, this.path); } - public isValid = async (): Promise => { + public isValid = async (): Promise => { try { await this.parse(); } catch (e) { - return false; + return { + isValid: false, + errors: [e] + }; } - if (!this.yaml){ - return false; + if (!this.yaml) { + return { + isValid: false, + errors: ["Empty yaml"] + }; } if (this.yaml.errors && this.yaml.errors.length > 0) { - return false; + var errors = this.yaml.errors.slice(0,3).map(x => { + //@ts-ignore + let line = (x.source && x.source.rangeAsLinePos && x.source.rangeAsLinePos.start) ? ` (Line: ${x.source.rangeAsLinePos.start.line})` : ""; + return `${x.name}: ${x.message}${line}`; + }); + if (this.yaml.errors.length > 3){ + errors.push(` - And ${this.yaml.errors.length - 3} more errors...`) + } + return { + isValid: false, + errors: errors + }; } - return true; + return { + isValid: true + }; } public getIncludes = async (): Promise => { @@ -214,3 +233,7 @@ export class HomeAssistantYamlFile { } } +export interface ValidationResults { + isValid: boolean; + errors?: string[]; +} \ No newline at end of file diff --git a/src/server/server.ts b/src/server/server.ts index 3ff9166f70..3984fa0ce6 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -29,7 +29,7 @@ connection.onInitialize(async params => { var haConnection = new HaConnection(configurationService); var fileAccessor = new VsCodeFileAccessor(params.rootUri, connection, documents); var haConfig = new HomeAssistantConfiguration(fileAccessor); - + var definitionProviders = [ new IncludeDefinitionProvider(fileAccessor), new ScriptDefinitionProvider(haConfig) @@ -48,9 +48,6 @@ connection.onInitialize(async params => { definitionProviders ); - await haConfig.discoverFiles(); - await homeAsisstantLanguageService.findAndApplySchemas(connection); - documents.onDidChangeContent((e) => homeAsisstantLanguageService.onDocumentChange(e, connection)); documents.onDidOpen((e) => homeAsisstantLanguageService.onDocumentOpen(e, connection)); documents.onDidSave((e) => homeAsisstantLanguageService.onDidSave(e, connection)); @@ -71,6 +68,17 @@ connection.onInitialize(async params => { } }); + //fire and forget + setTimeout(async () => { + try { + await haConfig.discoverFiles(); + await homeAsisstantLanguageService.findAndApplySchemas(connection); + } + catch (e) { + console.error(`Unexpected error during initial configuration discover: ${e}`); + } + }, 0); + return { capabilities: { textDocumentSync: documents.syncKind,