-
Notifications
You must be signed in to change notification settings - Fork 9
Replace help syntax form with ,doc REPL command.
#509
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
130 changes: 130 additions & 0 deletions
130
fusioncli/src/main/java/dev/ionfusion/fusioncli/repl/cmd/DocCmd.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,130 @@ | ||
| // Copyright Ion Fusion contributors. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package dev.ionfusion.fusioncli.repl.cmd; | ||
|
|
||
| import static dev.ionfusion.fusion.FusionSyntax.isIdentifier; | ||
| import static dev.ionfusion.fusion._Private_Trampoline.findBindingDoc; | ||
|
|
||
| import dev.ionfusion.fusioncli.framework.Command; | ||
| import dev.ionfusion.fusioncli.framework.Executor; | ||
| import dev.ionfusion.fusioncli.framework.UsageException; | ||
| import dev.ionfusion.fusioncli.repl.ReplContext; | ||
| import dev.ionfusion.fusioncli.repl.ReplExecutor; | ||
| import dev.ionfusion.runtime._private.doc.BindingDoc; | ||
| import dev.ionfusion.runtime.base.FusionException; | ||
| import dev.ionfusion.runtime.embed.TopLevel; | ||
| import java.io.PrintWriter; | ||
|
|
||
| public class DocCmd | ||
| extends Command<ReplContext> | ||
| { | ||
| private static final String HELP_ONE_LINER = | ||
| "Print documentation for a given identifier"; | ||
|
|
||
| private static final String HELP_USAGE = | ||
| "doc IDENTIFIER"; | ||
|
|
||
| private static final String HELP_BODY = | ||
| "Resolves the given identifier in the current namespace and prints any associated\n" + | ||
| "documentation."; | ||
|
|
||
|
|
||
| public DocCmd() | ||
| { | ||
| super("doc"); | ||
| putHelpText(HELP_ONE_LINER, HELP_USAGE, HELP_BODY); | ||
| } | ||
|
|
||
| @Override | ||
| public Executor makeExecutor(ReplContext replContext, String[] args) | ||
| throws UsageException | ||
| { | ||
| assert args.length < 2; | ||
| if (args.length == 0) | ||
| { | ||
| throw usage("Expected an identifier"); | ||
| } | ||
|
|
||
| return new DocExecutor(replContext, args[0]); | ||
| } | ||
|
|
||
|
|
||
| private class DocExecutor | ||
| extends ReplExecutor | ||
| { | ||
| private final String myArg; | ||
|
|
||
| private DocExecutor(ReplContext replContext, String arg) | ||
| { | ||
| super(replContext); | ||
| myArg = arg; | ||
| } | ||
|
|
||
| @Override | ||
| public int execute() | ||
| throws Exception | ||
| { | ||
| Object id = determineIdentifier(); | ||
| displayDoc(id); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| private Object determineIdentifier() | ||
| throws Exception | ||
| { | ||
| TopLevel top = context().top(); | ||
|
|
||
| // TODO This assumes the normal binding of `quote_syntax` | ||
|
|
||
| Object stx; | ||
| try | ||
| { | ||
| stx = top.eval("(quote_syntax " + myArg + ")"); | ||
| } | ||
| catch (FusionException e) | ||
| { | ||
| throw usage("Expected an identifier"); | ||
| } | ||
|
|
||
| if (! isIdentifier(top, stx)) | ||
| { | ||
| throw usage("Expected an identifier"); | ||
| } | ||
|
|
||
| return stx; | ||
| } | ||
|
|
||
| private void displayDoc(Object id) | ||
| { | ||
| PrintWriter out = context().out(); | ||
|
|
||
| BindingDoc doc = findBindingDoc(context().top(), id); | ||
| if (doc == null) | ||
| { | ||
| out.println("No documentation available."); | ||
| return; | ||
| } | ||
|
|
||
| if (doc.getKind() != null) | ||
| { | ||
| out.append("["); | ||
| // Using enum toString() allows display name to be changed | ||
| out.append(doc.getKind().toString()); | ||
| out.append("] "); | ||
| } | ||
| if (doc.getUsage() != null) | ||
| { | ||
| out.append(doc.getUsage()); | ||
| } | ||
|
|
||
| if (doc.getBody() != null) | ||
| { | ||
| out.append('\n'); | ||
| out.append(doc.getBody()); | ||
| out.append('\n'); | ||
| } | ||
| } | ||
| } | ||
| } |
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
77 changes: 77 additions & 0 deletions
77
fusioncli/src/test/java/dev/ionfusion/fusioncli/repl/ReplDocTest.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,77 @@ | ||
| // Copyright Ion Fusion contributors. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package dev.ionfusion.fusioncli.repl; | ||
|
|
||
| import org.junit.jupiter.api.Disabled; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ReplDocTest | ||
| extends ReplTestCase | ||
| { | ||
| @Test | ||
| public void docWithoutArg() | ||
| throws Exception | ||
| { | ||
| supplyInput(",doc\n"); | ||
| runRepl(); | ||
|
|
||
| expectError("Expected an identifier"); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void docForUnbound() | ||
| throws Exception | ||
| { | ||
| supplyInput(",doc unbound_id\n"); | ||
| runRepl(); | ||
|
|
||
| // TODO Distinguish between unbound vars and undocumented bindings. | ||
| expectError("No documentation available."); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void docForBuiltin() | ||
| throws Exception | ||
| { | ||
| supplyInput(",doc * \n"); // Extra space is intentional. | ||
| runRepl(); | ||
|
|
||
| expectResponse("Returns the product"); | ||
| } | ||
|
|
||
| @Test | ||
| @Disabled("https://github.com/ion-fusion/fusion-java/issues/510") | ||
| public void docForLocal() | ||
| throws Exception | ||
| { | ||
| supplyInput("(define (local) '''local docs''' true)\n"); | ||
| supplyInput(",doc local\n"); | ||
| runRepl(); | ||
|
|
||
| expectResponse("local docs"); | ||
| } | ||
|
|
||
| @Test | ||
| public void docForBadSyntax() | ||
| throws Exception | ||
| { | ||
| supplyInput(",doc [\n"); // Extra space is intentional. | ||
| runRepl(); | ||
|
|
||
| expectError("Expected an identifier"); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void docForNonIdentifier() | ||
| throws Exception | ||
| { | ||
| supplyInput(",doc 12\n"); // Extra space is intentional. | ||
| runRepl(); | ||
|
|
||
| expectError("Expected an identifier"); | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.