Skip to content

Commit 93c52f8

Browse files
authored
Merge pull request #13 from codewars/use-unicode-helper
Rewrite using codemirror-unicode-helper
2 parents 285a4fc + e52d382 commit 93c52f8

File tree

12 files changed

+205
-92
lines changed

12 files changed

+205
-92
lines changed

.github/workflows/publish_demo.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@ on:
55
- master
66
jobs:
77
deploy:
8-
runs-on: ubuntu-18.04
8+
runs-on: ubuntu-latest
99
steps:
1010
- uses: actions/checkout@v2
11-
- name: Setup Node
12-
uses: actions/setup-node@v1
11+
- uses: actions/setup-node@v1
1312
with:
14-
node-version: '10.x'
15-
- name: Install build deps
16-
run: npm ci
17-
- name: Prepare docs/
18-
run: cp lib/index.js docs/codemirror-agda.js
19-
- name: Deploy demo
20-
uses: peaceiris/actions-gh-pages@v3
13+
node-version: "12.x"
14+
- run: npm ci
15+
- run: cp umd/*.js docs/
16+
- uses: peaceiris/actions-gh-pages@v3
2117
with:
2218
github_token: ${{ secrets.GITHUB_TOKEN }}
2319
publish_dir: ./docs

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules/
22
lib/
3+
umd/
4+
docs/*.js

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
11
# codemirror-agda
22

3-
Agda mode and input helper for CodeMirror
3+
CodeMirror extension for editing Agda.
44

5+
Provides a function `defineMode` to define `agda` mode, and an array `UNICODE_HELPER_PAIRS`.
6+
7+
- `defineMode(CodeMirror)`: Define `agda` mode and MIME `text/x-agda`.
8+
- `UNICODE_HELPER_PAIRS` (`[seq: string, sym: string][]`): For entering Unicode characters with ASCII sequences using [@codewars/codemirror-unicode-helper](https://github.com/codewars/codemirror-unicode-helper). A subset of `agda-input-translations` in `agda-input.el`.
9+
10+
See <https://codewars.github.io/codemirror-agda/>.
11+
12+
## Usage
13+
14+
```javascript
15+
import CodeMirror from "codemirror";
16+
import "codemirror/addon/mode/simple"; // `defineSimpleMode`
17+
import "codemirror/addon/hint/show-hint"; // optional
18+
import { unicodeHelperWith } from "@codewars/codemirror-unicode-helper"; // optional
19+
20+
import { defineMode, UNICODE_HELPER_PAIRS } from "@codewars/codemirror-agda";
21+
22+
defineMode(CodeMirror);
23+
// Optionally register unicode input helper.
24+
// `show-hint` addon is required.
25+
CodeMirror.registerGlobalHelper(
26+
"hint",
27+
"agda-input",
28+
// only enable in agda mode
29+
(mode, cm) => mode && mode.name === "agda",
30+
unicodeHelperWith(UNICODE_HELPER_PAIRS)
31+
);
32+
```
533

634
## Acknowledgments
735

8-
- Agda mode was contributed by [@Bubbler-4](https://github.com/Bubbler-4)
9-
- Agda input is based on [ejgallego/CodeMirror-TeX-input](https://github.com/ejgallego/CodeMirror-TeX-input)
36+
Agda mode was contributed by [@Bubbler-4](https://github.com/Bubbler-4).

docs/index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,18 @@ <h2>Agda mode</h2>
6565
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/keymap/vim.min.js"></script>
6666
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/keymap/emacs.min.js"></script>
6767
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/keymap/sublime.min.js"></script>
68-
<script src="codemirror-agda.js"></script>
68+
<script src="https://unpkg.com/@codewars/[email protected]/umd/codemirror-unicode-helper.min.js"></script>
69+
<script src="codemirror-agda.min.js"></script>
6970
<script>
71+
CodeMirrorAgda.defineMode(CodeMirror);
72+
CodeMirror.registerGlobalHelper(
73+
"hint",
74+
"agda-input",
75+
// only enable in agda mode
76+
(mode, cm) => mode && mode.name === "agda",
77+
CodeMirrorUnicodeHelper.unicodeHelperWith(CodeMirrorAgda.UNICODE_HELPER_PAIRS)
78+
);
79+
7080
const autocompleteKeyMap = {
7181
"\\": function(cm) {
7282
cm.replaceSelection("\\");

package-lock.json

Lines changed: 85 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
{
22
"name": "@codewars/codemirror-agda",
33
"version": "0.1.0",
4-
"description": "Agda mode and input helper for CodeMirror",
4+
"description": "CodeMirror extension for editing Agda",
55
"homepage": "https://github.com/codewars/codemirror-agda#readme",
66
"repository": "github:codewars/codemirror-agda",
7+
"license": "MIT",
78
"bugs": {
89
"url": "https://github.com/codewars/codemirror-agda/issues"
910
},
10-
"main": "lib/index.js",
11+
"main": "lib/cjs/index.js",
12+
"module": "lib/esm/index.js",
1113
"files": [
1214
"lib/*",
13-
"src/*"
15+
"umd/*"
1416
],
15-
"module": "src/index.js",
16-
"license": "MIT",
1717
"scripts": {
18-
"prepare": "rollup -i src/index.js -o lib/index.js --format umd --external codemirror --globals codemirror:CodeMirror"
18+
"prepare": "npm run build",
19+
"build": "npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:umd:min",
20+
"build:cjs": "tsc --target es2020 --module commonjs --outDir lib/cjs",
21+
"build:esm": "tsc --target es2020 --module es2020 --outDir lib/esm",
22+
"build:umd": "rollup lib/esm/index.js --format umd --name CodeMirrorAgda -o umd/codemirror-agda.js",
23+
"build:umd:min": "terser umd/codemirror-agda.js -o umd/codemirror-agda.min.js"
1924
},
2025
"peerDependencies": {
2126
"codemirror": "5.x"
2227
},
2328
"devDependencies": {
29+
"@codewars/codemirror-unicode-helper": "^0.1.0",
30+
"@types/codemirror": "0.0.108",
2431
"codemirror": "^5.44.0",
25-
"rollup": "^1.6.0"
32+
"rollup": "^2.41.2",
33+
"terser": "^5.6.0",
34+
"typescript": "^4.2.3"
2635
},
2736
"dependencies": {}
2837
}

src/agda-input.js

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
const hint = (cm, self, data) =>
2-
cm.replaceRange(data.symbol, self.from, self.to);
3-
4-
const cmObj = (k, v) => ({
5-
text: "\\" + k,
6-
symbol: v,
7-
displayText: `${v} \\${k}`,
8-
hint: hint,
9-
});
10-
11-
const toTable = pairs =>
12-
pairs.reduce((a, p) => {
13-
a.push.apply(
14-
a,
15-
Array.from(p[1].replace(/\s/g, "")).map(v => cmObj(p[0], v))
16-
);
17-
return a;
18-
}, []).sort((a,b) => a.text.localeCompare(b.text));
1+
import type { Pair } from "@codewars/codemirror-unicode-helper";
2+
3+
const toPairs = (trans: [string, string][]): Pair[] =>
4+
trans
5+
.reduce((a, [seq, syms]) => {
6+
a.push.apply(
7+
a,
8+
Array.from(syms.replace(/\s/g, "")).map((v) => [seq, v] as Pair)
9+
);
10+
return a;
11+
}, [] as Pair[])
12+
.sort((a, b) => a[0].localeCompare(b[0]));
1913

2014
// Subset of agda-input-translations + TeX-Input
2115
// TODO Add some more from TeX-Input
2216
// https://github.com/agda/agda/blob/master/src/data/emacs-mode/agda-input.el#L191
23-
export const translations = toTable([
17+
// prettier-ignore
18+
export const UNICODE_HELPER_PAIRS: Pair[] = toPairs([
2419
["ell", "ℓ"],
2520
// Equality and similar symbols.
2621
// ["eq", "=∼∽≈≋∻∾∿≀≃⋍≂≅ ≌≊≡≣≐≑≒≓≔≕≖≗≘≙≚≛≜≝≞≟≍≎≏≬⋕"],

src/agda-mode.js renamed to src/agda-mode.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import CodeMirror from "codemirror";
2-
// Requires addon/mode/simple.js
1+
/// <reference types="codemirror/addon/mode/simple" />
2+
import type { defineMIME, defineSimpleMode } from "codemirror";
3+
4+
export type CodeMirrorType = {
5+
defineMIME: typeof defineMIME;
6+
defineSimpleMode: typeof defineSimpleMode;
7+
};
38

49
const floatRegex = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?(?=[.;{}()@"\s]|$)/u;
510
const integerRegex = /-?(?:0|[1-9]\d*|0x[0-9A-Fa-f]+)(?=[.;{}()@"\s]|$)/u;
@@ -77,12 +82,18 @@ const stringState = [
7782
{ regex: /./u, token: "string error" },
7883
];
7984

80-
CodeMirror.defineSimpleMode("agda", {
81-
start: startState,
82-
comment: commentState,
83-
hole: holeState,
84-
charLit: charLitState,
85-
charLitEnd: charLitEndState,
86-
strLit: stringState,
87-
});
88-
CodeMirror.defineMIME("text/x-agda", "agda");
85+
/**
86+
* Defines `agda` mode and `text/x-agda` MIME.
87+
* @param CodeMirror - CodeMirror
88+
*/
89+
export const defineMode = (CodeMirror: CodeMirrorType) => {
90+
CodeMirror.defineSimpleMode("agda", {
91+
start: startState,
92+
comment: commentState,
93+
hole: holeState,
94+
charLit: charLitState,
95+
charLitEnd: charLitEndState,
96+
strLit: stringState,
97+
});
98+
CodeMirror.defineMIME("text/x-agda", "agda");
99+
};

src/index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)