Fixes: fakevim: work on writing

This commit is contained in:
hjk
2009-01-23 17:02:22 +01:00
parent 2d67b9a0a1
commit 8b3136f8b0

View File

@@ -1337,12 +1337,12 @@ int FakeVimHandler::Private::readLineCode(QString &cmd)
void FakeVimHandler::Private::selectRange(int beginLine, int endLine) void FakeVimHandler::Private::selectRange(int beginLine, int endLine)
{ {
m_tc.setPosition(positionForLine(beginLine), MoveAnchor); m_anchor = positionForLine(beginLine);
if (endLine == linesInDocument()) { if (endLine == linesInDocument()) {
m_tc.setPosition(positionForLine(endLine), KeepAnchor); m_tc.setPosition(positionForLine(endLine), MoveAnchor);
m_tc.movePosition(EndOfLine, KeepAnchor); m_tc.movePosition(EndOfLine, MoveAnchor);
} else { } else {
m_tc.setPosition(positionForLine(endLine + 1), KeepAnchor); m_tc.setPosition(positionForLine(endLine + 1), MoveAnchor);
} }
} }
@@ -1400,29 +1400,33 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
QString fileName = reWrite.cap(2); QString fileName = reWrite.cap(2);
if (fileName.isEmpty()) if (fileName.isEmpty())
fileName = m_currentFileName; fileName = m_currentFileName;
QFile file(fileName); QFile file1(fileName);
bool exists = file.exists(); bool exists = file1.exists();
if (exists && !forced && !noArgs) { if (exists && !forced && !noArgs) {
showRedMessage(tr("File '%1' exists (add ! to override)").arg(fileName)); showRedMessage(tr("File '%1' exists (add ! to override)").arg(fileName));
} else if (file.open(QIODevice::ReadWrite)) { } else if (file1.open(QIODevice::ReadWrite)) {
file.close(); file1.close();
QTextCursor tc = m_tc;
selectRange(beginLine, endLine); selectRange(beginLine, endLine);
QString contents = selectedText(); QString contents = selectedText();
m_tc = tc; qDebug() << "LINES: " << beginLine << endLine;
bool handled = false; bool handled = false;
emit q->writeFileRequested(&handled, fileName, contents); emit q->writeFileRequested(&handled, fileName, contents);
// nobody cared, so act ourselves // nobody cared, so act ourselves
if (!handled) { if (!handled) {
//qDebug() << "HANDLING MANUAL SAVE"; //qDebug() << "HANDLING MANUAL SAVE TO " << fileName;
QFile file(fileName); QFile::remove(fileName);
file.open(QIODevice::ReadWrite); QFile file2(fileName);
{ QTextStream ts(&file); ts << contents; } if (file2.open(QIODevice::ReadWrite)) {
file.close(); QTextStream ts(&file2);
ts << contents;
} else {
showRedMessage(tr("Cannot open file '%1' for writing").arg(fileName));
}
} }
// check result by reading back // check result by reading back
file.open(QIODevice::ReadOnly); QFile file3(fileName);
QByteArray ba = file.readAll(); file3.open(QIODevice::ReadOnly);
QByteArray ba = file3.readAll();
showBlackMessage(tr("\"%1\" %2 %3L, %4C written") showBlackMessage(tr("\"%1\" %2 %3L, %4C written")
.arg(fileName).arg(exists ? " " : " [New] ") .arg(fileName).arg(exists ? " " : " [New] ")
.arg(ba.count('\n')).arg(ba.size())); .arg(ba.count('\n')).arg(ba.size()));
@@ -1814,9 +1818,7 @@ QString FakeVimHandler::Private::selectedText() const
{ {
QTextCursor tc = m_tc; QTextCursor tc = m_tc;
tc.setPosition(m_anchor, KeepAnchor); tc.setPosition(m_anchor, KeepAnchor);
QString text = tc.selection().toPlainText(); return tc.selection().toPlainText();
tc.clearSelection();
return text;
} }
int FakeVimHandler::Private::positionForLine(int line) const int FakeVimHandler::Private::positionForLine(int line) const