Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aa6f0363 authored by Andrew Solovay's avatar Andrew Solovay Committed by Android (Google) Code Review
Browse files

Merge "Cherry-pick from master: AAPT2: Fix JavaDoc first sentence extraction." into oc-mr1-dev

parents bb0dff99 18dc03a9
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -18,12 +18,29 @@

#include <algorithm>

#include "text/Unicode.h"
#include "text/Utf8Iterator.h"
#include "util/Util.h"

using android::StringPiece;
using ::aapt::text::Utf8Iterator;
using ::android::StringPiece;

namespace aapt {

StringPiece AnnotationProcessor::ExtractFirstSentence(const StringPiece& comment) {
  Utf8Iterator iter(comment);
  while (iter.HasNext()) {
    const char32_t codepoint = iter.Next();
    if (codepoint == U'.') {
      const size_t current_position = iter.Position();
      if (!iter.HasNext() || text::IsWhitespace(iter.Next())) {
        return comment.substr(0, current_position);
      }
    }
  }
  return comment;
}

void AnnotationProcessor::AppendCommentLine(std::string& comment) {
  static const std::string sDeprecated = "@deprecated";
  static const std::string sSystemApi = "@SystemApi";
+2 −0
Original line number Diff line number Diff line
@@ -53,6 +53,8 @@ namespace aapt {
 */
class AnnotationProcessor {
 public:
  static android::StringPiece ExtractFirstSentence(const android::StringPiece& comment);

  /**
   * Adds more comments. Since resources can have various values with different
   * configurations,
+19 −5
Original line number Diff line number Diff line
@@ -18,6 +18,10 @@

#include "test/Test.h"

using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::Not;

namespace aapt {

TEST(AnnotationProcessorTest, EmitsDeprecated) {
@@ -33,7 +37,7 @@ TEST(AnnotationProcessorTest, EmitsDeprecated) {
  processor.WriteToStream(&result, "");
  std::string annotations = result.str();

  EXPECT_NE(std::string::npos, annotations.find("@Deprecated"));
  EXPECT_THAT(annotations, HasSubstr("@Deprecated"));
}

TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationAndRemovesFromComment) {
@@ -44,10 +48,20 @@ TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationAndRemovesFromComment) {
  processor.WriteToStream(&result, "");
  std::string annotations = result.str();

  EXPECT_NE(std::string::npos,
            annotations.find("@android.annotation.SystemApi"));
  EXPECT_EQ(std::string::npos, annotations.find("@SystemApi"));
  EXPECT_NE(std::string::npos, annotations.find("This is a system API"));
  EXPECT_THAT(annotations, HasSubstr("@android.annotation.SystemApi"));
  EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi")));
  EXPECT_THAT(annotations, HasSubstr("This is a system API"));
}

TEST(AnnotationProcessor, ExtractsFirstSentence) {
  EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence("This is the only sentence"),
              Eq("This is the only sentence"));
  EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence(
                  "This is the\n  first sentence.  This is the rest of the paragraph."),
              Eq("This is the\n  first sentence."));
  EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence(
                  "This is the first sentence with a {@link android.R.styleable.Theme}."),
              Eq("This is the first sentence with a {@link android.R.styleable.Theme}."));
}

}  // namespace aapt
+6 −14
Original line number Diff line number Diff line
@@ -299,24 +299,16 @@ void JavaClassGenerator::ProcessStyleable(const ResourceNameRef& name, const Res
      }

      const ResourceName& attr_name = entry.attr_ref->name.value();
      styleable_comment << "<tr><td>";
      styleable_comment << "<code>{@link #" << entry.field_name << " "
                        << (!attr_name.package.empty()
                                ? attr_name.package
      styleable_comment << "<tr><td><code>{@link #" << entry.field_name << " "
                        << (!attr_name.package.empty() ? attr_name.package
                                                       : context_->GetCompilationPackage())
                        << ":" << attr_name.entry << "}</code>";
      styleable_comment << "</td>";

      styleable_comment << "<td>";
                        << ":" << attr_name.entry << "}</code></td>";

      // Only use the comment up until the first '.'. This is to stay compatible with
      // the way old AAPT did it (presumably to keep it short and to avoid including
      // annotations like @hide which would affect this Styleable).
      auto iter = std::find(attr_comment_line.begin(), attr_comment_line.end(), '.');
      if (iter != attr_comment_line.end()) {
        attr_comment_line = attr_comment_line.substr(0, (iter - attr_comment_line.begin()) + 1);
      }
      styleable_comment << attr_comment_line << "</td></tr>\n";
      styleable_comment << "<td>" << AnnotationProcessor::ExtractFirstSentence(attr_comment_line)
                        << "</td></tr>\n";
    }
    styleable_comment << "</table>\n";

+11 −0
Original line number Diff line number Diff line
@@ -66,6 +66,17 @@ bool IsXidContinue(char32_t codepoint) {
  return FindCharacterProperties(codepoint) & CharacterProperties::kXidContinue;
}

// Hardcode the White_Space characters since they are few and the external/icu project doesn't
// list them as data files to parse.
// Sourced from http://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
bool IsWhitespace(char32_t codepoint) {
  return (codepoint >= 0x0009 && codepoint <= 0x000d) || (codepoint == 0x0020) ||
         (codepoint == 0x0085) || (codepoint == 0x00a0) || (codepoint == 0x1680) ||
         (codepoint >= 0x2000 && codepoint <= 0x200a) || (codepoint == 0x2028) ||
         (codepoint == 0x2029) || (codepoint == 0x202f) || (codepoint == 0x205f) ||
         (codepoint == 0x3000);
}

bool IsJavaIdentifier(const StringPiece& str) {
  Utf8Iterator iter(str);

Loading