Skip to content

Commit

Permalink
Fixes formatting and enables basic systax error check. (#85)
Browse files Browse the repository at this point in the history
- Creates searchProblemsFromRaw
- Restore error check on formatting
  • Loading branch information
faustinoaq authored Feb 8, 2019
1 parent 5feb5f9 commit 1247047
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/crystalDiagnostic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as vscode from "vscode"
import { spawn } from "child_process"

import { Concurrent, isNotLib, spawnCompiler } from "./crystalUtils"

Expand Down
5 changes: 3 additions & 2 deletions src/crystalFormatting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from "vscode"
import { spawn } from "child_process"

import { searchProblems, childOnStd, childOnError } from "./crystalUtils"
import { searchProblemsFromRaw, childOnStd, childOnError } from "./crystalUtils"

/**
* Formatting provider using VSCode module
Expand Down Expand Up @@ -42,7 +42,8 @@ export class CrystalFormattingProvider implements vscode.DocumentFormattingEditP
// }

// QuickFix to replace current code with formated one only if no syntax error is found
if (!response.toString().startsWith("Error:")) {
if ((searchProblemsFromRaw(response.toString(), document.uri).length == 0) &&
response.toString().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())]
Expand Down
55 changes: 48 additions & 7 deletions src/crystalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,47 @@ export function searchProblems(response: string, uri: vscode.Uri) {
return diagnostics
}

/**
* Parse raw output from crystal tool format - response and create diagnostics
*/
export function searchProblemsFromRaw(response: string, uri: vscode.Uri) {
let diagnostics = []

const config = vscode.workspace.getConfiguration("crystal-lang")

let responseData = response.match(/.* in (.*):(\d+): (.*)/)

let parsedLine:number

try {
parsedLine = parseInt(responseData[2])
} catch (e) {
parsedLine = 0
}

let columnLocation = 1 // No way to get column from crystal tool format -

if (parsedLine != 0) {
let problem = {
line: parsedLine,
column: columnLocation,
message: responseData[3]
}

let range = new vscode.Range(problem.line - 1, problem.column - 1, problem.line - 1, problem.column - 1)
let diagnostic = new vscode.Diagnostic(range, problem.message, vscode.DiagnosticSeverity.Error)
diagnostics.push([uri, [diagnostic]])
}

if (diagnostics.length == 0) {
diagnosticCollection.clear()
} else if (config["problems"] != "none") {
diagnosticCollection.set(diagnostics)
}

return diagnostics
}

/**
* Execute Crystal tools context and implementations
*/
Expand Down Expand Up @@ -258,24 +299,24 @@ export function spawnCompiler(document, build) {
]
}
return [
"build",
"--no-debug",
"tool",
"format",
"--check",
"--no-color",
"--no-codegen",
scope,
"-f",
"json"
scope
]
})()
let child = spawn(config["compiler"], args, spawnOptions)
childOnStd(child, "data", (data) => {
response += data
})
childOnStd(child, "end", () => {
searchProblems(response.toString(), document.uri)
if (build) {
searchProblems(response.toString(), document.uri)
Concurrent.counter -= 1
statusBarItem.hide()
} else {
searchProblemsFromRaw(response.toString(), document.uri)
}
})
childOnError(child)
Expand Down

0 comments on commit 1247047

Please sign in to comment.