|
1 | 1 | # tree-sitter-stack-graphs definition for Java |
2 | 2 |
|
3 | | -This project defines tree-sitter-stack-graphs rules for Java using the [tree-sitter-java](https://www.npmjs.com/package/tree-sitter-java) grammar. |
| 3 | +This project defines tree-sitter-stack-graphs rules for Java using the [tree-sitter-java][] grammar. |
4 | 4 |
|
5 | | -## Local Development |
| 5 | +[tree-sitter-java]: https://crates.io/crates/tree-sitter-java |
| 6 | + |
| 7 | +- [API documentation](https://docs.rs/tree-sitter-stack-graphs-java/) |
| 8 | +- [Release notes](https://github.com/github/stack-graphs/blob/main/languages/tree-sitter-stack-graphs-java/CHANGELOG.md) |
| 9 | + |
| 10 | +## Using the API |
| 11 | + |
| 12 | +To use this library, add the following to your `Cargo.toml`: |
| 13 | + |
| 14 | +```toml |
| 15 | +[dependencies] |
| 16 | +tree-sitter-stack-graphs-java = "0.2.0" |
| 17 | +``` |
| 18 | + |
| 19 | +Check out our [documentation](https://docs.rs/tree-sitter-stack-graphs-java/*/) for more details on how to use this library. |
| 20 | + |
| 21 | +## Using the Command-line Program |
| 22 | + |
| 23 | +The command-line program for `tree-sitter-stack-graphs-java` lets you do stack graph based analysis and lookup from the command line. |
| 24 | + |
| 25 | +The CLI can be run as follows: |
| 26 | + |
| 27 | +1. _(Installed)_ Install the CLI using Cargo as follows: |
| 28 | + |
| 29 | + ```sh |
| 30 | + cargo install --features cli tree-sitter-stack-graphs-java |
| 31 | + ``` |
| 32 | + |
| 33 | + After this, the CLI should be available as `tree-sitter-stack-graphs-java`. |
| 34 | + |
| 35 | +2. _(From source)_ Instead of installing the CLI, it can also be run directly from the crate directory, as a replacement for a `tree-sitter-stack-graphs-java` invocation, as follows: |
| 36 | + |
| 37 | + ```sh |
| 38 | + cargo run --features cli -- |
| 39 | + ``` |
| 40 | + |
| 41 | +The basic CLI workflow for the command-line program is to index source code and issue queries against the resulting database: |
| 42 | + |
| 43 | +1. Index a source folder as follows: |
| 44 | + |
| 45 | + ```sh |
| 46 | + tree-sitter-stack-graphs-java index SOURCE_DIR |
| 47 | + ``` |
| 48 | + |
| 49 | + _Indexing will skip any files that have already be indexed. To force a re-index, add the `-f` flag._ |
| 50 | + |
| 51 | + To check the status if a source folder, run: |
| 52 | + |
| 53 | + ```sh |
| 54 | + tree-sitter-stack-graphs-java status SOURCE_DIR |
| 55 | + ``` |
| 56 | + |
| 57 | + To clean the database and start with a clean slate, run: |
| 58 | + |
| 59 | + ```sh |
| 60 | + tree-sitter-stack-graphs-java clean |
| 61 | + ``` |
| 62 | + |
| 63 | + _Pass the `--delete` flag to not just empty the database, but also delete it. This is useful to resolve `unsupported database version` errors that may occur after a version update._ |
| 64 | + |
| 65 | +2. Run a query to find the definition(s) for a reference on a given line and column, run: |
| 66 | + |
| 67 | + ```sh |
| 68 | + tree-sitter-stack-graphs-java query definition SOURCE_PATH:LINE:COLUMN |
| 69 | + ``` |
| 70 | + |
| 71 | + Resulting definitions are printed, including a source line if the source file is available. |
| 72 | + |
| 73 | +Discover all available commands and flags by passing the `-h` flag to the CLI directly, or to any of the subcommands. |
| 74 | + |
| 75 | +## Development |
| 76 | + |
| 77 | +The project is written in Rust, and requires a recent version installed. Rust can be installed and updated using [rustup][]. |
| 78 | + |
| 79 | +[rustup]: https://rustup.rs/ |
6 | 80 |
|
7 | 81 | The project is organized as follows: |
8 | 82 |
|
9 | 83 | - The stack graph rules are defined in `src/stack-graphs.tsg`. |
| 84 | +- Builtins sources and configuration are defined in `src/builtins.it` and `builtins.cfg` respectively. |
10 | 85 | - Tests are put into the `test` directory. |
11 | 86 |
|
12 | | -The following commands are intended to be run from the repo root. |
| 87 | +### Running Tests |
| 88 | + |
| 89 | +Run the tests as follows: |
| 90 | + |
| 91 | +```sh |
| 92 | +cargo test |
| 93 | +``` |
| 94 | + |
| 95 | +The project consists of a library and a CLI. By default, running `cargo` only applies to the library. To run `cargo` commands on the CLI as well, add `--features cli` or `--all-features`. |
| 96 | + |
| 97 | +Run the CLI from source as follows: |
| 98 | + |
| 99 | +```sh |
| 100 | +cargo run --features cli -- ARGS |
| 101 | +``` |
| 102 | + |
| 103 | +Sources are formatted using the standard Rust formatted, which is applied by running: |
| 104 | + |
| 105 | +```sh |
| 106 | +cargo fmt |
| 107 | +``` |
| 108 | + |
| 109 | +### Writing TSG |
| 110 | + |
| 111 | +The stack graph rules are written in [tree-sitter-graph][]. Checkout the [examples][], |
| 112 | +which contain self-contained TSG rules for specific language features. A VSCode |
| 113 | +[extension][] is available that provides syntax highlighting for TSG files. |
13 | 114 |
|
14 | | -Run all tests in the project by executing the following: |
| 115 | +[tree-sitter-graph]: https://github.com/tree-sitter/tree-sitter-graph |
| 116 | +[examples]: https://github.com/github/stack-graphs/blob/main/tree-sitter-stack-graphs/examples/ |
| 117 | +[extension]: https://marketplace.visualstudio.com/items?itemName=tree-sitter.tree-sitter-graph |
15 | 118 |
|
16 | | - cargo test -p tree-sitter-stack-graphs-java |
| 119 | +Parse and test a single file by executing the following commands: |
17 | 120 |
|
18 | | -Parse a single test file: |
19 | | - `cargo run -p tree-sitter-stack-graphs-java -- parse OPTIONS FILENAME` |
| 121 | +```sh |
| 122 | +cargo run --features cli -- parse FILES... |
| 123 | +cargo run --features cli -- test TESTFILES... |
| 124 | +``` |
20 | 125 |
|
21 | | -Test a single file: |
22 | | - `cargo run -p tree-sitter-stack-graphs-java -- test OPTIONS FILENAME` |
| 126 | +Generate a visualization to debug failing tests by passing the `-V` flag: |
23 | 127 |
|
24 | | -For debugging purposes, it is useful to run the visualization tool to generate graphs for the tests being run. |
| 128 | +```sh |
| 129 | +cargo run --features cli -- test -V TESTFILES... |
| 130 | +``` |
25 | 131 |
|
26 | | -To run a test and generate the visualization: |
| 132 | +To generate the visualization regardless of test outcome, execute: |
27 | 133 |
|
28 | | -`cargo run -p tree-sitter-stack-graphs-java -- test --output-mode=always -V=%r/%d/%n.html FILENAME` |
| 134 | +```sh |
| 135 | +cargo run --features cli -- test -V --output-mode=always TESTFILES... |
| 136 | +``` |
29 | 137 |
|
30 | | -Go to https://crates.io/crates/tree-sitter-stack-graphs for links to examples and documentation. |
| 138 | +Go to <https://crates.io/crates/tree-sitter-stack-graphs> for links to examples and documentation. |
0 commit comments