Skip to content

Commit 3d650b7

Browse files
committed
Revert "use more idiomatic rescript bindings"
This reverts commit 1213a43.
1 parent 201a0ea commit 3d650b7

File tree

1 file changed

+101
-64
lines changed

1 file changed

+101
-64
lines changed

src/components/CodeMirror.res

Lines changed: 101 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,10 @@ module CM6 = {
9696
type editorView
9797
type compartment
9898
type effect
99-
99+
type transaction
100+
type languageSupport
100101
type keymapSpec
101102

102-
module Extension = {
103-
type t = extension
104-
external fromArray: array<t> => t = "%identity"
105-
}
106-
107103
module Text = {
108104
type t
109105
type line
@@ -115,42 +111,40 @@ module CM6 = {
115111
}
116112

117113
module EditorState = {
118-
type createConfig = {doc: string, extensions: array<extension>}
119-
120114
@module("@codemirror/state") @scope("EditorState")
121-
external create: createConfig => editorState = "create"
115+
external create: {"doc": string, "extensions": array<extension>} => editorState = "create"
116+
117+
@module("@codemirror/state") @scope("EditorState") @val
118+
external readOnly: {"of": bool => extension} = "readOnly"
122119

123-
module ReadOnly = {
124-
@module("@codemirror/state") @scope(("EditorState", "readOnly")) @val
125-
external of_: bool => extension = "of"
126-
}
127120
@get external doc: editorState => Text.t = "doc"
128121
}
129122

130123
module Compartment = {
131124
@module("@codemirror/state") @new
132-
external create: unit => compartment = "Compartment"
133-
@send external make: (compartment, extension) => extension = "of"
125+
external make: unit => compartment = "Compartment"
126+
127+
@send external of_: (compartment, extension) => extension = "of"
134128
@send external reconfigure: (compartment, extension) => effect = "reconfigure"
135129
}
136130

137131
module EditorView = {
138-
type createConfig = {state: editorState, parent: WebAPI.DOMAPI.element}
139132
@module("@codemirror/view") @new
140-
external create: createConfig => editorView = "EditorView"
133+
external create: {"state": editorState, "parent": WebAPI.DOMAPI.element} => editorView =
134+
"EditorView"
141135

142136
@send external destroy: editorView => unit = "destroy"
143137
@get external state: editorView => editorState = "state"
144138
@get external dom: editorView => WebAPI.DOMAPI.htmlElement = "dom"
145139

146-
type change = {from: int, to: int, insert: string}
147-
type dispatchArg = {changes: change}
148140
@send
149-
external dispatch: (editorView, dispatchArg) => unit = "dispatch"
141+
external dispatch: (
142+
editorView,
143+
{"changes": {"from": int, "to": int, "insert": string}},
144+
) => unit = "dispatch"
150145

151-
type dispatchEffectsArg = {effects: effect}
152146
@send
153-
external dispatchEffects: (editorView, dispatchEffectsArg) => unit = "dispatch"
147+
external dispatchEffects: (editorView, {"effects": effect}) => unit = "dispatch"
154148

155149
@module("@codemirror/view") @scope("EditorView") @val
156150
external lineWrapping: extension = "lineWrapping"
@@ -175,8 +169,8 @@ module CM6 = {
175169
@get external view: update => editorView = "view"
176170
@get external docChanged: update => bool = "docChanged"
177171

178-
@module("@codemirror/view") @scope(("EditorView", "updateListener"))
179-
external of_: (update => unit) => extension = "of"
172+
@module("@codemirror/view") @scope("EditorView")
173+
external of_: (update => unit) => extension = "updateListener"
180174
}
181175
}
182176

@@ -203,18 +197,16 @@ module CM6 = {
203197
@module("@codemirror/language")
204198
external bracketMatching: unit => extension = "bracketMatching"
205199

206-
type syntaxConfig = {fallback: bool}
207-
208200
@module("@codemirror/language")
209-
external syntaxHighlighting: (extension, syntaxConfig) => extension = "syntaxHighlighting"
201+
external syntaxHighlighting: (extension, {"fallback": bool}) => extension = "syntaxHighlighting"
210202

211-
@module("@codemirror/language") @val
203+
@module("@lezer/highlight") @val
212204
external defaultHighlightStyle: extension = "defaultHighlightStyle"
213205
}
214206

215207
module Keymap = {
216-
@module("@codemirror/view") @scope("keymap") @val
217-
external of_: array<keymapSpec> => extension = "of"
208+
@module("@codemirror/state")
209+
external of_: array<keymapSpec> => extension = "keymap"
218210
}
219211

220212
module Lint = {
@@ -236,7 +228,7 @@ module CM6 = {
236228

237229
module JavaScript = {
238230
@module("@codemirror/lang-javascript")
239-
external javascript: unit => extension = "javascript"
231+
external javascript: unit => languageSupport = "javascript"
240232
}
241233

242234
module Vim = {
@@ -245,11 +237,11 @@ module CM6 = {
245237
}
246238

247239
module CustomLanguages = {
248-
@module("../../plugins/cm6-rescript-mode.js") @val
249-
external rescriptLanguage: extension = "rescriptLanguage"
240+
@module("../plugins/cm6-rescript-mode") @val
241+
external rescriptLanguage: languageSupport = "rescriptLanguage"
250242

251-
@module("../../plugins/cm6-reason-mode.js") @val
252-
external reasonLanguage: extension = "reasonLanguage"
243+
@module("../plugins/cm6-reason-mode") @val
244+
external reasonLanguage: languageSupport = "reasonLanguage"
253245
}
254246
}
255247

@@ -276,7 +268,7 @@ type editorConfig = {
276268
maxHeight: option<string>,
277269
}
278270

279-
let createLinterExtension = (errors: array<Error.t>): CM6.extension => {
271+
let createLinterExtension = (errors: array<Error.t>, lintConf: CM6.compartment): CM6.extension => {
280272
let linterSource = (view: CM6.editorView): array<CM6.Lint.diagnostic> => {
281273
if Array.length(errors) === 0 {
282274
[]
@@ -304,7 +296,7 @@ let createLinterExtension = (errors: array<Error.t>): CM6.extension => {
304296
message: err.text,
305297
}
306298

307-
Array.push(diagnostics, diagnostic)
299+
Array.push(diagnostics, diagnostic)->ignore
308300
} catch {
309301
| _ => Console.warn("Error creating lint marker")
310302
}
@@ -314,7 +306,7 @@ let createLinterExtension = (errors: array<Error.t>): CM6.extension => {
314306
}
315307
}
316308

317-
CM6.Lint.linter(linterSource)
309+
CM6.Compartment.of_(lintConf, CM6.Lint.linter(linterSource))
318310
}
319311

320312
let createEditor = (config: editorConfig): editorInstance => {
@@ -326,41 +318,41 @@ let createEditor = (config: editorConfig): editorInstance => {
326318
}
327319

328320
// Setup compartments for dynamic config
329-
let languageConf = CM6.Compartment.create()
330-
let readOnlyConf = CM6.Compartment.create()
331-
let keymapConf = CM6.Compartment.create()
332-
let lintConf = CM6.Compartment.create()
321+
let languageConf = CM6.Compartment.make()
322+
let readOnlyConf = CM6.Compartment.make()
323+
let keymapConf = CM6.Compartment.make()
324+
let lintConf = CM6.Compartment.make()
333325

334326
// Basic extensions
335327
let extensions = [
336-
CM6.Compartment.make(languageConf, (language: CM6.extension)),
328+
CM6.Compartment.of_(languageConf, (Obj.magic(language): CM6.extension)),
337329
CM6.Commands.history(),
338330
CM6.EditorView.drawSelection(),
339331
CM6.EditorView.dropCursor(),
340332
CM6.Language.bracketMatching(),
341333
CM6.Search.highlightSelectionMatches(),
342-
CM6.Language.syntaxHighlighting(CM6.Language.defaultHighlightStyle, {fallback: true}),
334+
CM6.Language.syntaxHighlighting(CM6.Language.defaultHighlightStyle, {"fallback": true}),
343335
]
344336

345337
// Add optional extensions
346338
if config.lineNumbers {
347-
Array.push(extensions, CM6.EditorView.lineNumbers())
348-
Array.push(extensions, CM6.EditorView.highlightActiveLineGutter())
339+
Array.push(extensions, CM6.EditorView.lineNumbers())->ignore
340+
Array.push(extensions, CM6.EditorView.highlightActiveLineGutter())->ignore
349341
}
350342

351343
if !config.readOnly {
352-
Array.push(extensions, CM6.EditorView.highlightActiveLine())
344+
Array.push(extensions, CM6.EditorView.highlightActiveLine())->ignore
353345
}
354346

355347
if config.lineWrapping {
356-
Array.push(extensions, CM6.EditorView.lineWrapping)
348+
Array.push(extensions, CM6.EditorView.lineWrapping)->ignore
357349
}
358350

359351
// Add readonly conf
360352
Array.push(
361353
extensions,
362-
CM6.Compartment.make(readOnlyConf, CM6.EditorState.ReadOnly.of_(config.readOnly)),
363-
)
354+
CM6.Compartment.of_(readOnlyConf, CM6.EditorState.readOnly["of"](config.readOnly)),
355+
)->ignore
364356

365357
// Add keymap
366358
let keymapExtension = if config.keyMap === "vim" {
@@ -370,16 +362,17 @@ let createEditor = (config: editorConfig): editorInstance => {
370362
let searchKeymapExt = CM6.Keymap.of_(CM6.Search.searchKeymap)
371363
// Return vim extension combined with keymap extensions
372364
// We need to wrap them in an array and convert to extension
373-
/* combine extensions into a JS array value */
374-
[vimExt, defaultKeymapExt, historyKeymapExt, searchKeymapExt]->CM6.Extension.fromArray
365+
let combined = Array.concat([vimExt], [defaultKeymapExt, historyKeymapExt, searchKeymapExt])
366+
(Obj.magic(combined): CM6.extension)
375367
} else {
376368
let defaultKeymapExt = CM6.Keymap.of_(CM6.Commands.defaultKeymap)
377369
let historyKeymapExt = CM6.Keymap.of_(CM6.Commands.historyKeymap)
378370
let searchKeymapExt = CM6.Keymap.of_(CM6.Search.searchKeymap)
379-
// Return combined keymap extensions as a JS array
380-
[defaultKeymapExt, historyKeymapExt, searchKeymapExt]->CM6.Extension.fromArray
371+
// Return combined keymap extensions
372+
let combined = [defaultKeymapExt, historyKeymapExt, searchKeymapExt]
373+
(Obj.magic(combined): CM6.extension)
381374
}
382-
Array.push(extensions, CM6.Compartment.make(keymapConf, keymapExtension))
375+
Array.push(extensions, CM6.Compartment.of_(keymapConf, keymapExtension))->ignore
383376

384377
// Add change listener
385378
switch config.onChange {
@@ -391,18 +384,24 @@ let createEditor = (config: editorConfig): editorInstance => {
391384
onChange(newValue)
392385
}
393386
})
394-
Array.push(extensions, updateListener)
387+
Array.push(extensions, updateListener)->ignore
395388
| None => ()
396389
}
397390

398-
// Add linter for errors (wrap the raw linter extension in the compartment)
399-
Array.push(extensions, CM6.Compartment.make(lintConf, createLinterExtension(config.errors)))
400-
Array.push(extensions, CM6.Lint.lintGutter())
391+
// Add linter for errors
392+
Array.push(extensions, createLinterExtension(config.errors, lintConf))->ignore
393+
Array.push(extensions, CM6.Lint.lintGutter())->ignore
401394

402395
// Create editor
403-
let state = CM6.EditorState.create({doc: config.initialValue, extensions})
396+
let state = CM6.EditorState.create({
397+
"doc": config.initialValue,
398+
"extensions": extensions,
399+
})
404400

405-
let view = CM6.EditorView.create({state, parent: config.parent})
401+
let view = CM6.EditorView.create({
402+
"state": state,
403+
"parent": config.parent,
404+
})
406405

407406
// Apply custom styling
408407
let dom = CM6.EditorView.dom(view)
@@ -430,7 +429,13 @@ let editorSetValue = (instance: editorInstance, value: string): unit => {
430429
let doc = CM6.EditorView.state(instance.view)->CM6.EditorState.doc
431430
CM6.EditorView.dispatch(
432431
instance.view,
433-
{changes: {from: 0, to: CM6.Text.toString(doc)->String.length, insert: value}},
432+
{
433+
"changes": {
434+
"from": 0,
435+
"to": CM6.Text.toString(doc)->String.length,
436+
"insert": value,
437+
},
438+
},
434439
)
435440
}
436441

@@ -451,15 +456,47 @@ let editorSetMode = (instance: editorInstance, mode: string): unit => {
451456

452457
CM6.EditorView.dispatchEffects(
453458
instance.view,
454-
{effects: CM6.Compartment.reconfigure(instance.languageConf, (language: CM6.extension))},
459+
{
460+
"effects": CM6.Compartment.reconfigure(
461+
instance.languageConf,
462+
(Obj.magic(language): CM6.extension),
463+
),
464+
},
465+
)
466+
}
467+
468+
let editorSetKeyMap = (instance: editorInstance, keyMap: string): unit => {
469+
let keymapExtension = if keyMap === "vim" {
470+
let vimExt = CM6.Vim.vim()
471+
let defaultKeymapExt = CM6.Keymap.of_(CM6.Commands.defaultKeymap)
472+
let historyKeymapExt = CM6.Keymap.of_(CM6.Commands.historyKeymap)
473+
let searchKeymapExt = CM6.Keymap.of_(CM6.Search.searchKeymap)
474+
let combined = Array.concat([vimExt], [defaultKeymapExt, historyKeymapExt, searchKeymapExt])
475+
(Obj.magic(combined): CM6.extension)
476+
} else {
477+
let defaultKeymapExt = CM6.Keymap.of_(CM6.Commands.defaultKeymap)
478+
let historyKeymapExt = CM6.Keymap.of_(CM6.Commands.historyKeymap)
479+
let searchKeymapExt = CM6.Keymap.of_(CM6.Search.searchKeymap)
480+
let combined = [defaultKeymapExt, historyKeymapExt, searchKeymapExt]
481+
(Obj.magic(combined): CM6.extension)
482+
}
483+
484+
CM6.EditorView.dispatchEffects(
485+
instance.view,
486+
{
487+
"effects": CM6.Compartment.reconfigure(instance.keymapConf, keymapExtension),
488+
},
455489
)
456490
}
457491

458492
let editorSetErrors = (instance: editorInstance, errors: array<Error.t>): unit => {
459493
CM6.EditorView.dispatchEffects(
460494
instance.view,
461495
{
462-
effects: CM6.Compartment.reconfigure(instance.lintConf, createLinterExtension(errors)),
496+
"effects": CM6.Compartment.reconfigure(
497+
instance.lintConf,
498+
createLinterExtension(errors, instance.lintConf),
499+
),
463500
},
464501
)
465502
}

0 commit comments

Comments
 (0)