Permalink
Browse files

Refresh gives double outline

  • Loading branch information...
1 parent 19b54d5 commit a933e59b744da28ecb25911200e35a4b30ca22ae @jakobc jakobc committed Mar 4, 2013
@@ -3,7 +3,8 @@
*/
package org.erlide.core;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
@@ -27,53 +28,54 @@
*/
public class ParsingTests {
- IErlModule module;
+ IErlModule module;
- /**
- * @throws java.lang.Exception
- */
- @Before
- public void setUp() throws Exception {
- final IErlModel model = ErlModelManager.getErlangModel();
- module = model.getModuleFromText(model, "testing", "", null);
- }
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ final IErlModel model = ErlModelManager.getErlangModel();
+ module = model.getModuleFromText(model, "testing", "", null);
+ }
- /**
- * @throws java.lang.Exception
- */
- @After
- public void tearDown() throws Exception {
- module.dispose();
- module = null;
- }
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ module.dispose();
+ module = null;
+ }
- private boolean parse(final String s) {
- final String scannerModuleName = module.getScannerName();
- ErlideScanner.create(scannerModuleName);
- boolean result = false;
- try {
- ErlideScanner.initialScan(scannerModuleName, "", s, false);
- final IErlParser parser = ErlModelManager.getErlangModel()
- .getParser();
- result = parser.parse(module, scannerModuleName, false, "", false);
- } finally {
- ErlideScanner.dispose(scannerModuleName);
- }
- return result;
- }
+ private boolean parse(final String s) {
+ final String scannerModuleName = module.getScannerName();
+ ErlideScanner.create(scannerModuleName);
+ boolean result = false;
+ try {
+ ErlideScanner.initialScan(scannerModuleName, "", s, false);
+ final IErlParser parser = ErlModelManager.getErlangModel()
+ .getParser();
+ result = parser.parse(module, scannerModuleName, false, "", s,
+ false);
+ } finally {
+ ErlideScanner.dispose(scannerModuleName);
+ }
+ return result;
+ }
- @Test
- public void parseCompileDirective() throws ErlModelException {
- final String sourceContent = "[inline,{hipe,[{regalloc,linear_scan}]}]";
- final String source = "-compile(" + sourceContent + ").";
- assertTrue(parse(source));
- final IErlElement attribute = TestingSupport.createErlAttribute(module,
- "compile", null, sourceContent, 0, 50);
- final List<IErlElement> expected = new ArrayList<IErlElement>(1);
- expected.add(attribute);
- final Collection<IErlElement> actual = module.getChildren();
- // assertEquals(expected, actual);
- assertEquals(expected.toString(), actual.toString());
- }
+ @Test
+ public void parseCompileDirective() throws ErlModelException {
+ final String sourceContent = "[inline,{hipe,[{regalloc,linear_scan}]}]";
+ final String source = "-compile(" + sourceContent + ").";
+ assertTrue(parse(source));
+ final IErlElement attribute = TestingSupport.createErlAttribute(module,
+ "compile", null, sourceContent, 0, 50);
+ final List<IErlElement> expected = new ArrayList<IErlElement>(1);
+ expected.add(attribute);
+ final Collection<IErlElement> actual = module.getChildren();
+ // assertEquals(expected, actual);
+ assertEquals(expected.toString(), actual.toString());
+ }
}
@@ -9,7 +9,7 @@
%%
%% called from Java
--export([initial_parse/5, reparse/2, remove_cache_files/2]).
+-export([initial_parse/6, reparse/2, remove_cache_files/2]).
%% called from Erlang
-export([get_module_refs/4]).
@@ -20,7 +20,7 @@
%% -define(DEBUG, 1).
--define(CACHE_VERSION, 29).
+-define(CACHE_VERSION, 30).
-define(SERVER, erlide_noparse).
-include("erlide.hrl").
@@ -32,17 +32,19 @@
%% API Functions
%%
--spec initial_parse(atom(), string(), string(), boolean(), boolean()) ->
+-spec initial_parse(atom(), string(), string(), string(), boolean(), boolean()) ->
{ok, #model{}, cached | renewing | dont_use_cache, [#ref{}]}
| {error, term(), term()}.
-initial_parse(ScannerName, ModuleFileName, StateDir, UseCache,
+initial_parse(ScannerName, ModuleFileName, InitialText, StateDir, UseCache,
UpdateSearchServer) ->
try
+ ?D({ScannerName, ModuleFileName, UseCache, UpdateSearchServer}),
BaseName = filename:join(StateDir, atom_to_list(ScannerName)),
RefsFileName = BaseName ++ ".refs",
RenewFun = fun(_F) ->
- Tokens = get_tokens(ScannerName, ModuleFileName, StateDir),
+ Tokens = get_tokens(ScannerName, ModuleFileName,
+ InitialText, StateDir),
{Model, Refs} =
do_parse(ScannerName, RefsFileName, Tokens, StateDir,
UpdateSearchServer),
@@ -82,8 +84,14 @@ get_module_refs(ScannerName, ModulePath, StateDir, UpdateSearchServer) ->
{ok, Binary} ->
binary_to_term(Binary);
_ ->
- {ok, _, _, Refs= initial_parse(ScannerName, ModulePath, StateDir,
- true, UpdateSearchServer),
+ InitialText = case file:read_file(ModulePath) of
+ {ok, InitialTextBin} ->
+ binary_to_list(InitialTextBin);
+ _ ->
+ ""
+ end,
+ {ok, _, _, Refs= initial_parse(ScannerName, ModulePath, InitialText,
+ StateDir, true, UpdateSearchServer),
Refs
end.
@@ -102,27 +110,21 @@ remove_cache_files(ScannerName, StateDir) ->
%% Internal functions
%%
-get_tokens(ScannerName, ModuleFileName, StateDir) ->
+get_tokens(ScannerName, ModuleFileName, InitialText, StateDir) ->
case whereis(ScannerName) of
undefined ->
- case file:read_file(ModuleFileName) of
- {ok, InitialTextBin} ->
- InitialText = binary_to_list(InitialTextBin),
- {{_Cached, Module}, _Text} = erlide_scanner:initial_scan(ScannerName, ModuleFileName, InitialText, StateDir, true),
- erlide_scan_model:get_all_tokens(Module);
- {error, Error} ->
- erlide_log:log({"Module not found: ", ModuleFileName, Error}),
- %% not much to do here
- []
- end;
+ {{_Cached, Module}, _Text} = erlide_scanner:initial_scan(ScannerName, ModuleFileName, InitialText, StateDir, true),
+ erlide_scan_model:get_all_tokens(Module);
_ ->
erlide_scanner:get_tokens(ScannerName)
end.
do_parse(ScannerName, RefsFileName, Tokens, StateDir, UpdateSearchServer) ->
{Forms, Comments, References} = erlide_np:parse(Tokens),
+ ?D(Forms),
Model = #model{forms=Forms, comments=Comments},
CompactModel = erlide_np_util:compact_model(Model),
+ ?D(CompactModel),
case StateDir of
"" -> ok;
_ ->
@@ -28,7 +28,7 @@
do_scan(ScannerName, InitialText) ->
?D(do_scan),
- Lines = split_lines_w_lengths(InitialText),
+ Lines = erlide_scan_util:split_lines_w_lengths(InitialText),
LineTokens = [scan_line(L) || L <- Lines],
?D([ScannerName]), % , InitialText, LineTokens]),
#module{name=ScannerName, lines=Lines, tokens=LineTokens}.
@@ -56,7 +56,7 @@ get_token_window(Module, Offset, Before, After) ->
{A, B}.
get_token_at(Module, Offset) ->
- case find_line_w_offset(Offset, Module#module.tokens) of
+ case erlide_scan_util:find_line_w_offset(Offset, Module#module.tokens) of
{N, Pos, _Length, Tokens, false} ->
case get_token_at_aux(Tokens, Offset - Pos) of
token_not_found ->
@@ -76,59 +76,16 @@ get_text(#module{lines=Lines}) ->
%% Local Functions
%%
+replace_between(_From, _Length, With, []) ->
+ %% allow for special case when replacing empty set
+ %% (the Length may be 1 from replace_between_lines)
+ With;
replace_between(From, Length, With, In) ->
+ ?D({From, Length, With, In}),
{A, B} = lists:split(From, In),
{_, C} = lists:split(Length, B),
A++With++C.
-%% [{Length, TextIncNL}...]
-split_lines_w_lengths(Text) ->
- split_lines_w_lengths(Text, 0, [], []).
-
-split_lines_w_lengths("", _Length, [], Acc) ->
- lists:reverse(Acc);
-split_lines_w_lengths("", Length, LineAcc, Acc) ->
- lists:reverse(Acc, [{Length, lists:reverse(LineAcc)}]);
-split_lines_w_lengths("\r\n" ++ Text, Length, LineAcc, Acc) ->
- split_lines_w_lengths(Text, 0, [],
- [{Length+2, lists:reverse(LineAcc, "\r\n")} | Acc]);
-split_lines_w_lengths("\n" ++ Text, Length, LineAcc, Acc) ->
- split_lines_w_lengths(Text, 0, [],
- [{Length+1, lists:reverse(LineAcc, "\n")} | Acc]);
-split_lines_w_lengths("\r" ++ Text, Length, LineAcc, Acc) ->
- split_lines_w_lengths(Text, 0, [],
- [{Length+1, lists:reverse(LineAcc, "\r")} | Acc]);
-split_lines_w_lengths([C | Text], Length, LineAcc, Acc) ->
- split_lines_w_lengths(Text, Length+1, [C | LineAcc], Acc).
-
-%% Find a line from [{Length, Line
-
-find_line_w_offset(Offset, Lines) ->
- find_line_w_offset(Offset, 0, 0, Lines).
-
-find_line_w_offset(0, _Pos, _N, []) ->
- {0, 0, 0, "", on_eof};
-find_line_w_offset(_Offset, _Pos, _N, []) ->
- not_found;
-find_line_w_offset(Offset, Pos, N, [{Length, _Line} | Lines]) when Offset >= Pos+Length, Lines =/= [] ->
- find_line_w_offset(Offset, Pos+Length, N+1, Lines);
-find_line_w_offset(Offset, Pos, N, [{Length, Line} |_]) when Pos =< Offset, Offset < Pos + Length ->
- {N, Pos, Length, Line, false};
-find_line_w_offset(Offset, Pos, N, [{Length, Line}]) ->
- case ends_with_newline(Line) orelse Offset > Pos + Length of
- true ->
- {N+1, Pos+Length, 0, "", beyond_eof};
- false ->
- {N, Pos+Length, Length, Line, on_eof}
- end.
-
-ends_with_newline("") -> false;
-ends_with_newline("\n") -> true;
-ends_with_newline("\r") -> true;
-ends_with_newline("\r\n") -> true;
-ends_with_newline([_C | R]) ->
- ends_with_newline(R).
-
%%
%% Nicer version of string:substring/2 accepting out-of-bounds parameters
%% (should be removed eventually)
@@ -154,15 +111,15 @@ substr(Text, Start, Length) ->
string:substr(Text, Start, Length).
replace_between_lines(From, Length, With, Lines) ->
- {LineNo1, Pos1, _Length1, Line1, Beyond1} = find_line_w_offset(From, Lines),
+ {LineNo1, Pos1, _Length1, Line1, Beyond1} = erlide_scan_util:find_line_w_offset(From, Lines),
?D({LineNo1, Pos1, _Length1, Line1, Beyond1}),
FirstPiece = substr(Line1, 1, From-Pos1),
{LineNo2, Pos2, _Length2, Line2, Beyond2} =
case Length of
0 ->
{LineNo1, Pos1, unused, Line1, Beyond1};
_ ->
- find_line_w_offset(From+Length, Lines)
+ erlide_scan_util:find_line_w_offset(From+Length, Lines)
end,
?D({LineNo2, Pos2, _Length2, Line2, Beyond2}),
LastPiece = substr(Line2, From+Length-Pos2+1),
@@ -178,7 +135,7 @@ replace_between_lines(From, Length, With, Lines) ->
_ ->
{FirstPiece++With++LastPiece, LineNo2-LineNo1+1}
end,
- WLines = split_lines_w_lengths(NewText),
+ WLines = erlide_scan_util:split_lines_w_lengths(NewText),
?D(WLines),
{LineNo1, NOldLines, WLines,
replace_between(LineNo1, NOldLines, WLines, Lines)}.
@@ -269,7 +226,7 @@ get_tokens_at(Module, Offset, N) ->
get_tokens_at(_Module, _Offset, 0, Acc) ->
lists:reverse(Acc);
get_tokens_at(Module, Offset, N, Acc0) ->
- case find_line_w_offset(Offset, Module#module.tokens) of
+ case erlide_scan_util:find_line_w_offset(Offset, Module#module.tokens) of
{LineNo, Pos, Length, Tokens, false} ->
{M, Ts} = get_tokens_at_aux(Tokens, Offset - Pos, N),
Acc1 =
@@ -0,0 +1,55 @@
+%% @author jakob
+%% @doc @todo Add description to erlide_scan_util.
+
+
+-module(erlide_scan_util).
+
+-export([split_lines_w_lengths/1, find_line_w_offset/2]).
+
+%% [{Length, TextIncNL}...]
+split_lines_w_lengths(Text) ->
+ split_lines_w_lengths(Text, 0, [], []).
+
+split_lines_w_lengths("", _Length, [], Acc) ->
+ lists:reverse(Acc);
+split_lines_w_lengths("", Length, LineAcc, Acc) ->
+ lists:reverse(Acc, [{Length, lists:reverse(LineAcc)}]);
+split_lines_w_lengths("\r\n" ++ Text, Length, LineAcc, Acc) ->
+ split_lines_w_lengths(Text, 0, [],
+ [{Length+2, lists:reverse(LineAcc, "\r\n")} | Acc]);
+split_lines_w_lengths("\n" ++ Text, Length, LineAcc, Acc) ->
+ split_lines_w_lengths(Text, 0, [],
+ [{Length+1, lists:reverse(LineAcc, "\n")} | Acc]);
+split_lines_w_lengths("\r" ++ Text, Length, LineAcc, Acc) ->
+ split_lines_w_lengths(Text, 0, [],
+ [{Length+1, lists:reverse(LineAcc, "\r")} | Acc]);
+split_lines_w_lengths([C | Text], Length, LineAcc, Acc) ->
+ split_lines_w_lengths(Text, Length+1, [C | LineAcc], Acc).
+
+%% Find a line from [{Length, Line
+
+find_line_w_offset(Offset, Lines) ->
+ find_line_w_offset(Offset, 0, 0, Lines).
+
+find_line_w_offset(0, _Pos, _N, []) ->
+ {0, 0, 0, "", on_eof};
+find_line_w_offset(_Offset, _Pos, _N, []) ->
+ not_found;
+find_line_w_offset(Offset, Pos, N, [{Length, _Line} | Lines]) when Offset >= Pos+Length, Lines =/= [] ->
+ find_line_w_offset(Offset, Pos+Length, N+1, Lines);
+find_line_w_offset(Offset, Pos, N, [{Length, Line} |_]) when Pos =< Offset, Offset < Pos + Length ->
+ {N, Pos, Length, Line, false};
+find_line_w_offset(Offset, Pos, N, [{Length, Line}]) ->
+ case ends_with_newline(Line) orelse Offset > Pos + Length of
+ true ->
+ {N+1, Pos+Length, 0, "", beyond_eof};
+ false ->
+ {N, Pos+Length, Length, Line, on_eof}
+ end.
+
+ends_with_newline("") -> false;
+ends_with_newline("\n") -> true;
+ends_with_newline("\r") -> true;
+ends_with_newline("\r\n") -> true;
+ends_with_newline([_C | R]) ->
+ ends_with_newline(R).
Oops, something went wrong.

0 comments on commit a933e59

Please sign in to comment.