Skip to content

Commit 3efa61f

Browse files
committed
(vim-erlang#19) Find records/macros/type in quotes
Closes vim-erlang#19.
1 parent 4763678 commit 3efa61f

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

bin/vim-erlang-tags.erl

+46-28
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@
8383
CRE
8484
end).
8585

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

9092
-define(DEFAULT_PATH, ".").
9193

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

289291
scan_tags(Contents, {Tags, File, ModName}) ->
290-
case re:run(Contents, ?RE_FUNCTIONS, [{capture, all, binary}, global]) of
291-
nomatch ->
292-
ok;
293-
{match, Matches1} ->
294-
[ add_func_tags(Tags, File, ModName, FuncName )
295-
|| [_, FuncName] <- Matches1 ]
296-
end,
297-
case re:run(Contents, ?RE_TYPESPECS, [{capture, all, binary}, global]) of
298-
nomatch ->
299-
ok;
300-
{match, Matches2} ->
301-
[ add_type_tags(Tags, File, ModName, Attr, TypeName)
302-
|| [_, Attr, TypeName] <- Matches2 ]
303-
end,
304-
case re:run(Contents, ?RE_DEFINES, [{capture, all, binary}, global]) of
292+
scan_tags_core(
293+
Contents, ?RE_FUNCTIONS,
294+
fun([_, FuncName]) ->
295+
add_func_tags(Tags, File, ModName, FuncName)
296+
end),
297+
scan_tags_core(
298+
Contents, ?RE_TYPESPECS1,
299+
fun([_, Attr, TypeName]) ->
300+
InnerPattern = [TypeName, "\\>"],
301+
add_type_tags(Tags, File, ModName, Attr, TypeName, InnerPattern)
302+
end),
303+
scan_tags_core(
304+
Contents, ?RE_TYPESPECS2,
305+
fun([_, Attr, TypeName]) ->
306+
InnerPattern = [$', TypeName, $'],
307+
add_type_tags(Tags, File, ModName, Attr, TypeName, InnerPattern)
308+
end),
309+
scan_tags_core(
310+
Contents, ?RE_DEFINES1,
311+
fun([_, Attr, Name]) ->
312+
InnerPattern = [Name, "\\>"],
313+
add_record_or_macro_tag(Tags, File, Attr, Name, InnerPattern)
314+
end),
315+
scan_tags_core(
316+
Contents, ?RE_DEFINES2,
317+
fun([_, Attr, Name]) ->
318+
InnerPattern = [$', Name, $'],
319+
add_record_or_macro_tag(Tags, File, Attr, Name, InnerPattern)
320+
end),
321+
ok.
322+
323+
scan_tags_core(Contents, Pattern, Fun) ->
324+
case re:run(Contents, Pattern, [{capture, all, binary}, global]) of
305325
nomatch ->
306326
ok;
307-
{match, Matches3} ->
308-
[ add_record_or_macro_tag(Tags, File, Attr, Name )
309-
|| [_, Attr, Name] <- Matches3 ]
310-
end,
311-
ok.
327+
{match, Matches} ->
328+
lists:foreach(Fun, Matches)
329+
end.
312330

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)