package gherkin.formatter;

import java.util.List;

public class StepPrinter {
    public void writeStep(NiceAppendable out, Format textFormat, Format argFormat, String stepName, List<Argument> arguments) {
        int textStart = 0;
        for (Argument argument : arguments) {
            if (argument.getOffset() != 0) {
                String text = stepName.substring(textStart, argument.getOffset());
                out.append(textFormat.text(text));
            }
            String val = argument.getVal();
            out.append(argFormat.text(val));
            textStart = argument.getOffset() + argument.getVal().length();
        }
        if (textStart != stepName.length()) {
            String text = stepName.substring(textStart, stepName.length());
            out.append(textFormat.text(text));
        }
    }
}

@@ -265,32 +265,85 @@
  : arguments) %7B%0A
+            // can be -1 if the argument is missing.%0A
             if (
@@ -363,18 +363,17 @@
 ffset() 
-!=
+%3E
  0) %7B%0A  
@@ -558,24 +558,159 @@
 t.getVal();%0A
+            // val can be null if the argument isn't there, for example @And(%22(it )?has something%22)%0A            if (val != null) %7B%0A    
             
@@ -738,24 +738,28 @@
 text(val));%0A
+    
             
@@ -817,24 +817,38 @@
 ).length();%0A
+            %7D%0A
         %7D%0A  

Fix for exception when an argument is missing (vs just being empty)