Skip to content
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

Improve pretty printing #1520

Merged
merged 27 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
771dfb2
prettier block, function block, and pre/post conditions
turbolent Mar 21, 2022
c13b0f6
prettier function expressions and function declarations
turbolent Mar 21, 2022
2b22280
prettier pragma
turbolent Mar 21, 2022
4230c23
prettier variable declaration
turbolent Mar 21, 2022
28a938f
refactor, use common constant
turbolent Mar 21, 2022
052a0b9
prettier imports
turbolent Mar 21, 2022
fc44352
improve naming
turbolent Mar 21, 2022
8f7c911
prettier composites, including fields and enum cases
turbolent Mar 21, 2022
99a8f27
prettier interfaces
turbolent Mar 21, 2022
b06ceb9
prettier members
turbolent Mar 21, 2022
e0a8c4b
prettier transactions
turbolent Mar 21, 2022
1545aeb
remove empty parameter lists from parsed transaction declaration's ex…
turbolent Mar 21, 2022
7b8f5bd
require statements to implement Doc
turbolent Mar 21, 2022
9379632
require declarations to implement Doc
turbolent Mar 21, 2022
81f48c2
update prettier
turbolent Mar 21, 2022
b1b11d3
make logical and and logical or left associative
turbolent Mar 21, 2022
6760979
prettier expressions: parenthesize sub-expressions if needed
turbolent Mar 21, 2022
54cbf67
prettier programs
turbolent Mar 21, 2022
79afaa8
improve layout
turbolent Mar 21, 2022
48467e2
fix lint
turbolent Mar 21, 2022
42a5ab4
use pretty printing for AST element String implementations
turbolent Apr 3, 2022
3992af9
add Prettier helper function
turbolent Apr 3, 2022
cff736e
Merge branch 'master' into bastian/prettier-4
turbolent May 28, 2022
c7bde6c
Merge branch 'master' into bastian/prettier-4
turbolent Jun 17, 2022
9091664
Apply suggestions from code review
turbolent Jun 17, 2022
64fae15
revert version bumps
turbolent Jun 17, 2022
0dbc209
refactor to global
turbolent Jun 17, 2022
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/rivo/uniseg v0.2.1-0.20211004051800-57c86be7915a
github.com/schollz/progressbar/v3 v3.8.3
github.com/stretchr/testify v1.7.1
github.com/turbolent/prettier v0.0.0-20210613180524-3a3f5a5b49ba
github.com/turbolent/prettier v0.0.0-20220320183459-661cc755135d
go.uber.org/goleak v1.1.10
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/text v0.3.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/turbolent/prettier v0.0.0-20210613180524-3a3f5a5b49ba h1:GPg+SVJURgCt6b4IwuRQupixdBM+KzjXPGvawnaQ15E=
github.com/turbolent/prettier v0.0.0-20210613180524-3a3f5a5b49ba/go.mod h1:Nlx5Y115XQvNcIdIy7dZXaNSUpzwBSge4/Ivk93/Yog=
github.com/turbolent/prettier v0.0.0-20220320183459-661cc755135d h1:5JInRQbk5UBX8JfUvKh2oYTLMVwj3p6n+wapDDm7hko=
github.com/turbolent/prettier v0.0.0-20220320183459-661cc755135d/go.mod h1:Nlx5Y115XQvNcIdIy7dZXaNSUpzwBSge4/Ivk93/Yog=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/zeebo/assert v1.1.0 h1:hU1L1vLTHsnO8x8c9KAR5GmM5QscxHg5RNU5z5qbUWY=
Expand Down
22 changes: 14 additions & 8 deletions runtime/ast/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ package ast

import (
"encoding/json"
"strings"

"github.com/turbolent/prettier"

"github.com/onflow/cadence/runtime/common"
)
Expand Down Expand Up @@ -68,13 +69,7 @@ func (a *Argument) EndPosition(memoryGauge common.MemoryGauge) Position {
}

func (a *Argument) String() string {
var builder strings.Builder
if a.Label != "" {
builder.WriteString(a.Label)
builder.WriteString(": ")
}
builder.WriteString(a.Expression.String())
return builder.String()
return Prettier(a)
}

func (a *Argument) MarshalJSON() ([]byte, error) {
Expand All @@ -87,3 +82,14 @@ func (a *Argument) MarshalJSON() ([]byte, error) {
Alias: (*Alias)(a),
})
}

func (a *Argument) Doc() prettier.Doc {
argumentDoc := a.Expression.Doc()
if a.Label == "" {
return argumentDoc
}
return prettier.Concat{
prettier.Text(a.Label + ": "),
argumentDoc,
}
}
84 changes: 84 additions & 0 deletions runtime/ast/argument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/turbolent/prettier"
)

func TestArgument_MarshalJSON(t *testing.T) {
Expand Down Expand Up @@ -108,3 +109,86 @@ func TestArgument_MarshalJSON(t *testing.T) {
)
})
}

