Skip to content

Commit

Permalink
Merge pull request #87 from lukasstadler/source_setcode_fix
Browse files Browse the repository at this point in the history
clear text map when updating code in ClientManagedFileSource
  • Loading branch information
lukasstadler committed Mar 9, 2016
2 parents dff6f76 + 0a7044b commit c9dc9cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,30 @@ public void literalSources() throws IOException {
assertEquals(literal.getReader().read(buffer), code.length());
assertEquals(new String(buffer), code);
}

@Test
public void clientManagedSourceChange() throws IOException {
final String path = "test.input";
final String code1 = "test\ntest";
final String code2 = "test\ntest\nlonger\ntest";
final Source source1 = Source.fromFileName(code1, path);
assertEquals(source1.getCode(), code1);
assertEquals(source1.getLineNumber(code1.length() - 1), 2);
final Source source2 = Source.fromFileName(code2, path);
assertEquals(source2.getCode(), code2);
assertEquals(source2.getLineNumber(code2.length() - 1), 4);
}

@Test
public void clientManagedSourceChangeAbsolute() throws IOException {
final String path = new File("test.input").getAbsolutePath();
final String code1 = "test\ntest";
final String code2 = "test\ntest\nlonger\ntest";
final Source source1 = Source.fromFileName(code1, path);
assertEquals(source1.getCode(), code1);
assertEquals(source1.getLineNumber(code1.length() - 1), 2);
final Source source2 = Source.fromFileName(code2, path);
assertEquals(source2.getCode(), code2);
assertEquals(source2.getLineNumber(code2.length() - 1), 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,16 @@ public static Source fromFileName(CharSequence chars, String fileName) throws IO
if (source == null) {
source = new ClientManagedFileSource(file, fileName, path, chars);
nameToSource.put(path, new WeakReference<>(source));
return source;
}
} else if (source instanceof ClientManagedFileSource) {
}
if (source instanceof ClientManagedFileSource) {
final ClientManagedFileSource modifiableSource = (ClientManagedFileSource) source;
modifiableSource.setCode(chars);
return modifiableSource;
} else {
throw new IOException("Attempt to modify contents of a file Source");
}
return source;
}

/**
Expand Down Expand Up @@ -1052,6 +1053,7 @@ private static final class ClientManagedFileSource extends Source implements Clo
}

void setCode(CharSequence chars) {
clearTextMap();
this.code = chars.toString();
}

Expand Down

0 comments on commit c9dc9cb

Please sign in to comment.