Skip to content

Commit be40b4b

Browse files
chore: correct origin line numbers after newline separators (#850)
1 parent 8a92847 commit be40b4b

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

config/src/main/java/com/typesafe/config/impl/ConfigNodeField.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ protected Token separator() {
6666
return null;
6767
}
6868

69+
protected int newlineCountBeforeValue() {
70+
int newlineCount = 0;
71+
for (AbstractConfigNode child : children) {
72+
if (child instanceof AbstractConfigNodeValue)
73+
return newlineCount;
74+
75+
if (child instanceof ConfigNodeSingleToken
76+
&& Tokens.isNewline(((ConfigNodeSingleToken) child).token())) {
77+
newlineCount++;
78+
}
79+
}
80+
return newlineCount;
81+
}
82+
6983
protected List<String> comments() {
7084
List<String> comments = new ArrayList<String>();
7185
for (AbstractConfigNode child : children) {

config/src/main/java/com/typesafe/config/impl/ConfigParser.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ private AbstractConfigValue parseValue(AbstractConfigNodeValue n, List<String> c
120120
return v;
121121
}
122122

123+
private void advanceLineNumberBeforeValue(ConfigNodeField field) {
124+
lineNumber += field.newlineCountBeforeValue();
125+
}
126+
123127
private static AbstractConfigObject createValueUnderPath(Path path,
124128
AbstractConfigValue value) {
125129
// for path foo.bar, we are creating
@@ -237,13 +241,14 @@ private AbstractConfigObject parseObject(ConfigNodeObject n) {
237241
parseInclude(values, (ConfigNodeInclude)node);
238242
lastWasNewline = false;
239243
} else if (node instanceof ConfigNodeField) {
244+
ConfigNodeField field = (ConfigNodeField) node;
240245
lastWasNewline = false;
241-
Path path = ((ConfigNodeField) node).path().value();
242-
comments.addAll(((ConfigNodeField) node).comments());
246+
Path path = field.path().value();
247+
comments.addAll(field.comments());
243248

244249
// path must be on-stack while we parse the value
245250
pathStack.push(path);
246-
if (((ConfigNodeField) node).separator() == Tokens.PLUS_EQUALS) {
251+
if (field.separator() == Tokens.PLUS_EQUALS) {
247252
// we really should make this work, but for now throwing
248253
// an exception is better than producing an incorrect
249254
// result. See
@@ -262,12 +267,13 @@ private AbstractConfigObject parseObject(ConfigNodeObject n) {
262267
AbstractConfigNodeValue valueNode;
263268
AbstractConfigValue newValue;
264269

265-
valueNode = ((ConfigNodeField) node).value();
270+
valueNode = field.value();
271+
advanceLineNumberBeforeValue(field);
266272

267273
// comments from the key token go to the value token
268274
newValue = parseValue(valueNode, comments);
269275

270-
if (((ConfigNodeField) node).separator() == Tokens.PLUS_EQUALS) {
276+
if (field.separator() == Tokens.PLUS_EQUALS) {
271277
arrayCount -= 1;
272278

273279
List<AbstractConfigValue> concat = new ArrayList<AbstractConfigValue>(2);

config/src/test/scala/com/typesafe/config/impl/ConfParserTest.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,21 @@ class ConfParserTest extends TestUtils {
878878
assertEquals("bar", conf.getString("foo"))
879879
}
880880

881+
@Test
882+
def valuesAfterSeparatorNewlineHaveCorrectOriginLine() {
883+
val scalar = parseConfig("a=\n42")
884+
assertEquals(2, scalar.getValue("a").origin().lineNumber())
885+
886+
val objectAfterEquals = parseConfig("a=\n{\n b=1\n}")
887+
assertEquals(2, objectAfterEquals.getObject("a").origin().lineNumber())
888+
889+
val objectAfterColon = parseConfig("a:\n{\n b=1\n}")
890+
assertEquals(2, objectAfterColon.getObject("a").origin().lineNumber())
891+
892+
val arrayAfterEquals = parseConfig("a=\n[\n 1\n]")
893+
assertEquals(2, arrayAfterEquals.getList("a").origin().lineNumber())
894+
}
895+
881896
@Test
882897
def acceptMultiPeriodNumericPath() {
883898
val conf1 = ConfigFactory.parseString("0.1.2.3=foobar1")

0 commit comments

Comments
 (0)