diff --git a/.gitignore b/.gitignore index 8e5962e..977d545 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ out -node_modules \ No newline at end of file +node_modules +elm-stuff \ No newline at end of file diff --git a/.vscodeignore b/.vscodeignore index 795e714..db39920 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -7,3 +7,4 @@ src/** .gitignore tsconfig.json vsc-extension-quickstart.md +elm-stuff \ No newline at end of file diff --git a/examples/Hello.elm b/examples/Hello.elm index 6cdbaba..8fa683f 100644 --- a/examples/Hello.elm +++ b/examples/Hello.elm @@ -1,3 +1,6 @@ -import Html +import Graphics.Element exposing (..) +import Graphics.Element exposing (..) -main = Html.text "Hello, World!" \ No newline at end of file +main : Element +main = + show "Hello, World!" diff --git a/examples/elm-package.json b/examples/elm-package.json new file mode 100644 index 0000000..54765dc --- /dev/null +++ b/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" +} \ No newline at end of file diff --git a/src/elmMain.ts b/src/elmMain.ts index c546958..e939d14 100644 --- a/src/elmMain.ts +++ b/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 { + return new Promise((resolve, reject) => { + var cmd = 'elm-make ' + filename + ' --report=json --output /dev/null' + cp.exec(cmd, {}, (err, stdout, stderr) => { + try { + if (err && (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) + }) } \ No newline at end of file diff --git a/test/extension.test.ts b/test/extension.test.ts index 1cec6f4..f20d9d0 100644 --- a/test/extension.test.ts +++ b/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", () => {