Skip to content

Commit cf76984

Browse files
committed
fixes / improvements
1 parent f6e7871 commit cf76984

File tree

3 files changed

+12
-23
lines changed

3 files changed

+12
-23
lines changed

src/commands/analyse.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import showOutput from "./showOutput";
99
import stopAnalyse from "./stopAnalyse";
1010
import { spawn } from "child_process";
1111
import { Diagnostic, DiagnosticSeverity, Range, Uri } from "vscode";
12-
import { getFileLine } from "../utils/fs";
12+
import { getFileLines } from "../utils/fs";
1313

1414
function setStatusBarProgress(ext: Ext, progress?: number) {
1515
let text = "$(sync~spin) PHPStan analysing...";
@@ -56,7 +56,7 @@ async function refreshDiagnostics(ext: Ext, result: PHPStanAnalyseResult) {
5656
}
5757
});
5858

59-
ext.log('Using paths mappings: ' + JSON.stringify(pathMaps));
59+
ext.log('Using path mappings: ' + JSON.stringify(pathMaps));
6060

6161
for (const path in result.files) {
6262
let realPath = path;
@@ -74,19 +74,17 @@ async function refreshDiagnostics(ext: Ext, result: PHPStanAnalyseResult) {
7474
}
7575
}
7676

77+
const fileLines: string[] = await getFileLines(resolve(realPath));
78+
7779
const pathItem = result.files[path];
7880
const diagnostics: Diagnostic[] = [];
7981
for (const messageItem of pathItem.messages) {
8082
const line = messageItem.line ? messageItem.line - 1 : 0;
83+
const lineText = messageItem.line ? (fileLines[line] ?? '') : '';
8184

82-
const lineText = await getFileLine(resolve(realPath), messageItem.line ?? 0);
8385
const startCol = Math.max(0, lineText.search(/[^\s]/g));
8486
const endCol = Math.max(0, lineText.search(/\s*$/g));
8587

86-
ext.log(JSON.stringify({
87-
line, startCol, endCol
88-
}));
89-
9088
const range = new Range(line, startCol, line, endCol);
9189
const diagnostic = new Diagnostic(
9290
range,

src/utils/fs.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export async function checkFile(path: string): Promise<boolean> {
1010
}
1111
}
1212

13-
export async function getFileLine(path: string, lineNumber: number): Promise<string> {
13+
export async function getFileLines(path: string): Promise<string[]> {
1414
if (!checkFile(path)) {
15-
return Promise.resolve('');
15+
return Promise.resolve([]);
1616
}
1717

1818
return new Promise((resolve, reject) => {
@@ -23,24 +23,14 @@ export async function getFileLine(path: string, lineNumber: number): Promise<str
2323
input: stream,
2424
});
2525

26-
let currentLineNumber = 0;
27-
let found = false;
26+
const lines: string[] = [];
2827

2928
rl.on('line', (line) => {
30-
if (!found && ++currentLineNumber === lineNumber) {
31-
found = true;
32-
rl.close();
33-
stream.close();
34-
stream.push(null);
35-
stream.read(0);
36-
resolve(line);
37-
}
29+
lines.push(line);
3830
});
3931

4032
rl.on('close', () => {
41-
if (!found) {
42-
resolve('');
43-
}
33+
resolve(lines);
4434
});
4535
} catch (err) {
4636
reject(err);

src/utils/neon.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFile } from "fs/promises";
22
import { load } from "js-yaml";
3+
import { join, resolve } from "path";
34

45
export function resolveNeon(contents: string, env: Record<string, string>) {
56
return contents.replace(/(?:%(\w+)%)/g, (_, name) => env[name] ?? "");
@@ -9,7 +10,7 @@ export async function parseNeonFile<T = unknown>(
910
path: string,
1011
env: Record<string, string> = {}
1112
): Promise<T> {
12-
const contents = (await readFile(path)).toString();
13+
const contents = (await readFile(resolve(join(env.currentWorkingDirectory, path)))).toString();
1314
const yaml = resolveNeon(contents.replace(/\t/g, " "), env);
1415
return load(yaml) as Promise<T>;
1516
}

0 commit comments

Comments
 (0)