Skip to content

Commit

Permalink
SourceUtil works with all cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Jul 11, 2013
1 parent 9a4e7b0 commit 168e4ad
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static String insertString(String source, String string, int lineNumber,
int position = positionInSource(source, lineNumber, columnNumber);
return source.substring(0, position) + string + source.substring(position);
}

/**
* Inserts string at specified position in source.
*/
Expand Down Expand Up @@ -77,13 +77,26 @@ public static String removeSourceFragment(String source, int lineNumber, int col
int endingPosition = positionInSource(source, lastLineNumber, lastColumnNumber);
return source.substring(0, begginingPosition) + source.substring(endingPosition);
}

/**
* Appends given code as the last line of the closure.
* Appends given code as the last line of the closure.
*/
public static String appendLineToClosure(String source, InvocationWithClosure invocation, String codeToBeInserted)
{
return insertString(source, codeToBeInserted, invocation.getLastLineNumber(), invocation.getLastColumnNumber() - 1);
codeToBeInserted = codeToBeInserted.trim();

String sourceToInvocation = source.substring(0,
positionInSource(source, invocation.getLineNumber(), invocation.getColumnNumber()));
String invocationIndentation = sourceToInvocation.substring(sourceToInvocation.lastIndexOf("\n") + 1);
StringBuilder indent = new StringBuilder(invocationIndentation.length());
for (int i = 0; i < invocationIndentation.length(); i++)
{
indent.append(' ');
}
invocationIndentation = indent.toString();

return insertString(source, addNewLineAtEnd(INDENT + codeToBeInserted) + invocationIndentation, invocation.getLastLineNumber(),
invocation.getLastColumnNumber() - 1);
}

/**
Expand Down Expand Up @@ -118,12 +131,16 @@ public static String insertIntoInvocationAtPath(String source, String codeToBeIn
invocationOptional = previousInvocation.invocationWithClosureByName(path[level]);
if (!invocationOptional.isPresent())
{
String invocationPath = createInvocationPath(level, codeToBeInserted, Arrays.copyOfRange(path, level, path.length));
source = insertString(source, invocationPath, previousInvocation.getLastLineNumber(), previousInvocation.getLastColumnNumber() - 1);
String invocationPath = createInvocationPath(level, codeToBeInserted,
Arrays.copyOfRange(path, level, path.length));
source = appendLineToClosure(source, previousInvocation, invocationPath);
return source;
}
}


InvocationWithClosure invocation = invocationOptional.get();
source = appendLineToClosure(source, invocation, codeToBeInserted);

return source;
}

Expand All @@ -144,7 +161,7 @@ public static String createInvocationPath(int indentLevel, String content, Strin
builder.append(path[level]);
builder.append(" {\n");
}

indent(builder, indentLevel + path.length);
builder.append(addNewLineAtEnd(content));

Expand Down Expand Up @@ -178,7 +195,7 @@ public static String indent(int times)
indent(builder, times);
return builder.toString();
}

public static String addNewLineAtEnd(String source)
{
return source.endsWith("\n") ? source : source + "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testAppendLineToClosure()
"\n" +
"println xyz\n";
String codeToBeInserted = "" +
" something new\n";
"something new\n";

SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
InvocationWithClosure invocation;
Expand Down Expand Up @@ -153,6 +153,34 @@ public void testInsertIntoInvocationAtPathCreatePath()
String result = SourceUtil.insertIntoInvocationAtPath(source, "println x", "a", "b", "c");
assertEquals(expected, result);
}

@Test
public void testInsertIntoInvocationAtPathPartiallyCreatePath()
{
String source = "" +
"a {\n" +
" b {\n" +
" c {\n" +
" compile 'x:y:z'\n" +
" }\n" +
" }\n" +
"}\n";
String expected = "" +
"a {\n" +
" b {\n" +
" c {\n" +
" compile 'x:y:z'\n" +
" d {\n" +
" e {\n" +
" xyz\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n";
String result = SourceUtil.insertIntoInvocationAtPath(source, "xyz", "a", "b", "c", "d", "e");
assertEquals(expected, result);
}

@Test
public void testCreateInvocationPath()
Expand Down

0 comments on commit 168e4ad

Please sign in to comment.