Skip to content

Commit 639827b

Browse files
authored
Merge pull request #3 from permafrost-dev/add-get-line-numbers-method
Add helper methods
2 parents 52ce7bc + 9cb3605 commit 639827b

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
All notable changes to `code-snippets` will be documented in this file.
44

5-
## 1.1.0 - 2021-07-25
5+
## 1.2.0 - 2021-07-27
6+
7+
- add `getLineNumbers()` method
8+
- add `getSelectedBounds()` method
9+
- add `Bounds::size()` method
10+
11+
## 1.1.0 - 2021-07-27
612

713
- add `toString()` and `__toString()` methods
814

src/Bounds.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ public function copy(self $bounds): self
4747

4848
return $this;
4949
}
50+
51+
public function size(): int
52+
{
53+
return count(range($this->start, $this->end));
54+
}
5055
}

src/CodeSnippet.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ public function getLineNumberEnd(): int
142142
return $this->surroundingLines[count($this->surroundingLines) - 1] ?? 0;
143143
}
144144

145+
public function getLineNumbers(): array
146+
{
147+
return array_keys($this->getLines());
148+
}
149+
150+
public function getSelectedBounds(): Bounds
151+
{
152+
return Bounds::create($this->getLineNumberStart(), $this->getLineNumberEnd());
153+
}
154+
145155
public function toString()
146156
{
147157
$result = '';

tests/CodeSnippetTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,27 @@ public function it_returns_a_string_when_the_snippet_is_cast_to_a_string()
171171

172172
$this->assertMatchesSnapshot((string)$snippet);
173173
}
174+
175+
/** @test */
176+
public function it_returns_the_line_numbers()
177+
{
178+
$snippet = (new CodeSnippet())
179+
->surroundingLines(10, 12)
180+
->snippetLineCount(8)
181+
->fromFile($this->testsPath('data/file3.php'));
182+
183+
$this->assertCount(8, $snippet->getLineNumbers());
184+
$this->assertMatchesSnapshot($snippet->getLineNumbers());
185+
}
186+
187+
/** @test */
188+
public function it_returns_the_selected_bounds()
189+
{
190+
$snippet = (new CodeSnippet())
191+
->surroundingLines(10, 12)
192+
->snippetLineCount(8)
193+
->fromFile($this->testsPath('data/file3.php'));
194+
195+
$this->assertEquals([10, 11, 12], range($snippet->getSelectedBounds()->start, $snippet->getSelectedBounds()->end));
196+
}
174197
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- 7
2+
- 8
3+
- 9
4+
- 10
5+
- 11
6+
- 12
7+
- 13
8+
- 14

0 commit comments

Comments
 (0)