Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes formatting and enables basic systax error check. #85

Merged
merged 1 commit into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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