1:- module(aop_docs,[
    2  ]).    3
    4:- use_module('./internal').    5:- use_module('./context').    6
    7% aop:aspect_comment(Aspect, File, Comment)
    8:- dynamic aop:aspect_comment/3.    9
   10% aop:object_comment(Aspect, Object, File, Comment)
   11:- dynamic aop:object_comment/4.   12
   13% aop:method_comment(Aspect, Object, Method, File, Comment)
   14:- dynamic aop:method_comment/5.   15
   16prolog:comment_hook(PosComments, TermPos, Term) :-
   17  prolog_load_context(file, File),
   18  % filter for just our project files
   19  working_directory(Cwd, Cwd),
   20  string_concat(Cwd, SourceFile, File),
   21  process_comments(PosComments, SourceFile, TermPos, Term).
   22
   23process_comments(PosComments, SourceFile, TermPos, :- new_aspect(Aspect)) :-
   24  process_aspect_comments(Aspect, PosComments, SourceFile, TermPos).
   25
   26process_comments(PosComments, SourceFile, TermPos, :- in_aspect(Aspect)) :-
   27  process_aspect_comments(Aspect, PosComments, SourceFile, TermPos).
   28
   29process_comments(PosComments, SourceFile, TermPos, :- new_object(Object)) :-
   30  aop_load_context(aspect, Aspect),
   31  process_object_comments(Aspect, Object, PosComments, SourceFile, TermPos).
   32
   33process_comments(PosComments, SourceFile, TermPos, :- new_object(Object,_)) :-
   34  aop_load_context(aspect, Aspect),
   35  process_object_comments(Aspect, Object, PosComments, SourceFile, TermPos).
   36
   37process_comments(PosComments, SourceFile, TermPos, :- in_object(Object)) :-
   38  aop_load_context(aspect, Aspect),
   39  process_object_comments(Aspect, Object, PosComments, SourceFile, TermPos).
   40
   41process_comments(PosComments, SourceFile, TermPos, :- method Name/Arity) :-
   42  aop_load_context(object, Object),
   43  aop_load_context(aspect, Aspect),
   44  process_method_comments(Aspect, Object, Name/Arity, PosComments, SourceFile, TermPos).
   45
   46process_comments(PosComments, SourceFile, TermPos, ::Message :- Body ) :-
   47  process_comments(PosComments, SourceFile, TermPos, Message :- Body).
   48
   49process_comments(PosComments, SourceFile, TermPos, Message :- _Body ) :-
   50  aop_load_context(object, Object),
   51  aop_load_context(aspect, Aspect),
   52  functor(Message, Name, Arity),
   53  process_method_comments(Aspect, Object, Name/Arity, PosComments, SourceFile, TermPos).
   54
   55process_comments(PosComments, SourceFile, TermPos, ::Message ) :-
   56  process_comments(PosComments, SourceFile, TermPos, Message ).
   57
   58process_comments(PosComments, SourceFile, TermPos, Message ) :-
   59  aop_load_context(object, Object),
   60  aop_load_context(aspect, Aspect),
   61  functor(Message, Name, Arity),
   62  process_method_comments(Aspect, Object, Name/Arity, PosComments, SourceFile, TermPos).
   63
   64
   65process_aspect_comments(Aspect, PosComments, SourceFile, _TermPos) :-
   66  collect_comments(PosComments, Comments),
   67  retractall(aop:aspect_comment(Aspect, SourceFile, _)),
   68  retractall(aop:object_comment(Aspect, _, SourceFile, _)),
   69  retractall(aop:method_comment(Aspect, _, _, SourceFile, _)),
   70  assertz(aop:aspect_comment(Aspect, SourceFile, Comments)).
   71
   72process_object_comments(Aspect, Object, PosComments, SourceFile, _TermPos) :-
   73  collect_comments(PosComments, Comments),
   74  retractall(aop:object_comment(Aspect, Object, SourceFile, _)),
   75  retractall(aop:method_comment(Aspect, Object, _, SourceFile, _)),
   76  assertz(aop:object_comment(Aspect, Object, SourceFile, Comments)).
   77
   78process_method_comments(Aspect, Object, Name/Arity, PosComments, SourceFile, _TermPos) :-
   79  collect_comments(PosComments, Comments),
   80  assertz(aop:method_comment(Aspect, Object, Name/Arity, SourceFile, Comments)).
   81
   82% Comments are always in form of a list of Pos-Comment pairs, so just group them together
   83collect_comments(PosComments, Comments) :-
   84  findall(Comment,member(_Pos-Comment, PosComments),Comments)