-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
420 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Diagnostic } from "@codemirror/lint"; | ||
import { EditorView } from "@codemirror/view"; | ||
import { debounce } from "lodash"; | ||
|
||
import { Linter } from "./types"; | ||
|
||
const DEBOUNCE_TIME = 500; | ||
|
||
export const debouncedLinter = (lintFunction: Linter, time = DEBOUNCE_TIME) => { | ||
const debouncedFn = debounce( | ||
( | ||
view: EditorView, | ||
resolve: (diagnostics: ReadonlyArray<Diagnostic> | Promise<ReadonlyArray<Diagnostic>>) => void | ||
) => { | ||
const diagnostics = lintFunction(view); | ||
resolve(diagnostics); | ||
}, | ||
time | ||
); | ||
|
||
return (view: EditorView) => { | ||
return new Promise<ReadonlyArray<Diagnostic>>((resolve) => { | ||
debouncedFn(view, resolve); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Diagnostic, linter } from "@codemirror/lint"; | ||
import { JSHINT as jshint } from "jshint"; | ||
|
||
import { ExtensionCreator } from "../types"; | ||
|
||
const lintOptions = { | ||
esversion: 11, | ||
browser: true, | ||
}; | ||
|
||
/** | ||
* Sets up the javascript linter. Documentation: https://codemirror.net/examples/lint/ | ||
*/ | ||
export const jsLinter: ExtensionCreator = () => { | ||
return linter((view) => { | ||
const diagnostics: Array<Diagnostic> = []; | ||
const codeText = view.state.doc.toJSON(); | ||
jshint(codeText, lintOptions); | ||
const errors = jshint?.data()?.errors; | ||
|
||
if (errors && errors.length > 0) { | ||
errors.forEach((error) => { | ||
const selectedLine = view.state.doc.line(error.line); | ||
|
||
const diagnostic: Diagnostic = { | ||
from: selectedLine.from, | ||
to: selectedLine.to, | ||
severity: "error", | ||
message: error.reason, | ||
}; | ||
|
||
diagnostics.push(diagnostic); | ||
}); | ||
} | ||
|
||
return diagnostics; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Diagnostic, linter } from "@codemirror/lint"; | ||
import { EditorView } from "@codemirror/view"; | ||
import { Parser } from "n3"; | ||
|
||
import { debouncedLinter } from "../debouncedLinter"; | ||
import { ExtensionCreator, Linter } from "../types"; | ||
|
||
const parser = new Parser(); | ||
|
||
const EMPTY_RESOURCE = "<>"; | ||
|
||
const getError = (message: string, view: EditorView) => { | ||
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 ?? /\S/); | ||
|
||
const from = line.from + position; | ||
const errorLength = errorContent?.length; | ||
|
||
return { from, to: errorLength ? from + errorLength : line.to }; | ||
}; | ||
|
||
const getQuadError = (view: EditorView) => { | ||
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: Linter = (view) => { | ||
const diagnostics: Array<Diagnostic> = []; | ||
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 as Error; | ||
|
||
const { from, to } = getError(message, view); | ||
|
||
view.dispatch({ | ||
scrollIntoView: true, | ||
}); | ||
|
||
diagnostics.push({ | ||
from, | ||
to, | ||
severity: "error", | ||
message: (error as Error).message, | ||
}); | ||
} | ||
|
||
return diagnostics; | ||
}; | ||
|
||
/** | ||
* Sets up the turtle linter. Documentation: https://codemirror.net/examples/lint/ | ||
*/ | ||
export const turtleLinter: ExtensionCreator = () => linter(debouncedLinter(n3Linter)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Diagnostic } from "@codemirror/lint"; | ||
import { Extension } from "@codemirror/state"; | ||
import { EditorView } from "@codemirror/view"; | ||
|
||
export type Linter = (view: EditorView) => ReadonlyArray<Diagnostic> | Promise<ReadonlyArray<Diagnostic>>; | ||
|
||
export enum EditorMode { | ||
Turtle = "turtle", | ||
SPARQL = "sparql", | ||
JavaScript = "javascript", | ||
} | ||
|
||
export type ExtensionCreator<T = unknown> = (options?: T) => Extension; |
Oops, something went wrong.