/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.spockframework.compiler;

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.control.Janitor;
import org.codehaus.groovy.control.SourceUnit;

public class SourceLookup {
  private final SourceUnit sourceUnit;
  private final Janitor janitor = new Janitor();

  public SourceLookup(SourceUnit sourceUnit) {
    this.sourceUnit = sourceUnit;
  }

  public String lookup(ASTNode node) {
    String text = "";
    for (int i = node.getLineNumber(); i <= node.getLastLineNumber(); i++) {
      String line = sourceUnit.getSample(i, 0, janitor);
      if (line == null) {
        return null; // most probably a Groovy bug, but we'd like to handle this situation gracefully
        // throw new Error(String.format("null line detected; lineNum=%s, expr=%s", i, node.getText()));
      }

      try {
        if (i == node.getLastLineNumber()) line = line.substring(0, node.getLastColumnNumber() - 1);
        if (i == node.getLineNumber()) line = line.substring(node.getColumnNumber() - 1);
        text += line;
        if (i != node.getLastLineNumber()) text += '\n';
      } catch (StringIndexOutOfBoundsException e) {
        return null; // most probably a Groovy bug, but we'd like to handle this situation gracefully
        // throw new RuntimeException(String.format("error getting node text; expr='%s', lineNumber='%s', lastLineNumber='%s', columnNumber='%s', lastColumnNumber='%s', currentLine='%s'", node, node.getLineNumber(), node.getLastLineNumber(), node.getColumnNumber(), node.getLastColumnNumber(), line), e);
      }
    }
    return text.trim();
  }

  public void close() {
    janitor.cleanup();
  }
}

@@ -1035,16 +1035,23 @@
 ring
+Builder
  text = 
 %22%22;%0A
@@ -1050,10 +1050,27 @@
 t = 
-%22%22
+new StringBuilder()
 ;%0A  
@@ -1277,39 +1277,39 @@
 oovy bug, but we
-'d like
+ prefer
  to handle this 
@@ -1333,113 +1333,8 @@
 lly%0A
-        // throw new Error(String.format(%22null line detected; lineNum=%25s, expr=%25s%22, i, node.getText()));%0A
     
@@ -1557,16 +1557,21 @@
 text
- += 
+.append(
 line
+)
 ;%0A  
@@ -1619,16 +1619,21 @@
 text
- += 
+.append(
 '%5Cn'
+)
 ;%0A  
@@ -1744,15 +1744,15 @@
 t we
-'d like
+ prefer
  to 
@@ -1794,313 +1794,8 @@
     
-  // throw new RuntimeException(String.format(%22error getting node text; expr='%25s', lineNumber='%25s', lastLineNumber='%25s', columnNumber='%25s', lastColumnNumber='%25s', currentLine='%25s'%22, node, node.getLineNumber(), node.getLastLineNumber(), node.getColumnNumber(), node.getLastColumnNumber(), line), e);%0A      
 %7D%0A  
@@ -1813,16 +1813,27 @@
 urn text
+.toString()
 .trim();

use StringBuilder rather than concatenating strings