123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170(* Claude Code
*
* Copyright (C) 2026 Yoann Padioleau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* (LGPL) as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*)(* See Highlight_code.mli *)typecategory=|Comment|Comment_section|Keyword|Keyword_control|Keyword_module|Def_function|Def_value|Def_type|Def_module|Parameter|Local|Global|Module|Constructor|Type|Type_var|Label|Capability|Number|String|Operator|Punctuation|Attribute|Normal|Error|Fieldletshow=function|Comment->"Comment"|Comment_section->"Comment_section"|Keyword->"Keyword"|Keyword_control->"Keyword_control"|Keyword_module->"Keyword_module"|Def_function->"Def_function"|Def_value->"Def_value"|Def_type->"Def_type"|Def_module->"Def_module"|Parameter->"Parameter"|Local->"Local"|Global->"Global"|Module->"Module"|Constructor->"Constructor"|Type->"Type"|Type_var->"Type_var"|Label->"Label"|Capability->"Capability"|Number->"Number"|String->"String"|Operator->"Operator"|Punctuation->"Punctuation"|Attribute->"Attribute"|Normal->"Normal"|Error->"Error"|Field->"Field"(* claude: a new category goes at the end: a category's place here is
* its byte in a code map's picture of a file *)letall=[|Comment;Comment_section;Keyword;Keyword_control;Keyword_module;Def_function;Def_value;Def_type;Def_module;Parameter;Local;Global;Module;Constructor;Type;Type_var;Label;Capability;Number;String;Operator;Punctuation;Attribute;Normal;Error;Field|]letindex(c:category):int=letrecgoi=ifall.(i)=cthenielsego(i+1)ingo0(*****************************************************************************)(* Colours *)(*****************************************************************************)(* claude: X11's names, as codemap gives them (info_of_category) *)letrgb=function|Comment->(190,190,190)(* gray *)|Comment_section->(255,127,80)(* coral *)|Keyword->(255,165,0)(* orange *)|Keyword_control->(255,140,0)(* DarkOrange *)|Keyword_module->(210,105,30)(* chocolate *)|Def_function->(238,201,0)(* gold2 *)|Def_value->(255,215,0)(* gold *)|Def_type->(154,205,50)(* YellowGreen *)|Def_module->(255,127,36)(* chocolate1 *)|Parameter->(92,172,238)(* SteelBlue2 *)|Local->(135,206,255)(* SkyBlue1 *)|Global->(250,128,114)(* salmon *)|Module->(210,105,30)(* chocolate *)|Constructor->(255,181,197)(* pink1 *)|Type->(127,255,0)(* chartreuse *)|Type_var->(50,205,50)(* LimeGreen *)|Label->(100,149,237)(* CornflowerBlue *)|Capability->(255,64,64)(* ours: red, authority *)|Number->(205,205,0)(* yellow3 *)|String->(60,179,113)(* MediumSeaGreen *)|Operator->(0,154,205)(* DeepSkyBlue3 *)|Punctuation->(0,205,205)(* cyan3 *)|Attribute->(238,118,0)(* DarkOrange2 *)|Normal->(245,222,179)(* wheat *)|Error->(255,99,71)(* tomato *)|Field->(159,121,238)(* MediumPurple2, codemap's field *)letbackground=(47,79,79)(* DarkSlateGray *)letemphasis=function|Def_module|Def_type|Comment_section->5.|Def_function->3.5|Def_value->2.5|_->1.(*****************************************************************************)(* Lines *)(*****************************************************************************)typespan={col:int;text:string;category:category}typeoccurrence={line:int;col:int;len:int;bound_at:int*int}letoccurrences(tokens:(int*int*string)array)(binds:(int,int)Hashtbl.t):occurrencelist=Hashtbl.fold(funibacc->letline,col,text=tokens.(i)andbline,bcol,_=tokens.(b)in{line=line-1;col;len=String.lengthtext;bound_at=(bline-1,bcol)}::acc)binds[]typespace=Value|Type|Constr|Tagtypedefinition={dname:string;dspace:space;dline:int;dcol:int;drank:int}typereference={rline:int;rcol:int;rlen:int;rpath:stringlist;rname:string;rspace:space;ropens:stringlist}typeanalysis={spans:spanlistarray;occurrences:occurrencelist;definitions:definitionlist;references:referencelist;opens:stringlist;includes:stringlist;}letdefinition?name(tokens:(int*int*string)array)(i:int)(space:space)(rank:int):definition=letline,col,text=tokens.(i)in{dname=Option.valuename~default:text;dspace=space;dline=line-1;dcol=col;drank=rank}letreference?(opens=[])(tokens:(int*int*string)array)(i:int)(path:stringlist)(space:space):reference=letline,col,text=tokens.(i)in{rline=line-1;rcol=col;rlen=String.lengthtext;rpath=path;rname=text;rspace=space;ropens=opens}letlines(src:string)(tokens:(int*int*string*category)list):spanlistarray=letnlines=List.length(String.split_on_char'\n'src)inletout=Array.makenlines[]inList.iter(fun(line1,col,text,category)->(* a token over several lines: a span per line, the first at the
* token's column, the others at 0 *)List.iteri(funkpiece->letline=line1-1+kinifpiece<>""&&line>=0&&line<nlinesthenout.(line)<-{col=(ifk=0thencolelse0);text=piece;category}::out.(line))(String.split_on_char'\n'text))tokens;Array.mapList.revout