Skip to content

Commit 7f61456

Browse files
Christoph Läubrichlaeubi
authored andcommitted
Add support to autoformat a gherking file
Fix #544 Fix #478
1 parent a18d0a6 commit 7f61456

File tree

5 files changed

+159
-4
lines changed

5 files changed

+159
-4
lines changed

io.cucumber.eclipse.editor/META-INF/MANIFEST.MF

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ Require-Bundle: org.eclipse.ui;bundle-version="3.5.0",
3434
org.eclipse.ui.console,
3535
org.eclipse.ui.workbench.texteditor,
3636
io.cucumber.tag-expressions,
37-
org.eclipse.unittest.ui;bundle-version="1.0.0"
37+
org.eclipse.unittest.ui;bundle-version="1.0.0",
38+
io.cucumber.gherkin-utils;bundle-version="9.0.0",
39+
org.eclipse.core.expressions;bundle-version="3.9.500"
3840
Bundle-RequiredExecutionEnvironment: JavaSE-21
3941
Automatic-Module-Name: io.cucumber.eclipse.editor
4042
Bundle-ActivationPolicy: lazy

io.cucumber.eclipse.editor/plugin.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,64 @@
88
editorId="org.eclipse.ui.genericeditor.GenericEditor">
99
</editorContentTypeBinding>
1010
</extension>
11+
12+
<extension point="org.eclipse.ui.bindings">
13+
<key sequence="M1+M2+F"
14+
commandId="io.cucumber.eclipse.editor.format"
15+
contextId="org.eclipse.ui.textEditorScope"
16+
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" />
17+
</extension>
18+
<extension point="org.eclipse.ui.menus">
19+
<menuContribution
20+
allPopups="true"
21+
locationURI="popup:org.eclipse.ui.genericeditor.source.menu?after=additions">
22+
<command commandId="io.cucumber.eclipse.editor.format">
23+
<visibleWhen>
24+
<with variable="activeEditorInput">
25+
<adapt type="org.eclipse.core.resources.IResource">
26+
<test
27+
property="org.eclipse.core.resources.contentTypeId"
28+
value="io.cucumber.eclipse.editor.content-type.feature">
29+
</test>
30+
</adapt>
31+
</with>
32+
</visibleWhen>
33+
</command>
34+
</menuContribution>
35+
</extension>
36+
37+
<extension point="org.eclipse.ui.commands">
38+
<command
39+
categoryId="io.cucumber.eclipse.editor.category"
40+
id="io.cucumber.eclipse.editor.format"
41+
name="Format Feature File" />
42+
</extension>
43+
44+
45+
<extension point="org.eclipse.ui.handlers">
46+
<handler
47+
class="io.cucumber.eclipse.editor.format.GherkingFormatHandler"
48+
commandId="io.cucumber.eclipse.editor.format">
49+
<activeWhen>
50+
<with variable="activePart">
51+
<test property="io.cucumber.eclipse.editor.format.isGherkingDocument" value="true">
52+
</test>
53+
</with>
54+
</activeWhen>
55+
</handler>
56+
</extension>
57+
58+
<extension
59+
point="org.eclipse.core.expressions.propertyTesters">
60+
<propertyTester
61+
class="io.cucumber.eclipse.editor.format.EditorInputPropertyTester"
62+
id="io.cucumber.eclipse.editor.format.editorInputPropertyTester"
63+
namespace="io.cucumber.eclipse.editor.format"
64+
properties="isGherkingDocument"
65+
type="org.eclipse.ui.IWorkbenchPart">
66+
</propertyTester>
67+
</extension>
68+
1169
<extension
1270
point="org.eclipse.core.contenttype.contentTypes">
1371
<content-type
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.cucumber.eclipse.editor.format;
2+
3+
import org.eclipse.core.expressions.PropertyTester;
4+
import org.eclipse.core.filebuffers.FileBuffers;
5+
import org.eclipse.core.filebuffers.ITextFileBuffer;
6+
import org.eclipse.core.filebuffers.LocationKind;
7+
import org.eclipse.ui.IEditorInput;
8+
import org.eclipse.ui.IEditorPart;
9+
import org.eclipse.ui.IFileEditorInput;
10+
11+
import io.cucumber.eclipse.editor.document.GherkinEditorDocument;
12+
13+
public class EditorInputPropertyTester extends PropertyTester {
14+
15+
@Override
16+
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
17+
if (receiver instanceof IEditorPart part) {
18+
if ("isGherkingDocument".equals(property)) {
19+
IEditorInput editorInput = part.getEditorInput();
20+
if (editorInput instanceof IFileEditorInput fileInput) {
21+
ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager()
22+
.getTextFileBuffer(fileInput.getFile().getFullPath(), LocationKind.IFILE);
23+
if (buffer != null) {
24+
return GherkinEditorDocument.isCompatible(buffer.getDocument());
25+
}
26+
}
27+
}
28+
}
29+
return false;
30+
}
31+
32+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.cucumber.eclipse.editor.format;
2+
3+
import java.util.Optional;
4+
5+
import org.eclipse.core.commands.AbstractHandler;
6+
import org.eclipse.core.commands.ExecutionEvent;
7+
import org.eclipse.core.commands.ExecutionException;
8+
import org.eclipse.core.resources.IFile;
9+
import org.eclipse.ui.IEditorInput;
10+
import org.eclipse.ui.IEditorPart;
11+
import org.eclipse.ui.IFileEditorInput;
12+
import org.eclipse.ui.IWorkbenchPart;
13+
import org.eclipse.ui.handlers.HandlerUtil;
14+
15+
import io.cucumber.eclipse.editor.document.GherkinEditorDocument;
16+
import io.cucumber.gherkin.utils.pretty.Pretty;
17+
import io.cucumber.gherkin.utils.pretty.Syntax;
18+
import io.cucumber.messages.types.GherkinDocument;
19+
20+
public class GherkingFormatHandler extends AbstractHandler {
21+
22+
@Override
23+
public Object execute(ExecutionEvent event) throws ExecutionException {
24+
IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
25+
if (part instanceof IEditorPart editor) {
26+
IEditorInput editorInput = editor.getEditorInput();
27+
if (editorInput instanceof IFileEditorInput fileInput) {
28+
GherkinEditorDocument document = GherkinEditorDocument.get(fileInput.getFile());
29+
if (document != null) {
30+
formatDocument(document, fileInput.getFile());
31+
}
32+
}
33+
}
34+
return null;
35+
}
36+
37+
private void formatDocument(GherkinEditorDocument document, IFile file) {
38+
Optional<GherkinDocument> gherkinDocument = document.getGherkinDocument();
39+
if (gherkinDocument.isEmpty()) {
40+
return;
41+
}
42+
String prettyPrint = Pretty.prettyPrint(gherkinDocument.get(), Syntax.gherkin);
43+
document.getDocument().set(prettyPrint);
44+
}
45+
}

