diff --git a/README.md b/README.md index b0c916bd..8955a310 100644 --- a/README.md +++ b/README.md @@ -404,7 +404,17 @@ The language server additionally includes this custom request to return a textua ## Plugins -You can register additional languages that can flow through the same CLI with Syntax Tree's plugin system. To register a new language, call: +You can register additional configuration and additional languages that can flow through the same CLI with Syntax Tree's plugin system. When invoking the CLI, you pass through the list of plugins with the `--plugins` options to the commands that accept them. They should be a comma-delimited list. When the CLI first starts, it will require the files corresponding to those names. + +### Configuration + +To register additional configuration, define a file somewhere in your load path named `syntax_tree/my_plugin` directory. Then when invoking the CLI, you will pass `--plugins=my_plugin`. That will get required. In this way, you can modify Syntax Tree however you would like. Some plugins ship with Syntax Tree itself. They are: + +* `plugin/single_quotes` - This will change all of your string literals to use single quotes instead of the default double quotes. + +### Languages + +To register a new language, call: ```ruby SyntaxTree.register_handler(".mylang", MyLanguage) @@ -416,13 +426,11 @@ In this case, whenever the CLI encounters a filepath that ends with the given ex * `MyLanguage.parse(source)` - this should return the syntax tree corresponding to the given source. Those objects should implement the `pretty_print` interface. * `MyLanguage.format(source)` - this should return the formatted version of the given source. -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. - -* [SyntaxTree::Haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/). -* [SyntaxTree::JSON](https://github.com/ruby-syntax-tree/syntax_tree-json) for JSON. -* [SyntaxTree::RBS](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs). +Below are listed all of the "official" language plugins hosted under the same GitHub organization, which can be used as references for how to implement other plugins. -When invoking the CLI, you pass through the list of plugins with the `--plugins` options to the commands that accept them. They should be a comma-delimited list. When the CLI first starts, it will require the files corresponding to those names. +* [haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/). +* [json](https://github.com/ruby-syntax-tree/syntax_tree-json) for JSON. +* [rbs](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs). ## Integration diff --git a/lib/syntax_tree/formatter/single_quotes.rb b/lib/syntax_tree/formatter/single_quotes.rb new file mode 100644 index 00000000..4d1f41b3 --- /dev/null +++ b/lib/syntax_tree/formatter/single_quotes.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module SyntaxTree + class Formatter + # This module overrides the quote method on the formatter to use single + # quotes for everything instead of double quotes. + module SingleQuotes + def quote + "'" + end + end + end +end diff --git a/lib/syntax_tree/plugin/single_quotes.rb b/lib/syntax_tree/plugin/single_quotes.rb new file mode 100644 index 00000000..d8034084 --- /dev/null +++ b/lib/syntax_tree/plugin/single_quotes.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +require "syntax_tree/formatter/single_quotes" +SyntaxTree::Formatter.prepend(SyntaxTree::Formatter::SingleQuotes) diff --git a/test/formatter/single_quotes_test.rb b/test/formatter/single_quotes_test.rb new file mode 100644 index 00000000..8bf82cb8 --- /dev/null +++ b/test/formatter/single_quotes_test.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require_relative "../test_helper" +require "syntax_tree/formatter/single_quotes" + +module SyntaxTree + class Formatter + class TestFormatter < Formatter + prepend Formatter::SingleQuotes + end + + def test_empty_string_literal + assert_format("''\n", "\"\"") + end + + def test_string_literal + assert_format("'string'\n", "\"string\"") + end + + def test_string_literal_with_interpolation + assert_format("\"\#{foo}\"\n") + end + + def test_dyna_symbol + assert_format(":'symbol'\n", ":\"symbol\"") + end + + def test_label + assert_format( + "{ foo => foo, :'bar' => bar }\n", + "{ foo => foo, \"bar\": bar }" + ) + end + + private + + def assert_format(expected, source = expected) + formatter = TestFormatter.new(source, []) + SyntaxTree.parse(source).format(formatter) + + formatter.flush + assert_equal(expected, formatter.output.join) + end + end +end