Page MenuHomePhabricator

D9678.id27549.diff
No OneTemporary

D9678.id27549.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -150,6 +150,8 @@
'PhutilDocblockParserTestCase' => 'parser/__tests__/PhutilDocblockParserTestCase.php',
'PhutilEditDistanceMatrix' => 'utils/PhutilEditDistanceMatrix.php',
'PhutilEditDistanceMatrixTestCase' => 'utils/__tests__/PhutilEditDistanceMatrixTestCase.php',
+ 'PhutilEditorConfig' => 'parser/PhutilEditorConfig.php',
+ 'PhutilEditorConfigTestCase' => 'parser/__tests__/PhutilEditorConfigTestCase.php',
'PhutilEmailAddress' => 'parser/PhutilEmailAddress.php',
'PhutilEmailAddressTestCase' => 'parser/__tests__/PhutilEmailAddressTestCase.php',
'PhutilEmptyAuthAdapter' => 'auth/PhutilEmptyAuthAdapter.php',
@@ -587,6 +589,7 @@
'PhutilDisqusAuthAdapter' => 'PhutilOAuthAuthAdapter',
'PhutilDocblockParserTestCase' => 'PhutilTestCase',
'PhutilEditDistanceMatrixTestCase' => 'PhutilTestCase',
+ 'PhutilEditorConfigTestCase' => 'PhutilTestCase',
'PhutilEmailAddressTestCase' => 'PhutilTestCase',
'PhutilEmptyAuthAdapter' => 'PhutilAuthAdapter',
'PhutilErrorHandlerTestCase' => 'PhutilTestCase',
diff --git a/src/parser/PhutilEditorConfig.php b/src/parser/PhutilEditorConfig.php
new file mode 100644
--- /dev/null
+++ b/src/parser/PhutilEditorConfig.php
@@ -0,0 +1,155 @@
+<?php
+
+/**
+ * Parser for [[http://editorconfig.org/ | EditorConfig]] files.
+ */
+final class PhutilEditorConfig {
+
+ private $root;
+
+ private static $knownProperties = array(
+ 'charset' => array('latin1', 'utf-8', 'utf-8-bom', 'utf-16be', 'utf-16le'),
+ 'end_of_line' => array('lf', 'cr', 'lfcr'),
+ 'indent_size' => 'int|string',
+ 'indent_style' => array('space', 'tab'),
+ 'insert_final_newline' => 'bool',
+ 'tab_width' => 'int',
+ 'trim_trailing_whitespace' => 'bool',
+ );
+
+ /**
+ * Constructor.
+ *
+ * @param string The root directory.
+ */
+ public function __construct($root) {
+ $this->root = $root;
+ }
+
+ /**
+ * Get the specified EditorConfig option for the specified path.
+ *
+ * @param string
+ * @param string
+ * @return wild
+ */
+ public function getConfig($path, $key) {
+ if (idx(self::$knownProperties, $key) === null) {
+ throw new InvalidArgumentException(pht('Invalid EditorConfig option.'));
+ }
+
+ $configs = $this->getConfigs($path);
+
+ switch ($key) {
+ case 'indent_size':
+ if (idx($configs, 'indent_size') === null &&
+ idx($configs, 'indent_style') === 'tab') {
+ return 'tab';
+ } else if (idx($configs, 'indent_size') == 'tab' &&
+ idx($configs, 'tab_size') !== null) {
+ return idx($configs, 'tab_size');
+ }
+ break;
+
+ case 'tab_width':
+ if (idx($configs, 'indent_size') !== null &&
+ idx($configs, 'tab_width') === null &&
+ idx($configs, 'indent_size') !== 'tab') {
+ return idx($configs, 'indent_size');
+ }
+ break;
+ }
+
+ return idx($configs, $key);
+ }
+
+ /**
+ * Get the EditorConfig options for the specified path.
+ *
+ * @param string
+ * @return map<string, wild>
+ */
+ public function getConfigs($path) {
+ $configs = $this->getEditorConfigs($path);
+ $matches = array();
+
+ foreach ($configs as $config) {
+ $path_prefix = $config['path'];
+
+ foreach ($config as $glob => $options) {
+ if (!$glob) {
+ continue;
+ }
+
+ switch (strpos($glob, '/')) {
+ case false:
+ $glob = '**/'.$glob;
+ break;
+
+ case 0:
+ $glob = substr($glob, 1);
+ break;
+ }
+
+ $glob = $path_prefix.'/'.$glob;
+ $regex = phutil_glob_to_regex($glob);
+
+ if (!preg_match($regex, $path)) {
+ continue;
+ }
+
+ foreach ($options as $option => $value) {
+ $option = strtolower($option);
+
+ if (idx(self::$knownProperties, $option) === null) {
+ continue;
+ }
+
+ // TODO: Maybe we should validate the data type of `$value` here.
+
+ if (is_string($value)) {
+ $value = strtolower($value);
+ }
+ $matches[$option] = $value;
+ }
+ }
+ }
+
+ return $matches;
+ }
+
+ /**
+ * Returns the EditorConfig files which affect the specified path.
+ *
+ * Find and parse all `.editorconfig` files between the specified path and
+ * the root directory. The results are returned in the same order that they
+ * should be matched.
+ *
+ * return list<map<regex, dict>>
+ */
+ private function getEditorConfigs($path) {
+ $configs = array();
+ $root = $this->root;
+
+ do {
+ $path = dirname($path);
+ $file = $path.'/.editorconfig';
+
+ if (!Filesystem::pathExists($file)) {
+ continue;
+ }
+
+ $config = phutil_ini_decode(Filesystem::readFile($file));
+ $config['path'] = $path;
+
+ array_unshift($configs, $config);
+
+ if (idx($config, 'root')) {
+ break;
+ }
+ } while ($path != $root && Filesystem::isDescendant($path, $root));
+
+ return $configs;
+ }
+
+}
diff --git a/src/parser/__tests__/PhutilEditorConfigTestCase.php b/src/parser/__tests__/PhutilEditorConfigTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/parser/__tests__/PhutilEditorConfigTestCase.php
@@ -0,0 +1,47 @@
+<?php
+
+final class PhutilEditorConfigTestCase extends PhutilTestCase {
+
+ public function testGetConfigs() {
+ $parser = new PhutilEditorConfig(dirname(phutil_get_library_root()));
+
+ $tests = array(
+ 'file' => array(
+ 'indent_style' => 'tab',
+ 'indent_size' => 4,
+ 'charset' => 'utf-8',
+ 'trim_trailing_whitespace' => true,
+ 'insert_final_newline' => true,
+ ),
+ 'other-file' => array(
+ 'indent_style' => 'tab',
+ 'indent_size' => 4,
+ 'charset' => 'utf-8',
+ ),
+ 'file.txt' => array(
+ 'indent_style' => 'tab',
+ 'indent_size' => 4,
+ 'charset' => 'latin1',
+ 'trim_trailing_whitespace' => false,
+ 'insert_final_newline' => false,
+ ),
+ );
+
+ foreach ($tests as $path => $config) {
+ foreach ($config as $key => $value) {
+ $this->assertEqual(
+ $value,
+ $parser->getConfig($this->getTestFile($path), $key));
+ }
+
+ $this->assertEqual(
+ $config,
+ $parser->getConfigs($this->getTestFile($path)));
+ }
+ }
+
+ private function getTestFile($path) {
+ return dirname(__FILE__).'/editorconfig/'.$path;
+ }
+
+}
diff --git a/src/parser/__tests__/editorconfig/.editorconfig b/src/parser/__tests__/editorconfig/.editorconfig
new file mode 100644
--- /dev/null
+++ b/src/parser/__tests__/editorconfig/.editorconfig
@@ -0,0 +1,15 @@
+root = true
+
+[*]
+indent_style = tab
+indent_size = 4
+charset = utf-8
+
+[*.txt]
+charset = latin1
+trim_trailing_whitespace = false
+insert_final_newline = false
+
+[file]
+trim_trailing_whitespace = true
+insert_final_newline = true

File Metadata

Mime Type
text/plain
Expires
Sun, Mar 16, 12:38 AM (13 h, 18 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7704723
Default Alt Text
D9678.id27549.diff (7 KB)

Event Timeline