func TestArgument_Doc(t *testing.T) {

t.Parallel()

t.Run("without label", func(t *testing.T) {

t.Parallel()

argument := &Argument{
Expression: &BoolExpression{
Value: false,
},
}

require.Equal(
t,
prettier.Text("false"),
argument.Doc(),
)
})

t.Run("with label", func(t *testing.T) {

t.Parallel()

argument := &Argument{
Label: "ok",
Expression: &BoolExpression{
Value: false,
},
}

require.Equal(
t,
prettier.Concat{
prettier.Text("ok: "),
prettier.Text("false"),
},
argument.Doc(),
)
})
}

func TestArgument_String(t *testing.T) {

t.Parallel()

t.Run("without label", func(t *testing.T) {

t.Parallel()

argument := &Argument{
Expression: &BoolExpression{
Value: false,
},
}

require.Equal(
t,
"false",
argument.String(),
)
})

t.Run("with label", func(t *testing.T) {

t.Parallel()

argument := &Argument{
Label: "ok",
Expression: &BoolExpression{
Value: false,
},
}

require.Equal(
t,
"ok: false",
argument.String(),
)
})
}
124 changes: 113 additions & 11 deletions runtime/ast/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,21 @@ func (b *Block) Doc() prettier.Doc {
}

func StatementsDoc(statements []Statement) prettier.Doc {
var statementsDoc prettier.Concat
var doc prettier.Concat

for _, statement := range statements {
// TODO: replace once Statement implements Doc
hasDoc, ok := statement.(interface{ Doc() prettier.Doc })
if !ok {
continue
}

statementsDoc = append(
statementsDoc,
doc = append(
doc,
prettier.HardLine{},
hasDoc.Doc(),
statement.Doc(),
)
}

return statementsDoc
return doc
}

func (b *Block) String() string {
return Prettier(b)
}

func (b *Block) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -174,6 +172,61 @@ func (b *FunctionBlock) EndPosition(common.MemoryGauge) Position {
return b.Block.EndPos
}

var preConditionsKeywordDoc = prettier.Text("pre")
var postConditionsKeywordDoc = prettier.Text("post")

func (b *FunctionBlock) Doc() prettier.Doc {
if b.IsEmpty() {
return blockEmptyDoc
}

var conditionDocs []prettier.Doc

if conditionsDoc := b.PreConditions.Doc(preConditionsKeywordDoc); conditionsDoc != nil {
conditionDocs = append(
conditionDocs,
prettier.HardLine{},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can prettier.HardLine{} be re-used (e.g: a global variable) or is there a possibility that it would get updated in the downstream code/ inside the prettier library?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All documents are immutable, so they won't get updated in downstream code / inside the prettier library.
It's an empty struct, so IDK if there's any improvement other than saving two characters

conditionsDoc,
)
}

if conditionsDoc := b.PostConditions.Doc(postConditionsKeywordDoc); conditionsDoc != nil {
conditionDocs = append(
conditionDocs,
prettier.HardLine{},
conditionsDoc,
)
}

var bodyDoc prettier.Doc

statementsDoc := StatementsDoc(b.Block.Statements)

if len(conditionDocs) > 0 {
bodyConcatDoc := prettier.Concat(conditionDocs)
bodyConcatDoc = append(
bodyConcatDoc,
statementsDoc,
)
bodyDoc = bodyConcatDoc
} else {
bodyDoc = statementsDoc
}

return prettier.Concat{
blockStartDoc,
prettier.Indent{
Doc: bodyDoc,
},
prettier.HardLine{},
blockEndDoc,
}
}

func (b *FunctionBlock) String() string {
return Prettier(b)
}

// Condition

type Condition struct {
Expand All @@ -182,10 +235,59 @@ type Condition struct {
Message Expression
}

func (c Condition) Doc() prettier.Doc {
doc := c.Test.Doc()
if c.Message != nil {
doc = prettier.Concat{
doc,
prettier.Text(":"),
prettier.Indent{
Doc: prettier.Concat{
prettier.HardLine{},
c.Message.Doc(),
},
},
}
}

return prettier.Group{
Doc: doc,
}
}

// Conditions

type Conditions []*Condition

func (c *Conditions) IsEmpty() bool {
return c == nil || len(*c) == 0
}

func (c *Conditions) Doc(keywordDoc prettier.Doc) prettier.Doc {
if c.IsEmpty() {
return nil
}

var doc prettier.Concat

for _, condition := range *c {
doc = append(
doc,
prettier.HardLine{},
condition.Doc(),
)
}

return prettier.Group{
Doc: prettier.Concat{
keywordDoc,
prettier.Space,
blockStartDoc,
prettier.Indent{
Doc: doc,
},
prettier.HardLine{},
blockEndDoc,
},
}
}
Loading