|
| 1 | +# Contributing to tree-sitter-scala |
| 2 | + |
| 3 | +First off, thanks for being willing to contribute to making syntax highlighting |
| 4 | +in Scala better! This document will hopefully help you get set up, understand |
| 5 | +how to work on the codebase, or link to places to help you understand |
| 6 | +tree-sitter. |
| 7 | + |
| 8 | +## Requirements |
| 9 | + |
| 10 | +- Node.js version 18.0 or greater |
| 11 | +- C Compiler |
| 12 | + |
| 13 | +Refer to the [tree-sitter |
| 14 | +documentation](https://tree-sitter.github.io/tree-sitter/creating-parsers#dependencies) |
| 15 | +for more details and specifics. |
| 16 | + |
| 17 | +If you use nix you can enter a nix-shell (`nix-shell .`) which will install them |
| 18 | +for you. |
| 19 | + |
| 20 | +## Getting Started |
| 21 | + |
| 22 | +To get started with contributing to the grammar you'll first need to install the |
| 23 | +project's dependencies: |
| 24 | + |
| 25 | +```sh |
| 26 | +npm install |
| 27 | +``` |
| 28 | + |
| 29 | +The general flow will often start with adding a test case to `./corpus`. You can |
| 30 | +find details on how testing works with tree-sitter |
| 31 | +[here](https://tree-sitter.github.io/tree-sitter/creating-parsers#command-test). |
| 32 | + |
| 33 | +Once you've added your test case you'll want to then make the required changes |
| 34 | +to `grammar.js`, regenerate and recompile the parser, and run the tests: |
| 35 | + |
| 36 | +```sh |
| 37 | +npm run build |
| 38 | +npm test |
| 39 | +``` |
| 40 | + |
| 41 | +Then adjust as necessary. Note that depending on your change you may also have |
| 42 | +to touch the `/src/scanner.c` file if you need more advanced features like |
| 43 | +look-ahead. |
| 44 | + |
| 45 | +## Syntax highlighting |
| 46 | + |
| 47 | +Right now the most common use-case for syntax highlight using tree-sitter is |
| 48 | +[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter), which |
| 49 | +means much of our testing is in relation to it. You can find the syntax |
| 50 | +highlighting tests in `test/highlight/*.scala`. You can read more about this |
| 51 | +type of testing |
| 52 | +[here](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#unit-testing). These test will be automatically ran with `npm run test`. |
| 53 | + |
| 54 | +### tree-sitter highlight |
| 55 | + |
| 56 | +Another way to test your syntax highlighting locally is to use the `tree-sitter |
| 57 | +highlight` command. Note that you'll need to have `tree-sitter` installed |
| 58 | +globally for this to work. Once you have it installed you'll want to follow the |
| 59 | +instructions [here](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#per-user-configuration) to setup a local config that points towards this repo to be used as a parser. Once done you can then do the following: |
| 60 | + |
| 61 | +```sh |
| 62 | +tree-sitter highlight some/scala/file.scala |
| 63 | +``` |
| 64 | + |
| 65 | +And see the syntax highlighting spit out. This is also the format used for |
| 66 | +testing, so it provides an easy way to get the output we use for testing. |
| 67 | + |
| 68 | +## Generation |
| 69 | + |
| 70 | +In order to help not cause conflicts with multiple prs being open at the same |
| 71 | +time, we don't check in the parser on each pr. Instead, just check in the |
| 72 | +changes to the `grammar.js` and the CI will take care of the rest. Each night if |
| 73 | +changes are detected it will automatically be generated. |
| 74 | + |
| 75 | +## Smoke testing |
| 76 | + |
| 77 | +You'll noticed that there is a part of CI that checks parsing against the Scala |
| 78 | +2 and Scala 3 repositories to ensure we don't introduce unexpected regressions. |
| 79 | +The script for this is in `script/smoke_test.sh`. If you're change is increasing |
| 80 | +the coverage in either the library or compiler, please do update the expected |
| 81 | +percentages at the top of the file. |
| 82 | + |
| 83 | +## Obtaining an error reproduction |
| 84 | + |
| 85 | +_With Neovim_ |
| 86 | + |
| 87 | +When creating an issue you'll want to ensure to include the `ERROR` node in it's |
| 88 | +context. There's a couple ways to do this. If you're using Neovim and utilizing |
| 89 | +treesitter, you can see the tree with `:lua vim.treesitter.show_tree()`. You can |
| 90 | +copy it directly from the open panel. |
| 91 | + |
| 92 | +_Manually_ |
| 93 | + |
| 94 | +A manual way to get this would be to ensure that when you're in the repo you |
| 95 | +have the latest parser with |
| 96 | + |
| 97 | +```sh |
| 98 | +npm run build |
| 99 | +``` |
| 100 | + |
| 101 | +Then you can copy your Scala code in a file and pass that file into `npm run |
| 102 | +parse`: |
| 103 | + |
| 104 | +``` |
| 105 | +npm run parse <path/to/your/file.scala> |
| 106 | +``` |
| 107 | + |
| 108 | +Then the tree will be printed out for you to copy. |
| 109 | + |
0 commit comments