-
-
Notifications
You must be signed in to change notification settings - Fork 153
Add support to autoformat a gherking file #553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
...umber.eclipse.editor/src/io/cucumber/eclipse/editor/format/EditorInputPropertyTester.java
This file contains hidden or 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,32 @@ | ||
package io.cucumber.eclipse.editor.format; | ||
|
||
import org.eclipse.core.expressions.PropertyTester; | ||
import org.eclipse.core.filebuffers.FileBuffers; | ||
import org.eclipse.core.filebuffers.ITextFileBuffer; | ||
import org.eclipse.core.filebuffers.LocationKind; | ||
import org.eclipse.ui.IEditorInput; | ||
import org.eclipse.ui.IEditorPart; | ||
import org.eclipse.ui.IFileEditorInput; | ||
|
||
import io.cucumber.eclipse.editor.document.GherkinEditorDocument; | ||
|
||
public class EditorInputPropertyTester extends PropertyTester { | ||
|
||
@Override | ||
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { | ||
if (receiver instanceof IEditorPart part) { | ||
if ("isGherkingDocument".equals(property)) { | ||
IEditorInput editorInput = part.getEditorInput(); | ||
if (editorInput instanceof IFileEditorInput fileInput) { | ||
ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager() | ||
.getTextFileBuffer(fileInput.getFile().getFullPath(), LocationKind.IFILE); | ||
if (buffer != null) { | ||
return GherkinEditorDocument.isCompatible(buffer.getDocument()); | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
io.cucumber.eclipse.editor/src/io/cucumber/eclipse/editor/format/GherkingFormatHandler.java
This file contains hidden or 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,45 @@ | ||
package io.cucumber.eclipse.editor.format; | ||
|
||
import java.util.Optional; | ||
|
||
import org.eclipse.core.commands.AbstractHandler; | ||
import org.eclipse.core.commands.ExecutionEvent; | ||
import org.eclipse.core.commands.ExecutionException; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.ui.IEditorInput; | ||
import org.eclipse.ui.IEditorPart; | ||
import org.eclipse.ui.IFileEditorInput; | ||
import org.eclipse.ui.IWorkbenchPart; | ||
import org.eclipse.ui.handlers.HandlerUtil; | ||
|
||
import io.cucumber.eclipse.editor.document.GherkinEditorDocument; | ||
import io.cucumber.gherkin.utils.pretty.Pretty; | ||
import io.cucumber.gherkin.utils.pretty.Syntax; | ||
import io.cucumber.messages.types.GherkinDocument; | ||
|
||
public class GherkingFormatHandler extends AbstractHandler { | ||
|
||
@Override | ||
public Object execute(ExecutionEvent event) throws ExecutionException { | ||
IWorkbenchPart part = HandlerUtil.getActivePartChecked(event); | ||
if (part instanceof IEditorPart editor) { | ||
IEditorInput editorInput = editor.getEditorInput(); | ||
if (editorInput instanceof IFileEditorInput fileInput) { | ||
GherkinEditorDocument document = GherkinEditorDocument.get(fileInput.getFile()); | ||
if (document != null) { | ||
formatDocument(document, fileInput.getFile()); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private void formatDocument(GherkinEditorDocument document, IFile file) { | ||
Optional<GherkinDocument> gherkinDocument = document.getGherkinDocument(); | ||
if (gherkinDocument.isEmpty()) { | ||
return; | ||
} | ||
String prettyPrint = Pretty.prettyPrint(gherkinDocument.get(), Syntax.gherkin); | ||
document.getDocument().set(prettyPrint); | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
cucumber.messages.internal
still exists.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mpkorstanje year this is maybe not required anymore and is a bit cumbersome to manage anyways. Because of that I would suggest adding the data in cucumber directly as with:
this is much more appropriate and consistent.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with these definitions is that it they're equally cumbersome if kept with the project itself. Problems exclusively show up at runtime. In this case it is harmless, but forgetting to expose a package would break that module.
But lets keep that discussion in github.com/cucumber/gherkin-utils/pull/131. Consider this FYI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mpkorstanje in this case superfluous instructions simply do not taken into account but of course to not add to the clarity. So we can see this more as no one ever noticed this has gone :-D