Last active
October 13, 2015 19:30
-
-
Save tsteur/4244185 to your computer and use it in GitHub Desktop.
Converts Titanium Mobile documentation from API.JSON to sublime-completions (Sublime Text 2 Auto Completion)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// usage: php -d memory_limit=256M titanium_mobile_apidoc_2_sublimetext.php > Tita.sublime-completions | |
function shorten_titanium($key) | |
{ | |
return str_replace('Titanium', 'Ti', $key); | |
} | |
function get_completions($content) | |
{ | |
$processed = array(); | |
$completions = array('Titanium'); | |
foreach ($content as $namespace => $value) { | |
$namespace = shorten_titanium($namespace); | |
$completions[] = $namespace; | |
if (!empty($value->properties)) { | |
foreach ($value->properties as $prop) { | |
if (array_key_exists($prop->name, $processed)) { | |
continue; | |
} | |
if (strtoupper($prop->name) === $prop->name) { | |
$completions[] = sprintf('%s.%s', $namespace, $prop->name); | |
} else { | |
$completions[] = $prop->name; | |
} | |
$processed[$prop->name] = true; | |
} | |
} | |
if (!empty($value->methods)) { | |
foreach ($value->methods as $method) { | |
if (array_key_exists($method->name, $processed)) { | |
continue; | |
} | |
$params = array(); | |
if (!empty($method->parameters)) { | |
foreach ($method->parameters as $index => $parameter) { | |
$params[] = sprintf('${%d:%s}', $index, $parameter->name); | |
} | |
} | |
$params = implode(',', $params); | |
$completions[] = array('trigger' => sprintf('%s()', $method->name), | |
'contents' => sprintf('%s(%s)', $method->name, $params)); | |
$processed[$method->name] = true; | |
} | |
} | |
} | |
return $completions; | |
} | |
// just copy/paste current api.json to same directory, you should get the file here: http://docs.appcelerator.com/titanium/data/index.html | |
$content = file_get_contents('api.json'); | |
$content = json_decode($content); | |
$completions = get_completions($content); | |
$result = (object) array('scope' => 'source.js', 'completions' => $completions); | |
echo json_encode($result); // , JSON_PRETTY_PRINT (requires PHP5.4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment