Provide symmetric content/set_content for document_t

This commit is contained in:
Allan Odgaard
2013-05-14 18:34:29 +07:00
parent 6aa871576f
commit 6e4926177e
7 changed files with 32 additions and 21 deletions

View File

@@ -383,7 +383,7 @@ static be::entry_ptr parent_for_column (NSBrowser* aBrowser, NSInteger aColumn,
plist::dictionary_t plist = plist::convert((__bridge CFPropertyListRef)bundleItemProperties);
std::string const& content = bundleItemContent->buffer().substr(0, bundleItemContent->buffer().size());
std::string const& content = bundleItemContent->content();
item_info_t const& info = info_for(bundleItem->kind());
plist::any_t parsedContent;

View File

@@ -1002,7 +1002,7 @@ doScroll:
if(false) {
} HANDLE_ATTR(Value) {
AUTO_REFRESH;
document->buffer().replace(0, document->buffer().size(), to_s((NSString*)value));
document->set_content(to_s((NSString*)value));
} HANDLE_ATTR(SelectedText) {
AUTO_REFRESH;
editor->insert(to_s((NSString*)value));

View File

@@ -330,7 +330,7 @@ namespace document
document_ptr doc = create();
if(fileType != NULL_STR)
doc->set_file_type(fileType);
doc->set_content(io::bytes_ptr(new io::bytes_t(content)));
doc->set_content(content);
return doc;
}
@@ -540,7 +540,7 @@ namespace document
void document_t::mark_pristine ()
{
ASSERT(_buffer);
_pristine_buffer = _buffer->substr(0, _buffer->size()); // TODO We should use a cheap ng::detail::storage_t copy
_pristine_buffer = content(); // TODO We should use a cheap ng::detail::storage_t copy
}
void document_t::post_load (std::string const& path, io::bytes_ptr content, std::map<std::string, std::string> const& attributes, std::string const& fileType, encoding::type const& encoding)
@@ -992,13 +992,24 @@ namespace document
}
}
void document_t::set_content (io::bytes_ptr const& bytes)
void document_t::set_content (std::string const& str)
{
D(DBF_Document, bug("%.*s… (%zu bytes), file type %s\n", std::min<int>(32, bytes->size()), bytes->get(), bytes->size(), _file_type.c_str()););
ASSERT(!_buffer);
_content = bytes;
D(DBF_Document, bug("%.*s… (%zu bytes), file type %s\n", std::min<int>(32, str.size()), str.data(), str.size(), _file_type.c_str()););
if(_buffer)
_buffer->replace(0, _buffer->size(), str);
else _content.reset(new io::bytes_t(str));
}
std::string document_t::content () const
{
if(_buffer)
return _buffer->substr(0, _buffer->size());
else if(_content)
return std::string(_content->begin(), _content->end());
return NULL_STR;
}
namespace
{
struct file_reader_t : reader::open_t

View File

@@ -239,6 +239,9 @@ namespace document
ng::undo_manager_t& undo_manager () { ASSERT(_undo_manager); return *_undo_manager; }
ng::undo_manager_t const& undo_manager () const { ASSERT(_undo_manager); return *_undo_manager; }
std::string content () const;
void set_content (std::string const& str);
// =============
// = Accessors =
// =============
@@ -269,9 +272,6 @@ namespace document
void setup_buffer ();
void grammar_did_change ();
void set_content (io::bytes_ptr const& bytes);
std::string content () const { ASSERT(_buffer); return _buffer->substr(0, _buffer->size()); }
void set_modified (bool flag);
// ==============

View File

@@ -31,8 +31,8 @@ class document_tests : public CxxTest::TestSuite
document::document_ptr doc = document::create(path);
doc->open();
TS_ASSERT_EQUALS(doc->is_open(), true);
TS_ASSERT_EQUALS(doc->buffer().substr(0, doc->buffer().size()), content);
doc->buffer().replace(0, content.size(), "world");
TS_ASSERT_EQUALS(doc->content(), content);
doc->set_content("world");
doc->save();
doc->close();
}
@@ -78,7 +78,7 @@ public:
document::document_ptr doc = document::create(jail.path("mac-roman.txt"));
jail.set_content("mac-roman.txt", std::string("\xAE\x62\x6C\x65\x67\x72\xBF\x64", 8));
open(doc, "MacRoman");
TS_ASSERT_EQUALS(doc->buffer().substr(0, doc->buffer().size()), "Æblegrød");
TS_ASSERT_EQUALS(doc->content(), "Æblegrød");
doc->close();
}
@@ -88,7 +88,7 @@ public:
document::document_ptr doc = document::from_content(std::string("\xAE\x62\x6C\x65\x67\x72\xBF\x64", 8));
open(doc, "MacRoman");
TS_ASSERT_EQUALS(doc->buffer().substr(0, doc->buffer().size()), "Æblegrød");
TS_ASSERT_EQUALS(doc->content(), "Æblegrød");
doc->close();
}
@@ -99,7 +99,7 @@ public:
document::document_ptr doc = document::create(jail.path("cp-1252.txt"));
jail.set_content("cp-1252.txt", std::string("\xC6\x62\x6C\x65\x67\x72\xF8\x64", 8));
open(doc, "CP1252");
TS_ASSERT_EQUALS(doc->buffer().substr(0, doc->buffer().size()), "Æblegrød");
TS_ASSERT_EQUALS(doc->content(), "Æblegrød");
doc->close();
}
@@ -143,7 +143,7 @@ public:
setxattr(jail.path("cp-1252.txt").c_str(), "com.apple.TextEncoding", "UTF-8", 5, 0, 0);
bool err = open(doc, "CP1252");
TS_ASSERT_EQUALS(err, false);
TS_ASSERT_EQUALS(doc->buffer().substr(0, doc->buffer().size()), "Æblegrød");
TS_ASSERT_EQUALS(doc->content(), "Æblegrød");
doc->close();
}
};

View File

@@ -13,7 +13,7 @@ void notify (document::document_ptr doc, int flags, std::string const& path = NU
std::string content (document::document_ptr doc)
{
return doc->buffer().substr(0, doc->buffer().size());
return doc->content();
}
class FileWatchTests : public CxxTest::TestSuite
@@ -60,7 +60,7 @@ public:
document::document_ptr doc = document::create(jail.path("file.txt"));
doc->open();
doc->buffer().replace(0, doc->buffer().size(), buffer);
doc->set_content(buffer);
doc->set_revision(doc->buffer().bump_revision());
TS_ASSERT_EQUALS(content(doc), buffer);
TS_ASSERT_EQUALS(doc->is_modified(), true);

View File

@@ -12,7 +12,7 @@ public:
replacements.insert(std::make_pair(text::range_t("1:29-1:40"), "abetarda"));
doc->replace(replacements);
doc->open();
TS_ASSERT_EQUALS(doc->buffer().substr(0, doc->buffer().size()), "Lorem ipsum charum sit amet, abetarda adipisicing elit.");
TS_ASSERT_EQUALS(doc->content(), "Lorem ipsum charum sit amet, abetarda adipisicing elit.");
doc->close();
}
@@ -26,7 +26,7 @@ public:
replacements.insert(std::make_pair(text::range_t("2:1-2:4"), "Jazz"));
doc->replace(replacements);
doc->open();
TS_ASSERT_EQUALS(doc->buffer().substr(0, doc->buffer().size()), "Foo\nJazz\nFud\n");
TS_ASSERT_EQUALS(doc->content(), "Foo\nJazz\nFud\n");
doc->close();
}