bool DocumentEditor::saveFile(const QString &fileName_) { //remove old path from watcher _watcher.removePath(_fullPath); QFile file(fileName_); if (!file.open(QFile::WriteOnly)) { QMessageBox::warning(this, PACKAGE_NAME, tr("Cannot save file %1:\n%2.") .arg(fileName_) .arg(file.errorString())); //re add the old path to the watcher if(!_fullPath.isEmpty()) _watcher.addPath(_fullPath); return false; } QTextCodec* codec = QTextCodec::codecForName(_codec.toUtf8()); if(codec == 0) { QMessageBox::critical(this, PACKAGE_NAME, tr("Cannot save file %1:\nUnsupported charset %2 !!") .arg(fileName_).arg(_codec)); //re add the old path to the watcher if(!_fullPath.isEmpty()) _watcher.addPath(_fullPath); return false; } QApplication::setOverrideCursor(Qt::WaitCursor); //file.resize(0); //file.write(codec->fromUnicode(text())); // check if strip spaces if(_trimOnSave == true) trimTrailingSpaces(); // check if add new line to the end if (_addNewLineOnSave == true) { if (!isLineEmpty(lines() - 1)) { append(getEol()); } } QTextStream out(&file); out.setCodec(codec); out.setGenerateByteOrderMark(needBOM()); out << text(); out.flush(); _fullPath = fileName_; setModified(false); _isNew = false; QApplication::restoreOverrideCursor(); //add it to the watcher _watcher.addPath(_fullPath); _notified = false; return true; }
void SourceTASReader::ParseVariable() { if (isLineEmpty()) return; std::string type; std::string name; std::string value; GetTriplet(lineStream, type, name, value, ' '); variables.AddNewVariable(type, name, value); }
void SourceTASReader::ParseFrameBulk() { if (isLineEmpty()) { return; } else if (line.find("ss") == 0) { currentScript.AddSaveState(); } else { FrameBulkInfo info(lineStream); auto output = HandleFrameBulk(info); currentScript.AddFrameBulk(output); } }
void SourceTASReader::ParseProp() { if (isLineEmpty()) { return; } std::string prop; std::string value; GetDoublet(lineStream, prop, value, ' '); if (propertyHandlers.find(prop) != propertyHandlers.end()) { (this->*propertyHandlers[prop])(value); } else throw std::exception("Unknown property name"); }
std::string readFile(std::string fileName) { std::string result, line; bool isComment = false; std::ifstream fSource; fSource.open(fileName, std::ios::in); while (!fSource.eof()) { getline(fSource, line); //check empty lines if (isLineEmpty(line)) line = ""; //check multiline comment flag if (!isComment) { //check line for strings and comments line = deleteStringAndComment(line, &isComment); } else { //check line for multiline comment end int commentEnd = line.find("*/"); if (commentEnd > -1) { isComment = false; line = line.substr(commentEnd + 2, line.length()); line = deleteStringAndComment(line, &isComment); } } if (line != "") { line += '\n'; FileLines.push_back(line); } result += line; } return result; }
void DocumentEditor::toggleComment(bool lineCommentPrefered_) { QsciLexer* l = lexer(); if(l == 0) return; QString comment = l->commentLine(); QStringList commentBlock = l->commentBlock(); if(comment.isEmpty() && commentBlock.isEmpty()){ qDebug() << "Toggle comment is not supported for " << l->language(); return; } if (!hasSelectedText()) { //if line is empty, skip it int line = getCurrentLine(); if (isLineEmpty(line)) return; QString selText = text(line); selText.remove("\n"); selText.remove("\r"); QString selTextTrimmed = selText.trimmed(); int pos_start = SendScintilla(SCI_POSITIONFROMLINE, line); int pos_end = SendScintilla(SCI_GETLINEENDPOSITION, line); // check for block comments on a line if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); if (selTextTrimmed.startsWith(blockStart) && selTextTrimmed.endsWith(blockEnd)) { beginUndoAction(); int idx1 = selText.indexOf(blockStart); selText.remove(idx1, blockEnd.size()); int idx2 = selText.lastIndexOf(blockEnd); selText.remove(idx2, blockEnd.size()); SendScintilla(SCI_SETTARGETSTART, pos_start); SendScintilla(SCI_SETTARGETEND, pos_end); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); endUndoAction(); return; } } // check for single comments if (!comment.isEmpty()) { if (selTextTrimmed.startsWith(comment)) { // remove comment int idx = selText.indexOf(comment); selText = selText.remove(idx, comment.size()); } else { // set comment selText = selText.prepend(comment); } SendScintilla(SCI_SETTARGETSTART, pos_start); SendScintilla(SCI_SETTARGETEND, pos_end); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); return; } }else{ // comment out the selection QString selText = selectedText(); QString selTextTrimmed = selText.trimmed(); if (selTextTrimmed.isEmpty()) return; int lineFrom, lineTo, indexFrom, indexTo; getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo); int pos_start = positionFromLineIndex(lineFrom, indexFrom); int pos_end = positionFromLineIndex(lineTo, indexTo); // check if it is double commented block - to do before single check! if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); // comment exists? remove? if (selTextTrimmed.startsWith(blockStart) && selTextTrimmed.endsWith(blockEnd)) { beginUndoAction(); int idx1 = selText.indexOf(blockStart); selText.remove(idx1, blockStart.size()); int idx2 = selText.lastIndexOf(blockEnd); selText.remove(idx2, blockEnd.size()); SendScintilla(SCI_TARGETFROMSELECTION); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); SendScintilla(SCI_SETSEL, SendScintilla(SCI_GETTARGETSTART), SendScintilla(SCI_GETTARGETEND)); endUndoAction(); return; } } // check if this block can be single commented if (!comment.isEmpty() && lineCommentPrefered_) { bool empty_start = false, empty_end = false; if (indexFrom == 0) empty_start = true; else empty_start = getTextRange(positionFromLineIndex(lineFrom, 0), pos_start).trimmed().isEmpty(); if (indexTo == 0) empty_end = true; else empty_end = getTextRange(pos_end, positionFromLineIndex(lineTo+1, 0)).trimmed().isEmpty(); if (empty_start && empty_end) { beginUndoAction(); // corrections if (indexTo == 0) lineTo--; if (isLineEmpty(lineFrom)) { lineFrom++; indexFrom = 0; } // a workaround: move cursor to the next line to replace EOL as well setSelection(lineFrom, 0, lineTo+1, 0); QStringList sl; for (int i = lineFrom; i <= lineTo; i++) sl += text(i); bool comm = false; for (int i = 0; i < sl.count(); i++) if (!sl.at(i).trimmed().startsWith(comment)) { comm = true; break; } for (int i = 0; i < sl.count(); i++) { if (comm) sl[i] = sl[i].prepend(comment); else { int idx = sl.at(i).indexOf(comment); sl[i] = sl[i].remove(idx, comment.size()); } } SendScintilla(SCI_TARGETFROMSELECTION); SendScintilla(SCI_REPLACETARGET, -1, sl.join("").toUtf8().data()); SendScintilla(SCI_SETSEL, SendScintilla(SCI_GETTARGETSTART), SendScintilla(SCI_GETTARGETEND)); endUndoAction(); return; } } // else, set double comment if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); beginUndoAction(); // last is first SendScintilla(SCI_INSERTTEXT, pos_end, blockEnd.toUtf8().data()); SendScintilla(SCI_INSERTTEXT, pos_start, blockStart.toUtf8().data()); // select everything if(lineFrom == lineTo) setSelection(lineFrom, indexFrom, lineTo, indexTo + blockStart.size() + blockEnd.size()); else setSelection(lineFrom, indexFrom, lineTo, indexTo + blockEnd.size()); endUndoAction(); return; } } }