Changeset View
Changeset View
Standalone View
Standalone View
src/parser/PhutilEditorConfig.php
<?php | <?php | ||||
/** | /** | ||||
* Parser for [[http://editorconfig.org/ | EditorConfig]] files. | * Parser for [[http://editorconfig.org/ | EditorConfig]] files. | ||||
*/ | */ | ||||
final class PhutilEditorConfig { | final class PhutilEditorConfig { | ||||
const CHARSET = 'charset'; | |||||
const END_OF_LINE = 'end_of_line'; | |||||
const INDENT_SIZE = 'indent_size'; | |||||
const INDENT_STYLE = 'indent_style'; | |||||
const FINAL_NEWLINE = 'insert_final_newline'; | |||||
const LINE_LENGTH = 'max_line_length'; | |||||
const TAB_WIDTH = 'tab_width'; | |||||
const TRAILING_WHITESPACE = 'trim_trailing_whitespace'; | |||||
/** | /** | ||||
* Valid properties. | * Valid properties. | ||||
* | * | ||||
* See http://editorconfig.org/#file-format-details. | * See http://editorconfig.org/#file-format-details. | ||||
*/ | */ | ||||
private static $knownProperties = array( | private static $knownProperties = array( | ||||
'charset' => array('latin1', 'utf-8', 'utf-8-bom', 'utf-16be', 'utf-16le'), | self::CHARSET => array( | ||||
'end_of_line' => array('lf', 'cr', 'crlf'), | 'latin1', | ||||
'indent_size' => 'int|string', | 'utf-8', | ||||
'indent_style' => array('space', 'tab'), | 'utf-8-bom', | ||||
'insert_final_newline' => 'bool', | 'utf-16be', | ||||
'max_line_length' => 'int', | 'utf-16le', | ||||
'tab_width' => 'int', | ), | ||||
'trim_trailing_whitespace' => 'bool', | self::END_OF_LINE => array('lf', 'cr', 'crlf'), | ||||
self::INDENT_SIZE => 'int|string', | |||||
self::INDENT_STYLE => array('space', 'tab'), | |||||
self::FINAL_NEWLINE => 'bool', | |||||
self::LINE_LENGTH => 'int', | |||||
self::TAB_WIDTH => 'int', | |||||
self::TRAILING_WHITESPACE => 'bool', | |||||
); | ); | ||||
private $root; | private $root; | ||||
/** | /** | ||||
* Constructor. | * Constructor. | ||||
* | * | ||||
* @param string The root directory. | * @param string The root directory. | ||||
Show All 12 Lines | final class PhutilEditorConfig { | ||||
public function getProperty($path, $key) { | public function getProperty($path, $key) { | ||||
if (!idx(self::$knownProperties, $key)) { | if (!idx(self::$knownProperties, $key)) { | ||||
throw new InvalidArgumentException(pht('Invalid EditorConfig property.')); | throw new InvalidArgumentException(pht('Invalid EditorConfig property.')); | ||||
} | } | ||||
$props = $this->getProperties($path); | $props = $this->getProperties($path); | ||||
switch ($key) { | switch ($key) { | ||||
case 'indent_size': | case self::INDENT_SIZE: | ||||
if (idx($props, 'indent_size') === null && | if (idx($props, self::INDENT_SIZE) === null && | ||||
idx($props, 'indent_style') === 'tab') { | idx($props, self::INDENT_STYLE) === 'tab') { | ||||
return 'tab'; | return 'tab'; | ||||
} else if (idx($props, 'indent_size') === 'tab' && | } else if (idx($props, self::INDENT_SIZE) === 'tab' && | ||||
idx($props, 'tab_width') === null) { | idx($props, self::TAB_WIDTH) === null) { | ||||
return idx($props, 'tab_width'); | return idx($props, self::TAB_WIDTH); | ||||
} | } | ||||
break; | break; | ||||
case 'tab_width': | case self::TAB_WIDTH: | ||||
if (idx($props, 'tab_width') === null && | if (idx($props, self::TAB_WIDTH) === null && | ||||
idx($props, 'indent_size') !== null && | idx($props, self::INDENT_SIZE) !== null && | ||||
idx($props, 'indent_size') !== 'tab') { | idx($props, self::INDENT_SIZE) !== 'tab') { | ||||
return idx($props, 'indent_size'); | return idx($props, self::INDENT_SIZE); | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
return idx($props, $key); | return idx($props, $key); | ||||
} | } | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 108 Lines • Show Last 20 Lines |