From e62585f6deeecd8b6d98ac5ae5ab0a5c1e8fb579 Mon Sep 17 00:00:00 2001 From: Alexey Merkulov Date: Wed, 11 Apr 2018 19:00:45 +0300 Subject: [PATCH] RUBY-21351 Added highlighting for invalid symbols in scalar headers --- .../org/jetbrains/yaml/lexer/_YAMLLexer.java | 22 ++++++------ .../resources/messages/YAMLBundle.properties | 2 ++ .../src/org/jetbrains/yaml/lexer/yaml.flex | 15 ++++---- .../org/jetbrains/yaml/parser/YAMLParser.java | 4 ++- .../highlighting/YAMLHighlightingTest.java | 20 +++++++++++ .../data/blockScalarHeaderError.yml | 3 ++ .../jetbrains/yaml/parser/YAMLParserTest.java | 4 +++ .../parser/data/ErrorInBlockScalarHeader.txt | 36 +++++++++++++++++++ .../parser/data/ErrorInBlockScalarHeader.yml | 6 ++++ 9 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/YAMLHighlightingTest.java create mode 100644 plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/data/blockScalarHeaderError.yml create mode 100644 plugins/yaml/testSrc/org/jetbrains/yaml/parser/data/ErrorInBlockScalarHeader.txt create mode 100644 plugins/yaml/testSrc/org/jetbrains/yaml/parser/data/ErrorInBlockScalarHeader.yml diff --git a/plugins/yaml/gen/org/jetbrains/yaml/lexer/_YAMLLexer.java b/plugins/yaml/gen/org/jetbrains/yaml/lexer/_YAMLLexer.java index 0f94465d9c77..62ce837d8e54 100644 --- a/plugins/yaml/gen/org/jetbrains/yaml/lexer/_YAMLLexer.java +++ b/plugins/yaml/gen/org/jetbrains/yaml/lexer/_YAMLLexer.java @@ -83,11 +83,11 @@ public class _YAMLLexer implements FlexLexer, YAMLTokenTypes { "\1\21\7\0\1\1\1\0\1\22\1\0\2\23\2\24"+ "\1\11\5\0\1\25\2\0\1\26\1\0\2\27\4\0"+ "\1\15\1\1\1\0\1\22\3\0\1\1\4\0\1\25"+ - "\1\26\1\1\3\0\1\30\1\2\13\0\1\31\1\0"+ - "\1\1\2\0\1\32\3\0\1\33\1\0\1\1\5\0"+ - "\1\1\2\0\2\1\2\0\2\1\2\0\1\1\2\0"+ - "\1\1\1\0\1\1\4\0\2\1\1\0\4\1\1\0"+ - "\1\1\2\0\1\1"; + "\1\26\1\1\3\0\1\30\14\0\1\31\1\0\1\1"+ + "\2\0\1\32\3\0\1\33\1\0\1\1\5\0\1\1"+ + "\2\0\2\1\2\0\2\1\2\0\1\1\2\0\1\1"+ + "\1\0\1\1\4\0\2\1\1\0\4\1\1\0\1\1"+ + "\2\0\1\1"; private static int [] zzUnpackAction() { int [] result = new int[165]; @@ -375,12 +375,12 @@ public class _YAMLLexer implements FlexLexer, YAMLTokenTypes { "\1\11\1\1\4\0\1\1\1\0\1\1\1\0\1\11"+ "\1\1\1\11\1\1\1\11\5\0\1\1\2\0\1\1"+ "\1\0\1\11\1\1\4\0\1\11\1\1\1\0\1\1"+ - "\3\0\1\1\4\0\1\11\2\1\3\0\1\11\3\1"+ - "\5\0\1\1\2\0\1\1\1\11\1\0\1\1\2\0"+ - "\1\11\3\0\1\11\1\0\1\1\5\0\1\1\2\0"+ - "\2\1\2\0\2\1\2\0\1\1\2\0\1\1\1\0"+ - "\1\1\2\0\1\11\1\0\2\1\1\0\4\1\1\0"+ - "\1\1\2\0\1\1"; + "\3\0\1\1\4\0\1\11\2\1\3\0\1\11\1\0"+ + "\2\1\5\0\1\1\2\0\1\1\1\11\1\0\1\1"+ + "\2\0\1\11\3\0\1\11\1\0\1\1\5\0\1\1"+ + "\2\0\2\1\2\0\2\1\2\0\1\1\2\0\1\1"+ + "\1\0\1\1\2\0\1\11\1\0\2\1\1\0\4\1"+ + "\1\0\1\1\2\0\1\1"; private static int [] zzUnpackAttribute() { int [] result = new int[165]; diff --git a/plugins/yaml/resources/messages/YAMLBundle.properties b/plugins/yaml/resources/messages/YAMLBundle.properties index dfee97f6691c..cbce30279d27 100644 --- a/plugins/yaml/resources/messages/YAMLBundle.properties +++ b/plugins/yaml/resources/messages/YAMLBundle.properties @@ -37,3 +37,5 @@ YamlMetaClass.error.scalar.value=Scalar value is not allowed here YamlScalarType.error.scalar.value=Scalar value expected YamlIntegerType.error.integer.value=Integer value expected YamlNumberType.error.numeric.value=Numeric value expected + +YAMLParser.invalid.header.symbols=Invalid block scalar header diff --git a/plugins/yaml/src/org/jetbrains/yaml/lexer/yaml.flex b/plugins/yaml/src/org/jetbrains/yaml/lexer/yaml.flex index 44694d78a244..2b4b10663b64 100644 --- a/plugins/yaml/src/org/jetbrains/yaml/lexer/yaml.flex +++ b/plugins/yaml/src/org/jetbrains/yaml/lexer/yaml.flex @@ -165,14 +165,14 @@ SCALAR_BLOCK_ERR_WORD = [^ \t#\n] [^ \t\n]* ///////////////////////////// STATES DECLARATIONS ////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -%xstate BRACES, VALUE, VALUE_OR_KEY, VALUE_BRACE, INDENT_VALUE, SB_HEADER_TAIL +%xstate BRACES, VALUE, VALUE_OR_KEY, VALUE_BRACE, INDENT_VALUE, BS_HEADER_TAIL %% //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// RULES declarations //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - { + { {COMMENT} { // YAML spec: when a comment follows another syntax element, // it must be separated from it by space characters. @@ -252,7 +252,7 @@ SCALAR_BLOCK_ERR_WORD = [^ \t#\n] [^ \t\n]* } } - { + { {WHITE_SPACE} { return getWhitespaceTypeAndUpdateIndent(); } @@ -289,20 +289,21 @@ SCALAR_BLOCK_ERR_WORD = [^ \t#\n] [^ \t\n]* { -">"("-"|"+")? { yyBegin(SB_HEADER_TAIL); +">"("-"|"+")? { yyBegin(BS_HEADER_TAIL); valueTokenType = SCALAR_TEXT; return valueTokenType; } -"|"("-"|"+")? { yyBegin(SB_HEADER_TAIL); +"|"("-"|"+")? { yyBegin(BS_HEADER_TAIL); valueTokenType = SCALAR_LIST; return valueTokenType; } } -{ -([^ \t#\n] [^ \t\n]* [ \t]* )+ { return TEXT; } +{ +{BS_HEADER_ERR_WORD} ([ \t]* {BS_HEADER_ERR_WORD})* + { return TEXT; } {EOL} { yyBegin(INDENT_VALUE); valueIndent = currentLineIndent; diff --git a/plugins/yaml/src/org/jetbrains/yaml/parser/YAMLParser.java b/plugins/yaml/src/org/jetbrains/yaml/parser/YAMLParser.java index 506c9d7521e2..8efc22089835 100644 --- a/plugins/yaml/src/org/jetbrains/yaml/parser/YAMLParser.java +++ b/plugins/yaml/src/org/jetbrains/yaml/parser/YAMLParser.java @@ -8,6 +8,7 @@ import com.intellij.psi.tree.TokenSet; import com.intellij.util.containers.Stack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.yaml.YAMLBundle; import org.jetbrains.yaml.YAMLElementTypes; import org.jetbrains.yaml.YAMLTokenTypes; @@ -222,8 +223,9 @@ public class YAMLParser implements PsiParser, YAMLTokenTypes { // Parse header tail: TEXT is used as placeholder for invalid symbols in this context if (getTokenType() == TEXT) { + PsiBuilder.Marker err = myBuilder.mark(); advanceLexer(); - + err.error(YAMLBundle.message("YAMLParser.invalid.header.symbols")); } PsiBuilder.Marker endOfValue = myBuilder.mark(); diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/YAMLHighlightingTest.java b/plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/YAMLHighlightingTest.java new file mode 100644 index 000000000000..42be8586db61 --- /dev/null +++ b/plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/YAMLHighlightingTest.java @@ -0,0 +1,20 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.yaml.highlighting; + +import com.intellij.openapi.application.ex.PathManagerEx; +import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; + +public class YAMLHighlightingTest extends LightPlatformCodeInsightFixtureTestCase { + @Override + protected String getTestDataPath() { + return PathManagerEx.getCommunityHomePath() + "/plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/data/"; + } + + public void testBlockScalarHeaderError() { + doTest(); + } + + private void doTest() { + myFixture.testHighlighting(true, false, false, getTestName(true) + ".yml"); + } +} diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/data/blockScalarHeaderError.yml b/plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/data/blockScalarHeaderError.yml new file mode 100644 index 000000000000..e34466a43995 --- /dev/null +++ b/plugins/yaml/testSrc/org/jetbrains/yaml/highlighting/data/blockScalarHeaderError.yml @@ -0,0 +1,3 @@ +| invalid w#ords in this context #comments starts here + hello + world diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/parser/YAMLParserTest.java b/plugins/yaml/testSrc/org/jetbrains/yaml/parser/YAMLParserTest.java index 6b47ef76764c..71d2049a4d62 100644 --- a/plugins/yaml/testSrc/org/jetbrains/yaml/parser/YAMLParserTest.java +++ b/plugins/yaml/testSrc/org/jetbrains/yaml/parser/YAMLParserTest.java @@ -232,4 +232,8 @@ public class YAMLParserTest extends ParsingTestCase { public void testCommentInBlockScalarHeader() { doTest(true); } + + public void testErrorInBlockScalarHeader() { + doTest(true); + } } diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/parser/data/ErrorInBlockScalarHeader.txt b/plugins/yaml/testSrc/org/jetbrains/yaml/parser/data/ErrorInBlockScalarHeader.txt new file mode 100644 index 000000000000..71de913d5dbe --- /dev/null +++ b/plugins/yaml/testSrc/org/jetbrains/yaml/parser/data/ErrorInBlockScalarHeader.txt @@ -0,0 +1,36 @@ +YAML file + YAML document + YAML sequence + YAML sequence item + PsiElement(-)('-') + PsiWhiteSpace(' ') + YAML scalar text + PsiElement(scalar text)('>') + PsiWhiteSpace(' ') + PsiErrorElement:Invalid block scalar header + PsiElement(text)('some invalid symbols') + PsiWhiteSpace(' ') + PsiComment(comment)('# comment') + PsiElement(Eol)('\n') + PsiElement(indent)(' ') + PsiElement(scalar text)('hello') + PsiElement(Eol)('\n') + PsiElement(indent)(' ') + PsiElement(scalar text)('world') + PsiElement(Eol)('\n') + YAML sequence item + PsiElement(-)('-') + PsiWhiteSpace(' ') + YAML scalar list + PsiElement(scalar list)('|') + PsiWhiteSpace(' ') + PsiErrorElement:Invalid block scalar header + PsiElement(text)('some invalid symbols') + PsiWhiteSpace(' ') + PsiComment(comment)('# comment') + PsiElement(Eol)('\n') + PsiElement(indent)(' ') + PsiElement(scalar list)('forever') + PsiElement(Eol)('\n') + PsiElement(indent)(' ') + PsiElement(scalar list)('young') \ No newline at end of file diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/parser/data/ErrorInBlockScalarHeader.yml b/plugins/yaml/testSrc/org/jetbrains/yaml/parser/data/ErrorInBlockScalarHeader.yml new file mode 100644 index 000000000000..10a7dda312f0 --- /dev/null +++ b/plugins/yaml/testSrc/org/jetbrains/yaml/parser/data/ErrorInBlockScalarHeader.yml @@ -0,0 +1,6 @@ +- > some invalid symbols # comment + hello + world +- | some invalid symbols # comment + forever + young