Content assists for application properties
This commit is contained in:
@@ -12,10 +12,12 @@ package org.springframework.ide.vscode.java.properties.antlr.parser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.BitSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.antlr.v4.runtime.ANTLRErrorListener;
|
||||
import org.antlr.v4.runtime.ANTLRInputStream;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.ConsoleErrorListener;
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.RecognitionException;
|
||||
import org.antlr.v4.runtime.Recognizer;
|
||||
@@ -23,6 +25,7 @@ import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.atn.ATNConfigSet;
|
||||
import org.antlr.v4.runtime.dfa.DFA;
|
||||
import org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.CommentLineContext;
|
||||
import org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.EmptyLineContext;
|
||||
import org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.KeyContext;
|
||||
import org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.PropertyLineContext;
|
||||
import org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.SeparatorAndValueContext;
|
||||
@@ -53,6 +56,9 @@ public class AntlrParser implements Parser {
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
JavaPropertiesParser parser = new JavaPropertiesParser(tokens);
|
||||
|
||||
// To avoid printing parse errors in the console
|
||||
parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
|
||||
|
||||
// Add listener to collect various parser errors
|
||||
parser.addErrorListener(new ANTLRErrorListener() {
|
||||
|
||||
@@ -90,7 +96,9 @@ public class AntlrParser implements Parser {
|
||||
|
||||
@Override
|
||||
public void exitPropertyLine(PropertyLineContext ctx) {
|
||||
astNodes.add(new KeyValuePair(ctx, key, value));
|
||||
KeyValuePair pair = new KeyValuePair(ctx, key, value);
|
||||
key.parent = value.parent = pair;
|
||||
astNodes.add(pair);
|
||||
key = null;
|
||||
value = null;
|
||||
}
|
||||
@@ -110,8 +118,13 @@ public class AntlrParser implements Parser {
|
||||
value = new Value(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitEmptyLine(EmptyLineContext ctx) {
|
||||
astNodes.add(new EmptyLine(ctx));
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
parser.parse();
|
||||
|
||||
// Collect and return parse results
|
||||
@@ -156,6 +169,9 @@ public class AntlrParser implements Parser {
|
||||
|
||||
private static abstract class Node implements PropertiesAst.Node {
|
||||
|
||||
Node parent;
|
||||
List<Node> children;
|
||||
|
||||
abstract protected ParserRuleContext getContext();
|
||||
|
||||
@Override
|
||||
@@ -167,14 +183,40 @@ public class AntlrParser implements Parser {
|
||||
public int getLength() {
|
||||
return getContext().getStop().getStartIndex() - getOffset() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Node> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class Comment implements PropertiesAst.Comment {
|
||||
private static class EmptyLine extends Node implements PropertiesAst.EmptyLine {
|
||||
|
||||
private EmptyLineContext context;
|
||||
|
||||
public EmptyLine(EmptyLineContext context) {
|
||||
super();
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected EmptyLineContext getContext() {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Comment extends Node implements PropertiesAst.Comment {
|
||||
|
||||
private CommentLineContext context;
|
||||
|
||||
public Comment(CommentLineContext context) {
|
||||
super();
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@@ -190,6 +232,11 @@ public class AntlrParser implements Parser {
|
||||
public int getLength() {
|
||||
return context.getStop().getStartIndex() - getOffset() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommentLineContext getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -200,9 +247,11 @@ public class AntlrParser implements Parser {
|
||||
private Value value;
|
||||
|
||||
public KeyValuePair(PropertyLineContext context, Key key, Value value) {
|
||||
super();
|
||||
this.context = context;
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.children = ImmutableList.of(key, value);
|
||||
}
|
||||
|
||||
protected PropertyLineContext getContext() {
|
||||
@@ -218,6 +267,18 @@ public class AntlrParser implements Parser {
|
||||
public Value getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
// Exclude the line break at the end
|
||||
int length = super.getLength();
|
||||
String text = getContext().getText();
|
||||
if (text.charAt(getContext().getStop().getStartIndex() - getOffset()) == '\n') {
|
||||
length--;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class Key extends Node implements PropertiesAst.Key {
|
||||
@@ -241,6 +302,11 @@ public class AntlrParser implements Parser {
|
||||
return context.getText().replace("\\:", ":").replace("\\=", "=");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyValuePair getParent() {
|
||||
return (KeyValuePair) super.getParent();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -257,9 +323,9 @@ public class AntlrParser implements Parser {
|
||||
|
||||
private void init() {
|
||||
// Remove the separator, if it exists
|
||||
value = context.getText().replaceAll("^\\s*[:=]?\\s*", "");
|
||||
value = context.getText().replaceAll("^\\s*[:=]?", "");
|
||||
// Remove all escaped line breaks with trailing spaces
|
||||
decoded = value.replaceAll("\\\\(\r?\n|\r)[ \t\f]*", "");
|
||||
decoded = value.replaceAll("^\\s*", "").replaceAll("\\\\(\r?\n|\r)[ \t\f]*", "");
|
||||
try {
|
||||
decoded = PropertiesFileEscapes.unescape(decoded);
|
||||
} catch (Exception e) {
|
||||
@@ -283,6 +349,10 @@ public class AntlrParser implements Parser {
|
||||
return context.getStart().getStartIndex() + (context.getText().length() - value.length());
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyValuePair getParent() {
|
||||
return (KeyValuePair) super.getParent();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -623,20 +623,20 @@ public class JavaPropertiesParser extends Parser {
|
||||
} else {
|
||||
consume();
|
||||
}
|
||||
setState(80);
|
||||
setState(82);
|
||||
_errHandler.sync(this);
|
||||
_la = _input.LA(1);
|
||||
do {
|
||||
while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << Backslash) | (1L << Colon) | (1L << Equals) | (1L << Exclamation) | (1L << Number) | (1L << Space) | (1L << IdentifierChar))) != 0)) {
|
||||
{
|
||||
{
|
||||
setState(79);
|
||||
valueChar();
|
||||
}
|
||||
}
|
||||
setState(82);
|
||||
setState(84);
|
||||
_errHandler.sync(this);
|
||||
_la = _input.LA(1);
|
||||
} while ( (((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << Backslash) | (1L << Colon) | (1L << Equals) | (1L << Exclamation) | (1L << Number) | (1L << Space) | (1L << IdentifierChar))) != 0) );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (RecognitionException re) {
|
||||
@@ -677,56 +677,56 @@ public class JavaPropertiesParser extends Parser {
|
||||
ValueCharContext _localctx = new ValueCharContext(_ctx, getState());
|
||||
enterRule(_localctx, 18, RULE_valueChar);
|
||||
try {
|
||||
setState(92);
|
||||
setState(93);
|
||||
switch (_input.LA(1)) {
|
||||
case IdentifierChar:
|
||||
enterOuterAlt(_localctx, 1);
|
||||
{
|
||||
setState(84);
|
||||
setState(85);
|
||||
match(IdentifierChar);
|
||||
}
|
||||
break;
|
||||
case Exclamation:
|
||||
enterOuterAlt(_localctx, 2);
|
||||
{
|
||||
setState(85);
|
||||
setState(86);
|
||||
match(Exclamation);
|
||||
}
|
||||
break;
|
||||
case Number:
|
||||
enterOuterAlt(_localctx, 3);
|
||||
{
|
||||
setState(86);
|
||||
setState(87);
|
||||
match(Number);
|
||||
}
|
||||
break;
|
||||
case Space:
|
||||
enterOuterAlt(_localctx, 4);
|
||||
{
|
||||
setState(87);
|
||||
setState(88);
|
||||
match(Space);
|
||||
}
|
||||
break;
|
||||
case Backslash:
|
||||
enterOuterAlt(_localctx, 5);
|
||||
{
|
||||
setState(88);
|
||||
match(Backslash);
|
||||
setState(89);
|
||||
match(Backslash);
|
||||
setState(90);
|
||||
match(LineBreak);
|
||||
}
|
||||
break;
|
||||
case Equals:
|
||||
enterOuterAlt(_localctx, 6);
|
||||
{
|
||||
setState(90);
|
||||
setState(91);
|
||||
match(Equals);
|
||||
}
|
||||
break;
|
||||
case Colon:
|
||||
enterOuterAlt(_localctx, 7);
|
||||
{
|
||||
setState(91);
|
||||
setState(92);
|
||||
match(Colon);
|
||||
}
|
||||
break;
|
||||
@@ -746,31 +746,31 @@ public class JavaPropertiesParser extends Parser {
|
||||
}
|
||||
|
||||
public static final String _serializedATN =
|
||||
"\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\3\na\4\2\t\2\4\3\t"+
|
||||
"\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\3\nb\4\2\t\2\4\3\t"+
|
||||
"\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t\13\3"+
|
||||
"\2\7\2\30\n\2\f\2\16\2\33\13\2\3\2\3\2\3\3\3\3\3\3\5\3\"\n\3\3\4\7\4%"+
|
||||
"\n\4\f\4\16\4(\13\4\3\4\3\4\3\5\7\5-\n\5\f\5\16\5\60\13\5\3\5\3\5\7\5"+
|
||||
"\64\n\5\f\5\16\5\67\13\5\3\5\3\5\3\6\7\6<\n\6\f\6\16\6?\13\6\3\6\3\6\3"+
|
||||
"\7\3\7\3\7\3\7\3\b\6\bH\n\b\r\b\16\bI\3\t\3\t\3\t\5\tO\n\t\3\n\3\n\6\n"+
|
||||
"S\n\n\r\n\16\nT\3\13\3\13\3\13\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\2\2\2-\60\3\2\2\2.,\3\2\2\2./\3\2\2\2/\61"+
|
||||
"\3\2\2\2\60.\3\2\2\2\61\65\t\2\2\2\62\64\n\3\2\2\63\62\3\2\2\2\64\67\3"+
|
||||
"\2\2\2\65\63\3\2\2\2\65\66\3\2\2\2\668\3\2\2\2\67\65\3\2\2\289\t\4\2\2"+
|
||||
"9\t\3\2\2\2:<\7\t\2\2;:\3\2\2\2<?\3\2\2\2=;\3\2\2\2=>\3\2\2\2>@\3\2\2"+
|
||||
"\2?=\3\2\2\2@A\7\b\2\2A\13\3\2\2\2BC\5\16\b\2CD\5\22\n\2DE\t\4\2\2E\r"+
|
||||
"\3\2\2\2FH\5\20\t\2GF\3\2\2\2HI\3\2\2\2IG\3\2\2\2IJ\3\2\2\2J\17\3\2\2"+
|
||||
"\2KO\7\n\2\2LM\7\3\2\2MO\t\5\2\2NK\3\2\2\2NL\3\2\2\2O\21\3\2\2\2PT\t\6"+
|
||||
"\2\2QS\5\24\13\2RQ\3\2\2\2SV\3\2\2\2TR\3\2\2\2TU\3\2\2\2U\23\3\2\2\2V"+
|
||||
"T\3\2\2\2W`\7\n\2\2X`\7\6\2\2Y`\7\7\2\2Z`\7\t\2\2[\\\7\3\2\2\\`\7\b\2"+
|
||||
"\2]`\7\5\2\2^`\7\4\2\2_W\3\2\2\2_X\3\2\2\2_Y\3\2\2\2_Z\3\2\2\2_[\3\2\2"+
|
||||
"\2_]\3\2\2\2_^\3\2\2\2`\25\3\2\2\2\f\31!&.\65=INT_";
|
||||
public static final ATN _ATN =
|
||||
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
|
||||
static {
|
||||
|
||||
@@ -31,7 +31,7 @@ public final class PropertiesAst {
|
||||
* Retrieves all AST nodes
|
||||
* @return List of AST nodes sorted by line number
|
||||
*/
|
||||
public List<? extends Node> getAllNodes() {
|
||||
public List<Node> getAllNodes() {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
@@ -48,6 +48,52 @@ public final class PropertiesAst {
|
||||
return (List<T>) l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find node in AST corresponding to offset position
|
||||
* @param offset Position in the text
|
||||
* @return AST node corresponding to the offset position
|
||||
*/
|
||||
public Node findNode(int offset) {
|
||||
return findNode(nodes, offset, 0, nodes.size() - 1);
|
||||
}
|
||||
|
||||
private Node findNode(List<? extends Node> nodes, int offset, int start, int end) {
|
||||
if (nodes == null) {
|
||||
return null;
|
||||
}
|
||||
if (start == end) {
|
||||
Node node = nodes.get(start);
|
||||
if (node.getOffset() <= offset && offset <= node.getOffset() + node.getLength()) {
|
||||
Node found = findChildNode(node, offset);
|
||||
return found == null ? node : found;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (start < end ) {
|
||||
int pivotIndex = (start + end) / 2;
|
||||
Node node = nodes.get(pivotIndex);
|
||||
if (node.getOffset() > offset) {
|
||||
return findNode(nodes, offset, start, pivotIndex - 1);
|
||||
} else if (offset > node.getOffset() + node.getLength()) {
|
||||
return findNode(nodes, offset, pivotIndex + 1, end);
|
||||
} else {
|
||||
Node found = findChildNode(node, offset);
|
||||
return found == null ? node : found;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Node findChildNode(Node node, int offset) {
|
||||
if (node.getChildren() == null) {
|
||||
return null;
|
||||
} else {
|
||||
return findNode(node.getChildren(), offset, 0, node.getChildren().size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Java Properties AST node
|
||||
*/
|
||||
@@ -65,6 +111,18 @@ public final class PropertiesAst {
|
||||
*/
|
||||
int getLength();
|
||||
|
||||
/**
|
||||
* Node's parent
|
||||
* @return parent node
|
||||
*/
|
||||
Node getParent();
|
||||
|
||||
/**
|
||||
* Node's children
|
||||
* @return children nodes
|
||||
*/
|
||||
List<? extends Node> getChildren();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,6 +130,13 @@ public final class PropertiesAst {
|
||||
*/
|
||||
public interface Comment extends Node {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* AST node for empty line
|
||||
*/
|
||||
public interface EmptyLine extends Node {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,6 +168,8 @@ public final class PropertiesAst {
|
||||
* @return Decoded property name
|
||||
*/
|
||||
String decode();
|
||||
|
||||
KeyValuePair getParent();
|
||||
|
||||
}
|
||||
|
||||
@@ -117,6 +184,7 @@ public final class PropertiesAst {
|
||||
*/
|
||||
String decode();
|
||||
|
||||
KeyValuePair getParent();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -83,34 +83,6 @@ public class PropertiesAntlrParserTest {
|
||||
testCommentLine(" # This is comment = ", "# This is comment = ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLines1() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n\n \t \t \n\t\t\n");
|
||||
assertTrue(results.syntaxErrors.isEmpty());
|
||||
assertTrue(results.problems.isEmpty());
|
||||
assertEquals(1, results.ast.getAllNodes().size());
|
||||
assertEquals(1, results.ast.getNodes(Comment.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLines2() throws Exception {
|
||||
ParseResults results = parser.parse("\n\n \t \t \n# Comment\n\t\t\n");
|
||||
assertTrue(results.syntaxErrors.isEmpty());
|
||||
assertTrue(results.problems.isEmpty());
|
||||
assertEquals(1, results.ast.getAllNodes().size());
|
||||
assertEquals(1, results.ast.getNodes(Comment.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLines3() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n\nkey = value 1 \n \t \t \n\t\t\n");
|
||||
assertTrue(results.syntaxErrors.isEmpty());
|
||||
assertTrue(results.problems.isEmpty());
|
||||
assertEquals(2, results.ast.getAllNodes().size());
|
||||
assertEquals(1, results.ast.getNodes(Comment.class).size());
|
||||
assertEquals(1, results.ast.getNodes(KeyValuePair.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyWithEqualsSeparator() throws Exception {
|
||||
testPropertyLine("key=value", "key", "key", "value", "value");
|
||||
@@ -118,7 +90,7 @@ public class PropertiesAntlrParserTest {
|
||||
|
||||
@Test
|
||||
public void testPropertyWithEqualsSeparatorAndSpaces() throws Exception {
|
||||
testPropertyLine("key \t = \t \tvalue", "key", "key", "value", "value");
|
||||
testPropertyLine("key \t = \t \tvalue", "key", "key", "value", " \t \tvalue");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,7 +100,7 @@ public class PropertiesAntlrParserTest {
|
||||
|
||||
@Test
|
||||
public void testPropertyWithColonSeparatorAndSpaces() throws Exception {
|
||||
testPropertyLine("key \t : \t \tvalue", "key", "key", "value", "value");
|
||||
testPropertyLine("key \t : \t \tvalue", "key", "key", "value", " \t \tvalue");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -168,17 +140,17 @@ public class PropertiesAntlrParserTest {
|
||||
|
||||
@Test
|
||||
public void testEqualsValueSeparatedWithEqualsAndSpace() throws Exception {
|
||||
testPropertyLine("key7 = =", "key7", "key7", "=", "=");
|
||||
testPropertyLine("key7 = =", "key7", "key7", "=", " =");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueWithTrailingSpaces() throws Exception {
|
||||
testPropertyLine("key = value 1 ", "key", "key", "value 1 ", "value 1 ");
|
||||
testPropertyLine("key = value 1 ", "key", "key", "value 1 ", " value 1 ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnodeCharKeyAndValue() throws Exception {
|
||||
testPropertyLine("k\u2b22ey\u2b28 = val\u2b24ue 1\u2b24 ", "k\u2b22ey\u2b28", "k\u2b22ey\u2b28", "val\u2b24ue 1\u2b24 ", "val\u2b24ue 1\u2b24 ");
|
||||
testPropertyLine("k\u2b22ey\u2b28 = val\u2b24ue 1\u2b24 ", "k\u2b22ey\u2b28", "k\u2b22ey\u2b28", "val\u2b24ue 1\u2b24 ", " val\u2b24ue 1\u2b24 ");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
package org.springframework.ide.vscode.java.properties.parser.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser;
|
||||
import org.springframework.ide.vscode.java.properties.parser.ParseResults;
|
||||
import org.springframework.ide.vscode.java.properties.parser.Parser;
|
||||
import org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Comment;
|
||||
import org.springframework.ide.vscode.java.properties.parser.PropertiesAst.EmptyLine;
|
||||
import org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Key;
|
||||
import org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair;
|
||||
import org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Node;
|
||||
import org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Value;
|
||||
|
||||
public class PropertiesAstTest {
|
||||
|
||||
Parser parser = new AntlrParser();
|
||||
|
||||
@Test
|
||||
public void testLines1() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n\n \t \t \n\t\t\n");
|
||||
assertTrue(results.syntaxErrors.isEmpty());
|
||||
assertTrue(results.problems.isEmpty());
|
||||
assertEquals(4, results.ast.getAllNodes().size());
|
||||
assertEquals(1, results.ast.getNodes(Comment.class).size());
|
||||
assertEquals(3, results.ast.getNodes(EmptyLine.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLines2() throws Exception {
|
||||
ParseResults results = parser.parse("\n\n \t \t \n# Comment\n\t\t\n");
|
||||
assertTrue(results.syntaxErrors.isEmpty());
|
||||
assertTrue(results.problems.isEmpty());
|
||||
assertEquals(5, results.ast.getAllNodes().size());
|
||||
assertEquals(1, results.ast.getNodes(Comment.class).size());
|
||||
assertEquals(4, results.ast.getNodes(EmptyLine.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLines3() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n\nkey = value 1 \n \t \t \n\t\t\n");
|
||||
assertTrue(results.syntaxErrors.isEmpty());
|
||||
assertTrue(results.problems.isEmpty());
|
||||
assertEquals(5, results.ast.getAllNodes().size());
|
||||
assertEquals(1, results.ast.getNodes(Comment.class).size());
|
||||
assertEquals(1, results.ast.getNodes(KeyValuePair.class).size());
|
||||
assertEquals(3, results.ast.getNodes(EmptyLine.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLines4() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment-1\n\nkey = value 1 \n# Comment-2");
|
||||
assertEquals(4, results.ast.getAllNodes().size());
|
||||
assertEquals(2, results.ast.getNodes(Comment.class).size());
|
||||
assertEquals(1, results.ast.getNodes(KeyValuePair.class).size());
|
||||
assertEquals(1, results.ast.getNodes(EmptyLine.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLines5() throws Exception {
|
||||
ParseResults results = parser.parse("#comment\nliquibase.enabled=\n#comment");
|
||||
assertEquals(3, results.ast.getAllNodes().size());
|
||||
assertEquals(2, results.ast.getNodes(Comment.class).size());
|
||||
assertEquals(1, results.ast.getNodes(KeyValuePair.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positionComment() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n" + "key = value\n");
|
||||
|
||||
Node node = results.ast.findNode(7);
|
||||
assertTrue(node instanceof Comment);
|
||||
assertTrue(node.getOffset() <= 7 && 7 <= node.getOffset() + node.getLength());
|
||||
|
||||
node = results.ast.findNode(9);
|
||||
assertTrue(node instanceof Comment);
|
||||
assertTrue(node.getOffset() <= 9 && 9 <= node.getOffset() + node.getLength());
|
||||
|
||||
node = results.ast.findNode(0);
|
||||
assertTrue(node instanceof Comment);
|
||||
assertTrue(node.getOffset() <= 0 && 0 <= node.getOffset() + node.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positionEmptyLine() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n" + "key = value\n" + "\t\n");
|
||||
Node node = results.ast.findNode(23);
|
||||
assertTrue(node instanceof EmptyLine);
|
||||
assertTrue(node.getOffset() <= 23 && 23 <= node.getOffset() + node.getLength());
|
||||
|
||||
node = results.ast.findNode(24);
|
||||
assertTrue(node instanceof EmptyLine);
|
||||
assertTrue(node.getOffset() <= 24 && 24 <= node.getOffset() + node.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positionKey() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n" + "key = value\n" + "\t\n");
|
||||
Node node = results.ast.findNode(10);
|
||||
assertTrue(node instanceof Key);
|
||||
assertTrue(node.getOffset() <= 10 && 10 <= node.getOffset() + node.getLength());
|
||||
|
||||
node = results.ast.findNode(12);
|
||||
assertTrue(node instanceof Key);
|
||||
assertTrue(node.getOffset() <= 12 && 12 <= node.getOffset() + node.getLength());
|
||||
|
||||
node = results.ast.findNode(13);
|
||||
assertTrue(node instanceof Key);
|
||||
assertTrue(node.getOffset() <= 13 && 13 <= node.getOffset() + node.getLength());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positionPair() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n" + "key = value\n" + "\t\n");
|
||||
Node node = results.ast.findNode(15);
|
||||
assertTrue(node instanceof KeyValuePair);
|
||||
assertTrue(node.getOffset() <= 15 && 15 <= node.getOffset() + node.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positionValue() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n" + "key = value\n");
|
||||
Node node = results.ast.findNode(17);
|
||||
assertTrue(node instanceof Value);
|
||||
assertTrue(node.getOffset() <= 17 && 17 <= node.getOffset() + node.getLength());
|
||||
|
||||
node = results.ast.findNode(22);
|
||||
assertTrue(node instanceof Value);
|
||||
assertTrue(node.getOffset() <= 22 && 22 <= node.getOffset() + node.getLength());
|
||||
|
||||
node = results.ast.findNode(16);
|
||||
assertTrue(node instanceof Value);
|
||||
assertTrue(node.getOffset() <= 16 && 16 <= node.getOffset() + node.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positionValueEofAtEnd() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n" + "key = value");
|
||||
Node node = results.ast.findNode(22);
|
||||
assertTrue(node instanceof Value);
|
||||
assertTrue(node.getOffset() <= 22 && 22 <= node.getOffset() + node.getLength());
|
||||
|
||||
node = results.ast.findNode(16);
|
||||
assertTrue(node instanceof Value);
|
||||
assertTrue(node.getOffset() <= 16 && 16 <= node.getOffset() + node.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positionEmptyValue() throws Exception {
|
||||
ParseResults results = parser.parse("# Comment\n" + "key =");
|
||||
Node node = results.ast.findNode(16);
|
||||
assertTrue(node instanceof Value);
|
||||
assertTrue(node.getOffset() <= 16 && 16 <= node.getOffset() + node.getLength());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user