io.cucumber.eclipse.targetdefinition/cucumber.eclipse.targetdefinition.target

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<dependency>
1818
<groupId>io.cucumber</groupId>
1919
<artifactId>gherkin</artifactId>
20-
<version>33.0.0</version>
20+
<version>33.1.0</version>
2121
<type>jar</type>
2222
</dependency>
2323
</dependencies>
@@ -35,7 +35,7 @@ Export-Package: *;version="${version}";-noimport:=true
3535
<dependency>
3636
<groupId>io.cucumber</groupId>
3737
<artifactId>cucumber-java</artifactId>
38-
<version>7.26.0</version>
38+
<version>7.29.0</version>
3939
<type>jar</type>
4040
</dependency>
4141
</dependencies>
@@ -53,7 +53,7 @@ Export-Package: *;version="${version}";-noimport:=true
5353
<dependency>
5454
<groupId>io.cucumber</groupId>
5555
<artifactId>cucumber-core</artifactId>
56-
<version>7.26.0</version>
56+
<version>7.29.0</version>
5757
<type>jar</type>
5858
</dependency>
5959
</dependencies>
@@ -100,6 +100,24 @@ Export-Package: *;version="${version}";-noimport:=true
100100
</dependency>
101101
</dependencies>
102102
</location>
103+
<location includeDependencyDepth="infinite" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven">
104+
<dependencies>
105+
<dependency>
106+
<groupId>io.cucumber</groupId>
107+
<artifactId>gherkin-utils</artifactId>
108+
<version>9.0.0</version>
109+
<type>jar</type>
110+
</dependency>
111+
</dependencies>
112+
<instructions><![CDATA[
113+
Bundle-Name: ${mvnGroupId}:${mvnArtifactId}:${mvnVersion}
114+
version: ${version_cleanup;${mvnVersion}}
115+
Bundle-SymbolicName: ${mvnGroupId}.${mvnArtifactId}
116+
Bundle-Version: ${version}
117+
Import-Package: !sun.*,io.cucumber.messages.internal.com.google.gson*,!io.cucumber.messages.internal.*,!org.checkerframework.*,*
118+
Export-Package: *;version="${version}";-noimport:=true
119+
]]></instructions>
120+
</location>
103121
</locations>
104122
<targetJRE path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
105123
</target>

0 commit comments

Comments
 (0)