123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149(* {{{ COPYING *(
This file is part of Merlin, an helper for ocaml editors
Copyright (C) 2013 - 2015 Frédéric Bour <frederic.bour(_)lakaban.net>
Thomas Refis <refis.thomas(_)gmail.com>
Simon Castellan <simon.castellan(_)iuwt.fr>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The Software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall
the authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising
from, out of or in connection with the software or the use or other dealings
in the Software.
)* }}} *)openStdlet{Logger.log}=Logger.for_section"context"typet=|ConstructorofTypes.constructor_description*Location.t(* We attach the constructor description here so in the case of
disambiguated constructors we actually directly look for the type
path (cf. #486, #794). *)|Expr|LabelofTypes.label_description(* Similar to constructors. *)|Module_path|Module_type|Patt|Type|Constant|Unknownletto_string=function|Constructor(cd,_)->Printf.sprintf"constructor %s"cd.cstr_name|Expr->"expression"|Labellbl->Printf.sprintf"record field %s"lbl.lbl_name|Module_path->"module path"|Module_type->"module type"|Patt->"pattern"|Constant->"constant"|Type->"type"|Unknown->"unknown"(* Distinguish between "Mo[d]ule.something" and "Module.some[t]hing" *)letcursor_on_longident_end~cursor:cursor_pos~lid_loc:{Asttypes.loc;txt=lid}name=matchlidwith|Longident.Lident_->true|_->letend_offset=loc.loc_end.pos_cnuminletcstr_name_size=(* FIXME: this is britle, but lids don't have precise enough location
information to handle these cases correctly. *)letname_lenght=String.lengthnameinifPprintast.needs_parensnamethenname_lenght+2elsename_lenghtinletconstr_pos={loc.loc_endwithpos_cnum=end_offset-cstr_name_size}inLexing.compare_poscursor_posconstr_pos>=0letinspect_pattern(typea)~cursor~lid(p:aTypedtree.general_pattern)=log~title:"inspect_context""%a"Logger.fmt(funfmt->Format.fprintffmt"current pattern is: %a"(Printtyped.pattern0)p);matchp.pat_descwith|Tpat_anywhenLongident.lastlid="_"->None|Tpat_var(_,str_loc)whenLongident.lastlid=str_loc.txt->None|Tpat_alias(_,_,str_loc)whenLongident.lastlid=str_loc.txt->(* Assumption: if [Browse.enclosing] stopped on this node and not on the
subpattern, then it must mean that the cursor is on the alias. *)None|Tpat_construct(lid_loc,cd,_,_)whencursor_on_longident_end~cursor~lid_loccd.cstr_name&&Longident.lastlid=Longident.lastlid_loc.txt->(* Assumption: if [Browse.enclosing] stopped on this node and not on the
subpattern, then it must mean that the cursor is on the constructor
itself. *)Some(Constructor(cd,lid_loc.loc))|Tpat_construct_->SomeModule_path|_->SomePattletinspect_expression~cursor~lide:t=matche.Typedtree.exp_descwith|Texp_construct(lid_loc,cd,_)->(* TODO: is this first test necessary ? *)ifLongident.lastlid=Longident.lastlid_loc.txtthenifcursor_on_longident_end~cursor~lid_loccd.cstr_namethenConstructor(cd,lid_loc.loc)elseModule_pathelseModule_path|Texp_ident(p,lid_loc,_)->letname=Path.lastpinlog~title:"inspect_context""name is: [%s]"name;ifname="*type-error*"then(* For type_enclosing: it is enough to return Module_path here.
- If the cursor was on the end of the lid typing should fail anyway
- If the cursor is on a segment of the path it should be typed ad a
Module_path
TODO: double check that this is correct-enough behavior for Locate *)Module_pathelseifcursor_on_longident_end~cursor~lid_locnamethenExprelseModule_path|Texp_constant_->Constant|_->Exprletinspect_browse_tree~cursorlidbrowse:toption=log~title:"inspect_context""current node is: [%s]"(String.concat~sep:"|"(List.map~f:(Mbrowse.print())browse));matchMbrowse.enclosingcursorbrowsewith|[]->log~title:"inspect_context""no enclosing around: %a"Lexing.print_positioncursor;SomeUnknown|enclosings->(letopenBrowse_rawinletnode=Browse_tree.of_browseenclosingsinlog~title:"inspect_context""current enclosing node is: %s"(string_of_nodenode.Browse_tree.t_node);matchnode.Browse_tree.t_nodewith|Patternp->inspect_pattern~cursor~lidp|Value_description_|Type_declaration_|Extension_constructor_|Module_binding_name_|Module_declaration_name_->None|Module_expr_|Open_description_->SomeModule_path|Module_type_->SomeModule_type|Core_type{ctyp_desc=Ttyp_package_;_}->SomeModule_type|Core_type_->SomeType|Record_field(_,lbl,_)whenLongident.lastlid=lbl.lbl_name->(* if we stopped here, then we're on the label itself, and whether or
not punning is happening is not important *)Some(Labellbl)|Expressione->Some(inspect_expression~cursor~lide)|_->SomeUnknown)