Skip to content

Commit a9f08de

Browse files
authored
Remove CodeMirror.defineInitHook (#3)
Removing to avoid interfering with other modes. Now the registration needs to be handled by the users of this package. Example of only enabling in one mode is provided in the demo.
1 parent 245b686 commit a9f08de

File tree

3 files changed

+42
-59
lines changed

3 files changed

+42
-59
lines changed

docs/agda.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -514,34 +514,6 @@
514514
return { list: list, from: from, to: to };
515515
};
516516

517-
// TODO Is it possible to disable running this hook in other modes?
518-
CodeMirror.defineInitHook(function(cm) {
519-
cm.addKeyMap({
520-
"\\": function(cm) {
521-
cm.replaceSelection("\\");
522-
cm.execCommand("autocomplete");
523-
},
524-
});
525-
526-
const hintOptions = cm.getOption("hintOptions") || {};
527-
hintOptions.extraKeys = {
528-
// Complete with selected and insert space.
529-
Space: function(cm) {
530-
const cA = cm.state.completionActive;
531-
if (cA) {
532-
cA.widget.pick();
533-
cm.replaceSelection(" ");
534-
}
535-
},
536-
};
537-
// Use custom `closeCharacters` to allow text with ()[]{};:>,
538-
// Note that this isn't documented.
539-
hintOptions.closeCharacters = /[\s]/;
540-
// Disable auto completing even if there's only one choice.
541-
hintOptions.completeSingle = false;
542-
cm.setOption("hintOptions", hintOptions);
543-
});
544-
545517
CodeMirror.registerGlobalHelper(
546518
"hint",
547519
"agda-input",

docs/index.html

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ <h2>Agda mode</h2>
4545
<select id="mode-select" data-option-key="mode">
4646
<option value="agda">Agda</option>
4747
<option value="haskell">Haskell</option>
48+
<option value="python">Python</option>
4849
</select>
4950

5051
<label for="keymap-select">KeyMap:</label>
@@ -60,20 +61,58 @@ <h2>Agda mode</h2>
6061
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/addon/mode/simple.min.js"></script>
6162
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/addon/hint/show-hint.min.js"></script>
6263
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/mode/haskell/haskell.min.js"></script>
64+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/mode/python/python.min.js"></script>
6365
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/keymap/vim.min.js"></script>
6466
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/keymap/emacs.min.js"></script>
6567
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/keymap/sublime.min.js"></script>
6668
<script src="agda.js"></script>
6769
<script>
70+
const autocompleteKeyMap = {
71+
"\\": function(cm) {
72+
cm.replaceSelection("\\");
73+
cm.execCommand("autocomplete");
74+
},
75+
};
76+
const hintOptions = {
77+
extraKeys: {
78+
// Complete with selected and insert space.
79+
Space: function(cm) {
80+
const cA = cm.state.completionActive;
81+
if (cA) {
82+
cA.widget.pick();
83+
cm.replaceSelection(" ");
84+
}
85+
},
86+
},
87+
// Use custom `closeCharacters` to allow text with ()[]{};:>,
88+
// Note that this isn't documented.
89+
closeCharacters: /[\s]/,
90+
// Disable auto completing even if there's only one choice.
91+
completeSingle: false,
92+
};
93+
94+
// hook to only enable completion in agda mode
95+
function setOption(editor, key, value) {
96+
if (key === "mode") {
97+
if (value === "agda") {
98+
editor.addKeyMap(autocompleteKeyMap);
99+
editor.setOption("hintOptions", hintOptions);
100+
} else {
101+
editor.removeKeyMap(autocompleteKeyMap);
102+
editor.setOption("hintOptions", undefined);
103+
}
104+
}
105+
editor.setOption(key, value);
106+
}
107+
68108
const editor = CodeMirror.fromTextArea(document.getElementById("code"), {
69-
mode: "agda",
70109
lineNumbers: true,
71110
theme: "base16-dark",
72111
keyMap: "default",
73112
});
74-
113+
setOption(editor, "mode", "agda");
75114
document.getElementById("selects").addEventListener("change", function(e) {
76-
editor.setOption(e.target.dataset.optionKey, e.target.value);
115+
setOption(editor, e.target.dataset.optionKey, e.target.value);
77116
e.stopPropagation();
78117
});
79118
</script>

src/agda-input.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,6 @@ const createHint = table => (editor, _options) => {
1515
return { list: list, from: from, to: to };
1616
};
1717

18-
// TODO Is it possible to disable running this hook in other modes?
19-
CodeMirror.defineInitHook(function(cm) {
20-
cm.addKeyMap({
21-
"\\": function(cm) {
22-
cm.replaceSelection("\\");
23-
cm.execCommand("autocomplete");
24-
},
25-
});
26-
27-
const hintOptions = cm.getOption("hintOptions") || {};
28-
hintOptions.extraKeys = {
29-
// Complete with selected and insert space.
30-
Space: function(cm) {
31-
const cA = cm.state.completionActive;
32-
if (cA) {
33-
cA.widget.pick();
34-
cm.replaceSelection(" ");
35-
}
36-
},
37-
};
38-
// Use custom `closeCharacters` to allow text with ()[]{};:>,
39-
// Note that this isn't documented.
40-
hintOptions.closeCharacters = /[\s]/;
41-
// Disable auto completing even if there's only one choice.
42-
hintOptions.completeSingle = false;
43-
cm.setOption("hintOptions", hintOptions);
44-
});
45-
4618
CodeMirror.registerGlobalHelper(
4719
"hint",
4820
"agda-input",

0 commit comments

Comments
 (0)