Skip to content

Commit

Permalink
(#19) Find records/macros/type in quotes
Browse files Browse the repository at this point in the history
Closes #19.
  • Loading branch information
hcs42 committed Jan 24, 2015
1 parent 4763678 commit 3efa61f
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions bin/vim-erlang-tags.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@
CRE
end).

-define(RE_FUNCTIONS, ?COMPILE("^([a-z][a-zA-Z0-9_@]*)\\s*\\(")).
-define(RE_TYPESPECS, ?COMPILE("^-\\s*(type|opaque)\\s*([a-zA-Z0-9_@]*)\\b")).
-define(RE_DEFINES, ?COMPILE("^-\\s*(record|define)\\s*\\(\\s*([a-zA-Z0-9_@]*)\\b")).
-define(RE_FUNCTIONS, ?COMPILE("^([a-z][a-zA-Z0-9_@]*)\\s*\\(")).
-define(RE_TYPESPECS1, ?COMPILE("^-\\s*(type|opaque)\\s*([a-zA-Z0-9_@]+)\\b")).
-define(RE_TYPESPECS2, ?COMPILE("^-\\s*(type|opaque)\\s*'([^ \\t']+)'")).
-define(RE_DEFINES1, ?COMPILE("^-\\s*(record|define)\\s*\\(\\s*([a-zA-Z0-9_@]+)\\b")).
-define(RE_DEFINES2, ?COMPILE("^-\\s*(record|define)\\s*\\(\\s*'([^ \\t']+)'")).

-define(DEFAULT_PATH, ".").

Expand Down Expand Up @@ -287,28 +289,44 @@ add_tags_from_file(File, Tags) ->
ok = scan_tags(Contents, {Tags, File, ModName}).

scan_tags(Contents, {Tags, File, ModName}) ->
case re:run(Contents, ?RE_FUNCTIONS, [{capture, all, binary}, global]) of
nomatch ->
ok;
{match, Matches1} ->
[ add_func_tags(Tags, File, ModName, FuncName )
|| [_, FuncName] <- Matches1 ]
end,
case re:run(Contents, ?RE_TYPESPECS, [{capture, all, binary}, global]) of
nomatch ->
ok;
{match, Matches2} ->
[ add_type_tags(Tags, File, ModName, Attr, TypeName)
|| [_, Attr, TypeName] <- Matches2 ]
end,
case re:run(Contents, ?RE_DEFINES, [{capture, all, binary}, global]) of
scan_tags_core(
Contents, ?RE_FUNCTIONS,
fun([_, FuncName]) ->
add_func_tags(Tags, File, ModName, FuncName)
end),
scan_tags_core(
Contents, ?RE_TYPESPECS1,
fun([_, Attr, TypeName]) ->
InnerPattern = [TypeName, "\\>"],
add_type_tags(Tags, File, ModName, Attr, TypeName, InnerPattern)
end),
scan_tags_core(
Contents, ?RE_TYPESPECS2,
fun([_, Attr, TypeName]) ->
InnerPattern = [$', TypeName, $'],
add_type_tags(Tags, File, ModName, Attr, TypeName, InnerPattern)
end),
scan_tags_core(
Contents, ?RE_DEFINES1,
fun([_, Attr, Name]) ->
InnerPattern = [Name, "\\>"],
add_record_or_macro_tag(Tags, File, Attr, Name, InnerPattern)
end),
scan_tags_core(
Contents, ?RE_DEFINES2,
fun([_, Attr, Name]) ->
InnerPattern = [$', Name, $'],
add_record_or_macro_tag(Tags, File, Attr, Name, InnerPattern)
end),
ok.

scan_tags_core(Contents, Pattern, Fun) ->
case re:run(Contents, Pattern, [{capture, all, binary}, global]) of
nomatch ->
ok;
{match, Matches3} ->
[ add_record_or_macro_tag(Tags, File, Attr, Name )
|| [_, Attr, Name] <- Matches3 ]
end,
ok.
{match, Matches} ->
lists:foreach(Fun, Matches)
end.

%%%=============================================================================
%%% Add specific tags
Expand Down Expand Up @@ -344,11 +362,11 @@ add_func_tags(Tags, File, ModName, FuncName) ->
add_tag(Tags, FuncName, File, ["/^", FuncName, "\\>/"], local, $f).

% File contains the type ModName:Type; add this information to Tags.
add_type_tags(Tags, File, ModName, Attribute, TypeName) ->
add_type_tags(Tags, File, ModName, Attribute, TypeName, InnerPattern) ->

log("Type definition found: ~s~n", [TypeName]),

Pattern = ["/^-\\s\\*", Attribute, "\\s\\*", TypeName, "\\>/"],
Pattern = ["/^-\\s\\*", Attribute, "\\s\\*", InnerPattern, $/],

% Global entry:
% mymod:mytype <tab> ./mymod.erl <tab> /^-type\s\*mytype\>/
Expand All @@ -363,7 +381,7 @@ add_type_tags(Tags, File, ModName, Attribute, TypeName) ->
add_tag(Tags, TypeName, File, Pattern, local, $t).

% File contains a macro or record called Name; add this information to Tags.
add_record_or_macro_tag(Tags, File, Attribute, Name) ->
add_record_or_macro_tag(Tags, File, Attribute, Name, InnerPattern) ->

{Kind, Prefix} =
case Attribute of
Expand All @@ -388,15 +406,15 @@ add_record_or_macro_tag(Tags, File, Attribute, Name) ->
% mymac ./mymod.erl /^-define\s\*\<mymac\>/;" m file:
% mymac ./myhrl.hrl /^-define\s\*\<mymac\>/;" m
add_tag(Tags, Name, File,
["/^-\\s\\*", Attribute, "\\s\\*(\\s\\*", Name, "\\>/"],
["/^-\\s\\*", Attribute, "\\s\\*(\\s\\*", InnerPattern, "/"],
Scope, Kind),

% #myrec ./mymod.erl /^-record\s\*\<myrec\>/;" r file:
% #myrec ./myhrl.hrl /^-record\s\*\<myrec\>/;" r
% ?mymac ./mymod.erl /^-define\s\*\<mymac\>/;" m file:
% ?mymac ./myhrl.hrl /^-define\s\*\<mymac\>/;" m
add_tag(Tags, [Prefix|Name], File,
["/^-\\s\\*", Attribute, "\\s\\*(\\s\\*", Name, "\\>/"],
["/^-\\s\\*", Attribute, "\\s\\*(\\s\\*", InnerPattern, "/"],
Scope, Kind).

add_tag(Tags, Tag, File, TagAddress, Scope, Kind) ->
Expand Down

0 comments on commit 3efa61f

Please sign in to comment.