neo_doxygen: Add an option to remove the `def` keyword of Python.
[nit.git] / contrib / neo_doxygen / src / doxml / language_specific.nit
index b5afa8e..9566f9e 100644 (file)
@@ -29,6 +29,15 @@ abstract class SourceLanguage
                end
        end
 
+       # Apply the information deduced from `type_text` to `parameter`.
+       #
+       # `type_text` is the content of the `<type>` element.
+       fun apply_parameter_type(parameter: Parameter, type_text: RawType) do
+               if type_text["text"] != null then
+                       parameter.static_type = type_text
+               end
+       end
+
        # Extract the specified keyword at the beginning of the specified text.
        #
        # If the keyword is at the beginning of the specified text, return `true`
@@ -85,6 +94,63 @@ abstract class SourceLanguage
                end
                return found
        end
+
+       # Extract the specified suffix in the specified text.
+       #
+       # If the suffix is at the end of the specified text, return `true`
+       # and remove the suffix. Else, return false.
+       #
+       # Used to extract stuff like `...` that Doxygen puts in the type.
+       #
+       #     class DummySource
+       #       super JavaSource
+       #     #
+       #       fun test(text: LinkedText, s: String): Bool do
+       #               return extract_suffix(text, s)
+       #       end
+       #     end
+       #     #
+       #     var text = new RawType(new ProjectGraph(""))
+       #     var dummy = new DummySource
+       #     var res: Bool
+       #     #
+       #     text.add_part("Object...+++", "")
+       #     res = dummy.test(text, "...")
+       #     assert not res
+       #     res = dummy.test(text, "+++")
+       #     assert res
+       #     assert "Object..." == text["text"].as(JsonArray).first
+       #     res = dummy.test(text, "...")
+       #     assert res
+       #     assert "Object" == text["text"].as(JsonArray).first
+       protected fun extract_suffix(text: LinkedText, suffix: String): Bool do
+               var text_array = text["text"]
+               if text_array == null then return false
+               assert text_array isa JsonArray
+               if text_array.is_empty then return false
+
+               var content = text_array.last.as(String).r_trim
+               var link = text.links.first
+
+               if link == null and content.has_suffix(suffix) then
+                       content = content.substring(0, content.length - suffix.length).r_trim
+                       if "" == content then
+                               text.pop_part
+                       else
+                               text.set_part(0, content, "")
+                       end
+                       return true
+               else
+                       return false
+               end
+       end
+end
+
+# The default importation logics.
+#
+# Do nothing special.
+class DefaultSource
+       super SourceLanguage
 end
 
 # Importation logics for Java.
@@ -95,10 +161,32 @@ class JavaSource
                # For abstract members, Doxygen put `abstract` at the beginning of the type.
                # We assume that Doxygen do not put annotations in the type (it seems to
                # be the case).
-               member.is_abstract = extract_keyword(type_text, "abstract")
+               if extract_keyword(type_text, "abstract") then
+                       member.is_abstract = true
+               end
                # TODO final
                # TODO void
                # TODO Avoid using `RawType` when possible. Only use `RawType` as a fallback.
                super
        end
+
+       redef fun apply_parameter_type(parmeter, type_text) do
+               # We assume that Doxygen do not put annotations in the type (it seems to
+               # be the case).
+               # TODO final
+               # TODO Avoid using `RawType` when possible. Only use `RawType` as a fallback.
+               parmeter.is_vararg = extract_suffix(type_text, "...")
+               super
+       end
+end
+
+# Importation logics for Python.
+class PythonSource
+       super SourceLanguage
+
+       redef fun apply_member_type(member, type_text) do
+               # Doxygen may forgot to remove the `def` keyword on methods.
+               extract_keyword(type_text, "def")
+               super
+       end
 end