@kurrawongai/codemirror-lang-turtle12 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1,124 @@
1
+ import type { SyntaxNode } from '@lezer/common';
2
+ import type { EditorState } from '@codemirror/state';
3
+ import {
4
+ LRLanguage,
5
+ LanguageSupport,
6
+ delimitedIndent,
7
+ foldInside,
8
+ foldNodeProp,
9
+ indentNodeProp,
10
+ syntaxTree,
11
+ } from '@codemirror/language';
12
+ import type { LRParser } from '@lezer/lr';
13
+ import { parser as rawParser } from './turtle.grammar';
14
+ import { turtleHighlighting } from './highlight';
15
+ import { turtleCompletionSource } from './complete';
16
+ import type { CompletionOptions } from './complete';
17
+
18
+ export type { CompletionOptions, PrefixMap, PrefixSource } from './complete';
19
+ export { documentPrefixes, knownPrefixes, turtleCompletionSource } from './complete';
20
+ export { turtleHighlighting } from './highlight';
21
+
22
+ /** The unconfigured grammar, for callers that want their own `top` or props. */
23
+ export const parser: LRParser = rawParser;
24
+
25
+ /**
26
+ * Fold a whole statement from the end of its first line.
27
+ *
28
+ * This is the one fold in Turtle with no brackets around it: a subject with a
29
+ * long `;` list is the common shape a reader wants collapsed, and the structure
30
+ * that shape has is a statement, not a delimiter.
31
+ *
32
+ * It stands aside when a bracket opens on the same line. Folding
33
+ * `ex:s ex:p [` at the statement level would hide the `] .` as well, leaving
34
+ * what is left looking unterminated; the bracket's own fold — which CodeMirror
35
+ * would otherwise never reach, since the statement node encloses it — reads
36
+ * correctly.
37
+ */
38
+ const statementFold = (node: SyntaxNode, state: EditorState) => {
39
+ const firstLineEnd = state.doc.lineAt(node.from).to;
40
+ if (node.to <= firstLineEnd) return null;
41
+ for (let n: SyntaxNode | null = syntaxTree(state).resolveInner(firstLineEnd, -1); n; n = n.parent) {
42
+ if (n.from < node.from) break;
43
+ if (n.type.prop(foldNodeProp) && n.from <= firstLineEnd && n.to > firstLineEnd && n.type !== node.type) return null;
44
+ }
45
+ return { from: firstLineEnd, to: node.to };
46
+ };
47
+
48
+ /**
49
+ * Highlighting, folding and indentation, shared by all four dialects.
50
+ *
51
+ * Exported so a consumer can re-configure the parser with props of their own —
52
+ * extra `styleTags`, say, to tag the three RDF 1.2 constructs' regions apart
53
+ * rather than all as `quote`. `codemirror-lang-sparql12` exports `sparqlProps`
54
+ * for the same reason.
55
+ */
56
+ export const turtleProps = [
57
+ turtleHighlighting,
58
+ indentNodeProp.add({
59
+ BlankNodePropertyList: delimitedIndent({ closing: ']' }),
60
+ Collection: delimitedIndent({ closing: ')' }),
61
+ WrappedGraph: delimitedIndent({ closing: '}' }),
62
+ AnnotationBlock: delimitedIndent({ closing: '|}' }),
63
+ TripleTerm: delimitedIndent({ closing: ')>>' }),
64
+ ReifiedTriple: delimitedIndent({ closing: '>>' }),
65
+ }),
66
+ foldNodeProp.add({
67
+ BlankNodePropertyList: foldInside,
68
+ Collection: foldInside,
69
+ WrappedGraph: foldInside,
70
+ AnnotationBlock: foldInside,
71
+ 'Triples Triples2 TriplesOrGraph': statementFold,
72
+ }),
73
+ ];
74
+
75
+ const languageData = {
76
+ commentTokens: { line: '#' },
77
+ closeBrackets: { brackets: ['[', '(', '<', '"', "'"] },
78
+ indentOnInput: /^\s*[\]})]$/,
79
+ };
80
+
81
+ function define(top: string) {
82
+ return LRLanguage.define({
83
+ name: top === 'TurtleDoc' ? 'turtle' : top === 'TrigDoc' ? 'trig' : top === 'NTriplesDoc' ? 'ntriples' : 'nquads',
84
+ parser: rawParser.configure({ top, props: turtleProps }),
85
+ languageData,
86
+ });
87
+ }
88
+
89
+ export const turtleLanguage = define('TurtleDoc');
90
+ export const trigLanguage = define('TrigDoc');
91
+ export const ntriplesLanguage = define('NTriplesDoc');
92
+ export const nquadsLanguage = define('NQuadsDoc');
93
+
94
+ function support(language: LRLanguage, options: CompletionOptions, dialect: 'turtle' | 'trig') {
95
+ return new LanguageSupport(language, [
96
+ language.data.of({ autocomplete: turtleCompletionSource(options, dialect) }),
97
+ ]);
98
+ }
99
+
100
+ /** RDF 1.2 Turtle. */
101
+ export function turtle(options: CompletionOptions = {}): LanguageSupport {
102
+ return support(turtleLanguage, options, 'turtle');
103
+ }
104
+
105
+ /** RDF 1.2 TriG. */
106
+ export function trig(options: CompletionOptions = {}): LanguageSupport {
107
+ return support(trigLanguage, options, 'trig');
108
+ }
109
+
110
+ /**
111
+ * RDF 1.2 N-Triples.
112
+ *
113
+ * A strict subset with its own entry point rather than Turtle in a lenient
114
+ * mood: a prefixed name or a `;` list in an N-Triples document is an error, and
115
+ * an editor that colours it as valid is lying about the file it will write.
116
+ */
117
+ export function ntriples(): LanguageSupport {
118
+ return new LanguageSupport(ntriplesLanguage);
119
+ }
120
+
121
+ /** RDF 1.2 N-Quads. */
122
+ export function nquads(): LanguageSupport {
123
+ return new LanguageSupport(nquadsLanguage);
124
+ }
Binary file