Refactor
parent
1ba3596923
commit
5963e58ab1
|
@ -51,6 +51,7 @@
|
||||||
"vscode": "^0.11.17"
|
"vscode": "^0.11.17"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"mz": "^2.4.0",
|
||||||
"semver": "^5.3.0",
|
"semver": "^5.3.0",
|
||||||
"vscode": "^0.11.18",
|
"vscode": "^0.11.18",
|
||||||
"vscode-languageclient": "^2.5.0"
|
"vscode-languageclient": "^2.5.0"
|
||||||
|
|
164
src/extension.ts
164
src/extension.ts
|
@ -1,97 +1,103 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { spawn, execFile, ChildProcess } from 'child_process';
|
import { spawn, execFile, ChildProcess } from 'mz/child_process';
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { LanguageClient, LanguageClientOptions, StreamInfo } from 'vscode-languageclient';
|
import { LanguageClient, LanguageClientOptions, StreamInfo } from 'vscode-languageclient';
|
||||||
import * as semver from 'semver';
|
import * as semver from 'semver';
|
||||||
import * as net from 'net';
|
import * as net from 'net';
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export async function activate(context: vscode.ExtensionContext): Promise<void> {
|
||||||
|
|
||||||
const conf = vscode.workspace.getConfiguration('php');
|
const conf = vscode.workspace.getConfiguration('php');
|
||||||
const executablePath = conf.get<string>('executablePath') || 'php';
|
const executablePath = conf.get<string>('executablePath') || 'php';
|
||||||
|
|
||||||
// Check path (if PHP is available and version is ^7.0.0).
|
// Check path (if PHP is available and version is ^7.0.0)
|
||||||
execFile(executablePath, ['--version'], (err: NodeJS.ErrnoException, stdout: Buffer, stderr: Buffer) => {
|
let stdout: string;
|
||||||
if (err) {
|
try {
|
||||||
if (err.code === 'ENOENT') {
|
[stdout] = await execFile(executablePath, ['--version']);
|
||||||
vscode.window.showErrorMessage('PHP executable not found. You need PHP 7 installed and in your PATH');
|
} catch (err) {
|
||||||
} else {
|
if (err.code === 'ENOENT') {
|
||||||
vscode.window.showErrorMessage('Error spawning PHP: ' + err.message);
|
const selected = await vscode.window.showErrorMessage(
|
||||||
console.error(err);
|
'PHP executable not found. Install PHP 7 and add it to your PATH or set the php.executablePath setting',
|
||||||
|
'Open settings'
|
||||||
|
);
|
||||||
|
if (selected === 'Open settings') {
|
||||||
|
await vscode.commands.executeCommand('workbench.action.openGlobalSettings');
|
||||||
}
|
}
|
||||||
return;
|
} else {
|
||||||
|
vscode.window.showErrorMessage('Error spawning PHP: ' + err.message);
|
||||||
|
console.error(err);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Parse version and discard OS info like 7.0.8--0ubuntu0.16.04.2
|
// Parse version and discard OS info like 7.0.8--0ubuntu0.16.04.2
|
||||||
const match = stdout.toString().match(/^PHP ([^\s]+)/);
|
const match = stdout.match(/^PHP ([^\s]+)/);
|
||||||
if (!match) {
|
if (!match) {
|
||||||
vscode.window.showErrorMessage('Error parsing PHP version. Please check the output of php --version');
|
vscode.window.showErrorMessage('Error parsing PHP version. Please check the output of php --version');
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
let version = match[1].split('-')[0];
|
||||||
|
// Convert PHP prerelease format like 7.0.0rc1 to 7.0.0-rc1
|
||||||
|
if (!/^\d+.\d+.\d+$/.test(version)) {
|
||||||
|
version = version.replace(/(\d+.\d+.\d+)/, '$1-');
|
||||||
|
}
|
||||||
|
if (semver.lt(version, '7.0.0')) {
|
||||||
|
vscode.window.showErrorMessage('The language server needs at least PHP 7 installed. Version found: ' + version);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const serverOptions = () => new Promise<ChildProcess | StreamInfo>((resolve, reject) => {
|
||||||
|
function spawnServer(...args: string[]): ChildProcess {
|
||||||
|
// The server is implemented in PHP
|
||||||
|
args.unshift(context.asAbsolutePath(path.join('vendor', 'felixfbecker', 'language-server', 'bin', 'php-language-server.php')));
|
||||||
|
const childProcess = spawn('php', args);
|
||||||
|
childProcess.stderr.on('data', (chunk: Buffer) => {
|
||||||
|
console.error(chunk + '');
|
||||||
|
});
|
||||||
|
childProcess.stdout.on('data', (chunk: Buffer) => {
|
||||||
|
console.log(chunk + '');
|
||||||
|
});
|
||||||
|
return childProcess;
|
||||||
}
|
}
|
||||||
let version = match[1].split('-')[0];
|
if (process.platform === 'win32') {
|
||||||
// Convert PHP prerelease format like 7.0.0rc1 to 7.0.0-rc1
|
// Use a TCP socket on Windows because of blocking STDIO
|
||||||
if (!/^\d+.\d+.\d+$/.test(version)) {
|
const server = net.createServer(socket => {
|
||||||
version = version.replace(/(\d+.\d+.\d+)/, '$1-');
|
// 'connection' listener
|
||||||
|
console.log('PHP process connected');
|
||||||
|
socket.on('end', () => {
|
||||||
|
console.log('PHP process disconnected');
|
||||||
|
});
|
||||||
|
server.close();
|
||||||
|
resolve({ reader: socket, writer: socket });
|
||||||
|
});
|
||||||
|
// Listen on random port
|
||||||
|
server.listen(0, '127.0.0.1', () => {
|
||||||
|
const address = '127.0.0.1:' + server.address().port;
|
||||||
|
spawnServer('--tcp', address);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Use STDIO on Linux / Mac
|
||||||
|
resolve(spawnServer());
|
||||||
}
|
}
|
||||||
if (semver.lt(version, '7.0.0')) {
|
|
||||||
vscode.window.showErrorMessage('The language server needs at least PHP 7 installed and in your PATH. Version found: ' + version);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const serverOptions = () => new Promise<ChildProcess | StreamInfo>((resolve, reject) => {
|
|
||||||
function spawnServer(...args: string[]): ChildProcess {
|
|
||||||
// The server is implemented in PHP
|
|
||||||
args.unshift(context.asAbsolutePath(path.join('vendor', 'felixfbecker', 'language-server', 'bin', 'php-language-server.php')));
|
|
||||||
const childProcess = spawn('php', args);
|
|
||||||
childProcess.stderr.on('data', (chunk: Buffer) => {
|
|
||||||
console.error(chunk + '');
|
|
||||||
});
|
|
||||||
childProcess.stdout.on('data', (chunk: Buffer) => {
|
|
||||||
console.log(chunk + '');
|
|
||||||
});
|
|
||||||
return childProcess;
|
|
||||||
}
|
|
||||||
if (process.platform === 'win32') {
|
|
||||||
// Use a TCP socket on Windows because of blocking STDIO
|
|
||||||
const server = net.createServer(socket => {
|
|
||||||
// 'connection' listener
|
|
||||||
console.log('PHP process connected');
|
|
||||||
socket.on('end', () => {
|
|
||||||
console.log('PHP process disconnected');
|
|
||||||
});
|
|
||||||
server.close();
|
|
||||||
resolve({ reader: socket, writer: socket });
|
|
||||||
});
|
|
||||||
// Listen on random port
|
|
||||||
server.listen(0, '127.0.0.1', () => {
|
|
||||||
const address = '127.0.0.1:' + server.address().port;
|
|
||||||
spawnServer('--tcp', address);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Use STDIO on Linux / Mac
|
|
||||||
resolve(spawnServer());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Options to control the language client
|
|
||||||
let clientOptions: LanguageClientOptions = {
|
|
||||||
// Register the server for php documents
|
|
||||||
documentSelector: ['php']
|
|
||||||
// synchronize: {
|
|
||||||
// // Synchronize the setting section 'php' to the server
|
|
||||||
// configurationSection: 'php',
|
|
||||||
// // Notify the server about file changes to composer.json files contain in the workspace
|
|
||||||
// fileEvents: workspace.createFileSystemWatcher('**/composer.json')
|
|
||||||
// }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create the language client and start the client.
|
|
||||||
const disposable = new LanguageClient('PHP Language Client', serverOptions, clientOptions).start();
|
|
||||||
|
|
||||||
// Push the disposable to the context's subscriptions so that the
|
|
||||||
// client can be deactivated on extension deactivation
|
|
||||||
context.subscriptions.push(disposable);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Options to control the language client
|
||||||
|
const clientOptions: LanguageClientOptions = {
|
||||||
|
// Register the server for php documents
|
||||||
|
documentSelector: ['php'],
|
||||||
|
synchronize: {
|
||||||
|
// Synchronize the setting section 'php' to the server
|
||||||
|
configurationSection: 'php'
|
||||||
|
// Notify the server about file changes to composer.json files contain in the workspace
|
||||||
|
// fileEvents: vscode.workspace.createFileSystemWatcher('**/composer.json')
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create the language client and start the client.
|
||||||
|
const disposable = new LanguageClient('PHP Language Server', serverOptions, clientOptions).start();
|
||||||
|
|
||||||
|
// Push the disposable to the context's subscriptions so that the
|
||||||
|
// client can be deactivated on extension deactivation
|
||||||
|
context.subscriptions.push(disposable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
{
|
{
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"mz": "registry:npm/mz#2.4.0+20160911015431",
|
||||||
"semver": "registry:npm/semver#5.0.0+20160723033700"
|
"semver": "registry:npm/semver#5.0.0+20160723033700"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue