Skip to content

Commit

Permalink
CLI Changes Sub-PR: Visualisation subcommand (#597)
Browse files Browse the repository at this point in the history
* Note about not using infer_subcommands

* --tolerate-parsing-errors doesn't make sense for visualisation

* Separate out input source types so we can create a unified interface

* Fallback to the given output path if canonicalisation fails

Resolves #588

* We're going to need an InputFile, too

* WIP: InputFile type

* Correct blunder regarding --query

Note that clap doesn't support (--foo [--bar] | --quux) groups very
cleanly; it was a bit of a hack to get this to work, with the result
being the error text being a bit off when an illegal combination is
attempted. I've attempted to compensate for this by making the long help
text quite explicit.

Also updated clap, which contains the fix for clap-rs/clap#5022

* Missed change to README

* Add note RE clap-rs/clap#4707 workaround

* Machinery to unify inputs + downstream use to reimplement visualisation

* Don't flatten-away errors

* Don't open the input file until we need to read from it

* Into InputFrom should be from &AtLeastOneInput

* Add logging

* Moar logging!1!!
  • Loading branch information
Xophmeister authored Aug 18, 2023
1 parent 3db6d95 commit f978b73
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 193 deletions.
21 changes: 10 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ After (use IO redirection):
```
topiary [--skip-idempotence] \
[--tolerate-parsing-errors] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
< INPUT_FILE \
> OUTPUT_FILE
```
Expand All @@ -106,7 +106,7 @@ Before:
```
topariy [--skip-idempotence] \
[--tolerate-parsing-errors] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
(--input-files - | < INPUT_FILE) \
[--output-file -]
```
Expand All @@ -115,7 +115,7 @@ After (use IO redirection):
```
topiary [--skip-idempotence] \
[--tolerate-parsing-errors] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
< INPUT_FILE
```

Expand All @@ -132,8 +132,7 @@ topiary --visualise[=FORMAT] \

After:
```
topiary vis [--tolerate-parsing-errors] \
[--format FORMAT] \
topiary vis [--format FORMAT] \
INPUT_FILE \
[> OUTPUT_FILE]
```
Expand All @@ -143,16 +142,15 @@ topiary vis [--tolerate-parsing-errors] \
Before:
```
topiary --visualise[=FORMAT] \
(--langauge LANGUAGE | --query QUERY) \
(--langauge LANGUAGE [--query QUERY]) \
< INPUT_FILE \
[--output-file OUTPUT_FILE | > OUTPUT_FILE]
```

After (use IO redirection):
```
topiary vis [--tolerate-parsing-errors] \
[--format FORMAT] \
(--langauge LANGUAGE | --query QUERY) \
topiary vis [--format FORMAT] \
(--langauge LANGUAGE [--query QUERY]) \
< INPUT_FILE \
[> OUTPUT_FILE]
```
Expand Down Expand Up @@ -186,12 +184,13 @@ topiary --configuration CONFIG_FILE \

###### Examining Computed Configuration

Before (to standard error, then proceeding with other functions):
Before (to standard error, as debug output, then proceeding with other
functions):
```
topiary --output-configuration ...
```

After (to standard output, as a dedicated function):
After (to standard output, in TOML format, as a dedicated function):
```
topiary cfg
```
Expand Down
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 39 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ Options:
[default: merge]
Possible values:
- merge: When multiple sources of configuration are available, matching items are updated from
the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items (including
collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority source is
taken. All values from lower priority sources are discarded
- merge: When multiple sources of configuration are available, matching items are
updated from the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items
(including collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority
source is taken. All values from lower priority sources are discarded
-h, --help
Print help (see a summary with '-h')
Expand All @@ -211,12 +211,15 @@ Options:
```
Format inputs
Usage: topiary fmt [OPTIONS] <--language <LANGUAGE>|--query <QUERY>|FILES>
Usage: topiary fmt [OPTIONS] <--language <LANGUAGE>|FILES>
Arguments:
[FILES]...
Input files and directories (omit to read from stdin)
Language detection and query selection is automatic, mapped from file extensions defined
in the Topiary configuration.
Options:
-t, --tolerate-parsing-errors
Consume as much as possible in the presence of parsing errors
Expand All @@ -230,7 +233,7 @@ Options:
[possible values: json, nickel, ocaml, ocaml-interface, ocamllex, toml]
-q, --query <QUERY>
Topiary query file (for formatting stdin)
Topiary query file override (when formatting stdin)
-C, --configuration <CONFIGURATION>
Configuration file
Expand All @@ -244,12 +247,12 @@ Options:
[default: merge]
Possible values:
- merge: When multiple sources of configuration are available, matching items are updated from
the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items (including
collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority source is
taken. All values from lower priority sources are discarded
- merge: When multiple sources of configuration are available, matching items are
updated from the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items
(including collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority
source is taken. All values from lower priority sources are discarded
-h, --help
Print help (see a summary with '-h')
Expand All @@ -258,7 +261,8 @@ Options:

When formatting inputs from disk, language selection is detected from
the input files' extensions. To format standard input, you must specify
either `--language` or `--query` arguments, omitting any input files.
the `--language` and, optionally, `--query` arguments, omitting any
input files.

#### Visualise

Expand All @@ -267,16 +271,16 @@ either `--language` or `--query` arguments, omitting any input files.
```
Visualise the input's Tree-sitter parse tree
Usage: topiary vis [OPTIONS] <--language <LANGUAGE>|--query <QUERY>|FILE>
Usage: topiary vis [OPTIONS] <--language <LANGUAGE>|FILE>
Arguments:
[FILE]
Input file (omit to read from stdin)
Options:
-t, --tolerate-parsing-errors
Consume as much as possible in the presence of parsing errors
Language detection and query selection is automatic, mapped from file extensions defined
in the Topiary configuration.
Options:
-f, --format <FORMAT>
Visualisation format
Expand All @@ -292,7 +296,7 @@ Options:
[possible values: json, nickel, ocaml, ocaml-interface, ocamllex, toml]
-q, --query <QUERY>
Topiary query file (for formatting stdin)
Topiary query file override (when formatting stdin)
-C, --configuration <CONFIGURATION>
Configuration file
Expand All @@ -306,12 +310,12 @@ Options:
[default: merge]
Possible values:
- merge: When multiple sources of configuration are available, matching items are updated from
the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items (including
collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority source is
taken. All values from lower priority sources are discarded
- merge: When multiple sources of configuration are available, matching items are
updated from the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items
(including collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority
source is taken. All values from lower priority sources are discarded
-h, --help
Print help (see a summary with '-h')
Expand All @@ -320,8 +324,8 @@ Options:

When visualising inputs from disk, language selection is detected from
the input file's extension. To visualise standard input, you must
specify either `--language` or `--query` arguments, omitting the input
file. The visualisation output is written to standard out.
specify the `--language` and, optionally, `--query` arguments, omitting
the input file. The visualisation output is written to standard out.

#### Configuration

Expand All @@ -345,12 +349,12 @@ Options:
[default: merge]
Possible values:
- merge: When multiple sources of configuration are available, matching items are updated from
the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items (including
collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority source is
taken. All values from lower priority sources are discarded
- merge: When multiple sources of configuration are available, matching items are
updated from the higher priority source, with collections merged as the union of sets
- revise: When multiple sources of configuration are available, matching items
(including collections) are superseded from the higher priority source
- override: When multiple sources of configuration are available, the highest priority
source is taken. All values from lower priority sources are discarded
-h, --help
Print help (see a summary with '-h')
Expand Down Expand Up @@ -408,7 +412,7 @@ sources where Topiary checks for such a file.

### Configuration Sources

At buildtime the [languages.toml](./languages.toml) in the root of
At build time the [languages.toml](./languages.toml) in the root of
this repository is embedded into Topiary. This file is parsed at
runtime. The purpose of this `languages.toml` file is to provide sane
defaults for users of Topiary (both the library and the binary).
Expand Down
2 changes: 1 addition & 1 deletion topiary-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ path = "src/main.rs"
[dependencies]
# For now we just load the tree-sitter language parsers statically.
# Eventually we will want to dynamically load them, like Helix does.
# NOTE clap/wrap_help isn't perfect (see clap-rs/clap#5022)
clap = { workspace = true, features = ["derive", "env", "wrap_help"] }
directories = { workspace = true }
env_logger = { workspace = true }
Expand All @@ -39,6 +38,7 @@ tempfile = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
toml = { workspace = true }
topiary = { path = "../topiary" }
tree-sitter-facade = { workspace = true }

[dev-dependencies]
assert_cmd = { workspace = true }
Loading

0 comments on commit f978b73

Please sign in to comment.