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

Add support for format on save + add documentation for elm-format #62

Merged
merged 3 commits into from
Jul 7, 2016
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ Or start with some characters and use `Ctrl+Space` for autocompletion.

Want to know more? Look at the [snippet definitions](snippets/elm.json)

### Format

[elm-format](https://github.com/avh4/elm-format) is supported via the editor's `Format Code` command. To format your code using `elm-format`, press `Shift+Alt+F` on Windows, `Shift+Option+F` on Mac, or `Ctrl+Shift+I` on Linux.

You can also configure `elm-format` to run on save by enabling the `elm.formatOnSave` in your settings.

```
// settings.json
{
"elm.formatOnSave": true
}
```


## Help wanted
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
{
"command": "elm.install",
"title": "Elm: Install packages/dependencies"
},
{
"command": "elm.clean",
"title": "Elm: Clean build artifact"
}
],
"outputChannels": [
Expand Down Expand Up @@ -137,7 +141,8 @@
"onLanguage:elm",
"onCommand:elm.replStart",
"onCommand:elm.reactorStart",
"onCommand:elm.install"
"onCommand:elm.install",
"onCommand:elm.clean"
],
"main": "./out/src/elmMain",
"scripts": {
Expand All @@ -147,6 +152,7 @@
},
"devDependencies": {
"typescript": "^1.6.2",
"rimraf": "^2.5.2",
"vscode": "^0.10.7"
},
"dependencies": {
Expand Down
30 changes: 30 additions & 0 deletions src/elmClean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as vscode from 'vscode';
import * as utils from './elmUtils';
import * as path from 'path';
import * as fs from 'fs';
import * as rimraf from 'rimraf';

function runClean(editor: vscode.TextEditor) {
try {
const cwd: string = (editor.document) ? utils.detectProjectRoot(editor.document.fileName) : vscode.workspace.rootPath
const elmStuffDir = path.join(cwd, 'elm-stuff', 'build-artifacts');
console.log(elmStuffDir);
rimraf(elmStuffDir, (error) => {
if (error) {
console.error('Running Elm Clean failed', error);
vscode.window.showErrorMessage('Running Elm Clean failed');
} else {
vscode.window.showInformationMessage('Successfully deleted the build-artifacts folder');
}
});
}
catch (e) {
console.error('Running Elm Clean failed', e);
vscode.window.showErrorMessage('Running Elm Clean failed');
}
}

export function activateClean(): vscode.Disposable[] {
return [
vscode.commands.registerCommand('elm.clean', runClean)];
}
64 changes: 47 additions & 17 deletions src/elmFormat.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
import * as vscode from 'vscode'
import { Range, TextEdit } from 'vscode'

import { execCmd } from './elmUtils'
import { execCmd } from './elmUtils'

export class ElmFormatProvider implements vscode.DocumentFormattingEditProvider {

provideDocumentFormattingEdits(
document: vscode.TextDocument,
options: vscode.FormattingOptions,
token: vscode.CancellationToken)
: Thenable<TextEdit[]>
{
const format = execCmd('elm-format --stdin');

format.stdin.write(document.getText());
format.stdin.end();

return format
: Thenable<TextEdit[]> {
return elmFormat(document)
.then(({ stdout }) => {
const wholeDocument = new Range(0, 0, document.lineCount, document.getText().length);
return [TextEdit.replace(wholeDocument, stdout)];
})
.catch((err) => {
const message = (<string>err.message).includes('SYNTAX PROBLEM')
? "Running elm-format failed. Check the file for syntax errors."
: "Running elm-format failed. Install from "
+ "https://github.com/avh4/elm-format and make sure it's on your path";

return vscode.window.showErrorMessage(message).then(() => [])
.catch(showError);
}
}

const ignoreNextSave = new WeakSet<vscode.TextDocument>();
export function runFormatOnSave(document: vscode.TextDocument) {

if (document.languageId !== 'elm' || ignoreNextSave.has(document)) {
return;
}

const config = vscode.workspace.getConfiguration('elm');
const active = vscode.window.activeTextEditor;
const range = new vscode.Range(0, 0, document.lineCount, document.getText().length);

if (config['formatOnSave'] && active.document === document) {
elmFormat(active.document)
.then(({stdout}) => {
active.edit(editor => editor.replace(range, stdout));
ignoreNextSave.add(document);
return document.save();
})
.then(() => {
ignoreNextSave.delete(document);
})
.catch(showError);
}
}

function elmFormat(document: vscode.TextDocument) {
const format = execCmd('elm-format --stdin');

format.stdin.write(document.getText());
format.stdin.end();

return format;
}

function showError(err) {
const message = (<string>err.message).includes('SYNTAX PROBLEM')
? "Running elm-format failed. Check the file for syntax errors."
: "Running elm-format failed. Install from "
+ "https://github.com/avh4/elm-format and make sure it's on your path";

return vscode.window.showErrorMessage(message).then(() => [])
}
7 changes: 6 additions & 1 deletion src/elmMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {activateRepl} from './elmRepl';
import {activateReactor, deactivateReactor} from './elmReactor';
import {activateMake} from './elmMake';
import {activatePackage} from './elmPackage';
import {activateClean} from './elmClean';
import {ElmDefinitionProvider} from './elmDefinition';
import {ElmHoverProvider} from './elmInfo';
import {ElmCompletionProvider} from './elmAutocomplete';
import {ElmSymbolProvider} from './elmSymbol';
import {configuration} from './elmConfiguration';
import {ElmFormatProvider} from './elmFormat';
import {ElmFormatProvider, runFormatOnSave} from './elmFormat';

const ELM_MODE: vscode.DocumentFilter = { language: 'elm', scheme: 'file' };

Expand All @@ -18,10 +19,14 @@ export function activate(ctx: vscode.ExtensionContext) {
ctx.subscriptions.push(vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => {
runLinter(document);
}));
ctx.subscriptions.push(vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => {
runFormatOnSave(document);
}));
activateRepl().forEach((d: vscode.Disposable) => ctx.subscriptions.push(d));
activateReactor().forEach((d: vscode.Disposable) => ctx.subscriptions.push(d));
activateMake().forEach((d: vscode.Disposable) => ctx.subscriptions.push(d));
activatePackage().forEach((d: vscode.Disposable) => ctx.subscriptions.push(d));
activateClean().forEach((d: vscode.Disposable) => ctx.subscriptions.push(d));

ctx.subscriptions.push(vscode.languages.setLanguageConfiguration('elm', configuration))
ctx.subscriptions.push(vscode.languages.registerHoverProvider(ELM_MODE, new ElmHoverProvider()));
Expand Down
16 changes: 16 additions & 0 deletions typings/rimraf/rimraf.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Type definitions for rimraf
// Project: https://github.com/isaacs/rimraf
// Definitions by: Carlos Ballesteros Velasco <https://github.com/soywiz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

// Imported from: https://github.com/soywiz/typescript-node-definitions/rimraf.d.ts

declare module "rimraf" {
function rimraf(path: string, callback: (error: Error) => void): void;
namespace rimraf {
export function sync(path: string): void;
export var EMFILE_MAX: number;
export var BUSYTRIES_MAX: number;
}
export = rimraf;
}