Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.docgen.skylark; |
| 15 | |
fwe | 3d2c75c | 2017-07-20 15:19:18 +0200 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Damien Martin-Guillerez | 2ca9b72 | 2016-06-09 17:43:55 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.skylarkinterface.Param; |
cparsons | 0fcad77 | 2018-04-11 14:24:00 -0700 | [diff] [blame^] | 19 | import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; |
| 21 | import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature; |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.syntax.EvalUtils; |
| 23 | import com.google.devtools.build.lib.syntax.FuncallExpression; |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.syntax.Runtime.NoneType; |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.syntax.SkylarkList; |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; |
cparsons | 0fcad77 | 2018-04-11 14:24:00 -0700 | [diff] [blame^] | 28 | import com.google.devtools.build.lib.syntax.StringModule; |
| 29 | import java.lang.reflect.Method; |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 30 | import java.util.Arrays; |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 31 | import java.util.Map; |
| 32 | |
| 33 | /** |
| 34 | * Abstract class for containing documentation for a Skylark syntactic entity. |
| 35 | */ |
| 36 | abstract class SkylarkDoc { |
| 37 | protected static final String TOP_LEVEL_ID = "globals"; |
| 38 | |
| 39 | /** |
| 40 | * Returns a string containing the name of the entity being documented. |
| 41 | */ |
| 42 | public abstract String getName(); |
| 43 | |
| 44 | /** |
John Cater | fd71be7 | 2016-07-29 14:59:22 +0000 | [diff] [blame] | 45 | * Returns a string containing the formatted HTML documentation of the entity being documented. |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 46 | */ |
John Cater | 5d8a44c | 2017-01-03 20:07:37 +0000 | [diff] [blame] | 47 | public abstract String getDocumentation(); |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 48 | |
| 49 | protected String getTypeAnchor(Class<?> returnType, Class<?> generic1) { |
| 50 | return getTypeAnchor(returnType) + " of " + getTypeAnchor(generic1) + "s"; |
| 51 | } |
| 52 | |
| 53 | protected String getTypeAnchor(Class<?> type) { |
| 54 | if (type.equals(Boolean.class) || type.equals(boolean.class)) { |
Laurent Le Brun | 4f3b582 | 2017-02-15 16:20:24 +0000 | [diff] [blame] | 55 | return "<a class=\"anchor\" href=\"bool.html\">bool</a>"; |
| 56 | } else if (type.equals(int.class) || type.equals(Integer.class)) { |
| 57 | return "<a class=\"anchor\" href=\"int.html\">int</a>"; |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 58 | } else if (type.equals(String.class)) { |
| 59 | return "<a class=\"anchor\" href=\"string.html\">string</a>"; |
| 60 | } else if (Map.class.isAssignableFrom(type)) { |
| 61 | return "<a class=\"anchor\" href=\"dict.html\">dict</a>"; |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 62 | } else if (type.equals(Tuple.class)) { |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 63 | return "<a class=\"anchor\" href=\"list.html\">tuple</a>"; |
fwe | 3d2c75c | 2017-07-20 15:19:18 +0200 | [diff] [blame] | 64 | } else if (type.equals(MutableList.class) || type.equals(ImmutableList.class)) { |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 65 | return "<a class=\"anchor\" href=\"list.html\">list</a>"; |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 66 | } else if (type.equals(SkylarkList.class)) { |
| 67 | return "<a class=\"anchor\" href=\"list.html\">sequence</a>"; |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 68 | } else if (type.equals(Void.TYPE) || type.equals(NoneType.class)) { |
| 69 | return "<a class=\"anchor\" href=\"" + TOP_LEVEL_ID + ".html#None\">None</a>"; |
fwe | 3d2c75c | 2017-07-20 15:19:18 +0200 | [diff] [blame] | 70 | } else if (type.equals(NestedSet.class)) { |
| 71 | return "<a class=\"anchor\" href=\"depset.html\">depset</a>"; |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 72 | } else if (type.isAnnotationPresent(SkylarkModule.class)) { |
Florian Weikert | a469e8f | 2016-06-20 14:55:41 +0000 | [diff] [blame] | 73 | SkylarkModule module = type.getAnnotation(SkylarkModule.class); |
| 74 | if (module.documented()) { |
| 75 | return String.format("<a class=\"anchor\" href=\"%1$s.html\">%1$s</a>", |
| 76 | module.name()); |
| 77 | } |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 78 | } |
Florian Weikert | a469e8f | 2016-06-20 14:55:41 +0000 | [diff] [blame] | 79 | return EvalUtils.getDataTypeNameFromClass(type); |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
cparsons | 0fcad77 | 2018-04-11 14:24:00 -0700 | [diff] [blame^] | 82 | // Omit self parameter from parameters in class methods. |
| 83 | protected static Param[] withoutSelfParam(SkylarkSignature annotation) { |
Damien Martin-Guillerez | 014388c | 2016-06-14 10:28:31 +0000 | [diff] [blame] | 84 | Param[] params = annotation.parameters(); |
| 85 | if (params.length > 0 |
| 86 | && !params[0].named() |
| 87 | && (params[0].defaultValue() != null && params[0].defaultValue().isEmpty()) |
| 88 | && params[0].positional() |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 89 | && annotation.objectType() != Object.class |
| 90 | && !FuncallExpression.isNamespace(annotation.objectType())) { |
| 91 | // Skip the self parameter, which is the first mandatory positional parameter. |
Damien Martin-Guillerez | 014388c | 2016-06-14 10:28:31 +0000 | [diff] [blame] | 92 | return Arrays.copyOfRange(params, 1, params.length); |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 93 | } else { |
Damien Martin-Guillerez | 014388c | 2016-06-14 10:28:31 +0000 | [diff] [blame] | 94 | return params; |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
cparsons | 0fcad77 | 2018-04-11 14:24:00 -0700 | [diff] [blame^] | 97 | |
| 98 | // Omit self parameter from parameters in class methods. |
| 99 | protected static Param[] withoutSelfParam(SkylarkCallable annotation, Method method) { |
| 100 | Param[] params = annotation.parameters(); |
| 101 | if (method.getDeclaringClass().equals(StringModule.class)) { |
| 102 | // Skip the self parameter, which is the first mandatory positional parameter. |
| 103 | return Arrays.copyOfRange(params, 1, params.length); |
| 104 | } else { |
| 105 | return params; |
| 106 | } |
| 107 | } |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 108 | } |