Skip to content
Open
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
37 changes: 20 additions & 17 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
'use strict';
const vscode = require('vscode');
const {
commands,
workspace,
window,
languages,
Range,
Position
} = vscode;
const { commands, workspace, window, languages, Range, Position } = vscode;
const fs = require('fs');
const os = require('os');
const cp = require('child_process');
const path = require('path');
const beautifyHtml = require('./beautifyHtml');
const anymatch = require('anymatch');
const TmpDir = os.tmpdir();
let isRunning = false;
let outputChannel, statusBarItem;
let isRunning = false, outputChannel, statusBarItem, lastActiveEditor;

class PHPCSFixer {
constructor() {
Expand Down Expand Up @@ -66,6 +58,8 @@ class PHPCSFixer {
editorConfig.update('formatOnSaveTimeout', 5000, true);
}
}
this.fileAutoSave = workspace.getConfiguration('files', null).get('autoSave');
this.fileAutoSaveDelay = workspace.getConfiguration('files', null).get('autoSaveDelay', 1000);
}

getActiveWorkspacePath() {
Expand Down Expand Up @@ -195,7 +189,7 @@ class PHPCSFixer {
}

if (!isDiff) {
fs.unlink(filePath, function (err) {});
fs.unlink(filePath, function (err) { });
}
isRunning = false;
this.statusBar("php-cs-fixer: finished");
Expand Down Expand Up @@ -417,6 +411,11 @@ class PHPCSFixer {
return;
}

// only activeTextEditor, or last activeTextEditor
if (window.activeTextEditor == undefined
|| (window.activeTextEditor.document.uri.toString() != document.uri.toString() && lastActiveEditor != document.uri.toString()))
return;

isRunning = false;
return new Promise((resolve, reject) => {
let originalText = document.getText();
Expand Down Expand Up @@ -499,12 +498,10 @@ class PHPCSFixer {
let lastDownload = config.get('lastDownload', 1);
if (lastDownload !== 0 && executablePath == '${extensionPath}' + path.sep + 'php-cs-fixer.phar' && lastDownload + 1000 * 3600 * 24 * 10 < (new Date()).getTime()) {
console.log('php-cs-fixer: check for updating...');
let download = require('download');
download('https://cs.sensiolabs.org/download/php-cs-fixer-v2.phar', __dirname, {
'filename': 'php-cs-fixer.phar'
}).then(() => {
config.update('lastDownload', (new Date()).getTime(), true);
});
const { DownloaderHelper } = require('node-downloader-helper');
let dl = new DownloaderHelper('https://cs.symfony.com/download/php-cs-fixer-v2.phar', __dirname, { 'fileName': 'php-cs-fixer.phar', 'override': true });
dl.on('end', () => config.update('lastDownload', (new Date()).getTime(), true));
dl.start();
}
}, 1000 * 60);
}
Expand All @@ -513,6 +510,12 @@ class PHPCSFixer {
exports.activate = context => {
let pcf = new PHPCSFixer();

context.subscriptions.push(window.onDidChangeActiveTextEditor(te => {
if (pcf.fileAutoSave != 'off') {
setTimeout(() => lastActiveEditor = te == undefined ? undefined : te.document.uri.toString(), pcf.fileAutoSaveDelay + 100);
}
}));

context.subscriptions.push(workspace.onWillSaveTextDocument((event) => {
if (event.document.languageId == 'php' && pcf.onsave && pcf.editorFormatOnSave == false) {
event.waitUntil(commands.executeCommand("editor.action.formatDocument"));
Expand Down