From 6f0af301044a0d5e807539ce85a726d2c83cb4e4 Mon Sep 17 00:00:00 2001 From: yuri1969 <1969yuri1969@gmail.com> Date: Tue, 2 Apr 2019 23:16:10 +0200 Subject: [PATCH] Fix for a part of #45 As mentioned by @Galiaf47, not having the main file in project root causes validation errors. * Changed the working directory to match the main Swagger file * Refactored the validation and error reporting a bit --- src/language/server.ts | 128 ++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 72 deletions(-) diff --git a/src/language/server.ts b/src/language/server.ts index 154f753..b1f7d2e 100644 --- a/src/language/server.ts +++ b/src/language/server.ts @@ -10,7 +10,8 @@ import { DidChangeConfigurationNotification, CompletionItem, CompletionItemKind, - TextDocumentPositionParams + TextDocumentPositionParams, + Files } from 'vscode-languageserver'; import * as SwaggerParser from 'swagger-parser'; import * as YAML from 'js-yaml'; @@ -124,92 +125,75 @@ function getParsedContent(document: TextDocument){ return null; } -async function validateTextDocument(textDocument: TextDocument): Promise { - let swaggerObject = null; - try{ - swaggerObject = getParsedContent(textDocument); - } - catch(ex){ - let diagnostics: Diagnostic[] = []; - let diagnostic: Diagnostic = { - severity: DiagnosticSeverity.Warning, - code: 0, - message: ex.message, - range: { - start: { - line: 0, - character: 1 - }, - end: { - line: 0, - character: 1 - } +function sendErrorDiagnostic(error: any, textDocument: TextDocument, source: string){ + let diagnostics: Diagnostic[] = []; + let diagnostic: Diagnostic = { + severity: DiagnosticSeverity.Warning, + code: 0, + message: error.message, + range: { + start: { + line: 0, + character: 1 }, - source: "Swagger Viewer Parse" + end: { + line: 0, + character: 1 + } + }, + source: source + }; + + if (error.mark) { + diagnostic.range.start = diagnostic.range.end = { + line: error.mark.line, + character: error.mark.column }; + } - if (ex.mark) { - diagnostic.range.start = diagnostic.range.end = { - line: ex.mark.line, - character: ex.mark.column - }; - } + diagnostics.push(diagnostic); - diagnostics.push(diagnostic); + connection.sendDiagnostics({ + uri: textDocument.uri, + diagnostics + }); +} +function validateSwagger(swagger: any, textDocument: TextDocument) { + SwaggerParser.validate(swagger) + .then(() => { + const diagnostics: Diagnostic[] = []; connection.sendDiagnostics({ uri: textDocument.uri, diagnostics }); + }) + .catch(error => { + sendErrorDiagnostic(error, textDocument, "Swagger Viewer"); + }); +} - return; +function changeCurrentWorkDir(textDocument: TextDocument): void { + const documentPath = Files.uriToFilePath(textDocument.uri); + const documentDir = documentPath.substr(0, documentPath.lastIndexOf('/')); + // work dir == document base dir + process.chdir(documentDir); +} + +async function validateTextDocument(textDocument: TextDocument): Promise { + let swaggerObject = null; + try{ + swaggerObject = getParsedContent(textDocument); + } + catch(ex){ + sendErrorDiagnostic(ex, textDocument, "Swagger Viewer Parse"); } if(!swaggerObject) return; - SwaggerParser.validate(swaggerObject) - .then(api => { - let diagnostics: Diagnostic[] = []; - connection.sendDiagnostics({ - uri: textDocument.uri, - diagnostics - }); - }) - .catch(err => { - let diagnostics: Diagnostic[] = []; - let diagnostic: Diagnostic = { - severity: DiagnosticSeverity.Warning, - code: 0, - message: err.message, - range: { - start: { - line: 0, - character: 1 - }, - end: { - line: 0, - character: 1 - } - }, - source: "Swagger Viewer" - }; - - if (err.mark) { - diagnostic.range.start = diagnostic.range.end = { - line: err.mark.line, - character: err.mark.column - }; - } - - diagnostics.push(diagnostic); - - connection.sendDiagnostics({ - uri: textDocument.uri, - diagnostics - }); - - }) + changeCurrentWorkDir(textDocument); + validateSwagger(swaggerObject, textDocument); } connection.onDidChangeWatchedFiles(_change => {