@@ -8,14 +8,16 @@ Author: Leonardo de Moura
88#include < algorithm>
99#include < functional>
1010#include < cctype>
11+ #include < utility>
12+ #include < vector>
1113#include " util/sstream.h"
1214#include " library/module.h"
1315#include " library/documentation.h"
1416
1517namespace lean {
1618struct documentation_ext : public environment_extension {
17- /* * Doc string for the current module being processed. It does not include imported doc strings. */
18- list<doc_entry> m_module_doc ;
19+ /* * Doc strings for the current module being processed. It does not include imported doc strings. */
20+ std::vector<std::pair<pos_info, std::string>> m_module_docs ;
1921 /* * Doc strings for declarations (including imported ones). We store doc_strings for declarations in the .olean files. */
2022 name_map<std::string> m_doc_string_map;
2123};
@@ -36,25 +38,16 @@ static environment update(environment const & env, documentation_ext const & ext
3638struct doc_modification : public modification {
3739 LEAN_MODIFICATION (" doc" )
3840
39- /* * If non-empty, this is a doc on a declaration. Otherwise,
40- * it's a doc on the whole module. */
4141 name m_decl;
4242 std::string m_doc;
4343
4444 doc_modification () {}
45- /* * A docstring for the entire module. */
46- doc_modification (std::string const & doc) : m_decl(" " ), m_doc(doc) {}
4745 /* * A docstring for a declaration in the module. */
4846 doc_modification (name const & decl, std::string const & doc) : m_decl(decl), m_doc(doc) {}
4947
5048 void perform (environment & env) const override {
5149 auto ext = get_extension (env);
52- if (m_decl != " " ) {
53- ext.m_doc_string_map .insert (m_decl, m_doc);
54- }
55- // Note that we do NOT add anything to `m_module_doc` here, because `perform` is called
56- // when applying modifications from imported .olean modules whose doc strings are NOT
57- // part of the current module's documentation.
50+ ext.m_doc_string_map .insert (m_decl, m_doc);
5851 env = update (env, ext);
5952 }
6053
@@ -176,12 +169,12 @@ static std::string process_doc(std::string s) {
176169 return add_lean_suffix_to_code_blocks (s);
177170}
178171
179- environment add_module_doc_string (environment const & env, std::string doc) {
172+ environment add_module_doc_string (environment const & env, std::string doc, pos_info pos ) {
180173 doc = process_doc (doc);
181174 auto ext = get_extension (env);
182- ext.m_module_doc = cons ( doc_entry (doc), ext. m_module_doc );
175+ ext.m_module_docs . emplace_back (pos, doc );
183176 auto new_env = update (env, ext);
184- return module::add (new_env, std::make_shared<doc_modification>( doc) );
177+ return module::add_doc_string (new_env, doc, pos );
185178}
186179
187180environment add_doc_string (environment const & env, name const & n, std::string doc) {
@@ -191,7 +184,6 @@ environment add_doc_string(environment const & env, name const & n, std::string
191184 throw exception (sstream () << " environment already contains a doc string for '" << n << " '" );
192185 }
193186 ext.m_doc_string_map .insert (n, doc);
194- ext.m_module_doc = cons (doc_entry (n, doc), ext.m_module_doc );
195187 auto new_env = update (env, ext);
196188 return module::add (new_env, std::make_shared<doc_modification>(n, doc));
197189}
@@ -204,10 +196,13 @@ optional<std::string> get_doc_string(environment const & env, name const & n) {
204196 return optional<std::string>();
205197}
206198
207- void get_module_doc_strings (environment const & env, buffer<doc_entry > & result) {
199+ void get_module_doc_strings (environment const & env, buffer<mod_doc_entry > & result) {
208200 auto ext = get_extension (env);
209- to_buffer (ext.m_module_doc , result);
210- std::reverse (result.begin (), result.end ());
201+ auto const & mod_docs = module::get_doc_strings (env);
202+ for (auto const & pr : mod_docs) {
203+ result.push_back ({ optional<std::string>{ pr.first }, pr.second });
204+ }
205+ result.push_back ({ {}, ext.m_module_docs });
211206}
212207
213208void initialize_documentation () {
0 commit comments