Some templating improvements

This commit is contained in:
Max Bucknell 2017-03-15 14:40:53 +04:00
parent 6d0f9c95c4
commit 441b6df7f3
7 changed files with 207 additions and 65 deletions

View file

@ -25,6 +25,13 @@ def _parse_composer_manifest(path):
manifest = json.load(composer_file)
return manifest
def _get_package_name(manifest):
'''Return the name field from composer.json.'''
try:
return manifest['name']
except KeyError:
return 'foo/bar'
def _get_all_autoloaders(manifest):
'''Combine all PSR-0 and PSR-4 autoloaders'''
autoloaders = dict()
@ -61,7 +68,10 @@ def convert_path(dirname):
def get_namespace(dirname):
'''Convert dirname into namespace intelligently.'''
composer_json = find_in_parent('composer.json', dirname)
try:
composer_json = find_in_parent('composer.json', dirname)
except IOError:
return None
composer_manifest = _parse_composer_manifest(composer_json)
repo_root = path.dirname(composer_json)
relative_path = path.relpath(dirname, repo_root)
@ -72,9 +82,59 @@ def get_namespace(dirname):
return namespace + convert_path(remainder)
def get_package_name(dirname):
'''Find package name from current location.'''
try:
composer_json = find_in_parent('composer.json', dirname)
except IOError:
return 'foo/bar'
composer_manifest = _parse_composer_manifest(composer_json)
name = _get_package_name(composer_manifest)
return name
def prepare_arguments(param_tags):
arguments = generate_arguments(param_tags)
def add_argument(snip):
'''Search for the next argument list, and insert one.
These arguments will be of the form
TypeName $variableName
and will come from the body the @param snippet:
* @param Typename $variableName Description
'''
buffer = snip.buffer
param_line_number = snip.snippet_start[0]
param_line = buffer[param_line_number]
try:
argument_line_number = find_argument_line_number(param_line_number, buffer)
except IndexError:
return
argument_line = buffer[argument_line_number]
argument = get_argument_line(param_line)
new_argument_line = insert_argument(argument, argument_line)
buffer[argument_line_number] = new_argument_line
def insert_argument(arg, line):
insert_point = line.find(')')
# No arguments yet, so no comma
if line[insert_point - 1] is '(':
return line[:insert_point] + arg + line[insert_point:]
else:
return line[:insert_point] + ', ' + arg + line[insert_point:]
def find_argument_line_number(start, buffer):
for line_number, line in enumerate(buffer[start:]):
if line.endswith('*/'):
return line_number + 1 + start
else:
pass
raise IndexError('Could not find end of comment and start of method')
def format_method(snip):
'''Convert expanded snippet into method name and args.'''
params = get_params_map(snip)
@ -167,7 +227,7 @@ def is_array_type(type):
def format_argument_line(type, name):
if (type is None):
return ' {},'.format(name)
return name
else:
return ' {} {},'.format(type, name)
return '{} {}'.format(type, name)

View file

@ -1,42 +0,0 @@
global !p
from mbutils import get_namespace, format_method, map_type, prepare_arguments
import os
def full_path(filename):
cwd = os.getcwd()
filepath = os.path.join(cwd, filename)
return os.path.dirname(filepath)
endglobal
snippet class
<?php
/**
* @copyright Copyright 2017 ${1:Redbox Digital}
*/
namespace ${2:`!p snip.rv = get_namespace(full_path(path))`};
${3:// Use...}
class ${4:`!p snip.rv = snip.basename`}${5: extends ${6:ParentClass}}${7: implements ${8:SomeInterface, OtherInterface}}
\{
${0:// Implementation...}
\}
endsnippet
snippet interface
<?php
/**
* @copyright Copyright 2017 ${1:Redbox Digital}
*/
namespace ${2:`!p snip.rv = get_namespace(full_path(path))`};
${3:// Use...}
interface ${4:`!p snip.rv = snip.basename`}${5: extends ${6:ParentInterface}}
\{
${0:// Interface...}
\}
endsnippet

View file

@ -0,0 +1,65 @@
# Python
#
# `mbutils` is a collection of tooling I've built to do some intelligent
# stuff around PHP tooling.
# Found in lib/python/mbutils.py
global !p
import mbutils
import os
def full_path(filename):
cwd = os.getcwd()
filepath = os.path.join(cwd, filename)
return os.path.dirname(filepath)
endglobal
snippet class
<?php
/**
* This file is part of the ${1:`!p snip.rv = mbutils.get_package_name(full_path(path))`} package.
*
* @copyright Copyright `date +%Y` ${2:Redbox Digital}. All rights reserved.
*/
namespace ${12:`!p snip.rv = mbutils.get_namespace(full_path(path))`};${13:
use ${14:ImportedTypes;}}
/**
* ${3:${4:Short description...}
*
* ${5:A slightly longer description of what you want to do.}
*
* ${6:@tags...}}
*/
class ${7:`!p snip.rv = snip.basename`}${8: extends ${9:ParentClass}}${10: implements ${11:$7Interface}}
\{
${0:// Implementation...}
\}
endsnippet
snippet interface
<?php
/**
* This file is part of the ${1:`!p snip.rv = mbutils.get_package_name(full_path(path))`} package.
*
* @copyright Copyright `date +%Y` ${2:Redbox Digital}. All rights reserved.
*/
namespace ${12:`!p snip.rv = mbutils.get_namespace(full_path(path))`};${13:
use ${14:ImportedTypes;}}
/**
* ${3:${4:Short description...}
*
* ${5:A slightly longer description of what you want to do.}
*
* ${6:@tags...}}
*/
interface ${7:`!p snip.rv = snip.basename`}${8: extends ${9:ParentInterface}}
\{
${0:// Methods...}
\}
endsnippet

View file

@ -0,0 +1,22 @@
# Python
#
# `mbutils` is a collection of tooling I've built to do some intelligent
# stuff around PHP tooling.
# Found in lib/python/mbutils.py
global !p
import mbutils
import os
def full_path(filename):
cwd = os.getcwd()
filepath = os.path.join(cwd, filename)
return os.path.dirname(filepath)
endglobal
snippet field
/**
* @var ${1:Type}
*/
private ${2:\$fieldName};
endsnippet

View file

@ -4,7 +4,7 @@
# stuff around PHP tooling.
# Found in lib/python/mbutils.py
global !p
from mbutils import get_namespace, format_method, map_type, prepare_arguments
import mbutils
import os
def full_path(filename):
@ -17,6 +17,7 @@ endglobal
# @param
#
# To make param generation a little easier.
post_jump "if (snip.tabstop is 4): mbutils.add_argument(snip)"
snippet @param
@param ${1:Type}${2:|null} ${3:name} ${4:Description}
endsnippet
@ -24,21 +25,18 @@ endsnippet
# Public method.
#
# Forces me to write a really good docblock.
post_jump "if (snip.tabstop == 0): format_method(snip)"
snippet pubf
/**
* ${2:Short description...}
*
* ${3:A slightly longer description of what you want to do. This will automatically be wrapped}
* ${3:A slightly longer description of what you want to do.}
*
* ${4:@api}
* ${5:@params}
* ${5:@params...}
* @return ${6:ReturnType}
* ${7:@throws ${8:ExceptionType}}
*/
public function ${1:methodName}(
...\$args
): `!p snip.rv = map_type(t[6])` \{
public function ${1:methodName}(): `!p snip.rv = mbutils.map_type(t[6])` \{
$0
\}
endsnippet
@ -49,10 +47,10 @@ snippet prif
/**
* ${2:Short description...}
*
* ${3:A slightly longer description of what you want to do. This will automatically be wrapped}
* ${3:A slightly longer description of what you want to do.}
*
* ${4:@api}
* ${5:@params}
* ${5:@params...}
* @return ${6:ReturnType}
* ${7:@throws ${8:ExceptionType}}
*/
@ -64,21 +62,37 @@ private function ${1:methodName}(
endsnippet
# Protected method. As public.
post_jump "if (snip.tabstop == 0): format_method(snip)"
snippet prof
/**
* ${2:Short description...}
*
* ${3:A slightly longer description of what you want to do. This will automatically be wrapped}
* ${3:A slightly longer description of what you want to do.}
*
* ${4:@api}
* ${5:@params}
* ${5:@params...}
* @return ${6:ReturnType}
* ${7:@throws ${8:ExceptionType}}
*/
protected function ${1:methodName}(
...\$args
): `!p snip.rv = map_type(t[6])` \{
protected function ${1:methodName}(): `!p snip.rv = map_type(t[6])` \{
$0
\}
endsnippet
snippet construct
/**
* ${1:@params}
*/
public function __construct(
...\$args
) \{
/*
* Mark the ballot if you want the relevant code to be generated.
*
* [${2: }] - Make fields?
* [${3: }] - Make getters?
* [${4: }] - Make setters?
*/
$0
\}
endsnippet

View file

@ -1,7 +1,9 @@
snippet mmodxml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"
>
<module name="$1" setup_version="0.1.0">
$0
</module>
@ -10,15 +12,20 @@ endsnippet
snippet mdixml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"
>
$0
</config>
endsnippet
snippet mwidgets
<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
<widgets
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd"
>
$0
</widgets>
endsnippet

View file

@ -62,6 +62,10 @@ hi markdownH3 cterm=NONE ctermbg=NONE ctermfg=7
hi markdownH4 cterm=NONE ctermbg=NONE ctermfg=7
hi markdownH5 cterm=NONE ctermbg=NONE ctermfg=7
hi markdownH6 cterm=NONE ctermbg=NONE ctermfg=7
hi markdownBlockQuote cterm=NONE ctermbg=NONE ctermfg=7
" Residual HTML
hi htmlTitle cterm=NONE ctermbg=NONE ctermfg=7
" Miscellaneous leftovers
hi helpNote cterm=NONE ctermfg=7 ctermbg=NONE
@ -71,10 +75,16 @@ hi MatchParen cterm=NONE ctermfg=7 ctermbg=5
hi String cterm=NONE ctermfg=6 ctermbg=NONE
hi Number cterm=NONE ctermfg=6 ctermbg=NONE
hi Boolean cterm=NONE ctermfg=6 ctermbg=NONE
hi phpHereDoc cterm=NONE ctermfg=6 ctermbg=NONE
hi phpNowDoc cterm=NONE ctermfg=6 ctermbg=NONE
hi phpBackslashSequences cterm=NONE ctermfg=6 ctermbg=NONE
" Comments are green
hi Comment cterm=NONE ctermfg=2 ctermbg=NONE
" Todos are almost green
hi Todo cterm=NONE ctermfg=0 ctermbg=2
" Preprocessor statements aren't comments.
hi PreProc cterm=NONE ctermfg=7 ctermbg=NONE
@ -144,3 +154,9 @@ hi ALEErrorSign cterm=NONE ctermbg=1 ctermfg=1
hi snipLeadingSpaces cterm=NONE ctermbg=0 ctermfg=NONE
hi snipTabStop cterm=NONE ctermbg=NONE ctermfg=6
hi snipTabStopDefault cterm=NONE ctermbg=NONE ctermfg=6
""""""""""""
" Long lines
""""""""""""
hi ColorColumn cterm=NONE ctermbg=1 ctermfg=NONE