123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298(********************************************************************************)(* crs - A tool for managing code review comments embedded in source code *)(* Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com> *)(* *)(* This file is part of crs. *)(* *)(* crs is free software; you can redistribute it and/or modify it under the *)(* terms of the GNU Lesser General Public License as published by the Free *)(* Software Foundation either version 3 of the License, or any later version, *)(* with the LGPL-3.0 Linking Exception. *)(* *)(* crs is distributed in the hope that it will be useful, but WITHOUT ANY *)(* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *)(* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License and *)(* the file `NOTICE.md` at the root of this repository for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* and the LGPL-3.0 Linking Exception along with this library. If not, see *)(* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively. *)(********************************************************************************)(* This module is derived from Iron (v0.9.114.44+47), file
* [./hg/cr_comment.ml], which is released under Apache 2.0:
*
* Copyright (c) 2016-2017 Jane Street Group, LLC <opensource-contacts@janestreet.com>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* See the file `NOTICE.md` at the root of this repository for more details.
*
* Changes:
*
* - Remove dependency to [Core] - make small adjustments to use [Base] instead.
* - Remove dependency to [Async] - replace by [Shexp] and [Stdio].
* - Remove the [Stable] module - do not version the code.
* - Remove [bin_io] and [bin_digest] expect tests.
* - Remove [hash_consing] constructs - do not share the values.
* - Replace [Relpath] by [Vcs.Path_in_repo].
* - Remove [of_sexp] constructs.
* - Replace [Unresolved_name] by [Vcs.User_handle].
* - Remove [deriving fields] ppx constructs.
* - Remove alternate names and aliases resolution.
* - Remove incremental computation features.
* - Remove summary tables.
* - Introduce a local module for digests.
* - Remove verbose and debug logs.
* - Rewrite [String.slice] calls as equivalent [String.sub] calls.
* - Remove invariant checks.
* - Use [Vcs] instead of [Hg].
* - Remove [Crs_due_now_and_soon].
* - Remove support for in-file `Properties.
* - Remove support for extra headers.
* - Remove support for attributes.
* - Remove assignee computation (left as external work).
* - Replace [is_xcr] by a variant type [Status.t].
* - Make [reporter] mandatory.
* - Refactor [Raw], make [t] a record with a processed part that may fail.
* - Compute [digest_of_condensed_content] for all CR kinds.
* - Rename [Processed] to [Header].
* - Remove support for printing CRs without their content.
* - Compute positions and offsets with [Loc].
* - Some minor changes to the [reindented] content rendering.
* - Add [comment_prefix].
*)moduleDigest_hex=structtypet=string[@@derivingcompare,equal,sexp_of]letto_stringt=tletcreatestr=str|>Stdlib.Digest.string|>Stdlib.Digest.to_hexendmoduleHeader=structmoduleT=struct[@@@coverageoff]typet={status:Status.tLoc.Txt.t;qualifier:Qualifier.tLoc.Txt.t;reporter:Vcs.User_handle.tLoc.Txt.t;recipient:Vcs.User_handle.tLoc.Txt.toption}[@@derivingequal,sexp_of]endincludeTmoduleWith_loc=structletreportert=t.reporterletrecipientt=t.recipientletstatust=t.statusletqualifiert=t.qualifier(* Deprecated. *)letreported_by=reporterletfor_=recipientletdue=qualifierletkind=statusendletcreate~status~qualifier~reporter~recipient={status;qualifier;reporter;recipient};;letreportert=t.reporter.txtletrecipientt=Option.mapt.recipient~f:Loc.Txt.txtletstatust=t.status.txtletqualifiert=t.qualifier.txtletpriorityt:Priority.t=matchstatustwith|XCR->Now|CR->qualifiert|>Qualifier.priority;;(* Deprecated. *)letreported_by=reporterletfor_=recipientletkind=statusendmoduleT=struct[@@@coverageoff]typet={path:Vcs.Path_in_repo.t;whole_loc:Loc.t;content_start_offset:int;header:Header.tOr_error.t;comment_prefix:string;digest_of_condensed_content:Digest_hex.t;content:string}[@@derivingequal,sexp_of]endincludeTletpatht=t.pathletcontentt=t.contentletcontent_start_offsett=t.content_start_offsetletcomment_prefixt=t.comment_prefixletwhole_loct=t.whole_locletheadert=t.headerletcreate~path~whole_loc~content_start_offset~header~comment_prefix~digest_of_condensed_content~content={path;whole_loc;content_start_offset;header;comment_prefix;digest_of_condensed_content;content};;letdigest_ignoring_minor_text_changest=t.digest_of_condensed_contentmoduleFor_sorted_output:sigtypenonrect=t[@@derivingcompare]end=structtypenonrect=tletcomparet1t2=letc=Vcs.Path_in_repo.comparet1.patht2.pathinifc<>0thencelse(letc=Int.compare(Loc.start_linet1.whole_loc)(Loc.start_linet2.whole_loc)inifc<>0thencelseInt.compare(Loc.start_offsett1.whole_loc)(Loc.start_offsett2.whole_loc));;endletreindented_content?(new_line_prefix="")t=letrstrip_prefix=String.rstripnew_line_prefixinletexpect_comment_prefixed_lines=matcht.comment_prefixwith|"--"|";"|";;"|"//"|"#"|"##"->true|_->falseinletindent=letlen=ifexpect_comment_prefixed_linesthen(letstart=Loc.startt.whole_locinstart.pos_cnum-start.pos_bol)else(matcht.headerwith|Error_->letstart=Loc.startt.whole_locin(* The len of the indentation is a heuristic in this case. *)start.pos_cnum-start.pos_bol+String.lengtht.comment_prefix+1|Okh->letstart=Loc.starth.status.locinstart.pos_cnum-start.pos_bol)inString.makelen' 'inletstr=t.contentinletlines=String.splitstr~on:'\n'inletlines=List.maplines~f:(funline->letline=String.rstriplineinifString.is_emptylinethenrstrip_prefixelse(letline=matchString.chop_prefixline~prefix:indentwith|Somes->s|None->(* When the line have less indentation than expected, stripping it
this way may end up breaking some vertical alignment present in
the text. However the formatting of such CR would be flagged as
invalid per some linting rule, so this is not a major problem. *)String.lstriplineinletline=matchexpect_comment_prefixed_lineswith|false->line|true->(matchString.chop_prefixline~prefix:(t.comment_prefix^" ")with|Somes->s|None->(matchString.chop_prefixline~prefix:t.comment_prefixwith|Somes->s|None->line))innew_line_prefix^line))inString.concatlines~sep:"\n";;letsortts=List.sortts~compare:For_sorted_output.compareletstatust:Status.t=matcht.headerwith|Error_->CR|Okp->Header.statusp;;letpriorityt:Priority.t=matcht.headerwith|Error_->Now|Okp->Header.priorityp;;letto_stringt=String.concat~sep:"\n"[Loc.to_stringt.whole_loc;reindented_contentt~new_line_prefix:" ";""];;letoutput_one~include_delimcr~oc=letstr=to_stringcrinletnl=ifinclude_delimthen"\n"else""inOut_channel.output_stringoc(Printf.sprintf"%s%s"nlstr);;letoutput_listcrs~oc=letcrs=sortcrsinletinclude_delim=reffalseinList.itercrs~f:(funcr->output_one~include_delim:!include_delimcr~oc;include_delim:=true);;letprint_listcrs=output_listcrs~oc:Stdlib.stdoutmodulePrivate=structmoduleHeader=Headerletcreate=createend(* Deprecated *)letwork_on=priorityletkind=status