Skip to content

Commit

Permalink
Merge pull request #386 from savi-lang/add/no-indent-string
Browse files Browse the repository at this point in the history
Don't enforce indentation format rules within string literals.
  • Loading branch information
jemc authored Sep 17, 2022
2 parents 6fa1de2 + f6a71c8 commit 3ac46b8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
73 changes: 73 additions & 0 deletions spec/compiler/format.indent.savi.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,76 @@ Multi-line function signatures should be indented correctly.
"four"
]
```

---

No particular indentation is enforced inside literal string content.

```savi
:fun example_1:
<<<
This is the most common kind of multi-line string,
with indentation consistently one level deeper than the outside.
>>>
:fun example_2:
<<<
But that isn't enforced and it's totally valid
to have string content that is indented at different levels,
because the extra indentation bytes will be part of the string!
>>>
:fun example_3:
"Sometimes you'll also want to write a
multi-line string like this with normal quotes,
where any indentation will end up as part of the string,
which you might want to avoid.
"
:fun example_4:
"The most common reason to use normal quotes
is if you need to do something with string interpolation.
But it's worth noting that interpolated code will end up
having its indentation enforced, based on the indentation
of the code around the string literal rather than the
indentation of the literal content.
So the following interpolation will end up being 6 spaces deep: \(
@this_will_be(6).spaces_deep
)
"
```
```savi format.Indentation
:fun example_1:
<<<
This is the most common kind of multi-line string,
with indentation consistently one level deeper than the outside.
>>>
:fun example_2:
<<<
But that isn't enforced and it's totally valid
to have string content that is indented at different levels,
because the extra indentation bytes will be part of the string!
>>>
:fun example_3:
"Sometimes you'll also want to write a
multi-line string like this with normal quotes,
where any indentation will end up as part of the string,
which you might want to avoid.
"
:fun example_4:
"The most common reason to use normal quotes
is if you need to do something with string interpolation.
But it's worth noting that interpolated code will end up
having its indentation enforced, based on the indentation
of the code around the string literal rather than the
indentation of the literal content.
So the following interpolation will end up being 6 spaces deep: \(
@this_will_be(6).spaces_deep
)
"
```
19 changes: 16 additions & 3 deletions src/savi/ast/format/indent_state.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ class Savi::AST::Format::IndentState
property parent : Layer?
property node : AST::Node
property current_declare_indent : Int32
property enforce : Bool
property pos_list : Array({Source::Pos, Int32})?

def initialize(@parent, @node, @current_declare_indent)
def initialize(@parent, @node, @current_declare_indent, @enforce = true)
end

def enforces_indent?
@enforce
end

def empty?
Expand Down Expand Up @@ -52,11 +57,18 @@ class Savi::AST::Format::IndentState
# A single-line node never adds to the indentation stack.
return if node.pos.single_line?

# Certain nodes are exempted from adding to the indentation stack.
# Certain nodes are treated in special ways. Some return early such that
# they don't add to the indentation stack when they otherwise would.
enforce = true
case node
when AST::Document
# Document nodes can never add indentation.
return
when AST::LiteralString
# Don't force any specific indentation inside a literal string,
# as the spacing inside the string is part of the literal string,
# including the indentation (or lack thereof) on new lines.
enforce = false
when AST::Group
# Declare-level groups don't add indentation in this way.
# Their indentation is already covered in the declare depth logic.
Expand Down Expand Up @@ -89,7 +101,7 @@ class Savi::AST::Format::IndentState
end

# Push an indent stack layer for this node.
@indent_stack << Layer.new(@indent_stack.last, node, @current_declare_indent)
@indent_stack << Layer.new(@indent_stack.last, node, @current_declare_indent, enforce)
end

def maybe_pop_indent(visitor, node)
Expand Down Expand Up @@ -166,6 +178,7 @@ class Savi::AST::Format::IndentState
def each_indent_violation
@indent_map.each { |layer, pos_list|
next if layer.empty?
next unless layer.enforces_indent?

expected_level = layer.resolve_indent_level

Expand Down

0 comments on commit 3ac46b8

Please sign in to comment.