diff --git a/src/crystalFormatting.ts b/src/crystalFormatting.ts index cd8e1cc..2ca5ba8 100755 --- a/src/crystalFormatting.ts +++ b/src/crystalFormatting.ts @@ -10,20 +10,25 @@ export class CrystalFormattingProvider implements vscode.DocumentFormattingEditP /** * Execute crystal tool format and get response. + * Returns tuple of [stdout, stderr] texts. */ execFormat(document: vscode.TextDocument) { - return new Promise(function (resolve, reject) { - let response = "" + return new Promise<[string, string]>(function (resolve, reject) { + let responseOut = "" + let responseErr = "" const config = vscode.workspace.getConfiguration("crystal-lang") let child = spawn(`${config["compiler"]}`, ["tool", "format", "--no-color", "-"]) child.stdin.write(document.getText()) child.stdin.end() childOnError(child) - childOnStd(child, "data", (data) => { - response += data - }) + child.stdout.on("data", (data) => { + responseOut += data + }); + child.stderr.on("data", (data) => { + responseErr += data + }); childOnStd(child, "end", () => { - return resolve(response) + return resolve([responseOut, responseErr]) }) }) } @@ -33,6 +38,8 @@ export class CrystalFormattingProvider implements vscode.DocumentFormattingEditP */ async provideDocumentFormattingEdits(document: vscode.TextDocument) { let response = await this.execFormat(document) + let responseOut = response[0] + let responseErr = response[1] let textEditData: vscode.TextEdit[] = [] // OnFly error check is disabled because -f json was removed from crystal, see: @@ -42,11 +49,11 @@ export class CrystalFormattingProvider implements vscode.DocumentFormattingEditP // } // QuickFix to replace current code with formated one only if no syntax error is found - if ((searchProblemsFromRaw(response.toString(), document.uri).length == 0) && - response.toString().length > 0) { + if ((searchProblemsFromRaw(responseErr, document.uri).length == 0) && + responseOut.length > 0) { let lastLineId = document.lineCount - 1 let range = new vscode.Range(0, 0, lastLineId, document.lineAt(lastLineId).text.length) - textEditData = [vscode.TextEdit.replace(range, response.toString())] + textEditData = [vscode.TextEdit.replace(range, responseOut)] } return textEditData diff --git a/src/crystalUtils.ts b/src/crystalUtils.ts index be9c905..48a81ca 100755 --- a/src/crystalUtils.ts +++ b/src/crystalUtils.ts @@ -199,7 +199,7 @@ export function searchProblemsFromRaw(response: string, uri: vscode.Uri) { const config = vscode.workspace.getConfiguration("crystal-lang") - let responseData = response.match(/.* in .*(\d+): (.*)/) + let responseData = response.match(/.* in .*?(\d+):\S* (.*)/) let parsedLine:number