Skip to content

Commit

Permalink
Made lexer spacing really dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenKoehn committed Jan 16, 2016
1 parent b03ed56 commit 574ee9d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
48 changes: 25 additions & 23 deletions Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,33 +864,35 @@ protected function scanIndent()
$tabs = $this->strpos($indent, "\t") !== false;
$mixed = $spaces && $tabs;

//Don't allow mixed indentation, this will just confuse the lexer
if ($mixed || ($this->_indentStyle === self::INDENT_SPACE && $tabs)) {

//Well, let's try a conversion if were using spaces and have an indentWidth already
if ($this->_indentStyle === self::INDENT_SPACE && $this->_indentWidth !== null) {

//We replace all tabs (\t) by indentWidth * spaces
$spaces = str_replace("\t", str_repeat(self::INDENT_SPACE, $this->_indentWidth), $spaces);
$tabs = false;
$mixed = false;
} else {

$this->throwException(
"Mixed indentation style encountered. "
."Dont mix tabs and spaces. Stick to one of both."
);
if ($mixed) {

switch ($this->_indentStyle) {
case self::INDENT_SPACE:
default:

//Convert tabs to spaces based on indentWidth
$spaces = str_replace(self::INDENT_TAB, str_repeat(
self::INDENT_SPACE,
$this->_indentWidth ? $this->_indentWidth : 4
), $spaces);
$tabs = false;
$mixed = false;
break;
case self::INDENT_TAB:

//Convert spaces to tabs
$spaces = str_replace(self::INDENT_SPACE, str_repeat(
self::INDENT_TAB,
$this->_indentWidth ? $this->_indentWidth : 1
), $spaces);
$spaces = false;
$mixed = false;
break;
}
}

//Validate the indentation style
$indentStyle = $tabs ? self::INDENT_TAB : self::INDENT_SPACE;
if ($this->_indentStyle && $this->_indentStyle !== $indentStyle)
$this->throwException(
"Mixed indentation style encountered. "
."You used another indentation style in this line than in "
."previous lines. Dont do that."
);
$this->_indentStyle = $tabs ? self::INDENT_TAB : self::INDENT_SPACE;

//Validate the indentation width
if (!$this->_indentWidth)
Expand Down
16 changes: 16 additions & 0 deletions Test/IssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ public function testIssue44()
doctype html
JADE;


$renderer = new Renderer([
'adapterOptions' => [
'path' => __DIR__.'/cache/issues'
],
'pretty' => false,
'paths' => [__DIR__.'/views/issues']
]);

$this->assertEquals('<?php /** * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) */?><!DOCTYPE html>', $renderer->compile($jade));
$this->assertEquals('<?php /*** CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) */?><!DOCTYPE html><!DOCTYPE html> <html> <head><?=$view->Html->charset()?> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">', $renderer->compileFile('issue-44'));

$this->assertEquals('<ul class="right hide-on-med-and-down"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><ul id="nav-mobile" class="side-nav"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><a class="button-collapse" href="#" data-activates="nav-mobile"><i class="material-icons">menu</i></a><div class="progress -main"><div class="indeterminate"></div></div>', $this->_renderer->compileFile('issue-44/for_members.ctp.1'));
$this->assertEquals('<ul class="right hide-on-med-and-down"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><ul id="nav-mobile" class="side-nav"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><a class="button-collapse" href="#" data-activates="nav-mobile"><i class="material-icons">menu</i></a><div class="progress -main"><div class="indeterminate"></div></div>', $this->_renderer->compileFile('issue-44/for_members.ctp.2'));


$renderer = new Renderer([
'adapterOptions' => [
'path' => __DIR__.'/cache/issues'
Expand Down

0 comments on commit 574ee9d

Please sign in to comment.