-
Notifications
You must be signed in to change notification settings - Fork 593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Centralize comment parsing #3030
Changes from 2 commits
4bb8fb1
5d1740c
06a9687
eb2d8ca
88a1aee
0947315
02c384b
3609630
8a9ec95
f5a69e3
d97b2b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -639,14 +639,15 @@ namespace | |||||
} | ||||||
} | ||||||
|
||||||
StringList splitComment(const string& c, bool stripMarkup) | ||||||
StringList splitComment(string_view c, function<string(string)> linkFormatter, bool stripMarkup) | ||||||
{ | ||||||
string comment = c; | ||||||
string comment = string{c}; | ||||||
string::size_type pos; | ||||||
|
||||||
if (stripMarkup) | ||||||
{ | ||||||
// Strip HTML markup and javadoc links. | ||||||
string::size_type pos = 0; | ||||||
// Strip HTML markup. | ||||||
pos = 0; | ||||||
do | ||||||
{ | ||||||
pos = comment.find('<', pos); | ||||||
|
@@ -660,52 +661,38 @@ namespace | |||||
comment.erase(pos, endpos - pos + 1); | ||||||
} | ||||||
} while (pos != string::npos); | ||||||
} | ||||||
|
||||||
const string link = "{@link"; | ||||||
pos = 0; | ||||||
do | ||||||
// Fix any javadoc using the provided link formatter. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this comment be about Slice links, not javadoc links. I guess we have this comment because Slice doc comments are based on javadoc, still this is about parsing the Slice doc comment link. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I edited the comment to talk about Slice instead of javadoc. |
||||||
const string link = "{@link "; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should always be a space after the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this update break the link if you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
pos = 0; | ||||||
do | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, totally right. |
||||||
{ | ||||||
pos = comment.find(link, pos); | ||||||
if (pos != string::npos) | ||||||
{ | ||||||
pos = comment.find(link, pos); | ||||||
if (pos != string::npos) | ||||||
string::size_type endpos = comment.find('}', pos); | ||||||
if (endpos != string::npos) | ||||||
{ | ||||||
comment.erase(pos, link.size() + 1); // Erase trailing white space too. | ||||||
string::size_type endpos = comment.find('}', pos); | ||||||
if (endpos != string::npos) | ||||||
{ | ||||||
string ident = comment.substr(pos, endpos - pos); | ||||||
comment.erase(pos, endpos - pos + 1); | ||||||
|
||||||
// | ||||||
// Replace links of the form {@link Type#member} with "Type.member". | ||||||
// | ||||||
string::size_type hash = ident.find('#'); | ||||||
string rest; | ||||||
if (hash != string::npos) | ||||||
{ | ||||||
rest = ident.substr(hash + 1); | ||||||
ident = ident.substr(0, hash); | ||||||
if (!ident.empty()) | ||||||
{ | ||||||
if (!rest.empty()) | ||||||
{ | ||||||
ident += "." + rest; | ||||||
} | ||||||
} | ||||||
else if (!rest.empty()) | ||||||
{ | ||||||
ident = rest; | ||||||
} | ||||||
} | ||||||
|
||||||
comment.insert(pos, ident); | ||||||
} | ||||||
// Extract the linked to identifier. | ||||||
string::size_type identStart = comment.find_first_not_of(" \t", pos + link.size()); | ||||||
string::size_type identEnd = comment.find_last_not_of(" \t", endpos) + 1; | ||||||
string ident = comment.substr(identStart, identEnd - identStart); | ||||||
|
||||||
// Then erase the entire '{@link foo}' tag from the comment. | ||||||
comment.erase(pos, endpos - pos + 1); | ||||||
|
||||||
// In it's place, insert the correctly formatted link. | ||||||
string formattedLink = linkFormatter(ident); | ||||||
comment.insert(pos, formattedLink); | ||||||
pos += formattedLink.length(); | ||||||
} | ||||||
} while (pos != string::npos); | ||||||
} | ||||||
} | ||||||
} while (pos != string::npos); | ||||||
|
||||||
StringList result; | ||||||
|
||||||
string::size_type pos = 0; | ||||||
pos = 0; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to add a comment about the lines below. I think we can also slightly simplify it, we don't need special logic for the last line if we are calling trimLines. We can also update the calls to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a comment and simplified the way we handled the last line of text. |
||||||
string::size_type nextPos; | ||||||
while ((nextPos = comment.find_first_of('\n', pos)) != string::npos) | ||||||
{ | ||||||
|
@@ -770,23 +757,31 @@ namespace | |||||
} | ||||||
|
||||||
CommentPtr | ||||||
Slice::Contained::parseComment(bool stripMarkup) const | ||||||
Slice::Contained::parseComment( | ||||||
function<string(string)> linkFormatter, | ||||||
bool includeDeprecatedMetadata, | ||||||
bool stripMarkup) const | ||||||
{ | ||||||
return parseComment(_comment, stripMarkup); | ||||||
} | ||||||
CommentPtr comment = make_shared<Comment>(); | ||||||
|
||||||
CommentPtr | ||||||
Slice::Contained::parseComment(const string& text, bool stripMarkup) const | ||||||
{ | ||||||
if (text.empty()) | ||||||
// Check for deprecated metadata and add it to the comment if necessary. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't do that. I removed this behavior in #2970, and looking at this PR, it appears I forgot do removed it from Swift and MATLAB. Please fixes these too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fairness, the comment code for Swift and MATLAB was completely separate from all the other comment code, so it's not surprising. I removed all the includeDeprecated stuff, and am happy to see it go! |
||||||
if (includeDeprecatedMetadata) | ||||||
{ | ||||||
return nullptr; | ||||||
comment->_isDeprecated = isDeprecated(); | ||||||
if (auto reason = getDeprecationReason()) | ||||||
{ | ||||||
comment->_deprecated.push_back(IceInternal::trim(*reason)); | ||||||
} | ||||||
} | ||||||
|
||||||
// Split up the comment text into lines. | ||||||
CommentPtr comment = make_shared<Comment>(); | ||||||
StringList lines = splitComment(text, stripMarkup); | ||||||
// Split the comment's raw text up into lines. | ||||||
StringList lines = splitComment(_comment, linkFormatter, stripMarkup); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The linkFormater is not used here, we can move it to splitComment.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I guess there's no reason not to! |
||||||
if (lines.empty() && !comment->_isDeprecated) | ||||||
{ | ||||||
return nullptr; | ||||||
} | ||||||
|
||||||
// Parse the comment's text. | ||||||
StringList::const_iterator i; | ||||||
for (i = lines.begin(); i != lines.end(); ++i) | ||||||
{ | ||||||
|
@@ -853,6 +848,7 @@ Slice::Contained::parseComment(const string& text, bool stripMarkup) const | |||||
{ | ||||||
if (!line.empty()) | ||||||
{ | ||||||
state = StateMisc; // Reset the state; `@see` cannot span multiple lines. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixes a bug where we'd sometimes generate multi-line |
||||||
comment->_seeAlso.push_back(line); | ||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -375,8 +375,10 @@ namespace Slice | |
int line() const; | ||
|
||
std::string comment() const; | ||
CommentPtr parseComment(bool stripMarkup) const; | ||
CommentPtr parseComment(const std::string& text, bool stripMarkup) const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only need one function, the 2nd one was a hack-around to let |
||
CommentPtr parseComment( | ||
std::function<std::string(std::string)> linkFormatter, | ||
bool includeDeprecatedMetadata = false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment in .cpp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
bool stripMarkup = false) const; | ||
|
||
int includeLevel() const; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,6 +273,12 @@ namespace | |
return r; | ||
} | ||
|
||
/// Returns a doxygen formatted link to the provided Slice identifier. | ||
string cppLinkFormatter(string identifier) | ||
{ | ||
return "{@link " + fixKwd(identifier) + "}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently we weren't escaping names in C++ links... |
||
} | ||
|
||
void writeDocLines(Output& out, const StringList& lines, bool commentFirst, const string& space = " ") | ||
{ | ||
auto l = lines.cbegin(); | ||
|
@@ -374,7 +380,7 @@ namespace | |
return; | ||
} | ||
|
||
CommentPtr doc = p->parseComment(false); | ||
CommentPtr doc = p->parseComment(cppLinkFormatter); | ||
|
||
out << nl << "/**"; | ||
|
||
|
@@ -1689,7 +1695,7 @@ Slice::Gen::ProxyVisitor::visitOperation(const OperationPtr& p) | |
|
||
const string deprecatedSymbol = getDeprecatedSymbol(p); | ||
|
||
CommentPtr comment = p->parseComment(false); | ||
CommentPtr comment = p->parseComment(cppLinkFormatter); | ||
const string contextDoc = "@param " + contextParam + " The Context map to send with the invocation."; | ||
const string futureDoc = "The future object for the invocation."; | ||
|
||
|
@@ -2115,7 +2121,7 @@ Slice::Gen::DataDefVisitor::visitExceptionStart(const ExceptionPtr& p) | |
typeToString(dataMember->type(), dataMember->optional(), scope, dataMember->getMetadata(), _useWstring); | ||
allParamDecls.push_back(typeName + " " + fixKwd(dataMember->name())); | ||
|
||
CommentPtr comment = dataMember->parseComment(false); | ||
CommentPtr comment = dataMember->parseComment(cppLinkFormatter); | ||
if (comment) | ||
{ | ||
allComments[dataMember->name()] = comment; | ||
|
@@ -2592,15 +2598,13 @@ Slice::Gen::DataDefVisitor::emitOneShotConstructor(const ClassDefPtr& p) | |
string typeName = | ||
typeToString(dataMember->type(), dataMember->optional(), scope, dataMember->getMetadata(), _useWstring); | ||
allParamDecls.push_back(typeName + " " + fixKwd(dataMember->name())); | ||
CommentPtr comment = dataMember->parseComment(false); | ||
CommentPtr comment = dataMember->parseComment(cppLinkFormatter); | ||
if (comment) | ||
{ | ||
allComments[dataMember->name()] = comment; | ||
} | ||
} | ||
|
||
CommentPtr comment = p->parseComment(false); | ||
|
||
H << sp; | ||
H << nl << "/**"; | ||
H << nl << " * One-shot constructor to initialize all data members."; | ||
|
@@ -2950,7 +2954,7 @@ Slice::Gen::InterfaceVisitor::visitOperation(const OperationPtr& p) | |
const string currentTypeDecl = "const " + getUnqualified("::Ice::Current&", interfaceScope); | ||
const string currentDecl = currentTypeDecl + " " + currentParam; | ||
|
||
CommentPtr comment = p->parseComment(false); | ||
CommentPtr comment = p->parseComment(cppLinkFormatter); | ||
|
||
if (ret) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to line 644
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.