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

Commit

Permalink
Cache existing flow paths
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Dec 4, 2016
1 parent 66bd3a5 commit f0fb497
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 65 deletions.
106 changes: 56 additions & 50 deletions lib/flowMain.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
/* @flow */

/*
Copyright (c) 2015-present, Facebook, Inc.
All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
the root directory of this source tree.
*/

// Necessary to get the regenerator runtime, which transpiled async functions need
import * as _ from 'regenerator-runtime/runtime';

import * as vscode from 'vscode';

import {CompletionSupport} from './flowCompletion';
import {HoverSupport} from './flowHover'
import {DeclarationSupport} from './flowDeclaration';
import {setupDiagnostics} from './flowDiagnostics';

import {checkNode, checkFlow, isFlowEnabled} from './utils'

type Context = {
subscriptions: Array<vscode.Disposable>
}

const languages = [
{ language: 'javascript', scheme: 'file' },
'javascriptreact'
]

export function activate(context: Context): void {
//User can disable flow for some projects that previously used flow, but it's not have actual typing
if (!isFlowEnabled()) {
return
}
global.vscode = vscode
checkNode()
checkFlow()
// Language features
languages.forEach(lang => {
context.subscriptions.push(vscode.languages.registerHoverProvider(lang, new HoverSupport));
context.subscriptions.push(vscode.languages.registerDefinitionProvider(lang, new DeclarationSupport()));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(lang, new CompletionSupport(), '.'));
})
// https://github.com/Microsoft/vscode/issues/7031 Workaround for language scoring for language and in-memory. Check in nearest Insiders build
// context.subscriptions.push(vscode.languages.registerCompletionItemProvider({ language: 'javascript' }, new CompletionSupport(), '.'));
// Diagnostics
setupDiagnostics(context.subscriptions);
}
/* @flow */

/*
Copyright (c) 2015-present, Facebook, Inc.
All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
the root directory of this source tree.
*/

// Necessary to get the regenerator runtime, which transpiled async functions need
import * as _ from 'regenerator-runtime/runtime';

import * as vscode from 'vscode';

import {CompletionSupport} from './flowCompletion';
import {HoverSupport} from './flowHover'
import {DeclarationSupport} from './flowDeclaration';
import {setupDiagnostics} from './flowDiagnostics';

import {checkNode, checkFlow, isFlowEnabled} from './utils'
import {clearCaches} from './pkg/flow-base/lib/FlowHelpers'

type Context = {
subscriptions: Array<vscode.Disposable>
}

const languages = [
{ language: 'javascript', scheme: 'file' },
'javascriptreact'
]

export function activate(context: Context): void {
//User can disable flow for some projects that previously used flow, but it's not have actual typing
if (!isFlowEnabled()) {
return
}
global.vscode = vscode
checkNode()
checkFlow()

// Language features
languages.forEach(lang => {
context.subscriptions.push(vscode.languages.registerHoverProvider(lang, new HoverSupport));
context.subscriptions.push(vscode.languages.registerDefinitionProvider(lang, new DeclarationSupport()));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(lang, new CompletionSupport(), '.'));
})
// https://github.com/Microsoft/vscode/issues/7031 Workaround for language scoring for language and in-memory. Check in nearest Insiders build
// context.subscriptions.push(vscode.languages.registerCompletionItemProvider({ language: 'javascript' }, new CompletionSupport(), '.'));
// Diagnostics
setupDiagnostics(context.subscriptions);
}

vscode.workspace.onDidChangeConfiguration(params => {
clearWorkspaceCaches();
});
43 changes: 28 additions & 15 deletions lib/pkg/flow-base/lib/FlowHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ function isOptional(param: string): boolean {
return lastChar === '?';
}

function clearWorkspaceCaches() {
flowPathCache.reset();
flowConfigDirCache.reset();
cachedPathToFlowBin = undefined;
}

async function isFlowInstalled(): Promise<boolean> {
const flowPath = await getPathToFlow();
if (!flowPathCache.has(flowPath)) {
Expand All @@ -144,32 +150,38 @@ async function canFindFlow(flowPath: string): Promise<boolean> {
}

/**
* @return The path to Flow on the user's machine. First using the the user's config, then looking into
* the node_modules for the project.
* It is recommended not to cache the result of this function in case the user updates his or her
* preferences in Atom, in which case the return value will be stale.
* @return The path to Flow on the user's machine. First using the the user's
* config, then looking into the node_modules for the project.
*
* It is cached, so it is expected that changing the users settings will
* trigger a call to `clearWorkspaceCaches`.
*/
let cachedPathToFlowBin = undefined
async function getPathToFlow(): Promise<string> {
const config = global.vscode.workspace.getConfiguration('flow')
const userPath = config.get('pathToFlow')
if (cachedPathToFlowBin) return { cachedPathToFlow }

if (await canFindFlow(userPath)) {
return userPath
const config = global.vscode.workpace.getConfiguration('flow');
const shouldUseNodeModule = config.get('useNPMPackagedFlow');

if (shouldUseNodeModule && await canFindFlow(nodeModuleFlowLocation())){
cachedPathToFlow = nodeModuleFlowLocation();
return cachedPathToFlow
}

const shouldUseNodeModule = config.get('useNPMPackagedFlow')
if (shouldUseNodeModule && await canFindFlow(fallbackNodeModuleFlowLocation())){
return fallbackNodeModuleFlowLocation()
const userPath = config.get('pathToFlow');
if (await canFindFlow(userPath)) {
cachedPathToFlow = userPath
return cachedPathToFlow
}
return userPath
}

/**
* @return The potential path to Flow on the user's machine if they are using NPM/Yarn to manage
* @returnThe potential path to Flow on the user's machine if they are using NPM/Yarn to manage
* their installs of flow.
*/
function fallbackNodeModuleFlowLocation(): string {
return global.vscode.workspace.rootPath + "/node_modules/.bin/flow"
function nodeModuleFlowLocation(): string {
const flowBin = process.platform === 'win32' ? 'flow.cmd' : 'flow'
return `${global.vscode.workspace.rootPath}/node_modules/.bin/${flowBin}`
}

function getStopFlowOnExit(): boolean {
Expand Down Expand Up @@ -212,4 +224,5 @@ module.exports = {
processAutocompleteItem,
groupParamNames,
flowCoordsToAtomCoords,
clearWorkspaceCaches
};

0 comments on commit f0fb497

Please sign in to comment.