Skip to content

Commit 6fc54b1

Browse files
committed
fix offset lines calculation
1 parent 7b8e146 commit 6fc54b1

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

tests/tools_tests/src/expected/FormatDocstringsTestError.res.expected

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
Syntax error in code block in docstring
3-
/Users/zth/OSS/rescript-compiler/tests/tools_tests/src/docstrings-format/FormatDocstringsTestError.res:2:10-3:3
3+
/Users/zth/OSS/rescript-compiler/tests/tools_tests/src/docstrings-format/FormatDocstringsTestError.res:5:10-6:3
44

5-
1 │
6-
2 │ let name= 
7-
3 │ let x=12
5+
3 │
6+
4 │
7+
5 │ let name= 
8+
6 │ let x=12
89

910
This let-binding misses an expression
1011

tools/src/tools.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,12 +752,12 @@ module FormatDocstrings = struct
752752
mapRescriptCodeBlocks
753753
~colIndent:(payloadLoc.loc_start.pos_cnum - payloadLoc.loc_start.pos_bol)
754754
~mapper:(fun code currentLine ->
755-
(* TODO: Figure out the line offsets here so the error messages line up as intended. *)
755+
let codeLines = String.split_on_char '\n' code in
756+
let n = List.length codeLines in
756757
let newlinesNeeded =
757-
payloadLoc.loc_start.pos_lnum + currentLine - 5
758+
max 0 (payloadLoc.loc_start.pos_lnum + currentLine - n)
758759
in
759-
let codeOffset = String.make newlinesNeeded '\n' in
760-
let codeWithOffset = codeOffset ^ code in
760+
let codeWithOffset = String.make newlinesNeeded '\n' ^ code in
761761
let formatted_code =
762762
let {Res_driver.parsetree; comments; invalid; diagnostics} =
763763
Res_driver.parse_implementation_from_source ~for_printer:true

0 commit comments

Comments
 (0)