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

Use flow-language-server behind a flag #150

Merged
merged 10 commits into from
Mar 23, 2018
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
104 changes: 104 additions & 0 deletions lib/flowLSP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* @flow */
'use strict';

import * as path from 'path';
import * as vscode from 'vscode';
import {
workspace,
window,
Disposable,
ExtensionContext,
StatusBarAlignment,
TextEditor,
} from 'vscode';
import {
ErrorHandler,
LanguageClient,
LanguageClientOptions,
SettingMonitor,
ServerOptions,
State as ClientState,
TransportKind,
} from 'vscode-languageclient';
import { checkNode, checkFlow, isFlowEnabled } from './utils';
import { setupLogging } from './flowLogging';
import { clearWorkspaceCaches } from './pkg/flow-base/lib/FlowHelpers';

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

export function activate(context: ExtensionContext) {
if (!isFlowEnabled()) {
return;
}
global.vscode = vscode;

setupLogging();
checkNode();
checkFlow();

// The server is implemented in node
const SERVER_HOME = context.asAbsolutePath(
path.join('node_modules', 'flow-language-server', 'lib', 'bin', 'cli.js')
);

// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: ServerOptions = {
run: { module: SERVER_HOME, transport: TransportKind.ipc },
debug: {
module: SERVER_HOME,
transport: TransportKind.ipc,
options: { execArgv: ['--nolazy', '--debug=6009'] },
},
};

// Options to control the language client
const clientOptions: LanguageClientOptions = {
documentSelector: languages,
synchronize: {
configurationSection: 'flow',
// Notify the server about file changes to '.clientrc files contain in the workspace
fileEvents: workspace.createFileSystemWatcher('**/*.{js,jsx,mjs,js.flow}'),
},
};

const statusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 0);
let serverRunning: boolean = false;

// Create the language client and start the client.
const client = new LanguageClient('flow', 'Flow', serverOptions, clientOptions);
const defaultErrorHandler: ErrorHandler = client.createDefaultErrorHandler();
const running = 'Flow server is running.';
const stopped = 'Flow server stopped.';

client.onDidChangeState(event => {
if (event.newState === ClientState.Running) {
client.info(running);
statusBarItem.tooltip = running;
serverRunning = true;
} else {
client.info(stopped);
statusBarItem.tooltip = stopped;
serverRunning = false;
}
udpateStatusBarVisibility(statusBarItem, serverRunning);
});

const disposable = client.start();
// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(disposable);
}

function udpateStatusBarVisibility(statusBarItem, show: boolean): void {
if (show) {
statusBarItem.show();
statusBarItem.text = 'Flow';
} else {
statusBarItem.hide();
}
}

workspace.onDidChangeConfiguration(params => {
clearWorkspaceCaches();
});
3 changes: 0 additions & 3 deletions lib/flowMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
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 type {ExtensionContext} from 'vscode';
Expand Down
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* @flow */
import * as _ from 'regenerator-runtime/runtime'; // for async/await
import { ExtensionContext } from 'vscode';
import { useLSP } from './utils';

export function activate(context: ExtensionContext) {
if (useLSP()) {
require('./flowLSP').activate(context);
} else {
require('./flowMain').activate(context);
}
}
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/** @flow */

export {checkFlow, checkNode, isFlowEnabled} from './util'
export {checkFlow, checkNode, isFlowEnabled, useLSP} from './util'
4 changes: 4 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export function isFlowEnabled() {
return workspace.getConfiguration('flow').get('enabled')
}

export function useLSP() {
return workspace.getConfiguration('flow').get('useLSP')
}

export function isFlowStatusEnabled():boolean {
return workspace.getConfiguration('flow').get('showStatus')
}
Expand Down
Loading