My aggregated monorepo of OCaml code, automaintained
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Clamp lint diagnostics using CodeMirror's doc.length directly

Read doc.length from view.state.doc instead of reconstructing via
String.concat — our String reconstruction may differ from
CodeMirror's internal length.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+19 -17
+19 -17
merlin-js/src/extension/merlin_codemirror.ml
··· 15 15 let open Fut.Syntax in 16 16 let doc = Utils.get_full_doc @@ Editor.View.state view in 17 17 let+ result = Merlin_client.query_errors worker doc in 18 - (* Re-read doc length — the document may have changed while the 19 - query was in flight (e.g. formatter ran). Clamp positions to 20 - avoid CodeMirror RangeError. *) 21 - let cur_doc = Utils.get_full_doc @@ Editor.View.state view in 22 - let doc_len = String.length cur_doc in 23 - List.map (fun Protocol.{ kind; loc; main; sub = _; source } -> 24 - let from = max 0 (min loc.loc_start.pos_cnum doc_len) in 25 - let to_ = max from (min loc.loc_end.pos_cnum doc_len) in 26 - let source = Protocol.report_source_to_string source in 27 - let severity = match kind with 28 - | Report_error 29 - | Report_warning_as_error _ 30 - | Report_alert_as_error _ -> Lint.Diagnostic.Error 31 - | Report_warning _ -> Lint.Diagnostic.Warning 32 - | Report_alert _ -> Lint.Diagnostic.Info 33 - in 34 - Lint.Diagnostic.create ~source ~from ~to_ ~severity ~message:main () 18 + (* Read doc length directly from CodeMirror's state.doc.length 19 + rather than our String reconstruction, to avoid off-by-one 20 + mismatches that cause RangeError in the lint extension. *) 21 + let doc_len = Jv.to_int (Jv.get 22 + (Jv.get (Jv.get (Editor.View.to_jv view) "state") "doc") "length") in 23 + List.filter_map (fun Protocol.{ kind; loc; main; sub = _; source } -> 24 + let from = loc.loc_start.pos_cnum in 25 + let to_ = loc.loc_end.pos_cnum in 26 + if from < 0 || from > doc_len || to_ > doc_len then None 27 + else 28 + let source = Protocol.report_source_to_string source in 29 + let severity = match kind with 30 + | Report_error 31 + | Report_warning_as_error _ 32 + | Report_alert_as_error _ -> Lint.Diagnostic.Error 33 + | Report_warning _ -> Lint.Diagnostic.Warning 34 + | Report_alert _ -> Lint.Diagnostic.Info 35 + in 36 + Some (Lint.Diagnostic.create ~source ~from ~to_ ~severity ~message:main ()) 35 37 ) result 36 38 |> Array.of_list 37 39