@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
81 lines • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.turtleLinter = void 0;
const lint_1 = require("@codemirror/lint");
const n3_1 = require("n3");
const debouncedLinter_1 = require("../debouncedLinter");
const parser = new n3_1.Parser();
const EMPTY_RESOURCE = "<>";
const getError = (message, view) => {
const lineMatch = message.match(/(?<=line )\d{1,}/);
const valueMatch = message.match(/"([^"]*)"/);
const lineNumber = lineMatch ? Number(lineMatch[0]) : 1;
// the [1] index is used to get the caputre group
const errorContent = valueMatch && valueMatch[1];
const line = view.state.doc.line(lineNumber);
const position = line.text.search(errorContent !== null && errorContent !== void 0 ? errorContent : /\S/);
const from = line.from + position;
const errorLength = errorContent === null || errorContent === void 0 ? void 0 : errorContent.length;
return { from, to: errorLength ? from + errorLength : line.to };
};
const getQuadError = (view) => {
const lines = view.state.doc.toJSON();
for (let i = 0; i < lines.length; i += 1) {
const input = lines[i].trim();
if (!input) {
continue;
}
if (input.includes(EMPTY_RESOURCE)) {
// i + 1 is used here because the codemirror uses 1-indexes
const line = view.state.doc.line(i + 1);
const position = line.text.search(EMPTY_RESOURCE);
const from = line.from + position;
return {
from,
to: from + EMPTY_RESOURCE.length,
};
}
}
return { from: 0, to: view.state.doc.length };
};
const n3Linter = (view) => {
const diagnostics = [];
const value = view.state.doc.toString();
try {
const quads = parser.parse(value);
quads.forEach((quad) => {
if (!quad.subject || !quad.predicate || !quad.object) {
const { from, to } = getQuadError(view);
view.dispatch({
scrollIntoView: true,
});
diagnostics.push({
from,
to,
severity: "error",
message: `Invalid RDF quad:\n\nsubject: ${quad.subject}\npredicate: ${quad.predicate}\nobject: ${quad.object}`,
});
}
});
}
catch (error) {
const { message } = error;
const { from, to } = getError(message, view);
view.dispatch({
scrollIntoView: true,
});
diagnostics.push({
from,
to,
severity: "error",
message: error.message,
});
}
return diagnostics;
};
/**
* Sets up the turtle linter. Documentation: https://codemirror.net/examples/lint/
*/
const turtleLinter = () => (0, lint_1.linter)((0, debouncedLinter_1.debouncedLinter)(n3Linter));
exports.turtleLinter = turtleLinter;
//# sourceMappingURL=turtleLinter.js.map