Commit 71f37ecd authored by DanmeiChen's avatar DanmeiChen

nearly complete sphinx doc for swift

parent b1673bea
......@@ -195,6 +195,7 @@ if(ENABLE_CXX_WRAPPER OR ENABLE_CSHARP_WRAPPER OR ENABLE_JAVA_WRAPPER OR ENABLE_
check_python_module(sphinx)
check_python_module(javasphinx)
check_python_module(sphinx_csharp)
check_python_module(swift_domain)
endif()
endif()
......
......@@ -31,7 +31,7 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx_csharp.csharp', 'javasphinx']
extensions = ['sphinx_csharp.csharp', 'javasphinx', 'swift_domain']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
......
......@@ -171,7 +171,8 @@ class LangInfo:
'C' : 'C',
'Cpp' : 'C++',
'Java' : 'Java',
'CSharp': 'C#'
'CSharp': 'C#',
'Swift' : 'Swift'
}
......@@ -315,7 +316,7 @@ class EnumPage(SphinxPage):
filename = SphinxPage._classname_to_filename(enum.name)
SphinxPage.__init__(self, lang, langs, filename)
namespace = enum.find_first_ancestor_by_type(abstractapi.Namespace)
self.namespace = namespace.name.translate(lang.nameTranslator) if lang.langCode != 'C' else None
self.namespace = namespace.name.translate(lang.nameTranslator) if lang.langCode not in ['C', 'Swift'] else None
self.enum = EnumPart(enum, lang, langs, namespace=namespace)
......@@ -368,15 +369,19 @@ class ClassPage(SphinxPage):
propertyAttr['ref_label'] += (property_.getter.name.to_snake_case(fullName=True) if property_.getter is not None else property_.setter.name.to_snake_case(fullName=True))
translatedProperties.append(propertyAttr)
return translatedProperties
def _translate_methods(self, methods):
translatedMethods = []
for method in methods:
translatedMethods.append(self._translate_method(method))
return translatedMethods
def _translate_method(self, method):
namespace = method.find_first_ancestor_by_type(abstractapi.Class,abstractapi.Interface)
if self.lang.langCode == 'Swift':
namespace = method.find_first_ancestor_by_type(abstractapi.Namespace)
else:
namespace = method.find_first_ancestor_by_type(abstractapi.Class,abstractapi.Interface)
methAttr = {
'prototype' : method.translate_as_prototype(self.lang.langTranslator, namespace=namespace),
'briefDoc' : method.briefDescription.translate(self.docTranslator),
......@@ -385,7 +390,7 @@ class ClassPage(SphinxPage):
}
reference = metadoc.FunctionReference(None)
reference.relatedObject = method
methAttr['link'] = reference.translate(self.lang.docTranslator, namespace=method.find_first_ancestor_by_type(abstractapi.Class, abstractapi.Interface))
methAttr['link'] = reference.translate(self.lang.docTranslator, namespace=namespace)
return methAttr
@property
......@@ -449,9 +454,10 @@ class DocGenerator:
LangInfo('C'),
LangInfo('Cpp'),
LangInfo('Java'),
LangInfo('CSharp')
LangInfo('CSharp'),
LangInfo('Swift')
]
def generate(self, outputdir):
for lang in self.languages:
directory = os.path.join(args.outputdir, lang.directory)
......
......@@ -1042,8 +1042,8 @@ class CLangTranslator(CLikeLangTranslator):
return '1<<{0}'.format(value.position)
else:
raise TypeError('invalid enumerator value type: {0}'.format(value))
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideReturnType=False, stripDeclarators=False, namespace=None):
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideArgTypes=False, hideReturnType=False, stripDeclarators=False, namespace=None):
_class = method.find_first_ancestor_by_type(Class,Interface)
params = []
if not hideArguments:
......@@ -1152,13 +1152,13 @@ class CppLangTranslator(CLikeLangTranslator):
res = _type.containedTypeDesc.translate(self)
else:
res = _type.containedTypeDesc.translate(self, namespace=namespace)
if type(_type.parent) is Argument:
return 'const std::list<{0}> &'.format(res)
else:
return 'std::list<{0}>'.format(res)
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideReturnType=False, stripDeclarators=False, namespace=None):
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideArgTypes=False, hideReturnType=False, stripDeclarators=False, namespace=None):
argsAsString = ', '.join([arg.translate(self, hideArgName=hideArgNames, namespace=namespace) for arg in method.args]) if not hideArguments else ''
return '{return_}{name}({args}){const}'.format(
return_=(method.returnType.translate(self, namespace=namespace) + ' ') if not hideReturnType else '',
......@@ -1269,7 +1269,7 @@ class JavaLangTranslator(CLikeLangTranslator):
res += (' ' + arg.name.translate(self.nameTranslator))
return res
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideReturnType=False, stripDeclarators=False, namespace=None):
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideArgTypes=False, hideReturnType=False, stripDeclarators=False, namespace=None):
return '{public}{returnType}{methodName}({arguments})'.format(
public='public ' if not stripDeclarators else '',
returnType=(method.returnType.translate(self, isReturn=True, namespace=namespace) + ' ') if not hideReturnType else '',
......@@ -1280,7 +1280,7 @@ class JavaLangTranslator(CLikeLangTranslator):
class SwiftLangTranslator(CLikeLangTranslator):
def __init__(self):
self.nameTranslator = metaname.Translator.get('Swift')
self.nilToken = 'null'
self.nilToken = 'nil'
self.falseConstantToken = 'false'
self.trueConstantToken = 'true'
......@@ -1349,7 +1349,7 @@ class SwiftLangTranslator(CLikeLangTranslator):
return _type.desc.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace))
def translate_class_type(self, _type, dllImport=True, namespace=None):
return "IntPtr" if dllImport else _type.desc.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace))
return _type.desc.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace))
def translate_list_type(self, _type, dllImport=True, namespace=None):
if dllImport:
......@@ -1375,13 +1375,30 @@ class SwiftLangTranslator(CLikeLangTranslator):
arg.name.translate(self.nameTranslator)
)
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideReturnType=False, stripDeclarators=False, namespace=None):
return '{static}{override}{returnType}{name}({args})'.format(
static = 'static ' if method.type == Method.Type.Class and not stripDeclarators else '',
override = 'override ' if method.name.translate(self.nameTranslator) == 'ToString' and not stripDeclarators else '',
returnType = (method.returnType.translate(self, dllImport=False, namespace=namespace) + ' ') if not hideReturnType else '',
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideArgTypes=False, hideReturnType=False, stripDeclarators=False, namespace=None):
params = []
if not hideArguments:
for arg in method.args:
argType = arg.type.translate(self, dllImport=False, namespace=None)
argName = arg.name.translate(self.nameTranslator)
if argType.endswith('Listener'):
argType = argType[0:len(argType)-8] + "Delegate"
argName = "delegate"
elif argType == "UnsafePointer<Int>" and not arg.type.isconst:
argType = "UnsafeMutablePointer<Int32>"
elif argType == "UnsafeMutableRawPointer":
argType = "UnsafeMutableRawPointer?"
params.append(argName + ":" + argType if not hideArgTypes else argName + ":")
returnType = method.returnType.translate(self, dllImport=False)
returnValue = ""
if returnType != "void":
returnValue = " -> " + returnType + '?' if type(method.returnType) is ClassType and 'create' in method.name.to_word_list() else " -> " + returnType
return '{name}({params}){returnValue}'.format(
name = method.name.translate(self.nameTranslator, **Translator._namespace_to_name_translator_params(namespace)),
args = ', '.join([arg.translate(self, dllImport=False, namespace=namespace) for arg in method.args]) if not hideArguments else ''
params = ', '.join(params) if not hideArgTypes else ''.join(params),
returnValue= returnValue if not hideArgTypes else ''
)
......@@ -1468,14 +1485,14 @@ class CSharpLangTranslator(CLikeLangTranslator):
raise TranslationError('translation of bctbx_list_t of enums')
else:
raise TranslationError('translation of bctbx_list_t of unknow type !')
def translate_argument(self, arg, dllImport=True, namespace=None):
return '{0} {1}'.format(
arg.type.translate(self, dllImport=dllImport, namespace=None),
arg.name.translate(self.nameTranslator)
)
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideReturnType=False, stripDeclarators=False, namespace=None):
def translate_method_as_prototype(self, method, hideArguments=False, hideArgNames=False, hideArgTypes=False, hideReturnType=False, stripDeclarators=False, namespace=None):
return '{static}{override}{returnType}{name}({args})'.format(
static = 'static ' if method.type == Method.Type.Class and not stripDeclarators else '',
override = 'override ' if method.name.translate(self.nameTranslator) == 'ToString' and not stripDeclarators else '',
......
......@@ -594,6 +594,13 @@ class SphinxTranslator(Translator):
self.enumeratorDeclarator = 'field'
self.namespaceDeclarator = 'package'
self.methodReferencer = 'meth'
elif langCode == 'Swift':
self.domain = 'swift'
self.classDeclarator = 'class'
self.interfaceDeclarator = self.classDeclarator
self.methodDeclarator = 'class_method'
self.enumDeclarator = 'enum'
self.enumeratorDeclarator = 'enum_case'
else:
raise ValueError('invalid language code: {0}'.format(langCode))
......@@ -617,10 +624,15 @@ class SphinxTranslator(Translator):
raise ValueError("'{0}' referencer type not supported".format(typeName))
def translate_class_reference(self, ref, label=None, namespace=None):
refStr = Translator.translate_reference(self, ref, absName=True)
tag = self._sphinx_ref_tag(ref)
if self.domain == 'swift':
refStr = tag.split(':')[1] + " " + refStr
return ':{tag}:`{label} <{ref}>`'.format(
tag=self._sphinx_ref_tag(ref),
tag=tag,
label=label if label is not None else Translator.translate_reference(self, ref, namespace=namespace),
ref=Translator.translate_reference(self, ref, absName=True)
ref= refStr
)
def translate_function_reference(self, ref, label=None, useNamespace=True, namespace=None):
......@@ -628,16 +640,17 @@ class SphinxTranslator(Translator):
refStr = ref.relatedObject.name.translate(self.nameTranslator, **abstractapi.Translator._namespace_to_name_translator_params(namespace))
else:
refStr = ref.relatedObject.translate_as_prototype(self.langTranslator,
hideArguments=self.domain != 'java',
hideArguments=self.domain not in ['java', 'swift'],
hideArgNames=self.domain == 'java',
hideArgTypes=self.domain == 'swift',
hideReturnType=True,
stripDeclarators=True,
namespace=namespace
)
return ':{tag}:`{label} <{ref}>`'.format(
tag=self._sphinx_ref_tag(ref),
label=label if label is not None else '{0}()'.format(Translator.translate_reference(self, ref, namespace=namespace)),
ref=refStr
tag = self._sphinx_ref_tag(ref),
label = label if label is not None else '{0}()'.format(Translator.translate_reference(self, ref, namespace=namespace)),
ref = "static " + refStr if self.domain == 'swift' and ref.relatedObject.type == abstractapi.Method.Type.Class else refStr
)
def translate_keyword(self, keyword):
......
......@@ -331,9 +331,22 @@ class JavaTranslator(Translator):
class SwiftTranslator(JavaTranslator):
def __init__(self):
JavaTranslator.__init__(self)
self.nsSep = '::'
self.nsSep = '.'
self.keyWordEscapes = {'protocol' : 'proto'}
def translate_enum_name(self, name, recursive=False, topAncestor=None):
namespace = name
while namespace is not None and type(namespace) is not NamespaceName:
namespace = namespace.prev
nameCopy = name.copy()
if namespace is not None:
nameCopy.delete_prefix(namespace)
return nameCopy.to_camel_case(fullName=True)
def translate_interface_name(self, name, **params):
name = self.translate_class_name(name, **params)
return name[0:len(name)-8] + "Delegate"
def translate_class_name(self, name, recursive=False, topAncestor=None):
return name.to_camel_case()
......
......@@ -115,8 +115,7 @@ class SwiftTranslator(object):
for arg in method.args:
argType = arg.type.translate(self.langTranslator, dllImport=False)
argName = arg.name.translate(self.nameTranslator)
if argType.endswith('Listener'):
argType = argType[0:len(argType)-8] + "Delegate"
if argType.endswith('Delegate'):
argName = "delegate"
if arg is not method.args[0]:
methodDict['impl']['args'] += ', '
......@@ -348,8 +347,6 @@ class SwiftTranslator(object):
interfaceDict = {}
interfaceDict['interfaceName'] = interface.name.translate(self.nameTranslator)
if interfaceDict['interfaceName'].endswith('Listener'):
interfaceDict['interfaceName'] = interfaceDict['interfaceName'][0:len(interfaceDict['interfaceName'])-8] + "Delegate"
interfaceDict['create_user_data_name'] = 'linphone_factory_create' + interface.listenedClass.name.to_snake_case(fullName=True).lstrip('linphone') + '_cbs'
interfaceDict['methods'] = []
......@@ -368,8 +365,7 @@ class SwiftTranslator(object):
methodDict['has_getter'] = True
methodDict['has_setter'] = False
methodDict['return'] = prop.returnType.translate(self.langTranslator, dllImport=False)
if methodDict['return'].endswith('Listener'):
methodDict['return'] = methodDict['return'][0:len(methodDict['return'])-8] + "Delegate"
if methodDict['return'].endswith('Delegate'):
methodDict['is_callbacks'] = True
methodDict['exception'] = self.throws_exception(prop.returnType)
methodDict['getter_c_name'] = prop.name.to_c()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment