UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

77 lines 2.91 kB
import { linter } from "@codemirror/lint"; import { Parser } from "n3"; import { debouncedLinter } from "../debouncedLinter.js"; var parser = new Parser(); var EMPTY_RESOURCE = "<>"; var getError = function (message, view) { var lineMatch = message.match(/(?<=line )\d{1,}/); var valueMatch = message.match(/"([^"]*)"/); var lineNumber = lineMatch ? Number(lineMatch[0]) : 1; // the [1] index is used to get the caputre group var errorContent = valueMatch && valueMatch[1]; var line = view.state.doc.line(lineNumber); var position = line.text.search(errorContent !== null && errorContent !== void 0 ? errorContent : /\S/); var from = line.from + position; var errorLength = errorContent === null || errorContent === void 0 ? void 0 : errorContent.length; return { from: from, to: errorLength ? from + errorLength : line.to }; }; var getQuadError = function (view) { var lines = view.state.doc.toJSON(); for (var i = 0; i < lines.length; i += 1) { var input = lines[i].trim(); if (!input) { continue; } if (input.includes(EMPTY_RESOURCE)) { // i + 1 is used here because the codemirror uses 1-indexes var line = view.state.doc.line(i + 1); var position = line.text.search(EMPTY_RESOURCE); var from = line.from + position; return { from: from, to: from + EMPTY_RESOURCE.length, }; } } return { from: 0, to: view.state.doc.length }; }; var n3Linter = function (view) { var diagnostics = []; var value = view.state.doc.toString(); try { var quads = parser.parse(value); quads.forEach(function (quad) { if (!quad.subject || !quad.predicate || !quad.object) { var _a = getQuadError(view), from = _a.from, to = _a.to; view.dispatch({ scrollIntoView: true, }); diagnostics.push({ from: from, to: to, severity: "error", message: "Invalid RDF quad:\n\nsubject: ".concat(quad.subject, "\npredicate: ").concat(quad.predicate, "\nobject: ").concat(quad.object), }); } }); } catch (error) { var message = error.message; var _a = getError(message, view), from = _a.from, to = _a.to; view.dispatch({ scrollIntoView: true, }); diagnostics.push({ from: from, to: to, severity: "error", message: error.message, }); } return diagnostics; }; /** * Sets up the turtle linter. Documentation: https://codemirror.net/examples/lint/ */ export var turtleLinter = function () { return linter(debouncedLinter(n3Linter)); }; //# sourceMappingURL=turtleLinter.js.map