@actualwave/codemirror-lang-icu-messageformat 0.1.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/LICENSE +21 -0
- package/README.md +65 -0
- package/index.cjs +125 -0
- package/index.js +122 -0
- package/package.json +61 -0
- package/types/completion.d.ts +3 -0
- package/types/completion.d.ts.map +1 -0
- package/types/highlight.d.ts +2 -0
- package/types/highlight.d.ts.map +1 -0
- package/types/index.d.ts +4 -0
- package/types/index.d.ts.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2021 by Marijn Haverbeke <marijnh@gmail.com> and others
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# codemirror-lang-icu-messageformat
|
|
2
|
+
|
|
3
|
+
[ICU MessageFormat](https://unicode-org.github.io/icu/userguide/format_parse/messages/) syntax support for [CodeMirror 6](https://codemirror.net/).
|
|
4
|
+
|
|
5
|
+
ICU MessageFormat is the pattern syntax used for internationalized, pluralized, and gender/category-selected UI strings — widely used via `intl-messageformat`, `formatjs`, ICU4J/ICU4C, and many i18n libraries.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @actualwave/codemirror-lang-icu-messageformat
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { EditorState } from "@codemirror/state"
|
|
17
|
+
import { EditorView } from "@codemirror/view"
|
|
18
|
+
import { icuMessageFormat } from "@actualwave/codemirror-lang-icu-messageformat"
|
|
19
|
+
|
|
20
|
+
const state = EditorState.create({
|
|
21
|
+
doc: `You have {count, plural,
|
|
22
|
+
=0 {no messages}
|
|
23
|
+
one {# message}
|
|
24
|
+
other {# messages}
|
|
25
|
+
}.`,
|
|
26
|
+
extensions: [icuMessageFormat()],
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const view = new EditorView({ state, parent: document.body })
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
### `icuMessageFormat()`
|
|
35
|
+
|
|
36
|
+
Returns a `LanguageSupport` instance. This is the main extension to add to your editor.
|
|
37
|
+
|
|
38
|
+
### `icuMessageFormatLanguage`
|
|
39
|
+
|
|
40
|
+
The `LRLanguage` instance for ICU MessageFormat. Use this when you need direct access to the language object, for example to add language-specific extensions via `icuMessageFormatLanguage.data.of(...)`.
|
|
41
|
+
|
|
42
|
+
### `icuHighlighting`
|
|
43
|
+
|
|
44
|
+
The highlight style prop source used by the parser. Exported for advanced use cases where you want to build a custom language configuration.
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
- Literal message text, interleaved with `{argument}` placeholders
|
|
49
|
+
- Simple arguments: `{name, number}`, `{name, date, short}`, `{name, time, ::HH:mm}`, etc.
|
|
50
|
+
- Selector arguments with recursively nested sub-messages:
|
|
51
|
+
- `{name, plural, offset:1 =0 {...} one {...} other {...}}`
|
|
52
|
+
- `{name, selectordinal, one {...} two {...} few {...} other {...}}`
|
|
53
|
+
- `{name, select, male {...} female {...} other {...}}`
|
|
54
|
+
- The `#` placeholder inside plural/selectordinal branches
|
|
55
|
+
- Quoted literal text (`'{'`, `'#'`, etc.) and the `''` escaped-apostrophe sequence
|
|
56
|
+
- Positional (`{0}`) and named (`{name}`) argument references
|
|
57
|
+
|
|
58
|
+
## Known v1 limitations
|
|
59
|
+
|
|
60
|
+
- `#` is tokenized uniformly everywhere rather than only inside plural/selectordinal branches (it's only meaningful there per the ICU spec, but treating it context-free keeps the grammar simple — same spirit as this project's other DSL grammars).
|
|
61
|
+
- Argument `style` text (e.g. a number/date skeleton) is treated as an opaque run of text rather than parsed into its own sub-grammar.
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
package/index.cjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lr = require('@lezer/lr');
|
|
4
|
+
var highlight = require('@lezer/highlight');
|
|
5
|
+
var language = require('@codemirror/language');
|
|
6
|
+
var autocomplete = require('@codemirror/autocomplete');
|
|
7
|
+
|
|
8
|
+
const icuHighlighting = highlight.styleTags({
|
|
9
|
+
"Text": highlight.tags.content,
|
|
10
|
+
"PoundSign": highlight.tags.special(highlight.tags.number),
|
|
11
|
+
"EscapedApos": highlight.tags.escape,
|
|
12
|
+
"QuotedLiteral": highlight.tags.string,
|
|
13
|
+
"ArgName": highlight.tags.variableName,
|
|
14
|
+
"ArgType": highlight.tags.typeName,
|
|
15
|
+
"ArgStyle": highlight.tags.string,
|
|
16
|
+
"plural selectordinal select": highlight.tags.keyword,
|
|
17
|
+
"offset": highlight.tags.keyword,
|
|
18
|
+
"zero one two few many other": highlight.tags.atom,
|
|
19
|
+
"ExactValue": highlight.tags.number,
|
|
20
|
+
"number": highlight.tags.number,
|
|
21
|
+
"{ }": highlight.tags.brace,
|
|
22
|
+
"= : ,": highlight.tags.separator,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
26
|
+
const spec_identifier = {__proto__:null,plural:27, selectordinal:29, offset:33, zero:43, one:45, two:47, few:49, many:51, other:53, select:57, number:65, date:67, time:69, duration:71, spellout:73, ordinal:75};
|
|
27
|
+
const parser = lr.LRParser.deserialize({
|
|
28
|
+
version: 14,
|
|
29
|
+
states: "'zOhOPOOOoQQO'#CeOOOO'#DZ'#DZOOOO'#DT'#DTOwOPO'#C^QOOOOOOOQO'#Cf'#CfO!RQQO,59POOOO-E7R-E7RO!ZQQO'#D^OOOO1G.k1G.kO!xQQO1G.kO!}QQO'#CzOOQO,59x,59xO#VQQO'#ChO#[QQO'#CwOOQO'#C{'#C{OOOO7+$V7+$VO#aQSO,59fO#}QQO,59SO$UQQO,59cOOQO'#DS'#DSOOQO1G/Q1G/QO$aQQO'#CoO$fOQO'#CnOOQO'#DU'#DUO$nQQO1G.nO$uQQO'#CkOOOO'#D_'#D_O#fQQO1G.nO$zOQO'#CyOOQO'#DV'#DVO%SQQO1G.}OOOO,59Z,59ZO%bOPO,59YO%iOPO,59YOOQO-E7S-E7SO%nQQO,59VO%sQQO7+$YO%bOPO,59eO%zOPO,59eOOQO-E7T-E7TO&POPO1G.tO%bOPO1G.tOOQO1G.q1G.qO&UOPO1G/PO%bOPO1G/POOQO7+$`7+$`O&ZOPO7+$`OOQO7+$k7+$kO&`OPO7+$kOOQO<<Gz<<GzOOQO<<HV<<HV",
|
|
30
|
+
stateData: "&h~O|OS~ORQOSQOTQOUQOWPO~OzQP~PVO!OUO!PUO~OzQXVQX~PVOVYOZXO~O]^O^^Ol_Op`Oq`Or`Os`Ot`Ou`O~OVaO~OZbOVnX~OZcO~OZdO~O!SeO~OdgOelOflOglOhlOilOjlO~O`kO~P#fOdgOjnO!PnO~O!OqO~OWrO|sO~OV[i~P#fOauO~OWwO|xO~OdgOjnO!PnOVki~OVQP~PVOW{O~O!O|O~OV[q~P#fOW!OO~OV!PO~OV!RO~OV!TO~OV!UO~O|!S~",
|
|
31
|
+
goto: "$^!SPP!TPPPPPP!d!lP!oPP!rPP!u!{PPPPPPP!oP#V!o#ZPPPPPP#^#a#k#uPPP#{PP$T$WQTOQzrQ}wQ!Q{R!S!O]QOSrw{!ORVPR]XRmcXicjmvWhcjmvTndpTodpR[XRfbYSOrw{!ORWSQjcStjvRvmQpdRyp]ROSrw{!ORZVXhcjmv",
|
|
32
|
+
nodeNames: "⚠ Program Message Text PoundSign EscapedApos QuotedLiteral } { Argument ArgName , PluralArg plural selectordinal OffsetClause offset : PluralCase ExactValue = zero one two few many other SelectArg select SelectCase SimpleArg ArgType number date time duration spellout ordinal ArgStyle",
|
|
33
|
+
maxTerm: 50,
|
|
34
|
+
nodeProps: [
|
|
35
|
+
["openedBy", 7,"{"],
|
|
36
|
+
["closedBy", 8,"}"]
|
|
37
|
+
],
|
|
38
|
+
propSources: [icuHighlighting],
|
|
39
|
+
skippedNodes: [0],
|
|
40
|
+
repeatNodeCount: 3,
|
|
41
|
+
tokenData: "._~RnOX#PXY$eYZ$eZ]#P]^$e^p#Ppq$eqs#Pst%ptw#Pwx&Wx|#P|})l}!Q#P!Q![*b![!]+^!]!_#P!_!`,S!`!c#P!c!},x!}#R#P#R#S,x#S#T#P#T#o,x#o#p.T#p#q#P#q#r.Y#r;'S#P;'S;=`$_<%lO#PT#WXRP!SSOs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PS#xT!SSO#o#s#p#q#s#r;'S#s;'S;=`$X<%lO#sS$[P;=`<%l#sT$bP;=`<%l#PV$n`RP|U!SSOX#PXY$eYZ$eZ]#P]^$e^p#Ppq$eqs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PT%wTSP!SSO#o#s#p#q#s#r;'S#s;'S;=`$X<%lO#sT&]X!SSOw&xwx)Ox#o&x#o#p(W#p#q&x#q#r(W#r;'S&x;'S;=`(x<%lO&xT&}X!SSOw&xwx'jx#o&x#o#p(W#p#q&x#q#r(W#r;'S&x;'S;=`(x<%lO&xT'qVUP!SSOw#swx&xx#o#s#p#q#s#r;'S#s;'S;=`$X<%lO#sP(ZTOw(Wwx(jx;'S(W;'S;=`(r<%lO(WP(oPUPwx(WP(uP;=`<%l(WT({P;=`<%l&xT)VVTP!SSOw#swx&xx#o#s#p#q#s#r;'S#s;'S;=`$X<%lO#sV)uXZQRP!SSOs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PV*kZRP!OQ!SSOs#Pst#stw#Pwx#sx!Q#P!Q![*b![#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PV+gXaQRP!SSOs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PV,]XdQRP!SSOs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PV-R`RP!PQ!SSOs#Pst#stw#Pwx#sx!Q#P!Q![,x![!c#P!c!},x!}#R#P#R#S,x#S#T#P#T#o,x#p#q#P#r;'S#P;'S;=`$_<%lO#P~.YOW~~._OV~",
|
|
42
|
+
tokenizers: [0, 1, 2],
|
|
43
|
+
topRules: {"Program":[0,1]},
|
|
44
|
+
specialized: [{term: 47, get: (value) => spec_identifier[value] || -1}],
|
|
45
|
+
tokenPrec: 251
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// ICU MessageFormat completion is context-sensitive rather than a flat
|
|
49
|
+
// keyword list: `plural`/`select`/`other`/etc are only valid at specific
|
|
50
|
+
// grammar positions (see syntax.grammar's `kw<term>` comment — they share
|
|
51
|
+
// the open `identifier` namespace, so what's offered depends on where the
|
|
52
|
+
// cursor actually is).
|
|
53
|
+
const pluralCategoryComplete = autocomplete.completeFromList([
|
|
54
|
+
{ label: "zero", type: "keyword" },
|
|
55
|
+
{ label: "one", type: "keyword" },
|
|
56
|
+
{ label: "two", type: "keyword" },
|
|
57
|
+
{ label: "few", type: "keyword" },
|
|
58
|
+
{ label: "many", type: "keyword" },
|
|
59
|
+
{ label: "other", type: "keyword" },
|
|
60
|
+
]);
|
|
61
|
+
const selectCategoryComplete = autocomplete.completeFromList([
|
|
62
|
+
{ label: "other", type: "keyword" },
|
|
63
|
+
]);
|
|
64
|
+
const argTypeComplete = autocomplete.completeFromList([
|
|
65
|
+
{ label: "plural", type: "keyword" },
|
|
66
|
+
{ label: "selectordinal", type: "keyword" },
|
|
67
|
+
{ label: "select", type: "keyword" },
|
|
68
|
+
{ label: "number", type: "keyword" },
|
|
69
|
+
{ label: "date", type: "keyword" },
|
|
70
|
+
{ label: "time", type: "keyword" },
|
|
71
|
+
{ label: "duration", type: "keyword" },
|
|
72
|
+
{ label: "spellout", type: "keyword" },
|
|
73
|
+
{ label: "ordinal", type: "keyword" },
|
|
74
|
+
]);
|
|
75
|
+
const messageSnippetComplete = autocomplete.completeFromList([
|
|
76
|
+
autocomplete.snippetCompletion("{${name}, plural, one {# item} other {# items}}", { label: "plural", type: "keyword", detail: "plural argument" }),
|
|
77
|
+
autocomplete.snippetCompletion("{${name}, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}", { label: "selectordinal", type: "keyword", detail: "ordinal argument" }),
|
|
78
|
+
autocomplete.snippetCompletion("{${name}, select, ${key} {${value}} other {${other}}}", { label: "select", type: "keyword", detail: "select argument" }),
|
|
79
|
+
]);
|
|
80
|
+
const icuCompletionSource = (context) => {
|
|
81
|
+
const node = language.syntaxTree(context.state).resolveInner(context.pos, -1);
|
|
82
|
+
for (let n = node; n; n = n.parent) {
|
|
83
|
+
switch (n.name) {
|
|
84
|
+
// Already inside a case branch's own message text — its content is
|
|
85
|
+
// plain text (or nested arguments), not a selector keyword position.
|
|
86
|
+
case "PluralCase":
|
|
87
|
+
case "SelectCase":
|
|
88
|
+
return null;
|
|
89
|
+
case "PluralArg":
|
|
90
|
+
return pluralCategoryComplete(context);
|
|
91
|
+
case "SelectArg":
|
|
92
|
+
return selectCategoryComplete(context);
|
|
93
|
+
// Still naming the argument (`{na|`) — nothing to suggest until past
|
|
94
|
+
// the comma that follows ArgName.
|
|
95
|
+
case "ArgName":
|
|
96
|
+
return null;
|
|
97
|
+
case "Argument":
|
|
98
|
+
return n.getChild("ArgName") ? argTypeComplete(context) : null;
|
|
99
|
+
case "Message":
|
|
100
|
+
return messageSnippetComplete(context);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const icuMessageFormatLanguage = language.LRLanguage.define({
|
|
107
|
+
parser: parser.configure({
|
|
108
|
+
props: [
|
|
109
|
+
language.indentNodeProp.add({
|
|
110
|
+
Argument: language.delimitedIndent({ closing: "}", align: false }),
|
|
111
|
+
}),
|
|
112
|
+
language.foldNodeProp.add({
|
|
113
|
+
PluralCase: language.foldInside,
|
|
114
|
+
SelectCase: language.foldInside,
|
|
115
|
+
}),
|
|
116
|
+
]
|
|
117
|
+
}),
|
|
118
|
+
languageData: {}
|
|
119
|
+
});
|
|
120
|
+
function icuMessageFormat() {
|
|
121
|
+
return new language.LanguageSupport(icuMessageFormatLanguage, icuMessageFormatLanguage.data.of({ autocomplete: icuCompletionSource }));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
exports.icuMessageFormat = icuMessageFormat;
|
|
125
|
+
exports.icuMessageFormatLanguage = icuMessageFormatLanguage;
|
package/index.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { LRParser } from '@lezer/lr';
|
|
2
|
+
import { styleTags, tags } from '@lezer/highlight';
|
|
3
|
+
import { syntaxTree, LRLanguage, indentNodeProp, delimitedIndent, foldNodeProp, foldInside, LanguageSupport } from '@codemirror/language';
|
|
4
|
+
import { completeFromList, snippetCompletion } from '@codemirror/autocomplete';
|
|
5
|
+
|
|
6
|
+
const icuHighlighting = styleTags({
|
|
7
|
+
"Text": tags.content,
|
|
8
|
+
"PoundSign": tags.special(tags.number),
|
|
9
|
+
"EscapedApos": tags.escape,
|
|
10
|
+
"QuotedLiteral": tags.string,
|
|
11
|
+
"ArgName": tags.variableName,
|
|
12
|
+
"ArgType": tags.typeName,
|
|
13
|
+
"ArgStyle": tags.string,
|
|
14
|
+
"plural selectordinal select": tags.keyword,
|
|
15
|
+
"offset": tags.keyword,
|
|
16
|
+
"zero one two few many other": tags.atom,
|
|
17
|
+
"ExactValue": tags.number,
|
|
18
|
+
"number": tags.number,
|
|
19
|
+
"{ }": tags.brace,
|
|
20
|
+
"= : ,": tags.separator,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
24
|
+
const spec_identifier = {__proto__:null,plural:27, selectordinal:29, offset:33, zero:43, one:45, two:47, few:49, many:51, other:53, select:57, number:65, date:67, time:69, duration:71, spellout:73, ordinal:75};
|
|
25
|
+
const parser = LRParser.deserialize({
|
|
26
|
+
version: 14,
|
|
27
|
+
states: "'zOhOPOOOoQQO'#CeOOOO'#DZ'#DZOOOO'#DT'#DTOwOPO'#C^QOOOOOOOQO'#Cf'#CfO!RQQO,59POOOO-E7R-E7RO!ZQQO'#D^OOOO1G.k1G.kO!xQQO1G.kO!}QQO'#CzOOQO,59x,59xO#VQQO'#ChO#[QQO'#CwOOQO'#C{'#C{OOOO7+$V7+$VO#aQSO,59fO#}QQO,59SO$UQQO,59cOOQO'#DS'#DSOOQO1G/Q1G/QO$aQQO'#CoO$fOQO'#CnOOQO'#DU'#DUO$nQQO1G.nO$uQQO'#CkOOOO'#D_'#D_O#fQQO1G.nO$zOQO'#CyOOQO'#DV'#DVO%SQQO1G.}OOOO,59Z,59ZO%bOPO,59YO%iOPO,59YOOQO-E7S-E7SO%nQQO,59VO%sQQO7+$YO%bOPO,59eO%zOPO,59eOOQO-E7T-E7TO&POPO1G.tO%bOPO1G.tOOQO1G.q1G.qO&UOPO1G/PO%bOPO1G/POOQO7+$`7+$`O&ZOPO7+$`OOQO7+$k7+$kO&`OPO7+$kOOQO<<Gz<<GzOOQO<<HV<<HV",
|
|
28
|
+
stateData: "&h~O|OS~ORQOSQOTQOUQOWPO~OzQP~PVO!OUO!PUO~OzQXVQX~PVOVYOZXO~O]^O^^Ol_Op`Oq`Or`Os`Ot`Ou`O~OVaO~OZbOVnX~OZcO~OZdO~O!SeO~OdgOelOflOglOhlOilOjlO~O`kO~P#fOdgOjnO!PnO~O!OqO~OWrO|sO~OV[i~P#fOauO~OWwO|xO~OdgOjnO!PnOVki~OVQP~PVOW{O~O!O|O~OV[q~P#fOW!OO~OV!PO~OV!RO~OV!TO~OV!UO~O|!S~",
|
|
29
|
+
goto: "$^!SPP!TPPPPPP!d!lP!oPP!rPP!u!{PPPPPPP!oP#V!o#ZPPPPPP#^#a#k#uPPP#{PP$T$WQTOQzrQ}wQ!Q{R!S!O]QOSrw{!ORVPR]XRmcXicjmvWhcjmvTndpTodpR[XRfbYSOrw{!ORWSQjcStjvRvmQpdRyp]ROSrw{!ORZVXhcjmv",
|
|
30
|
+
nodeNames: "⚠ Program Message Text PoundSign EscapedApos QuotedLiteral } { Argument ArgName , PluralArg plural selectordinal OffsetClause offset : PluralCase ExactValue = zero one two few many other SelectArg select SelectCase SimpleArg ArgType number date time duration spellout ordinal ArgStyle",
|
|
31
|
+
maxTerm: 50,
|
|
32
|
+
nodeProps: [
|
|
33
|
+
["openedBy", 7,"{"],
|
|
34
|
+
["closedBy", 8,"}"]
|
|
35
|
+
],
|
|
36
|
+
propSources: [icuHighlighting],
|
|
37
|
+
skippedNodes: [0],
|
|
38
|
+
repeatNodeCount: 3,
|
|
39
|
+
tokenData: "._~RnOX#PXY$eYZ$eZ]#P]^$e^p#Ppq$eqs#Pst%ptw#Pwx&Wx|#P|})l}!Q#P!Q![*b![!]+^!]!_#P!_!`,S!`!c#P!c!},x!}#R#P#R#S,x#S#T#P#T#o,x#o#p.T#p#q#P#q#r.Y#r;'S#P;'S;=`$_<%lO#PT#WXRP!SSOs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PS#xT!SSO#o#s#p#q#s#r;'S#s;'S;=`$X<%lO#sS$[P;=`<%l#sT$bP;=`<%l#PV$n`RP|U!SSOX#PXY$eYZ$eZ]#P]^$e^p#Ppq$eqs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PT%wTSP!SSO#o#s#p#q#s#r;'S#s;'S;=`$X<%lO#sT&]X!SSOw&xwx)Ox#o&x#o#p(W#p#q&x#q#r(W#r;'S&x;'S;=`(x<%lO&xT&}X!SSOw&xwx'jx#o&x#o#p(W#p#q&x#q#r(W#r;'S&x;'S;=`(x<%lO&xT'qVUP!SSOw#swx&xx#o#s#p#q#s#r;'S#s;'S;=`$X<%lO#sP(ZTOw(Wwx(jx;'S(W;'S;=`(r<%lO(WP(oPUPwx(WP(uP;=`<%l(WT({P;=`<%l&xT)VVTP!SSOw#swx&xx#o#s#p#q#s#r;'S#s;'S;=`$X<%lO#sV)uXZQRP!SSOs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PV*kZRP!OQ!SSOs#Pst#stw#Pwx#sx!Q#P!Q![*b![#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PV+gXaQRP!SSOs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PV,]XdQRP!SSOs#Pst#stw#Pwx#sx#o#P#p#q#P#r;'S#P;'S;=`$_<%lO#PV-R`RP!PQ!SSOs#Pst#stw#Pwx#sx!Q#P!Q![,x![!c#P!c!},x!}#R#P#R#S,x#S#T#P#T#o,x#p#q#P#r;'S#P;'S;=`$_<%lO#P~.YOW~~._OV~",
|
|
40
|
+
tokenizers: [0, 1, 2],
|
|
41
|
+
topRules: {"Program":[0,1]},
|
|
42
|
+
specialized: [{term: 47, get: (value) => spec_identifier[value] || -1}],
|
|
43
|
+
tokenPrec: 251
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// ICU MessageFormat completion is context-sensitive rather than a flat
|
|
47
|
+
// keyword list: `plural`/`select`/`other`/etc are only valid at specific
|
|
48
|
+
// grammar positions (see syntax.grammar's `kw<term>` comment — they share
|
|
49
|
+
// the open `identifier` namespace, so what's offered depends on where the
|
|
50
|
+
// cursor actually is).
|
|
51
|
+
const pluralCategoryComplete = completeFromList([
|
|
52
|
+
{ label: "zero", type: "keyword" },
|
|
53
|
+
{ label: "one", type: "keyword" },
|
|
54
|
+
{ label: "two", type: "keyword" },
|
|
55
|
+
{ label: "few", type: "keyword" },
|
|
56
|
+
{ label: "many", type: "keyword" },
|
|
57
|
+
{ label: "other", type: "keyword" },
|
|
58
|
+
]);
|
|
59
|
+
const selectCategoryComplete = completeFromList([
|
|
60
|
+
{ label: "other", type: "keyword" },
|
|
61
|
+
]);
|
|
62
|
+
const argTypeComplete = completeFromList([
|
|
63
|
+
{ label: "plural", type: "keyword" },
|
|
64
|
+
{ label: "selectordinal", type: "keyword" },
|
|
65
|
+
{ label: "select", type: "keyword" },
|
|
66
|
+
{ label: "number", type: "keyword" },
|
|
67
|
+
{ label: "date", type: "keyword" },
|
|
68
|
+
{ label: "time", type: "keyword" },
|
|
69
|
+
{ label: "duration", type: "keyword" },
|
|
70
|
+
{ label: "spellout", type: "keyword" },
|
|
71
|
+
{ label: "ordinal", type: "keyword" },
|
|
72
|
+
]);
|
|
73
|
+
const messageSnippetComplete = completeFromList([
|
|
74
|
+
snippetCompletion("{${name}, plural, one {# item} other {# items}}", { label: "plural", type: "keyword", detail: "plural argument" }),
|
|
75
|
+
snippetCompletion("{${name}, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}", { label: "selectordinal", type: "keyword", detail: "ordinal argument" }),
|
|
76
|
+
snippetCompletion("{${name}, select, ${key} {${value}} other {${other}}}", { label: "select", type: "keyword", detail: "select argument" }),
|
|
77
|
+
]);
|
|
78
|
+
const icuCompletionSource = (context) => {
|
|
79
|
+
const node = syntaxTree(context.state).resolveInner(context.pos, -1);
|
|
80
|
+
for (let n = node; n; n = n.parent) {
|
|
81
|
+
switch (n.name) {
|
|
82
|
+
// Already inside a case branch's own message text — its content is
|
|
83
|
+
// plain text (or nested arguments), not a selector keyword position.
|
|
84
|
+
case "PluralCase":
|
|
85
|
+
case "SelectCase":
|
|
86
|
+
return null;
|
|
87
|
+
case "PluralArg":
|
|
88
|
+
return pluralCategoryComplete(context);
|
|
89
|
+
case "SelectArg":
|
|
90
|
+
return selectCategoryComplete(context);
|
|
91
|
+
// Still naming the argument (`{na|`) — nothing to suggest until past
|
|
92
|
+
// the comma that follows ArgName.
|
|
93
|
+
case "ArgName":
|
|
94
|
+
return null;
|
|
95
|
+
case "Argument":
|
|
96
|
+
return n.getChild("ArgName") ? argTypeComplete(context) : null;
|
|
97
|
+
case "Message":
|
|
98
|
+
return messageSnippetComplete(context);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const icuMessageFormatLanguage = LRLanguage.define({
|
|
105
|
+
parser: parser.configure({
|
|
106
|
+
props: [
|
|
107
|
+
indentNodeProp.add({
|
|
108
|
+
Argument: delimitedIndent({ closing: "}", align: false }),
|
|
109
|
+
}),
|
|
110
|
+
foldNodeProp.add({
|
|
111
|
+
PluralCase: foldInside,
|
|
112
|
+
SelectCase: foldInside,
|
|
113
|
+
}),
|
|
114
|
+
]
|
|
115
|
+
}),
|
|
116
|
+
languageData: {}
|
|
117
|
+
});
|
|
118
|
+
function icuMessageFormat() {
|
|
119
|
+
return new LanguageSupport(icuMessageFormatLanguage, icuMessageFormatLanguage.data.of({ autocomplete: icuCompletionSource }));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export { icuMessageFormat, icuMessageFormatLanguage };
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@actualwave/codemirror-lang-icu-messageformat",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "ICU MessageFormat syntax support for CodeMirror 6",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"codemirror",
|
|
7
|
+
"codemirror-language",
|
|
8
|
+
"icu",
|
|
9
|
+
"messageformat",
|
|
10
|
+
"i18n",
|
|
11
|
+
"l10n",
|
|
12
|
+
"syntax-highlighting",
|
|
13
|
+
"lezer"
|
|
14
|
+
],
|
|
15
|
+
"author": "Oleg Galaburda <burdiuz@gmail.com>",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/burdiuz/codemorror-lang-modules.git",
|
|
19
|
+
"directory": "packages/icu-messageformat"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/burdiuz/codemorror-lang-modules/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/burdiuz/codemorror-lang-modules/tree/main/packages/icu-messageformat#readme",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "mocha test/test.js",
|
|
27
|
+
"build": "rollup -c"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"directory": "dist"
|
|
31
|
+
},
|
|
32
|
+
"type": "module",
|
|
33
|
+
"main": "index.cjs",
|
|
34
|
+
"module": "index.js",
|
|
35
|
+
"exports": {
|
|
36
|
+
"types": "./types/index.d.ts",
|
|
37
|
+
"import": "./index.js",
|
|
38
|
+
"require": "./index.cjs"
|
|
39
|
+
},
|
|
40
|
+
"types": "types/index.d.ts",
|
|
41
|
+
"sideEffects": false,
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@codemirror/autocomplete": "^6.0.0",
|
|
44
|
+
"@codemirror/language": "^6.0.0",
|
|
45
|
+
"@lezer/highlight": "^1.0.0",
|
|
46
|
+
"@lezer/lr": "^1.0.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@codemirror/autocomplete": "^6.20.3",
|
|
50
|
+
"@lezer/common": "^1.5.3",
|
|
51
|
+
"@lezer/generator": "^1.0.0",
|
|
52
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
53
|
+
"@types/node": "^24.1.0",
|
|
54
|
+
"mocha": "^11.7.1",
|
|
55
|
+
"rollup": "^4.45.1",
|
|
56
|
+
"rollup-plugin-copy": "^3.5.0",
|
|
57
|
+
"tslib": "^2.8.1",
|
|
58
|
+
"typescript": "^5.8.3"
|
|
59
|
+
},
|
|
60
|
+
"license": "MIT"
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"completion.d.ts","sourceRoot":"","sources":["../../src/completion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,gBAAgB,EAA8C,MAAM,0BAA0B,CAAA;AAyCzH,eAAO,MAAM,mBAAmB,EAAE,gBAwBjC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlight.d.ts","sourceRoot":"","sources":["../../src/highlight.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,eAAe,wCAmB1B,CAAA"}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,UAAU,EAAE,eAAe,EAA4D,MAAM,sBAAsB,CAAA;AAG3H,eAAO,MAAM,wBAAwB,YAanC,CAAA;AAEF,wBAAgB,gBAAgB,oBAE/B"}
|