-
Notifications
You must be signed in to change notification settings - Fork 9
/
erlide_comment.erl
54 lines (42 loc) · 1.22 KB
/
erlide_comment.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
%% Author: jakob
%% Created: Mar 23, 2006
-module(erlide_comment).
%%
%% Include files
%%
%%
%% Exported Functions
%%
-export([toggle_comment/3]).
%-define(DEBUG, 1). %
-include_lib("erlide_common/include/erlide_dbglog.hrl").
%%
%% API Functions
%%
toggle_comment(Text, From, Length) ->
?D({Text, From, Length}),
{_, _, Lines} = erlide_text:get_text_and_lines(Text, From, Length),
?D(Lines),
LineF = case lists:all(fun(L) -> is_comment_line(L) end, Lines) of
true ->
fun(L) -> uncomment_line(L) end;
false ->
fun(L) -> comment_line(L) end
end,
lists:flatten(lists:map(LineF, Lines)).
%%
%% Local Functions
%%
is_comment_line("") -> false;
is_comment_line(" " ++ Rest) -> is_comment_line(Rest);
is_comment_line("\t" ++ Rest) -> is_comment_line(Rest);
is_comment_line("%" ++ _) -> true;
is_comment_line(_) -> false.
uncomment_line(Line) -> uncomment_line_x(Line).
uncomment_line_x("%% " ++ Rest) -> Rest;
uncomment_line_x("%%" ++ Rest) -> Rest;
uncomment_line_x("%" ++ Rest) -> Rest;
uncomment_line_x(" " ++ Rest) -> uncomment_line_x(Rest);
uncomment_line_x(Line) -> Line.
comment_line("") -> "";
comment_line(Line) -> ["%% ", Line].