Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matt Cholick (Rally laptop)
committed
Oct 29, 2012
0 parents
commit 8604f72
Showing
8 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
**.DS_Store | ||
**~ | ||
|
||
.idea | ||
*.iml | ||
|
||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Copyright (c) 2012, Matt Cholick | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
The views and conclusions contained in the software and documentation are those | ||
of the authors and should not be interpreted as representing official policies, | ||
either expressed or implied, of the FreeBSD Project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<idea-plugin version="2"> | ||
<name>Spock Enhancements</name> | ||
<description> | ||
Adds syntax highlighting to Spock labels. | ||
</description> | ||
<version>0.1-alpha</version> | ||
<vendor>Matt Cholick</vendor> | ||
|
||
<depends>org.intellij.groovy</depends> | ||
|
||
<!-- please see http://confluence.jetbrains.net/display/IDEADEV/Build+Number+Ranges for description --> | ||
<idea-version since-build="107.105"/> | ||
|
||
<application-components> | ||
|
||
</application-components> | ||
|
||
<project-components> | ||
</project-components> | ||
|
||
<actions> | ||
</actions> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<highlightVisitor implementation="com.cholick.idea.spock.highlight.SpockHighlightVisitor"/> | ||
</extensions> | ||
|
||
</idea-plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Spock Enhancements is a plugin for IntelliJ IDEA that improves integration with the Spock specification framework (http://code.google.com/p/spock/) | ||
|
||
This is very much alpha at the moment. |
50 changes: 50 additions & 0 deletions
50
src/main/com/cholick/idea/spock/highlight/SpockHighlightVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.cholick.idea.spock.highlight; | ||
|
||
import com.intellij.codeInsight.daemon.impl.HighlightVisitor; | ||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; | ||
|
||
public class SpockHighlightVisitor implements HighlightVisitor { | ||
|
||
private SpockPsiElementVisitor spockPsiElementVisitor; | ||
|
||
public SpockHighlightVisitor() { | ||
spockPsiElementVisitor = new SpockPsiElementVisitor(); | ||
} | ||
|
||
@Override | ||
public boolean suitableForFile(@NotNull PsiFile file) { | ||
return file instanceof GroovyFile; | ||
} | ||
|
||
@Override | ||
public void visit(@NotNull PsiElement element) { | ||
element.accept(spockPsiElementVisitor); | ||
} | ||
|
||
@Override | ||
public boolean analyze(@NotNull PsiFile file, boolean updateWholeFile, @NotNull HighlightInfoHolder holder, @NotNull Runnable action) { | ||
spockPsiElementVisitor.setHighlightInfoHolder(holder); | ||
try { | ||
action.run(); | ||
} finally { | ||
spockPsiElementVisitor.clearHighlightInfoHolder(); | ||
} | ||
return true; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public HighlightVisitor clone() { | ||
return new SpockHighlightVisitor(); | ||
} | ||
|
||
@Override | ||
public int order() { | ||
return 0; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/com/cholick/idea/spock/highlight/SpockLabelHighlightInfoTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.cholick.idea.spock.highlight; | ||
|
||
import com.intellij.codeInsight.daemon.impl.HighlightInfoType; | ||
import com.intellij.lang.annotation.HighlightSeverity; | ||
import com.intellij.openapi.editor.colors.TextAttributesKey; | ||
|
||
public class SpockLabelHighlightInfoTypes { | ||
|
||
private static final TextAttributesKey SPOCK_LABELS = TextAttributesKey.createTextAttributesKey("SPOCK_LABELS"); | ||
|
||
public static final HighlightInfoType SPOCK_LABEL = new HighlightInfoType.HighlightInfoTypeImpl(HighlightSeverity.INFORMATION, SPOCK_LABELS); | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/com/cholick/idea/spock/highlight/SpockPsiElementVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.cholick.idea.spock.highlight; | ||
|
||
import com.intellij.codeInsight.daemon.impl.HighlightInfo; | ||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder; | ||
import com.intellij.lang.annotation.HighlightSeverity; | ||
import com.intellij.openapi.editor.markup.EffectType; | ||
import com.intellij.openapi.editor.markup.TextAttributes; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiElementVisitor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrLabel; | ||
|
||
import java.awt.Color; | ||
import java.awt.Font; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class SpockPsiElementVisitor extends PsiElementVisitor { | ||
|
||
private static final List<String> LABELS = Arrays.asList("given", "setup", "when", "then", "and", "expect"); | ||
|
||
private HighlightInfoHolder highlightInfoHolder; | ||
|
||
@Override | ||
public void visitElement(PsiElement element) { | ||
if (!highlightInfoHolder.hasErrorResults()) { | ||
if (element instanceof GrLabel && LABELS.contains(element.getText())) { | ||
highlightInfoHolder.add(createHighlightInfo(element)); | ||
} | ||
} | ||
} | ||
|
||
private static HighlightInfo createHighlightInfo(@NotNull PsiElement element) { | ||
TextRange range = element.getTextRange(); | ||
int start = range.getStartOffset(); | ||
int end = range.getEndOffset(); | ||
|
||
TextAttributes textAttributes = new TextAttributes(Color.BLUE, null, null, EffectType.BOXED, Font.BOLD); | ||
return new HighlightInfo(textAttributes, SpockLabelHighlightInfoTypes.SPOCK_LABEL, start, end, null, null, HighlightSeverity.INFORMATION, false, null, false); | ||
} | ||
|
||
|
||
public void clearHighlightInfoHolder() { | ||
this.highlightInfoHolder = null; | ||
} | ||
|
||
public void setHighlightInfoHolder(HighlightInfoHolder highlightInfoHolder) { | ||
this.highlightInfoHolder = highlightInfoHolder; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/test/com/cholick/idea/spock/highlight/SpockPsiElementVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.cholick.idea.spock.highlight; | ||
|
||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder; | ||
import com.intellij.mock.MockProjectEx; | ||
import com.intellij.mock.MockPsiElement; | ||
import com.intellij.mock.MockPsiFile; | ||
import com.intellij.mock.MockPsiManager; | ||
import com.intellij.openapi.extensions.Extensions; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.impl.source.DummyHolderElement; | ||
import com.intellij.testFramework.UsefulTestCase; | ||
import org.jetbrains.plugins.groovy.lang.psi.impl.auxiliary.GrLabelImpl; | ||
|
||
public class SpockPsiElementVisitorTest extends UsefulTestCase { | ||
|
||
public void setUp() throws Exception { | ||
super.setUp(); | ||
Extensions.registerAreaClass("IDEA_PROJECT", null); | ||
} | ||
|
||
public void testVisitNonSpockElement() { | ||
HighlightInfoHolder highlightInfoHolder = new HighlightInfoHolder(getMockFile()); | ||
SpockPsiElementVisitor elementVisitor = new SpockPsiElementVisitor(); | ||
elementVisitor.setHighlightInfoHolder(highlightInfoHolder); | ||
|
||
elementVisitor.visitElement(new MockPsiElement(myTestRootDisposable)); | ||
|
||
assertEquals(0, highlightInfoHolder.size()); | ||
} | ||
|
||
public void testVisitNonSpockLabel() { | ||
HighlightInfoHolder highlightInfoHolder = new HighlightInfoHolder(getMockFile()); | ||
SpockPsiElementVisitor elementVisitor = new SpockPsiElementVisitor(); | ||
elementVisitor.setHighlightInfoHolder(highlightInfoHolder); | ||
|
||
elementVisitor.visitElement(new GrLabelImpl(new DummyHolderElement("uncle_bob"))); | ||
|
||
assertEquals(0, highlightInfoHolder.size()); | ||
} | ||
|
||
public void testVisitSpockElement() { | ||
String label = "given"; | ||
|
||
HighlightInfoHolder highlightInfoHolder = new HighlightInfoHolder(getMockFile()); | ||
SpockPsiElementVisitor elementVisitor = new SpockPsiElementVisitor(); | ||
elementVisitor.setHighlightInfoHolder(highlightInfoHolder); | ||
|
||
elementVisitor.visitElement(new GrLabelImpl(new DummyHolderElement(label))); | ||
|
||
assertEquals(1, highlightInfoHolder.size()); | ||
assertEquals(0, highlightInfoHolder.get(0).getStartOffset()); | ||
assertEquals(label.length(), highlightInfoHolder.get(0).getEndOffset()); | ||
} | ||
|
||
private PsiFile getMockFile() { | ||
return new MockPsiFile(new MockPsiManager(new MockProjectEx(myTestRootDisposable))); | ||
} | ||
|
||
} |