Skip to content

Commit

Permalink
Allow running flow on every edit as per flow#26
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Mar 15, 2017
1 parent c105ab6 commit b524f3f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Follow the [instructions](https://code.visualstudio.com/docs/editor/extension-ga
* `flow.enabled` (default: true) you can disable flow for some Project for example
* `flow.useNPMPackagedFlow` (default: false) you can also run Flow by defining it in your `package.json`
* `flow.showStatus` (default: `true`) If `true` will display a spinner in the statusbar while flow is type checking.
* `flow.runOnEdit` (default: `true`) If `true` will run flow on every edit, otherwise will run only when changes are saved.

## Features

Expand Down
16 changes: 12 additions & 4 deletions lib/flowDiagnostics.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {Status} from './flowStatus'
import type {DiagnosticCollection, Disposable} from 'vscode';

import {flowFindDiagnostics} from './pkg/flow-base/lib/FlowService';
import {isRunOnEditEnabled} from './utils/util'

let lastDiagnostics: null | DiagnosticCollection = null;
const status = new Status()
Expand All @@ -37,20 +38,27 @@ export function setupDiagnostics(disposables: Array<Disposable>): void {
updateDiagnostics(vscode.window.activeTextEditor.document);
}
}));

disposables.push(vscode.workspace.onDidChangeTextDocument(event => {
const isDocumentActive = vscode.window.activeTextEditor.document === event.document;
if (isDocumentActive && isRunOnEditEnabled()) {
updateDiagnostics(event.document, event.document.getText());
}
}));
}

async function updateDiagnostics(document) {
async function updateDiagnostics(document, content:?string) {
status.busy()
try {
let diagnostics = await getDiagnostics(document)
let diagnostics = await getDiagnostics(document, content)
applyDiagnostics(diagnostics)
} catch(error) {
console.error(error)
}
status.idle()
}

async function getDiagnostics(document) {
async function getDiagnostics(document, content:?string) {
let diags = Object.create(null);

if (!document) {
Expand All @@ -65,7 +73,7 @@ async function getDiagnostics(document) {
// flowFindDiagnostics takes the provided filePath and then walks up directories
// until a .flowconfig is found. The diagnostics are then valid for the entire
// flow workspace.
let rawDiag = await flowFindDiagnostics(filePath);
let rawDiag = await flowFindDiagnostics(filePath, content);
if (rawDiag && rawDiag.messages) {
const { flowRoot } = rawDiag;

Expand Down
4 changes: 4 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export function isFlowStatusEnabled():boolean {
return workspace.getConfiguration('flow').get('showStatus')
}

export function isRunOnEditEnabled():boolean {
return workspace.getConfiguration('flow').get('runOnEdit')
}

export function checkNode() {
try {
const check = spawn(process.platform === 'win32' ? 'where' : 'which', ['node'])
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"default": true,
"description": "If true will display flow status is the statusbar"
},
"flow.runOnEdit": {
"type": "boolean",
"default": true,
"description": "If true will run flow on every edit, otherwise will run only when changes are saved"
},
"flow.stopFlowOnExit": {
"type": "boolean",
"default": true,
Expand Down

0 comments on commit b524f3f

Please sign in to comment.