Skip to content

Commit

Permalink
Allow comments in things. (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign authored Nov 24, 2024
1 parent 5c88f96 commit 35aff16
Show file tree
Hide file tree
Showing 42 changed files with 370 additions and 49 deletions.
11 changes: 11 additions & 0 deletions spec/examples/array_literal
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ component Main {
<div/>
}
}
-------------------------------------------------------------------------------
module Test {
fun test : Array(String) {
[
// Start Comment
"Item 1",
// End comment
"Item 2"
] of String
}
}
7 changes: 7 additions & 0 deletions spec/examples/operation
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,10 @@ component Main {
"Hello" + "There"
}
}
-------------------------------------------------------------------------------
component Main {
fun render : String {
"Hello" // Some comment
+ "There"
}
}
14 changes: 14 additions & 0 deletions spec/examples/pipe
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,17 @@ component Main {
<div/>
}
}
-------------------------------------------------------------------------------
component Main {
fun test (value : String) {
value
}

fun render : Html {
"test"
// Some comment
|> test

<div/>
}
}
23 changes: 23 additions & 0 deletions spec/formatters/array_with_comments
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Test {
fun test : Array(String) {
[
// Item 1 Comment
"Item 1",
// Item 2 comment
"Item 2"
// End comment
] of String
}
}
--------------------------------------------------------------------------------
module Test {
fun test : Array(String) {
[
// Item 1 Comment
"Item 1",
// Item 2 comment
"Item 2"
// End comment
] of String
}
}
13 changes: 13 additions & 0 deletions spec/formatters/operation_with_comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
component Main {
fun render : String {
"Hello" // Some comment
+ "There"
}
}
--------------------------------------------------------------------------------
component Main {
fun render : String {
"Hello" // Some comment
+ "There"
}
}
27 changes: 27 additions & 0 deletions spec/formatters/pipe_with_comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
component Main {
fun test (value : String) {
value
}

fun render : Html {
"test"
// Some comment
|> test

<div/>
}
}
--------------------------------------------------------------------------------
component Main {
fun test (value : String) {
value
}

fun render : Html {
"test"
// Some comment
|> test

<div/>
}
}
33 changes: 33 additions & 0 deletions spec/formatters/record_update_with_comments
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
type A {
a : String
}

module Test {
fun test : A {
let x =
{ a : "Blah" }

{ x|
// Comment
a:"Hello"
// End Comment
}
}
}
--------------------------------------------------------------------------------
type A {
a : String
}

module Test {
fun test : A {
let x =
{ a: "Blah" }

{ x |
// Comment
a: "Hello"
// End Comment
}
}
}
27 changes: 27 additions & 0 deletions spec/formatters/record_with_comments
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type A {
a : String
}

module Test {
fun test : A {
{
// Comment
a:"Hello"
// End Comment
}
}
}
--------------------------------------------------------------------------------
type A {
a : String
}

module Test {
fun test : A {
{
// Comment
a: "Hello"
// End Comment
}
}
}
23 changes: 23 additions & 0 deletions spec/formatters/tuple_with_comments
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Test {
fun test : Tuple(String, String) {
{
// Comment for "Blah"
"Blah",
// Comment for "Joe"
"Joe"
// End comment
}
}
}
--------------------------------------------------------------------------------
module Test {
fun test : Tuple(String, String) {
{
// Comment for "Blah"
"Blah",
// Comment for "Joe"
"Joe"
// End comment
}
}
}
6 changes: 5 additions & 1 deletion spec/formatters/type_definition_with_comments
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/*A*/typeTest{/*B*/a:String,/*C*/b:Blah}
/*A*/typeTest{
/*B*/a:String,/*C*/b:Blah// End comment
}
--------------------------------------------------------------------------------
/* A */
type Test {
Expand All @@ -7,4 +9,6 @@ type Test {

/* C */
b : Blah

// End comment
}
7 changes: 4 additions & 3 deletions src/ast/array_literal.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module Mint
class Ast
class ArrayLiteral < Node
getter items, type
getter comment, items, type

def initialize(@from : Parser::Location,
def initialize(@items : Array(CommentedExpression),
@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@items : Array(Node),
@comment : Comment?,
@type : Node?)
end
end
Expand Down
14 changes: 14 additions & 0 deletions src/ast/commented_expression.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Mint
class Ast
class CommentedExpression < Node
getter expression, comment

def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@comment : Comment?,
@expression : Node)
end
end
end
end
3 changes: 2 additions & 1 deletion src/ast/operation.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Mint
class Ast
class Operation < Node
getter operator, right, left
getter operator, comment, right, left

def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@comment : Comment?,
@operator : String,
@right : Node,
@left : Node)
Expand Down
3 changes: 2 additions & 1 deletion src/ast/pipe.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Mint
class Ast
class Pipe < Node
getter expression, argument
getter expression, argument, comment

def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@comment : Comment?,
@expression : Node,
@argument : Node)
end
Expand Down
5 changes: 3 additions & 2 deletions src/ast/record.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module Mint
class Ast
class Record < Node
getter fields
getter fields, comment

def initialize(@from : Parser::Location,
@to : Parser::Location,
@fields : Array(Field),
@file : Parser::File)
@file : Parser::File,
@comment : Comment?)
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions src/ast/record_update.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module Mint
class Ast
class RecordUpdate < Node
getter expression, fields
getter expression, fields, comment

def initialize(@from : Parser::Location,
@expression : Ast::Node,
@to : Parser::Location,
@fields : Array(Field),
@file : Parser::File)
@file : Parser::File,
@comment : Comment?)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions src/ast/tuple_literal.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module Mint
class Ast
class TupleLiteral < Node
getter items
getter comment, items

def initialize(@from : Parser::Location,
def initialize(@items : Array(CommentedExpression),
@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@items : Array(Node))
@comment : Comment?)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion src/ast/type_definition.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Mint
class Ast
class TypeDefinition < Node
getter parameters, comment, fields, name
getter parameters, end_comment, comment, fields, name

def initialize(@fields : Array(TypeDefinitionField) | Array(TypeVariant),
@parameters : Array(TypeVariable),
@from : Parser::Location,
@end_comment : Comment?,
@to : Parser::Location,
@file : Parser::File,
@comment : Comment?,
Expand Down
9 changes: 9 additions & 0 deletions src/compilers/commented_expression.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Mint
class Compiler
def compile(node : Ast::CommentedExpression) : Compiled
compile node do
compile(node.expression)
end
end
end
end
10 changes: 4 additions & 6 deletions src/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ module Mint
#
record List,
items : Array(Tuple(Ast::Node, Nodes)),
separator : String? = nil
separator : String? = nil,
comment : Nodes? = nil

# Describes a group of nodes, with start and end characters and a separator
# character (or characters). The layout of the nodes are determined during
Expand Down Expand Up @@ -83,7 +84,8 @@ module Mint
items : Array(Nodes),
behavior : Behavior,
separator : String,
pad : Bool
pad : Bool,
comment : Nodes = [] of Node

# The possibilities on how to format groups.
enum Behavior
Expand Down Expand Up @@ -133,10 +135,6 @@ module Mint
[Group.new(**params)] of Node
end

def list(**params)
[List.new(**params)] of Node
end

# Actuall formatting things as strings...

def format!(*args, **named) : String
Expand Down
Loading

0 comments on commit 35aff16

Please sign in to comment.