Skip to content

Commit

Permalink
fix: makeNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat committed Dec 13, 2022
1 parent de998ac commit a34f6dd
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 23 deletions.
13 changes: 8 additions & 5 deletions internal/board/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

const (
cellType = "cellType"
block = "block"
arrow = "arrow"
cellType = "cellType"
blockChar = "blockChar"
blockSpace = "blockSpace"
arrow = "arrow"
)

type Block struct {
Expand All @@ -34,7 +35,9 @@ func (b *Block) Render(matrix *graphics.Matrix) error {
char := rune(b.Label[idIndex])
if char != ' ' {
cell.PlaceChar(char)
cell.Tag(cellType, block)
cell.Tag(cellType, blockChar)
} else {
cell.Tag(cellType, blockSpace)
}
}
}
Expand All @@ -48,7 +51,7 @@ func (b *Board) AddBlock(
y int,
) error {
if _, ok := b.blocks.Get(id); ok {
return fmt.Errorf("block %s is already present", id)
return fmt.Errorf("blockChar %s is already present", id)
}

newW := x + len(label)
Expand Down
2 changes: 1 addition & 1 deletion internal/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (b *Board) Render() (string, error) {
block, _ := b.blocks.Get(k)
err := block.Render(matrix)
if err != nil {
return matrix.Render(), fmt.Errorf("error rendering block %s: %w", block.Label, err)
return matrix.Render(), fmt.Errorf("error rendering blockChar %s: %w", block.Label, err)
}
}

Expand Down
13 changes: 12 additions & 1 deletion internal/board/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func expectTest(t *testing.T, result string) {
a := require.New(t)
_ = os.Mkdir(testPath, os.ModePerm)
fullPath := path.Join(testPath, path.Base(t.Name())+".txt")
print(result)
if fileExists(fullPath) && os.Getenv(RebuildTestsEnv) != "true" {
expected, err := os.ReadFile(fullPath)
a.NoError(err)
a.Equal(string(expected), result)
} else {
print(result)
err := os.WriteFile(fullPath, []byte(result), os.ModePerm)
a.NoError(err)
}
Expand Down Expand Up @@ -151,6 +151,17 @@ func TestBoard(t *testing.T) {
{from: 1, to: []int{0}},
},
},
{
Name: "Reverse dep with exactly same length",
Blocks: []TestBlock{
{name: "aaa", x: 0, y: 0},
{name: "b", x: 2, y: 1},
},
Connections: []TestConnection{
{from: 0, to: []int{1}},
{from: 1, to: []int{0}},
},
},
}

for _, tt := range tests {
Expand Down
31 changes: 16 additions & 15 deletions internal/board/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error { // TODO: factor thi
reverseX := c.to.Position.X < c.from.Position.X
reverseY := c.to.Position.Y < c.from.Position.Y

// 1. If the line is going upwards, start at the end of the block.
// 1. If the line is going upwards, start at the end of the blockChar.
from := c.from.Position
if reverseY {
from.X += len(c.from.Label) - 1
Expand All @@ -34,14 +34,14 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error { // TODO: factor thi
var cur utils.Vector
if reverseY {
cur = tracer.MoveHorizontal(false)
if matrix.Cell(cur) == nil {
if cur.X >= matrix.H() {
matrix.ExpandRight(1)
}
} else {
cur = tracer.MoveVertical(false)
}
cell := matrix.Cell(cur)
if cell.Is(cellType, block) || cell.Is(cellType, arrow) {
if cell.Is(cellType, blockChar) || cell.Is(cellType, arrow) {
return fmt.Errorf("could not draw first vertical step on (%d, %d) because there is no space", cur.X, cur.Y)
}

Expand All @@ -50,9 +50,9 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error { // TODO: factor thi
collides, err := matrix.RayCastVertical(
cur,
map[string]func(string) bool{
// if an arrow or a block is already present, then that is a hit.
// if an arrow or a blockChar is already present, then that is a hit.
cellType: func(value string) bool {
return utils.InArray(value, []string{block, arrow})
return utils.InArray(value, []string{blockChar, arrow})
},
// if there is a line, belonging to another connector which claimed ownership, then hit.
noCrossOwnership: func(value string) bool {
Expand All @@ -66,9 +66,10 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error { // TODO: factor thi
} else if !collides {
break
}

cur = tracer.MoveHorizontal(!reverseX)
cell := matrix.Cell(cur)
if cell == nil && reverseX {
if cell == nil && cur.X >= matrix.W() {
matrix.ExpandRight(1)
cell = matrix.Cell(cur)
}
Expand All @@ -80,17 +81,17 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error { // TODO: factor thi

// 3. displacing vertically until aligned...
for cur.Y != c.to.Position.Y && cur.Y >= 0 && cur.Y < matrix.H() {
cur = tracer.MoveVertical(reverseY)
cur = tracer.MoveVertical(cur.Y > c.to.Position.Y)
matrix.Cell(cur).Tag(noCrossOwnership, c.from.Id)
}

// 4. moving horizontally until meeting target node...
stopBefore := 1
if reverseX {
stopBefore = -len(c.to.Label)
}
for cur.X != c.to.Position.X-stopBefore && cur.X >= 0 && cur.X < matrix.W() {
cur = tracer.MoveHorizontal(reverseX)
for cur.X >= 0 && cur.X < matrix.W() {
next := matrix.Cell(utils.Vec(cur.X+utils.Bool2Int(!reverseX), cur.Y))
if next != nil && (next.Is(cellType, blockChar) || next.Is(cellType, blockSpace)) {
break
}
cur = tracer.MoveHorizontal(cur.X > c.to.Position.X)
}
err := tracer.Dump(matrix)
if err != nil {
Expand All @@ -112,12 +113,12 @@ func (b *Board) AddConnector(from string, to string) error {
if block, ok := b.blocks.Get(from); ok {
fromBlock = block
} else {
return fmt.Errorf("block with Id %s not found", from)
return fmt.Errorf("blockChar with Id %s not found", from)
}
if block, ok := b.blocks.Get(to); ok {
toBlock = block
} else {
return fmt.Errorf("block with Id %s not found", to)
return fmt.Errorf("blockChar with Id %s not found", to)
}

key := from + " -> " + to
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func makeNodes[T any](
} else if cached, ok := seen[root.Id]; ok {
return cached, nil
} else {
seen[entrypoint] = root
seen[root.Id] = root
}

deps := parser.Deps(root)
Expand Down
7 changes: 7 additions & 0 deletions internal/js/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func TestParser_Deps(t *testing.T) {
"parser_test/with-imports-index-imported/index.js",
},
},

{
Name: "custom-1",
Expected: []string{
"@parsers/DateSchema",
},
},
}

for _, tt := range tests {
Expand Down
1 change: 1 addition & 0 deletions internal/js/parser_test/custom-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import DateSchema from '../@parsers/DateSchema'

0 comments on commit a34f6dd

Please sign in to comment.