Skip to content

Commit

Permalink
Fixing unit tests and refactoring.
Browse files Browse the repository at this point in the history
Changed the name of items to code_lines (which denote sentences, parse errors and
comments). Made sure the correct API is called in the right areas.
  • Loading branch information
rtetley committed Feb 16, 2024
1 parent d6e74c5 commit ca4e475
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
17 changes: 10 additions & 7 deletions language-server/dm/document.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,31 @@ type comment = {
stop : int;
}

type item =
type code_line =
| Sentence of sentence
| ParsingError of parsing_error
| Comment of comment

let start_of_item = function
let start_of_code_line = function
| Sentence { start = x } -> x
| ParsingError { start = x } -> x
| Comment { start = x } -> x

let compare_item x y =
let s1 = start_of_item x in
let s2 = start_of_item y in
let compare_code_line x y =
let s1 = start_of_code_line x in
let s2 = start_of_code_line y in
s1 - s2

let sentences_sorted_by_loc parsed =
List.sort compare_item @@ List.concat [
let code_lines_sorted_by_loc parsed =
List.sort compare_code_line @@ List.concat [
(List.map (fun (_,x) -> Sentence x) @@ SM.bindings parsed.sentences_by_id) ;
(List.map (fun (_,x) -> ParsingError x) @@ LM.bindings parsed.parsing_errors_by_end) ;
[] (* todo comments *)
]

let sentences_sorted_by_loc parsed =
List.sort (fun ({start = s1} : sentence) {start = s2} -> s1 - s2) @@ List.map snd @@ SM.bindings parsed.sentences_by_id

let sentences_before parsed loc =
let (before,ov,_after) = LM.split loc parsed.sentences_by_end in
let before = Option.cata (fun v -> LM.add loc v before) before ov in
Expand Down
7 changes: 4 additions & 3 deletions language-server/dm/document.mli
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ type comment = {
stop : int;
}

type item =
type code_line =
| Sentence of sentence
| ParsingError of parsing_error
| Comment of comment

val sentences : document -> sentence list
val sentences_sorted_by_loc : document -> item list
val code_lines_sorted_by_loc : document -> code_line list
val sentences_sorted_by_loc : document -> sentence list

val get_sentence : document -> sentence_id -> sentence option
val sentences_before : document -> int -> sentence list
Expand Down Expand Up @@ -114,6 +115,6 @@ val range_of_id_with_blank_space : document -> Stateid.t -> Range.t
module Internal : sig

val string_of_sentence : sentence -> string
val string_of_item : item -> string
val string_of_item : code_line -> string

end
4 changes: 2 additions & 2 deletions language-server/dm/documentManager.ml
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ module Internal = struct
validate_document st

let string_of_state st =
let sentences = Document.sentences_sorted_by_loc st.document in
let code_lines = Document.code_lines_sorted_by_loc st.document in
let string_of_state id =
if ExecutionManager.is_executed st.execution_state id then "(executed)"
else if ExecutionManager.is_remotely_executed st.execution_state id then "(executed in worker)"
Expand All @@ -483,6 +483,6 @@ module Internal = struct
| ParsingError _ -> "(error)"
| Comment _ -> "(comment)"
in
String.concat "\n" @@ List.map string_of_item sentences
String.concat "\n" @@ List.map string_of_item code_lines

end
2 changes: 1 addition & 1 deletion language-server/tests/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let rec parse : type a. int -> int -> Document.sentence list -> Document.parsing
Error ("fewer sentences than expected, only " ^ Int.to_string m ^ " list of errors:\n" ^ errors)
| E _, _, [] -> Error ("fewer errors than expected, only " ^ Int.to_string n)

let d_sentences doc spec =
let d_sentences doc spec =
let sentences = Document.sentences_sorted_by_loc doc in
let errors = Document.parse_errors doc in
let r = run (parse 0 0 sentences errors spec) in
Expand Down
12 changes: 6 additions & 6 deletions language-server/tests/dm_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ let%test_unit "parse.init" =
let doc = Document.raw_document @@ DocumentManager.Internal.document st in
[%test_eq: int] (RawDocument.end_loc doc) 44;
let sentences = Document.sentences @@ DocumentManager.Internal.document st in
let positions = Stdlib.List.map (fun s -> s.Document.start) sentences in
let positions = Stdlib.List.map (fun (s : Document.sentence) -> s.Document.start) sentences in
[%test_eq: int list] positions [ 0; 22 ];
check_no_diag st

let%test_unit "parse.insert" =
let st, events = init_test_doc ~text:"Definition x := true. Definition y := false." in
let st = insert_text st ~loc:0 ~text:"Definition z := 0. " in
let sentences = Document.sentences @@ DocumentManager.Internal.document st in
let positions = Stdlib.List.map (fun s -> s.Document.start) sentences in
let positions = Stdlib.List.map (fun (s : Document.sentence) -> s.Document.start) sentences in
[%test_eq: int list] positions [ 0; 19; 41 ];
check_no_diag st

Expand All @@ -50,8 +50,8 @@ let%test_unit "parse.squash" =
let st = edit_text st ~start:20 ~stop:21 ~text:"" in
let doc = DocumentManager.Internal.document st in
let sentences = Document.sentences doc in
let start_positions = Stdlib.List.map (fun s -> s.Document.start) sentences in
let stop_positions = Stdlib.List.map (fun s -> s.Document.stop) sentences in
let start_positions = Stdlib.List.map (fun (s : Document.sentence) -> s.Document.start) sentences in
let stop_positions = Stdlib.List.map (fun (s : Document.sentence) -> s.Document.stop) sentences in
[%test_eq: int list] start_positions [ 44 ];
[%test_eq: int list] stop_positions [ 62 ];
[%test_eq: int] (List.length (Document.parse_errors doc)) 1
Expand All @@ -60,14 +60,14 @@ let%test_unit "parse.error_recovery" =
let st, events = init_test_doc ~text:"## . Definition x := true. !! . Definition y := false." in
let doc = DocumentManager.Internal.document st in
let sentences = Document.sentences doc in
let start_positions = Stdlib.List.map (fun s -> s.Document.start) sentences in
let start_positions = Stdlib.List.map (fun (s : Document.sentence) -> s.Document.start) sentences in
[%test_eq: int list] start_positions [ 5; 32 ];
[%test_eq: int] (List.length (Document.parse_errors doc)) 2

let%test_unit "parse.extensions" =
let st, events = init_test_doc ~text:"Notation \"## x\" := x (at level 0). Definition f (x : nat) := ##xx." in
let sentences = Document.sentences @@ DocumentManager.Internal.document st in
let start_positions = Stdlib.List.map (fun s -> s.Document.start) sentences in
let start_positions = Stdlib.List.map (fun (s : Document.sentence) -> s.Document.start) sentences in
[%test_eq: int list] start_positions [ 0; 35 ];
check_no_diag st

Expand Down

0 comments on commit ca4e475

Please sign in to comment.