Skip to content
This repository has been archived by the owner on Dec 17, 2019. It is now read-only.

Commit

Permalink
Add experimental error highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sascha Brink committed Nov 22, 2015
1 parent 90c561a commit 3570726
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
out
node_modules
node_modules
elm-stuff
1 change: 1 addition & 0 deletions .vscodeignore
Expand Up @@ -7,3 +7,4 @@ src/**
.gitignore
tsconfig.json
vsc-extension-quickstart.md
elm-stuff
7 changes: 5 additions & 2 deletions examples/Hello.elm
@@ -1,3 +1,6 @@
import Html
import Graphics.Element exposing (..)
import Graphics.Element exposing (..)

main = Html.text "Hello, World!"
main : Element
main =
show "Hello, World!"
14 changes: 14 additions & 0 deletions examples/elm-package.json
@@ -0,0 +1,14 @@
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "3.0.0 <= v < 4.0.0"
},
"elm-version": "0.16.0 <= v < 0.17.0"
}
93 changes: 81 additions & 12 deletions src/elmMain.ts
@@ -1,24 +1,93 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import cp = require('child_process');
import path = require('path');
import os = require('os');
import fs = require('fs');

export interface ICheckResult {
tag: string;
overview: string;
subregion: string;
details: string;
region: {
start: {
line: number;
column: number;
}
end: {
line: number;
column: number;
}
}
type: string;
file: string;
}

export function checkForErrors(filename): Promise<ICheckResult[]> {
return new Promise((resolve, reject) => {
var cmd = 'elm-make ' + filename + ' --report=json --output /dev/null'
cp.exec(cmd, {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.window.showInformationMessage("The 'elm-make' compiler is not available. Install Elm from http://elm-lang.org/.");
return resolve([]);
}
console.log('stdout', stdout.toString());
var lines:ICheckResult[] = JSON.parse(stdout.toString());
resolve(lines);
} catch(e) {
reject(e);
}
});

});
}


export function createDiagnostics(document: vscode.TextDocument) {
if (document.languageId != "elm") {
return;
}
let diagnostics: vscode.Diagnostic[] = [];
let compileErrors = vscode.languages.createDiagnosticCollection("compile");
let uri = document.uri;

checkForErrors(uri.fsPath).then((compilerErrors) => {
diagnostics = compilerErrors.map((error) => {
let lineRange = new vscode.Range(
error.region.start.line - 1,
error.region.start.column - 1,
error.region.end.line - 1,
error.region.end.column - 1
);
return new vscode.Diagnostic(lineRange, error.details.replace(/\[\d+m/g, ''), vscode.DiagnosticSeverity.Error);
})
compileErrors.set(document.uri, diagnostics);
console.log('check success', compilerErrors);
}, (error) => {
compileErrors.set(document.uri, []);
console.log('check error', error)
})
}

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

export function activate(disposables: vscode.Disposable[]) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "elm" is now active!');
console.log('Extension "elm" is now active!');

// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
var disposable = vscode.commands.registerCommand('extension.sayHello', () => {
// The code you place here will be executed every time your command is executed
// vscode.workspace.onDidChangeTextDocument(event => {
// createDiagnostics(event.document)
// }, undefined, disposables);

// Display a message box to the user
vscode.window.showInformationMessage('Hello World!');
});
vscode.workspace.onDidOpenTextDocument(document => {
createDiagnostics(document)
});

context.subscriptions.push(disposable);
vscode.workspace.onDidSaveTextDocument(document => {
createDiagnostics(document)
})
}
6 changes: 3 additions & 3 deletions test/extension.test.ts
@@ -1,15 +1,15 @@
//
//
// Note: This example test is leveraging the Mocha test framework.
// Please refer to their documentation on https://mochajs.org/ for help.
//

// The module 'assert' provides assertion methods from node
import * as assert from 'assert';
import {assert, test} assert from 'assert';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
import * as myExtension from '../src/extension';
import * as elmMain from './../src/elmMain';

// Defines a Mocha test suite to group tests of similar kind together
suite("Extension Tests", () => {
Expand Down

0 comments on commit 3570726

Please sign in to comment.