From 40e6849127c0f0e19ce5c29e15449622804eb97b Mon Sep 17 00:00:00 2001 From: dylan madisetti Date: Sat, 6 May 2023 18:29:40 -0400 Subject: [PATCH] Reuse exisiting buffer to solve double buffer problem da-h/AirLatex.vim#25 --- rplugin/python3/airlatex/documentbuffer.py | 23 ++++++++++++++++------ rplugin/python3/airlatex/sidebar.py | 9 +++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/rplugin/python3/airlatex/documentbuffer.py b/rplugin/python3/airlatex/documentbuffer.py index 8e48cfa..a6a5b6a 100644 --- a/rplugin/python3/airlatex/documentbuffer.py +++ b/rplugin/python3/airlatex/documentbuffer.py @@ -20,10 +20,21 @@ def __init__(self, path, nvim): self.buffer_mutex = RLock() self.saved_buffer = None - def getName(self): - return "/".join([p["name"] for p in self.path]) - def getExt(self): - return self.document["name"].split(".")[-1] + @staticmethod + def getName(path): + return "/".join([p["name"] for p in path]) + + @staticmethod + def getExt(document): + return document["name"].split(".")[-1] + + @property + def name(self): + return DocumentBuffer.getName(self.path) + + @property + def ext(self): + return DocumentBuffer.getExt(self.document) def initDocumentBuffer(self): self.log.debug_gui("initDocumentBuffer") @@ -31,7 +42,7 @@ def initDocumentBuffer(self): # Creating new Buffer self.nvim.command('wincmd w') self.nvim.command('enew') - self.nvim.command('file '+ self.getName()) + self.nvim.command('file '+ self.name) self.buffer = self.nvim.current.buffer DocumentBuffer.allBuffers[self.buffer] = self @@ -39,7 +50,7 @@ def initDocumentBuffer(self): self.nvim.command("syntax on") self.nvim.command('setlocal noswapfile') self.nvim.command('setlocal buftype=nofile') - self.nvim.command("set filetype="+self.getExt()) + self.nvim.command("set filetype=" + self.ext) # ??? Returning normal function to these buttons # self.nvim.command("nmap ") diff --git a/rplugin/python3/airlatex/sidebar.py b/rplugin/python3/airlatex/sidebar.py index 7d44372..0f078bd 100644 --- a/rplugin/python3/airlatex/sidebar.py +++ b/rplugin/python3/airlatex/sidebar.py @@ -355,5 +355,14 @@ def cursorAction(self, key="enter"): # is file elif self.cursorPos[-1]["type"] == "file": + name = DocumentBuffer.getName(self.cursorPos) + for buffer, document in DocumentBuffer.allBuffers.items(): + self.log.debug_gui("{name} vs {document.name}") + if name == document.name: + self.nvim.command('wincmd w') + self.nvim.command('enew') + self.nvim.command(f'buffer {buffer.number}') + return documentbuffer = DocumentBuffer(self.cursorPos, self.nvim) create_task(self.cursorPos[0]["handler"].joinDocument(documentbuffer)) +