Skip to content

Commit 5901fb8

Browse files
committed
Add the single quotes configuration option
1 parent 5697b15 commit 5901fb8

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,17 @@ The language server additionally includes this custom request to return a textua
404404

405405
## Plugins
406406

407-
You can register additional languages that can flow through the same CLI with Syntax Tree's plugin system. To register a new language, call:
407+
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.
408+
409+
### Configuration
410+
411+
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:
412+
413+
* `plugin/single_quotes` - This will change all of your string literals to use single quotes instead of the default double quotes.
414+
415+
### Languages
416+
417+
To register a new language, call:
408418

409419
```ruby
410420
SyntaxTree.register_handler(".mylang", MyLanguage)
@@ -416,13 +426,11 @@ In this case, whenever the CLI encounters a filepath that ends with the given ex
416426
* `MyLanguage.parse(source)` - this should return the syntax tree corresponding to the given source. Those objects should implement the `pretty_print` interface.
417427
* `MyLanguage.format(source)` - this should return the formatted version of the given source.
418428

419-
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.
420-
421-
* [SyntaxTree::Haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/).
422-
* [SyntaxTree::JSON](https://github.com/ruby-syntax-tree/syntax_tree-json) for JSON.
423-
* [SyntaxTree::RBS](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs).
429+
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.
424430

425-
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.
431+
* [haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/).
432+
* [json](https://github.com/ruby-syntax-tree/syntax_tree-json) for JSON.
433+
* [rbs](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs).
426434

427435
## Integration
428436

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module SyntaxTree
4+
class Formatter
5+
# This module overrides the quote method on the formatter to use single
6+
# quotes for everything instead of double quotes.
7+
module SingleQuotes
8+
def quote
9+
"'"
10+
end
11+
end
12+
end
13+
end
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require "syntax_tree/formatter/single_quotes"
4+
SyntaxTree::Formatter.prepend(SyntaxTree::Formatter::SingleQuotes)

test/formatter/single_quotes_test.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
require "syntax_tree/formatter/single_quotes"
5+
6+
module SyntaxTree
7+
class Formatter
8+
class TestFormatter < Formatter
9+
prepend Formatter::SingleQuotes
10+
end
11+
12+
def test_empty_string_literal
13+
assert_format("''\n", "\"\"")
14+
end
15+
16+
def test_string_literal
17+
assert_format("'string'\n", "\"string\"")
18+
end
19+
20+
def test_string_literal_with_interpolation
21+
assert_format("\"\#{foo}\"\n")
22+
end
23+
24+
def test_dyna_symbol
25+
assert_format(":'symbol'\n", ":\"symbol\"")
26+
end
27+
28+
def test_label
29+
assert_format(
30+
"{ foo => foo, :'bar' => bar }\n",
31+
"{ foo => foo, \"bar\": bar }"
32+
)
33+
end
34+
35+
private
36+
37+
def assert_format(expected, source = expected)
38+
formatter = TestFormatter.new(source, [])
39+
SyntaxTree.parse(source).format(formatter)
40+
41+
formatter.flush
42+
assert_equal(expected, formatter.output.join)
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)