Skip to content
Open
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
48 changes: 35 additions & 13 deletions phpcs-server/src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@ import { StringResources as SR } from "./strings";
import { PhpcsSettings } from "./settings";
import { PhpcsMessage } from "./message";

// PHPCS version cached data
const CACHED_PHPCS_VERSION_TTL = 300000; // 5 min
let phpcsCachedVersion: {
executableVersion: string|null,
lastChecked: Date|null
} = {
executableVersion: null,
lastChecked: null
};

export class PhpcsLinter {

private executablePath: string;
private executableVersion: string;
private ignorePatternReplacements: Map<RegExp, string>;
private documentLines: string[] | undefined;

private constructor(executablePath: string, executableVersion: string) {
this.executablePath = executablePath;
Expand All @@ -42,17 +53,22 @@ export class PhpcsLinter {
static async create(executablePath: string): Promise<PhpcsLinter> {
try {

let result: Buffer = cp.execSync(`"${executablePath}" --version`);
const now = new Date();
if (phpcsCachedVersion.lastChecked === null || now.getTime() - phpcsCachedVersion.lastChecked.getTime() > CACHED_PHPCS_VERSION_TTL)
{
let result: Buffer = cp.execSync(`"${executablePath}" --version`);

const versionPattern: RegExp = /^PHP_CodeSniffer version (\d+\.\d+\.\d+)/i;
const versionMatches = result.toString().match(versionPattern);
const versionPattern: RegExp = /^PHP_CodeSniffer version (\d+\.\d+\.\d+)/i;
const versionMatches = result.toString().match(versionPattern);

if (versionMatches === null) {
throw new Error(SR.InvalidVersionStringError);
}
if (versionMatches === null) {
throw new Error(SR.InvalidVersionStringError);
}

const executableVersion = versionMatches[1];
return new PhpcsLinter(executablePath, executableVersion);
phpcsCachedVersion.executableVersion = versionMatches[1];
phpcsCachedVersion.lastChecked = now;
}
return new PhpcsLinter(executablePath, phpcsCachedVersion.executableVersion);

} catch (error) {
let message = error.message ? error.message : SR.CreateLinterErrorDefaultMessage;
Expand Down Expand Up @@ -223,9 +239,16 @@ export class PhpcsLinter {
}

let diagnostics: Diagnostic[] = [];
messages.map(message => diagnostics.push(
this.createDiagnostic(document, message, settings.showSources)
));
if (messages.length > 0)
{
// Update document lines
this.documentLines = fileText.split("\n");
messages.map(message => diagnostics.push(
this.createDiagnostic(document, message, settings.showSources)
));
// Clear document lines
this.documentLines = undefined;
}

return diagnostics;
}
Expand All @@ -240,9 +263,8 @@ export class PhpcsLinter {

private createDiagnostic(document: TextDocument, entry: PhpcsMessage, showSources: boolean): Diagnostic {

let lines = document.getText().split("\n");
let line = entry.line - 1;
let lineString = lines[line];
let lineString = this.documentLines[line];

// Process diagnostic start and end characters.
let startCharacter = entry.column - 1;
Expand Down
2 changes: 1 addition & 1 deletion phpcs/src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class PhpcsStatus {
this.timer = new Timer(() => {
this.updateStatusText();
});
this.timer.interval = 100;
this.timer.interval = 250;
}
return this.timer;
}
Expand Down