Simplify the logic in buffer creating

This commit is contained in:
Don Ho 2023-08-02 21:11:55 +02:00
parent b09432cfa8
commit dea388bf59
3 changed files with 6 additions and 10 deletions

View File

@ -1329,7 +1329,7 @@ BufferID FileManager::newEmptyDocument()
return id; return id;
} }
BufferID FileManager::bufferFromDocument(Document doc, bool dontIncrease, bool dontRef) BufferID FileManager::bufferFromDocument(Document doc)
{ {
NppParameters& nppParamInst = NppParameters::getInstance(); NppParameters& nppParamInst = NppParameters::getInstance();
generic_string newTitle = (nppParamInst.getNativeLangSpeaker())->getLocalizedStrFromID("tab-untitled-string", UNTITLED_STR); generic_string newTitle = (nppParamInst.getNativeLangSpeaker())->getLocalizedStrFromID("tab-untitled-string", UNTITLED_STR);
@ -1337,8 +1337,6 @@ BufferID FileManager::bufferFromDocument(Document doc, bool dontIncrease, bool d
wsprintf(nb, TEXT("%d"), static_cast<int>(nextUntitledNewNumber())); wsprintf(nb, TEXT("%d"), static_cast<int>(nextUntitledNewNumber()));
newTitle += nb; newTitle += nb;
if (!dontRef)
_pscratchTilla->execute(SCI_ADDREFDOCUMENT, 0, doc); //set reference for FileManager
Buffer* newBuf = new Buffer(this, _nextBufferID, doc, DOC_UNNAMED, newTitle.c_str(), false); Buffer* newBuf = new Buffer(this, _nextBufferID, doc, DOC_UNNAMED, newTitle.c_str(), false);
BufferID id = newBuf; BufferID id = newBuf;
newBuf->_id = id; newBuf->_id = id;
@ -1347,8 +1345,7 @@ BufferID FileManager::bufferFromDocument(Document doc, bool dontIncrease, bool d
_buffers.push_back(newBuf); _buffers.push_back(newBuf);
++_nbBufs; ++_nbBufs;
if (!dontIncrease) ++_nextBufferID;
++_nextBufferID;
return id; return id;
} }

View File

@ -87,10 +87,9 @@ public:
BufferID loadFile(const TCHAR * filename, Document doc = static_cast<Document>(NULL), int encoding = -1, const TCHAR *backupFileName = nullptr, FILETIME fileNameTimestamp = {}); //ID == BUFFER_INVALID on failure. If Doc == NULL, a new file is created, otherwise data is loaded in given document BufferID loadFile(const TCHAR * filename, Document doc = static_cast<Document>(NULL), int encoding = -1, const TCHAR *backupFileName = nullptr, FILETIME fileNameTimestamp = {}); //ID == BUFFER_INVALID on failure. If Doc == NULL, a new file is created, otherwise data is loaded in given document
BufferID newEmptyDocument(); BufferID newEmptyDocument();
//create Buffer from existing Scintilla, used from new Scintillas. If dontIncrease = true, then the new document number isnt increased afterwards.
//usefull for temporary but neccesary docs //create Buffer from existing Scintilla, used from new Scintillas.
//If dontRef = false, then no extra reference is added for the doc. Its the responsibility of the caller to do so BufferID bufferFromDocument(Document doc);
BufferID bufferFromDocument(Document doc, bool dontIncrease = false, bool dontRef = false);
BufferID getBufferFromName(const TCHAR * name); BufferID getBufferFromName(const TCHAR * name);
BufferID getBufferFromDocument(Document doc); BufferID getBufferFromDocument(Document doc);

View File

@ -1881,7 +1881,7 @@ BufferID ScintillaEditView::attachDefaultDoc()
// get the doc pointer attached (by default) on the view Scintilla // get the doc pointer attached (by default) on the view Scintilla
Document doc = execute(SCI_GETDOCPOINTER, 0, 0); Document doc = execute(SCI_GETDOCPOINTER, 0, 0);
execute(SCI_ADDREFDOCUMENT, 0, doc); execute(SCI_ADDREFDOCUMENT, 0, doc);
BufferID id = MainFileManager.bufferFromDocument(doc, false, true);//true, true); //keep counter on 1 BufferID id = MainFileManager.bufferFromDocument(doc);
Buffer * buf = MainFileManager.getBufferByID(id); Buffer * buf = MainFileManager.getBufferByID(id);
MainFileManager.addBufferReference(id, this); //add a reference. Notepad only shows the buffer in tabbar MainFileManager.addBufferReference(id, this); //add a reference. Notepad only shows the buffer in tabbar