Skip to content

Commit

Permalink
Merge pull request #188 from equalsraf/tb-bindings
Browse files Browse the repository at this point in the history
Multiple improvements to binding generation
  • Loading branch information
equalsraf authored Oct 1, 2016
2 parents 2831d3f + b1bfc0f commit 4015d8c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ endif()
# Bindings
find_package(PythonInterp)
if (PYTHONINTERP_FOUND)
set(NVIM "nvim" CACHE STRING "Path to nvim executable")
add_custom_target(bindings
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/bindings/generate_bindings.py nvim ${CMAKE_SOURCE_DIR}/src/auto
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/bindings/generate_bindings.py ${NVIM} ${CMAKE_SOURCE_DIR}/src/auto
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Generating bindings"
)

add_custom_target(bindings-preview
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/bindings/generate_bindings.py ${NVIM}
)
endif()


2 changes: 1 addition & 1 deletion bindings/function_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const QList<Function> Function::knownFunctions = QList<Function>()
{% for param in f.parameters %}
<< QString("{{param.neovim_type}}")
{% endfor %}
, {% if f.can_fail%}true{%else%}false{%endif%})
, false)
{% endfor %}
;
35 changes: 27 additions & 8 deletions bindings/generate_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class NeovimTypeVal:
'String': 'QByteArray',
'Object': 'QVariant',
'Array': 'QVariantList',
'Dictionary': 'QVariantMap',
}
# msgpack extension types
EXTTYPES = {
Expand Down Expand Up @@ -109,6 +110,9 @@ class Function:
"""
Representation for a Neovim API Function
"""

# Attributes names that we support, see src/function.c for details
__KNOWN_ATTRIBUTES = set(['name', 'parameters', 'return_type', 'can_fail', 'deprecated_since', 'since', 'method', 'async', 'impl_name', 'noeval', 'receives_channel_id'])
def __init__(self, nvim_fun):
self.valid = False
self.fun = nvim_fun
Expand All @@ -121,30 +125,41 @@ def __init__(self, nvim_fun):
except UnsupportedType as ex:
print('Found unsupported type(%s) when adding function %s(), skipping' % (ex,self.name))
return

u_attrs = self.unknown_attributes()
if u_attrs:
print('Found unknown attributes for function %s: %s' % (self.name, u_attrs))

self.argcount = len(self.parameters)
self.can_fail = self.fun.get('can_fail', False)

# Build the argument string - makes it easier for the templates
self.argstring = ', '.join(['%s %s' % (tv.native_type, tv.name) for tv in self.parameters])
self.valid = True

def is_method(self):
return self.fun.get('method', False)
def is_async(self):
return self.fun.get('async', False)
def deprecated(self):
return self.fun.get('deprecated_since', None)

def unknown_attributes(self):
attrs = set(self.fun.keys()) - Function.__KNOWN_ATTRIBUTES
return attrs

def real_signature(self):
params = ''
for p in self.parameters:
params += '%s %s' % (p.native_type, p.name)
params += ', '
notes = ''
if self.can_fail:
notes += '!fails'
return '%s %s(%s) %s' % (self.return_type.native_type,self.name,params, notes)
def signature(self):
params = ''
for p in self.parameters:
params += '%s %s' % (p.neovim_type, p.name)
params += ', '
notes = ''
if self.can_fail:
notes += '!fails'
return '%s %s(%s) %s' % (self.return_type.neovim_type,self.name,params, notes)


Expand All @@ -160,8 +175,12 @@ def print_api(api):
sig = fundef.signature()
realsig = fundef.real_signature()
print('\t%s'% sig)
deprecated = fundef.deprecated()
if deprecated:
print('\t- Deprecated: %d' % deprecated)
if sig != realsig:
print('\t[aka %s]\n' % realsig)
print('\t- Native: %s\n' % realsig)

print('')
elif key == 'types':
print('Data Types')
Expand All @@ -176,7 +195,7 @@ def print_api(api):
elif key == 'features':
pass
else:
print('Unknown API info attribute: %s', key)
print('Unknown API info attribute: %s' % key)

if __name__ == '__main__':

Expand Down Expand Up @@ -212,5 +231,5 @@ def print_api(api):
generate_file(name, outpath, **env)

else:
print('Neovim api info:')
print('API info for %s:' % nvim)
print_api(api)
2 changes: 0 additions & 2 deletions bindings/neovim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ void Neovim::handleResponseError(quint32 msgid, Function::FunctionId fun, const

switch(fun) {
{% for f in functions %}
{% if f.can_fail %}
case Function::NEOVIM_FN_{{f.name.upper()}}:
emit err_{{f.name}}(errMsg, res);
break;
{% endif %}
{% endfor %}
default:
m_c->setError(NeovimConnector::RuntimeMsgpackError, QString("Received error for function that should not fail: %s").arg(fun));
Expand Down
5 changes: 3 additions & 2 deletions bindings/neovim.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ protected slots:
NeovimConnector *m_c;
public slots:
{% for f in functions %}
{% if f.deprecated() %}
// DEPRECATED
{% endif %}
// {{f.signature()}}
MsgpackRequest* {{f.name}}({{f.argstring}});
{% endfor %}

signals:
{% for f in functions %}
void on_{{f.name}}({{f.return_type.native_type}});
{% if f.can_fail %}
void err_{{f.name}}(const QString&, const QVariant&);
{% endif%}

{% endfor %}
};
Expand Down
14 changes: 9 additions & 5 deletions src/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ bool Function::operator==(const Function& other)
return false;
}

if ( this->can_fail != other.can_fail ) {
return false;
}

if ( this->return_type != other.return_type ) {
return false;
}
Expand Down Expand Up @@ -134,10 +130,18 @@ Function Function::fromVariant(const QVariant& fun)
// Deprecated
} else if ( it.key() == "receives_channel_id" ) {
// Internal
} else if ( it.key() == "impl_name" ) {
// Internal
} else if ( it.key() == "method" ) {
// Internal
} else if ( it.key() == "noeval" ) {
// API only function
} else if ( it.key() == "deferred" || it.key() == "async" ) {
// Internal, "deferred" renamed "async" in neovim/ccdeb91
} else if ( it.key() == "deprecated_since" || it.key() == "since" ) {
// Creation/Deprecation
} else {
qWarning() << "Unsupported function attribute"<< it.key() << it.value();
qDebug() << "Unsupported function attribute"<< it.key() << it.value();
}
}

Expand Down

0 comments on commit 4015d8c

Please sign in to comment.