diff --git a/internal/board/block.go b/internal/board/block.go index 0352344..0ca43f0 100644 --- a/internal/board/block.go +++ b/internal/board/block.go @@ -8,9 +8,10 @@ import ( ) const ( - cellType = "cellType" - block = "block" - arrow = "arrow" + cellType = "cellType" + blockChar = "blockChar" + blockSpace = "blockSpace" + arrow = "arrow" ) type Block struct { @@ -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) } } } @@ -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) diff --git a/internal/board/board.go b/internal/board/board.go index d85b60c..22bb216 100644 --- a/internal/board/board.go +++ b/internal/board/board.go @@ -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) } } diff --git a/internal/board/board_test.go b/internal/board/board_test.go index ffa70f5..539b3e4 100644 --- a/internal/board/board_test.go +++ b/internal/board/board_test.go @@ -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) } @@ -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 { diff --git a/internal/board/connector.go b/internal/board/connector.go index ff6d3c0..42aa6e6 100644 --- a/internal/board/connector.go +++ b/internal/board/connector.go @@ -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 @@ -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) } @@ -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 { @@ -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) } @@ -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 { @@ -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 diff --git a/internal/graph/graph.go b/internal/graph/graph.go index af6a628..1230713 100644 --- a/internal/graph/graph.go +++ b/internal/graph/graph.go @@ -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) diff --git a/internal/js/parser_test.go b/internal/js/parser_test.go index b075c43..c0a9481 100644 --- a/internal/js/parser_test.go +++ b/internal/js/parser_test.go @@ -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 { diff --git a/internal/js/parser_test/custom-1.js b/internal/js/parser_test/custom-1.js new file mode 100644 index 0000000..e04ac98 --- /dev/null +++ b/internal/js/parser_test/custom-1.js @@ -0,0 +1 @@ +import DateSchema from '../@parsers/DateSchema'