Skip to content

Commit 9598362

Browse files
committed
Document plugins
1 parent c2a5599 commit 9598362

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ It is built with only standard library dependencies. It additionally ships with
3333
- [textDocument/formatting](#textdocumentformatting)
3434
- [textDocument/inlayHints](#textdocumentinlayhints)
3535
- [syntaxTree/visualizing](#syntaxtreevisualizing)
36+
- [Plugins](#plugins)
3637
- [Contributing](#contributing)
3738
- [License](#license)
3839

@@ -307,6 +308,26 @@ Implicity, the `2 * 3` is going to be executed first because the `*` operator ha
307308

308309
The language server additionally includes this custom request to return a textual representation of the syntax tree underlying the source code of a file. Language server clients can use this to (for example) open an additional tab with this information displayed.
309310

311+
## Plugins
312+
313+
You can register additional languages that can flow through the same CLI with Syntax Tree's plugin system. To register a new language, call:
314+
315+
```ruby
316+
SyntaxTree.register_handler(".mylang", MyLanguage)
317+
```
318+
319+
In this case, whenever the CLI encounters a filepath that ends with the given extension, it will invoke methods on `MyLanguage` instead of `SyntaxTree` itself. To make sure your object conforms to each of the necessary APIs, it should implement:
320+
321+
* `MyLanguage.read(filepath)` - usually this is just an alias to `File.read(filepath)`, but if you need anything else that hook is here.
322+
* `MyLanguage.parse(source)` - this should return the syntax tree corresponding to the given source. Those objects should implement the `pretty_print` interface.
323+
* `MyLanguage.format(source)` - this should return the formatted version of the given source.
324+
325+
Below are listed all of the "official" plugins hosted under the same GitHub organization, which can be used as references for how to implement other plugins.
326+
327+
* [SyntaxTree::Haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/).
328+
* [SyntaxTree::JSON](https://github.com/ruby-syntax-tree/syntax_tree-json) for JSON.
329+
* [SyntaxTree::RBS](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs).
330+
310331
## Contributing
311332

312333
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/syntax_tree.

0 commit comments

Comments
 (0)