Skip to content

Commit

Permalink
code should not be null when toText is called
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnrd committed Apr 29, 2022
1 parent f35ce29 commit c2f7949
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions org.lflang/src/org/lflang/ast/ToText.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,35 @@ public String caseArraySpec(ArraySpec spec) {

@Override
public String caseCode(Code code) {
String text = "";
if (code != null) {
ICompositeNode node = NodeModelUtils.getNode(code);
if (node != null) {
StringBuilder builder = new StringBuilder(Math.max(node.getTotalLength(), 1));
for (ILeafNode leaf : node.getLeafNodes()) {
builder.append(leaf.getText());
}
String str = builder.toString().trim();
// Remove the code delimiters (and any surrounding comments).
// This assumes any comment before {= does not include {=.
int start = str.indexOf("{=");
int end = str.indexOf("=}", start);
if (start == -1 || end == -1) {
// Silent failure is needed here because toText is needed to create the intermediate representation,
// which the validator uses.
return str;
}
str = str.substring(start + 2, end);
if (str.split("\n").length > 1) {
// multi line code
text = StringUtil.trimCodeBlock(str);
} else {
// single line code
text = str.trim();
}
} else if (code.getBody() != null) {
// Code must have been added as a simple string.
text = code.getBody();
ICompositeNode node = NodeModelUtils.getNode(code);
if (node != null) {
StringBuilder builder = new StringBuilder(Math.max(node.getTotalLength(), 1));
for (ILeafNode leaf : node.getLeafNodes()) {
builder.append(leaf.getText());
}
String str = builder.toString().trim();
// Remove the code delimiters (and any surrounding comments).
// This assumes any comment before {= does not include {=.
int start = str.indexOf("{=");
int end = str.indexOf("=}", start);
if (start == -1 || end == -1) {
// Silent failure is needed here because toText is needed to create the intermediate representation,
// which the validator uses.
return str;
}
str = str.substring(start + 2, end);
if (str.split("\n").length > 1) {
// multi line code
return StringUtil.trimCodeBlock(str);
} else {
// single line code
return str.trim();
}
} else if (code.getBody() != null) {
// Code must have been added as a simple string.
return code.getBody();
}
return text;
return "";
}

@Override
Expand Down

0 comments on commit c2f7949

Please sign in to comment.