Skip to content

Commit 509ea10

Browse files
committed
Add a usage section to readme
1 parent 6d3fb93 commit 509ea10

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,51 @@ We aim to be an inclusive, welcoming community. To make that explicit,
1616
we have a [code of
1717
conduct](http://contributor-covenant.org/version/1/1/0/) that applies
1818
to communication around the project.
19+
20+
## Usage
21+
22+
Setting up a language from a [Lezer](https://lezer.codemirror.net)
23+
parser looks like this:
24+
25+
```javascript
26+
import {parser} from "@lezer/json"
27+
import {LRLanguage, continuedIndent, indentNodeProp,
28+
foldNodeProp, foldInside} from "@codemirror/language"
29+
30+
export const jsonLanguage = LRLanguage.define({
31+
name: "json",
32+
parser: parser.configure({
33+
props: [
34+
indentNodeProp.add({
35+
Object: continuedIndent({except: /^\s*\}/}),
36+
Array: continuedIndent({except: /^\s*\]/})
37+
}),
38+
foldNodeProp.add({
39+
"Object Array": foldInside
40+
})
41+
]
42+
}),
43+
languageData: {
44+
closeBrackets: {brackets: ["[", "{", '"']},
45+
indentOnInput: /^\s*[\}\]]$/
46+
}
47+
})
48+
```
49+
50+
Often, you'll also use this package just to access some specific
51+
language-related features, such as accessing the editor's syntax
52+
tree...
53+
54+
```javascript
55+
import {syntaxTree} from "@codemirror/language"
56+
57+
const tree = syntaxTree(view)
58+
```
59+
60+
... or computing the appriate indentation at a given point.
61+
62+
```javascript
63+
import {getIndentation} from "@codemirror/language"
64+
65+
console.log(getIndentation(view.state, view.state.selection.main.head))
66+
```

0 commit comments

Comments
 (0)