From 2394f1b6a1d451391ef52f372979fb6cc7a7fb91 Mon Sep 17 00:00:00 2001 From: bkolb Date: Mon, 21 Jul 2008 10:57:07 +0000 Subject: [PATCH] [241458] fixed problem with NPE when opening editor --- .../xtext/parsetree/impl/ParsetreeUtil.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/impl/ParsetreeUtil.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/impl/ParsetreeUtil.java index 2d3c076d3..3e3ff937b 100644 --- a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/impl/ParsetreeUtil.java +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/impl/ParsetreeUtil.java @@ -21,8 +21,11 @@ import org.eclipse.xtext.parsetree.SyntaxError; /** * * @author koehnlein + * @author bkolb */ public class ParsetreeUtil { + + private static final char[] separator = System.getProperty( "line.separator" ).toCharArray(); private static void checkArgument(AbstractNodeImpl abstractParserNode) { int classifierID = abstractParserNode.eClass().getClassifierID(); @@ -36,15 +39,11 @@ public class ParsetreeUtil { checkArgument(_this); AbstractNode rootContainer = (AbstractNode) EcoreUtil.getRootContainer(_this); EList leafNodes = rootContainer.getLeafNodes(_this); + // TODO JK: Why is line set here to 1 instead of 0? (BK) int line = 1; for (LeafNode leafNode : leafNodes) { String text = leafNode.getText(); - char[] charArray = text.toCharArray(); - for (char c : charArray) { - // TODO handle os specific newlines - if (c == '\n' || c == '\r') - line++; - } + line += countLines(text); } return line; } @@ -52,11 +51,27 @@ public class ParsetreeUtil { public static int endLine(AbstractNodeImpl _this) { int line = _this.getLine(); String text = _this.serialize(); + line += countLines(text); + return line; + } + + private static int countLines(String text) { + int line = 0; char[] charArray = text.toCharArray(); - for (char c : charArray) { - // TODO handle os specific newlines - if (c == '\n' || c == '\r') - line++; + if ( separator.length == 1) { + for (int i = 0; i=i+1 && charArray[i+1]==separator[1]) { + line++; + } + } + } else { + throw new IllegalArgumentException("Separators with more than two characters are unexpected"); } return line; }