Skip to content

Commit

Permalink
Errors & Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
keesschollaart81 committed May 15, 2019
1 parent f778739 commit e404d18
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/server/fileAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/server/haConfig/haConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 29 additions & 6 deletions src/server/haConfig/haYamlFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,39 @@ export class HomeAssistantYamlFile {
await this.parseAstRecursive(this.yaml.contents, this.path);
}

public isValid = async (): Promise<boolean> => {
public isValid = async (): Promise<ValidationResults> => {
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<IncludeReferences> => {
Expand Down Expand Up @@ -214,3 +233,7 @@ export class HomeAssistantYamlFile {
}
}

export interface ValidationResults {
isValid: boolean;
errors?: string[];
}
16 changes: 12 additions & 4 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
Expand All @@ -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: <ServerCapabilities>{
textDocumentSync: documents.syncKind,
Expand Down

0 comments on commit e404d18

Please sign in to comment.