Skip to content

Commit b33b248

Browse files
committed
Replace tabs with spaces
1 parent a1a0766 commit b33b248

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
## 0.5.2 - 2021-08-02
6+
7+
### Fixed
8+
- Replace tabs with spaces in code before it's sent to the API.
9+
510
## 0.5.1 - 2021-08-01
611

712
### Added

src/Block.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,21 @@ public function toRequestParams()
202202
*/
203203
protected function clean($code)
204204
{
205-
return $this->dedent(rtrim($code));
205+
$code = rtrim($code);
206+
$code = $this->replaceTabs($code);
207+
208+
return $this->dedent($code);
209+
}
210+
211+
/**
212+
* @param $code
213+
* @return string
214+
*/
215+
protected function replaceTabs($code)
216+
{
217+
$multiplier = Torchlight::config('spaces_per_tab', 4);
218+
219+
return str_replace("\t", str_repeat(" ", $multiplier), $code);
206220
}
207221

208222
/**

tests/BlockTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Torchlight\Tests;
77

88
use Torchlight\Block;
9+
use Torchlight\Torchlight;
910

1011
class BlockTest extends BaseTest
1112
{
@@ -23,7 +24,7 @@ public function it_dedents_code()
2324

2425
$block->code($code);
2526

26-
$dedented = $code = <<<EOT
27+
$dedented = <<<EOT
2728
echo 1;
2829
if (1) {
2930
return;
@@ -33,6 +34,46 @@ public function it_dedents_code()
3334
$this->assertEquals($block->code, $dedented);
3435
}
3536

37+
/** @test */
38+
public function it_replaces_tabs()
39+
{
40+
$block = Block::make();
41+
42+
$block->code("if (1) {\n\tif (1) {\n\t\treturn;\n\t}\n}");
43+
44+
$cleaned = <<<EOT
45+
if (1) {
46+
if (1) {
47+
return;
48+
}
49+
}
50+
EOT;
51+
52+
$this->assertEquals($block->code, $cleaned);
53+
}
54+
55+
/** @test */
56+
public function can_change_tab_size()
57+
{
58+
Torchlight::getConfigUsing([
59+
'spaces_per_tab' => 2
60+
]);
61+
62+
$block = Block::make();
63+
64+
$block->code("if (1) {\n\tif (1) {\n\t\treturn;\n\t}\n}");
65+
66+
$cleaned = <<<EOT
67+
if (1) {
68+
if (1) {
69+
return;
70+
}
71+
}
72+
EOT;
73+
74+
$this->assertEquals($block->code, $cleaned);
75+
}
76+
3677
/** @test */
3778
public function it_right_trims()
3879
{
@@ -41,6 +82,7 @@ public function it_right_trims()
4182
$this->assertEquals($block->code, 'echo 1;');
4283
}
4384

85+
4486
/** @test */
4587
public function you_can_set_your_own_id()
4688
{

0 commit comments

Comments
 (0)