Skip to content

Commit

Permalink
#235 Maintain source code indentation in Vivid report.
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes committed Oct 30, 2022
1 parent 0a92f4d commit 413e588
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,23 @@ class SpecSourceCode {

static String removeIndent( String code ) {
def lines = code.readLines()
if ( lines.size() < 2 ) {
return code
}

// do not use the first line because the first line never gets any indentation
def firstTextIndexes = lines[ 1..-1 ].collect { String line -> line.findIndexOf { it != ' ' } }
def firstTextIndexes = lines.collect { String line -> line.findIndexOf { it != ' ' } }
def minIndent = firstTextIndexes.min()

if ( minIndent > 0 ) {
def resultLines = [ lines[ 0 ] ] + lines[ 1..-1 ].collect { String line -> line.substring( minIndent ) }
def resultLines = lines.collect { String line -> trimLine( line, minIndent ) }
return resultLines.join( '\n' )
} else {
return code
}
}

private static String trimLine( String line, int minIndent ) {
def lastIndex = line.findLastIndexOf { it != ' ' }
line[minIndent..lastIndex]
}

}

@ToString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import org.codehaus.groovy.ast.ModuleNode
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.ast.stmt.Statement
import org.codehaus.groovy.control.Janitor
import org.codehaus.groovy.control.SourceUnit
import org.spockframework.compiler.SourceLookup
import org.spockframework.util.Nullable

import java.util.concurrent.ConcurrentHashMap

@Slf4j
@CompileStatic
class SpecSourceCodeCollector {
class SpecSourceCodeCollector implements AutoCloseable {

private final SourceUnit sourceUnit
private final Janitor janitor = new Janitor()

final SourceLookup sourceLookup
final ModuleNode module

private static final Map<String, SpecSourceCode> specSourceCodeByClassName = [ : ] as ConcurrentHashMap
Expand All @@ -29,7 +31,7 @@ class SpecSourceCodeCollector {
private MethodNode method

SpecSourceCodeCollector( SourceUnit sourceUnit ) {
this.sourceLookup = new SourceLookup( sourceUnit )
this.sourceUnit = sourceUnit
this.module = sourceUnit.AST
}

Expand Down Expand Up @@ -62,7 +64,7 @@ class SpecSourceCodeCollector {
assert className && method

def label = statement.statementLabel
def code = sourceLookup.lookup( statement )
def code = lookupCode( statement )
def specCode = specSourceCodeByClassName[ className ]

if ( !specCode ) {
Expand All @@ -85,6 +87,15 @@ class SpecSourceCodeCollector {
}
}

private String lookupCode( Statement statement) {
def text = new StringBuilder()
for (int i = statement.getLineNumber(); i <= statement.getLastLineNumber(); i++) {
def line = sourceUnit.getSample( i, 0, janitor )
text.append( line ).append( '\n' )
}
text.toString()
}

@Nullable
private static String stringConstant( Statement statement ) {
if ( statement instanceof ExpressionStatement ) {
Expand All @@ -97,4 +108,8 @@ class SpecSourceCodeCollector {
null
}

@Override
void close() {
janitor.cleanup()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,19 @@ class VividAstInspector {
final visitor = new VividASTVisitor( codeCollector )
final module = codeCollector.module

visitor.visitBlockStatement( module.statementBlock )
codeCollector.withCloseable {
visitor.visitBlockStatement( module.statementBlock )

for ( MethodNode method in module.methods ) {
visitor.visitMethod( method )
}
for ( MethodNode method in module.methods ) {
visitor.visitMethod( method )
}

for ( ClassNode clazz in module.classes ) {
visitor.visitClass( clazz )
}
for ( ClassNode clazz in module.classes ) {
visitor.visitClass( clazz )
}

codeCollector
codeCollector
}
}

private class VividClassLoader extends GroovyClassLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class VividAstInspectorSpec extends Specification {
blocks.size() == 4

and: 'The inspector should be able to provide the source code for each block'
blocks[ 0 ].statements == [ 'def x = 10 +', '20 +', '30' ]
blocks[ 0 ].statements == [ 'def x = 10 +', ' 20 +', ' 30' ]
blocks[ 1 ].statements == [ 'def y = """',
' hello',
' world',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ class SpecSourceCodeSpec extends Specification {
| println 2 * i
|}'''.stripMargin() | 'for (i in x) {\n println i\n println 2 * i\n}'

'''for (i in x) {
'''\
for (i in x) {
println i
println 2 * i
}''' | 'for (i in x) {\n println i\n println 2 * i\n}'


}

}

0 comments on commit 413e588

Please sign in to comment.