Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring getFileLocation to be static CMakeBuilder method #39

Merged
merged 2 commits into from
Nov 9, 2023
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
27 changes: 0 additions & 27 deletions src/actions/file_change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,6 @@ import { IAction, IJsonObject, IToJson, Project as IProject, IFileChangeManager

const log = H.getLogger("file_change");

/**
* Some errors have file:line:col encoded in the error log
* @param s error log
* @param reverse find the first or last pattern occurrence in error log
* @returns FileLocation or null
* examples: CMake Error at /home/test/tests.cmake:146 (message):
*/
export function getFileLocation(s: string, reverse: boolean): FileLocation | null {
let lines = s.trimStart().split("\n");
if (reverse) lines = lines.reverse();

// FIXME: this doesn't support file name which contains spaces. need to be fixed
const re = /\b(?<file>[^:\s]+):(?<line>\d+)(:(?<col>\d+))?\b/;

for (const line of lines) {
const m = line.match(re);
if (m && m.groups) {
return new FileLocation(
m.groups.file as string,
parseInt(m.groups.line as string),
m.groups.col ? parseInt(m.groups.col as string) : 0
);
}
}
return null;
}

export class FileLocation implements IToJson {
file: string;
line: number;
Expand Down
31 changes: 29 additions & 2 deletions src/builders/cmake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as H from "../helper";
import { ALL_BUILDER_FACTORIES } from "../builder";
import { checkJsonType } from "../json_factory";
import { ErrorAdviseRequest } from "../advise_requests/common_requests";
import { getFileLocation } from "../actions/file_change";
import { FileLocation } from "../actions/file_change";
import { isPrevBuildersAllNative } from "../builder";
import { dependencyDir } from "../constants";
import {
Expand Down Expand Up @@ -106,7 +106,7 @@ class CMakeBuilder implements IBuilder {
new ErrorAdviseRequest(
"cmake",
err,
getFileLocation(err, true /*search from last*/),
CMakeBuilder.getFileLocation(err, true /*search from last*/),
this.id
)
);
Expand Down Expand Up @@ -199,6 +199,33 @@ class CMakeBuilder implements IBuilder {
return true;
}
}

/**
* Some errors have file:line:col encoded in the error log
* @param s error log
* @param reverse find the first or last pattern occurrence in error log
* @returns FileLocation or null
* examples: CMake Error at /home/test/tests.cmake:146 (message):
* examples: CMake Error at /home/test file name with spaces/tests.cmake:146:12 (message):
*/
static getFileLocation(s: string, reverse: boolean): FileLocation | null {
let lines = s.trimStart().split("\n");
if (reverse) lines = lines.reverse();

// FIXME: this doesn't support file name which contains ':', but captures white space
const re = /(?:CMake Error at )?(?<file>[^:]+):(?<line>\d+)(:(?<col>\d+))?\b/;
for (const line of lines) {
const m = line.trimStart().match(re);
if (m && m.groups) {
return new FileLocation(
m.groups.file as string,
parseInt(m.groups.line as string),
m.groups.col ? parseInt(m.groups.col as string) : 0
);
}
}
return null;
}
}

// loading
Expand Down