From 49b571c24ecef4a5b1d0e9a32cf069cae154ef8b Mon Sep 17 00:00:00 2001 From: David North Date: Thu, 27 Sep 2012 09:39:44 +0100 Subject: [PATCH] Port ba3c695b8ecb90e215f30ed404c923ae51d97b27 from master Allow leading whitespace in steps, both for the parser and in autocomplete. --- .../StepContentAssistProcessor.java | 3 +++ .../story/scanner/StoryTokenScanner.java | 6 +---- .../eclipse/parser/VisitingStoryParser.java | 10 ++++++++- .../test/data/LeadingWhitespace.story | 22 +++++++++++++++++++ .../parser/VisitingStoryParserTest.java | 20 +++++++++++++++++ 5 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 org.jbehave.eclipse/test/data/LeadingWhitespace.story diff --git a/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/story/completion/StepContentAssistProcessor.java b/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/story/completion/StepContentAssistProcessor.java index a4467c6..151b69c 100644 --- a/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/story/completion/StepContentAssistProcessor.java +++ b/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/story/completion/StepContentAssistProcessor.java @@ -76,6 +76,9 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, fina } } + // Allow leading whitespace. + lineStart = Strings.removeLeadingSpaces(lineStart); + logger.debug("Autocompletion offset: {} partition text: {}", offset, partitionText); logger.debug("Autocompletion line start: {}", lineStart); diff --git a/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/story/scanner/StoryTokenScanner.java b/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/story/scanner/StoryTokenScanner.java index cc412a5..c147b93 100644 --- a/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/story/scanner/StoryTokenScanner.java +++ b/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/story/scanner/StoryTokenScanner.java @@ -164,12 +164,8 @@ private void consolidateFragments() { log.error("Failed to consolidate fragments", e); } - int expected = 0; for(Fragment fragment : fragments) { - if(fragment.offset!=expected) - log.warn("humpff"); - log.debug("fragment: {}, {}, {}", o(fragment.offset, fragment.length, fragment.token.getData())); - expected = fragment.offset + fragment.length; + log.debug("fragment: {}, {}, {}", o(fragment.offset, fragment.length, fragment.token.getData())); } } diff --git a/org.jbehave.eclipse/src/org/jbehave/eclipse/parser/VisitingStoryParser.java b/org.jbehave.eclipse/src/org/jbehave/eclipse/parser/VisitingStoryParser.java index f761594..4cd58f8 100644 --- a/org.jbehave.eclipse/src/org/jbehave/eclipse/parser/VisitingStoryParser.java +++ b/org.jbehave.eclipse/src/org/jbehave/eclipse/parser/VisitingStoryParser.java @@ -42,7 +42,15 @@ public void parse(CharIterator it, int baseOffset, StoryVisitor visitor) { if (read == CharIterator.EOF) { break; } - line.append((char) read); + + // Allow leading whitespace on steps + if(offset == line.offset && Character.isSpaceChar(read)) { + line.reset(offset+1); + } + else { + line.append((char)read); + } + if (isNewlineCharacter(read)) { if (line.startsWithBreakingKeyword(tree, block)) { block.emitTo(visitor); diff --git a/org.jbehave.eclipse/test/data/LeadingWhitespace.story b/org.jbehave.eclipse/test/data/LeadingWhitespace.story new file mode 100644 index 0000000..2592ecd --- /dev/null +++ b/org.jbehave.eclipse/test/data/LeadingWhitespace.story @@ -0,0 +1,22 @@ +Narrative: + In order to be more communicative + As a story writer +I want to explain the use of And steps and also show that I can use keywords in scenario title and comments + +Scenario: And steps should match the previous step type + +Given a 5 by 5 game +When I toggle the cell at (2, 3) +Then the grid should look like +..... +..... +..... +..X.. +..... +When I toggle the cell at (2, 4) +Then the grid should look like +..... +..... +..... +..X.. +..X.. diff --git a/org.jbehave.eclipse/test/org/jbehave/eclipse/parser/VisitingStoryParserTest.java b/org.jbehave.eclipse/test/org/jbehave/eclipse/parser/VisitingStoryParserTest.java index 87a155c..005daf0 100644 --- a/org.jbehave.eclipse/test/org/jbehave/eclipse/parser/VisitingStoryParserTest.java +++ b/org.jbehave.eclipse/test/org/jbehave/eclipse/parser/VisitingStoryParserTest.java @@ -45,6 +45,26 @@ public void parse_case1() throws IOException { assertElements(expected, parser.parse(storyAsText)); } + + @Test + public void parse_cas1_with_leading_whitespace() throws IOException { + String story = "/data/LeadingWhitespace.story"; + String storyAsText = readToString(story); + + String[] expected = { + "offset: 0, length: 11, content: >>Narrative:\n<<", // + "offset: 12, length: 34, content: >>In order to be more communicative\n<<", // + "offset: 51, length: 18, content: >>As a story writer\n<<", // + "offset: 69, length: 109, content: >>I want to explain the use of And steps and also show that I can use keywords in scenario title and comments\n\n<<", // + "offset: 178, length: 57, content: >>Scenario: And steps should match the previous step type\n\n<<", // + "offset: 235, length: 20, content: >>Given a 5 by 5 game\n<<", // + "offset: 255, length: 33, content: >>When I toggle the cell at (2, 3)\n<<", // + "offset: 288, length: 61, content: >>Then the grid should look like\n.....\n.....\n.....\n..X..\n.....\n<<", // + "offset: 349, length: 33, content: >>When I toggle the cell at (2, 4)\n<<", // + "offset: 382, length: 61, content: >>Then the grid should look like\n.....\n.....\n.....\n..X..\n..X..\n<<" }; + + assertElements(expected, parser.parse(storyAsText)); + } @Test public void parse_case2() throws Exception {