Skip to content

Add the single quotes configuration option #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
13 changes: 13 additions & 0 deletions lib/syntax_tree/formatter/single_quotes.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/syntax_tree/plugin/single_quotes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require "syntax_tree/formatter/single_quotes"
SyntaxTree::Formatter.prepend(SyntaxTree::Formatter::SingleQuotes)
45 changes: 45 additions & 0 deletions test/formatter/single_quotes_test.rb
Original file line number Diff line number Diff line change
@@ -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