Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8e73f50

Browse files
committed
Move formatter into its own file
1 parent 2ea7b5f commit 8e73f50

File tree

3 files changed

+83
-72
lines changed

3 files changed

+83
-72
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
## [2.0.0] - 2022-03-30
10+
11+
### Changed
12+
13+
- Changed `SyntaxTree` from being a class to being a module. The parser functionality is moved into `SyntaxTree::Parser`.
14+
- There is now a parent class for all of the nodes named `SyntaxTree::Node`.
15+
916
## [1.2.0] - 2022-01-09
1017

1118
### Added

lib/syntax_tree.rb

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require "ripper"
66
require "stringio"
77

8+
require_relative "syntax_tree/formatter"
89
require_relative "syntax_tree/node"
910
require_relative "syntax_tree/parser"
1011
require_relative "syntax_tree/version"
@@ -24,88 +25,20 @@
2425
end
2526

2627
module SyntaxTree
27-
# A slightly enhanced PP that knows how to format recursively including
28-
# comments.
29-
class Formatter < PP
30-
COMMENT_PRIORITY = 1
31-
HEREDOC_PRIORITY = 2
32-
33-
attr_reader :source, :stack, :quote
34-
35-
def initialize(source, ...)
36-
super(...)
37-
38-
@source = source
39-
@stack = []
40-
@quote = "\""
41-
end
42-
43-
def format(node, stackable: true)
44-
stack << node if stackable
45-
doc = nil
46-
47-
# If there are comments, then we're going to format them around the node
48-
# so that they get printed properly.
49-
if node.comments.any?
50-
leading, trailing = node.comments.partition(&:leading?)
51-
52-
# Print all comments that were found before the node.
53-
leading.each do |comment|
54-
comment.format(self)
55-
breakable(force: true)
56-
end
57-
58-
# If the node has a stree-ignore comment right before it, then we're
59-
# going to just print out the node as it was seen in the source.
60-
if leading.last&.ignore?
61-
doc = text(source[node.location.start_char...node.location.end_char])
62-
else
63-
doc = node.format(self)
64-
end
65-
66-
# Print all comments that were found after the node.
67-
trailing.each do |comment|
68-
line_suffix(priority: COMMENT_PRIORITY) do
69-
text(" ")
70-
comment.format(self)
71-
break_parent
72-
end
73-
end
74-
else
75-
doc = node.format(self)
76-
end
77-
78-
stack.pop if stackable
79-
doc
80-
end
81-
82-
def format_each(nodes)
83-
nodes.each { |node| format(node) }
84-
end
85-
86-
def parent
87-
stack[-2]
88-
end
89-
90-
def parents
91-
stack[0...-1].reverse_each
92-
end
93-
end
94-
28+
# Parses the given source and returns the syntax tree.
9529
def self.parse(source)
9630
parser = Parser.new(source)
9731
response = parser.parse
9832
response unless parser.error?
9933
end
10034

35+
# Parses the given source and returns the formatted source.
10136
def self.format(source)
102-
output = []
103-
104-
formatter = Formatter.new(source, output)
37+
formatter = Formatter.new(source, [])
10538
parse(source).format(formatter)
10639

10740
formatter.flush
108-
output.join
41+
formatter.output.join
10942
end
11043

11144
# Returns the source from the given filepath taking into account any potential

lib/syntax_tree/formatter.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# frozen_string_literal: true
2+
3+
module SyntaxTree
4+
# A slightly enhanced PP that knows how to format recursively including
5+
# comments.
6+
class Formatter < PP
7+
COMMENT_PRIORITY = 1
8+
HEREDOC_PRIORITY = 2
9+
10+
attr_reader :source, :stack, :quote
11+
12+
def initialize(source, ...)
13+
super(...)
14+
15+
@source = source
16+
@stack = []
17+
@quote = "\""
18+
end
19+
20+
def format(node, stackable: true)
21+
stack << node if stackable
22+
doc = nil
23+
24+
# If there are comments, then we're going to format them around the node
25+
# so that they get printed properly.
26+
if node.comments.any?
27+
leading, trailing = node.comments.partition(&:leading?)
28+
29+
# Print all comments that were found before the node.
30+
leading.each do |comment|
31+
comment.format(self)
32+
breakable(force: true)
33+
end
34+
35+
# If the node has a stree-ignore comment right before it, then we're
36+
# going to just print out the node as it was seen in the source.
37+
if leading.last&.ignore?
38+
doc = text(source[node.location.start_char...node.location.end_char])
39+
else
40+
doc = node.format(self)
41+
end
42+
43+
# Print all comments that were found after the node.
44+
trailing.each do |comment|
45+
line_suffix(priority: COMMENT_PRIORITY) do
46+
text(" ")
47+
comment.format(self)
48+
break_parent
49+
end
50+
end
51+
else
52+
doc = node.format(self)
53+
end
54+
55+
stack.pop if stackable
56+
doc
57+
end
58+
59+
def format_each(nodes)
60+
nodes.each { |node| format(node) }
61+
end
62+
63+
def parent
64+
stack[-2]
65+
end
66+
67+
def parents
68+
stack[0...-1].reverse_each
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)