-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathDocSetManager.cpp
More file actions
234 lines (211 loc) · 7.86 KB
/
Copy pathDocSetManager.cpp
File metadata and controls
234 lines (211 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// Created by Ivan Buhov on 11/6/15.
//
#include "DocSetManager.h"
#include <libxml/xpath.h>
#include <sstream>
#include <unistd.h>
namespace {
using namespace std;
bool isntspace(char ch) {
return !std::isspace(ch);
}
// trim from start
std::string& ltrim(std::string& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), isntspace));
return s;
}
// trim from end
std::string& rtrim(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), isntspace).base(), s.end());
return s;
}
// trim from both ends
std::string& trim(std::string& s)
{
return ltrim(rtrim(s));
}
void findAndReplaceIn(string& str, string searchFor, string replaceBy)
{
size_t found = str.find(searchFor);
while (found != string::npos) {
str.replace(found, searchFor.length(), replaceBy);
found = str.find(searchFor, found + replaceBy.length());
};
}
xmlNodeSetPtr all(const xmlChar* xpath, xmlDocPtr doc, xmlXPathObjectPtr& result)
{
xmlXPathContextPtr context = xmlXPathNewContext(doc);
result = xmlXPathEvalExpression(xpath, context);
xmlXPathFreeContext(context);
if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
xmlXPathFreeObject(result);
result = nullptr;
return NULL;
}
return result->nodesetval;
}
xmlNodePtr first(const xmlChar* xpath, xmlDocPtr doc, xmlXPathObjectPtr& result)
{
xmlNodeSetPtr nodes = all(xpath, doc, result);
if (nodes && nodes->nodeNr > 0)
return nodes->nodeTab[0];
return nullptr;
}
string innerTextOf(xmlNodePtr xmlNode, xmlDocPtr doc)
{
char* innerText = reinterpret_cast<char*>(xmlNodeGetContent(xmlNode));
string innerStr(innerText);
xmlFree(innerText);
return innerStr;
}
vector<string> innerTextOf(xmlNodeSetPtr xmlNodes, xmlDocPtr doc)
{
vector<string> result;
if (xmlNodes != nullptr) {
for (int i = 0; i < xmlNodes->nodeNr; ++i) {
result.push_back(innerTextOf(xmlNodes->nodeTab[i], doc));
}
}
return result;
}
}
namespace TypeScript {
using namespace std;
std::string TSComment::toString(std::string linePrefix)
{
if (description.length() == 0 && params.size() == 0) {
return std::string();
}
std::stringstream result;
result << linePrefix << "/**" << std::endl;
std::string processedDesc = description;
findAndReplaceIn(processedDesc, "\n", "");
result << linePrefix << " * " << processedDesc << std::endl;
for (std::pair<std::string, std::string>& param : params) {
// @param paramName - paramDesc
result << linePrefix << " * "
<< "@param " + param.first + " - " + param.second << std::endl;
}
result << linePrefix << " */" << std::endl;
return result.str();
}
TSComment DocSetManager::getCommentFor(Meta::Meta* meta, Meta::Meta* parent)
{
return (parent == nullptr) ? getCommentFor(meta->name, meta->type) : getCommentFor(meta->name, meta->type, parent->name, parent->type);
}
TSComment DocSetManager::getCommentFor(std::string name, Meta::MetaType type, std::string parentName, Meta::MetaType parentType)
{
xmlDocPtr doc = getXmlDocFileFor(name, type, parentName, parentType);
TSComment comment;
if (doc) {
xmlXPathObjectPtr abstractNodeResult = nullptr;
xmlNodePtr abstractNode = first(reinterpret_cast<const xmlChar*>("/*/Abstract"), doc, abstractNodeResult);
if (abstractNode != nullptr) {
std::string description = innerTextOf(abstractNode, doc);
comment.description = trim(description);
}
switch (type) {
case Meta::MetaType::Method:
case Meta::MetaType::Function: {
xmlXPathObjectPtr termNodesResult = nullptr;
std::vector<string> paramNames = innerTextOf(all(reinterpret_cast<const xmlChar*>("/*/Parameters/Parameter/Term"), doc, termNodesResult), doc);
if (paramNames.size() > 0) {
xmlXPathObjectPtr discussionNodesResult = nullptr;
std::vector<string> paramDescs = innerTextOf(all(reinterpret_cast<const xmlChar*>("/*/Parameters/Parameter/Discussion"), doc, discussionNodesResult), doc);
assert(paramNames.size() == paramDescs.size());
for (size_t i = 0; i < paramNames.size(); i++) {
comment.params.push_back(std::pair<std::string, std::string>(paramNames[i], trim(paramDescs[i])));
}
xmlXPathFreeObject(discussionNodesResult);
}
xmlXPathFreeObject(termNodesResult);
break;
}
case Meta::MetaType::Struct:
case Meta::MetaType::Union: {
xmlXPathObjectPtr discussionNodesResult = nullptr;
std::vector<string> fieldsDescs = innerTextOf(all(reinterpret_cast<const xmlChar*>("/*/Fields/Field/Discussion"), doc, discussionNodesResult), doc);
if (fieldsDescs.size() > 0) {
for (size_t i = 0; i < fieldsDescs.size(); i++) {
TSComment fieldComment;
fieldComment.description = trim(fieldsDescs[i]);
comment.fields.push_back(fieldComment);
}
}
xmlXPathFreeObject(discussionNodesResult);
break;
}
default: {
break;
}
}
xmlXPathFreeObject(abstractNodeResult);
}
xmlFreeDoc(doc);
return comment;
}
xmlDocPtr DocSetManager::getXmlDocFileFor(std::string name, Meta::MetaType type, std::string parentName, Meta::MetaType parentType)
{
std::string parent = (parentName == "") ? "-" : parentName;
std::vector<std::string> xmlPathCandidates;
switch (type) {
case Meta::MetaType::Struct: {
xmlPathCandidates.push_back(this->tokensPath + "/c/tdef/" + parent + "/" + name + ".xml");
xmlPathCandidates.push_back(this->tokensPath + "/c/tag/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::Function: {
xmlPathCandidates.push_back(this->tokensPath + "/c/func/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::Enum: {
xmlPathCandidates.push_back(this->tokensPath + "/c/tdef/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::EnumConstant: {
xmlPathCandidates.push_back(this->tokensPath + "/c/econst/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::Var: {
xmlPathCandidates.push_back(this->tokensPath + "/c/data/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::Interface: {
xmlPathCandidates.push_back(this->tokensPath + "/Objective-C/cl/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::Protocol: {
xmlPathCandidates.push_back(this->tokensPath + "/Objective-C/intf/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::Category: {
xmlPathCandidates.push_back(this->tokensPath + "/Objective-C/cat/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::Method: {
std::string type1 = (parentType == Meta::MetaType::Interface) ? "instm" : "intfm";
std::string type2 = (parentType == Meta::MetaType::Interface) ? "clm" : "intfcm";
xmlPathCandidates.push_back(this->tokensPath + "/Objective-C/" + type1 + "/" + parent + "/" + name + ".xml");
xmlPathCandidates.push_back(this->tokensPath + "/Objective-C/" + type2 + "/" + parent + "/" + name + ".xml");
break;
}
case Meta::MetaType::Property: {
std::string type = (parentType == Meta::MetaType::Interface) ? "instp" : "intfp";
xmlPathCandidates.push_back(this->tokensPath + "/Objective-C/" + type + "/" + parent + "/" + name + ".xml");
break;
}
default: {
break;
}
}
for (string& path : xmlPathCandidates) {
if (access(path.c_str(), F_OK) == 0) {
return xmlReadFile(path.c_str(), nullptr, 0);
}
}
return nullptr;
}
}