Skip to content

Commit

Permalink
Track changes and toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
dmadisetti committed May 7, 2023
1 parent 78e9819 commit 9a1416c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Variable | Possible Values | Description
`g:AirLatexLogFile` | `AirLatex.log` (default) | Log file name. (The file appears in the folder where vim has been started, but only if the log level is greater than `NOTSET`.)
`g:AirLatexWebsocketTimeout` | `10` (default) | Number of seconds to wait before declaring the connection as *stale*. This may happen if the server does not answer a request by AirLatex. Setting to `"none"` disables this feature. However, it can be the case that you will not notice when something is wrong with the connection.
`g:AirLatexAllowInsecure` | `0` (default, off), `1` (on) | Allow insecure connection. For example, if the server is self hosted and/or the certificate is self-signed
`g:AirLatexTrackChanges` | `0` (default, off), `1` (on) | Allow track changes to be sent.


Troubleshooting
Expand Down
4 changes: 3 additions & 1 deletion plugin/airlatex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ if !exists("g:AirLatexWebsocketTimeout")
let g:AirLatexWebsocketTimeout=10
endif


if !exists("g:AirLatexTrackChanges")
let g:AirLatexTrackChanges=0
endif

" vim: set sw=4 sts=4 et fdm=marker:
6 changes: 6 additions & 0 deletions rplugin/python3/airlatex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def compile(self, args):
def toggle(self, args):
self.sidebar.toggle()

@pynvim.function('AirLatexToggleTracking', sync=True)
def toggleTracking(self, args):
# Should be set, but just in case
tracking = self.nvim.eval("g:AirLatexTrackChanges")
self.nvim.command(f"let g:AirLatexTrackChanges={1 - tracking}")

@pynvim.function('AirLatex_Close', sync=True)
def sidebarClose(self, args):
if self.sidebar:
Expand Down
5 changes: 4 additions & 1 deletion rplugin/python3/airlatex/documentbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,10 @@ def writeBuffer(self):
# update saved buffer & send command
self.saved_buffer = self.buffer[:]
self.log.debug(" -> sending ops")
create_task(self.project_handler.sendOps(self.document, content_hash, ops))

track = self.nvim.eval("g:AirLatexTrackChanges") == 1
create_task(self.project_handler.sendOps(self.document, content_hash,
ops, track))

def applyUpdate(self, packet, comments):
self.log.debug("apply server updates to buffer")
Expand Down
17 changes: 10 additions & 7 deletions rplugin/python3/airlatex/project_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
from itertools import count
import json
from airlatex.util import _genTimeStamp
from airlatex.util import _genTimeStamp, generateId
import time
from tornado.locks import Lock, Event
from logging import DEBUG
Expand Down Expand Up @@ -184,11 +184,11 @@ async def updateCursor(self, doc, pos):
}, event=event)

# wrapper for the ioloop
async def sendOps(self, document, content_hash, ops=[]):
await self.ops_queue.put((document, content_hash, ops))
async def sendOps(self, document, content_hash, ops=[], track=False):
await self.ops_queue.put((document, content_hash, ops, track))

# actual sending of ops
async def _sendOps(self, document, content_hash, ops=[]):
async def _sendOps(self, document, content_hash, ops=[], track=False):

# append new ops to buffer
document["ops_buffer"] += ops
Expand Down Expand Up @@ -220,6 +220,9 @@ async def _sendOps(self, document, content_hash, ops=[]):
"hash": content_hash # overleaf/web: sends document hash (if it hasn't been sent in the last 5 seconds)
}

if track:
obj_to_send['meta'] = {'tc': generateId()}

# notify server of local change
self.log.debug("Sending %i changes to document %s (ver %i)." % (len(ops_buffer), document["_id"], document["version"]))
await self.send("cmd",{
Expand Down Expand Up @@ -254,7 +257,7 @@ async def sendOps_flush(self):
all_ops = {}

# await first element
document, content_hash, ops = await self.ops_queue.get()
document, content_hash, ops, track = await self.ops_queue.get()
if document["_id"] not in all_ops:
all_ops[document["_id"]] = ops
else:
Expand All @@ -263,7 +266,7 @@ async def sendOps_flush(self):
# get also all other elements that are currently in queue
num = self.ops_queue.qsize()
for i in range(num):
document, content_hash, ops = await self.ops_queue.get()
document, content_hash, ops, track = await self.ops_queue.get()
if document["_id"] not in all_ops:
all_ops[document["_id"]] = ops
else:
Expand All @@ -272,7 +275,7 @@ async def sendOps_flush(self):
# apply all ops one after another
for doc_id, ops in all_ops.items():
document = self.documents[doc_id]
await self._sendOps(document, content_hash, ops)
await self._sendOps(document, content_hash, ops, track)


async def joinDocument(self, buffer):
Expand Down
1 change: 0 additions & 1 deletion rplugin/python3/airlatex/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ async def _getWebSocketURL(self):
self.log.debug("Websocket wsChannel '%s'"%wsChannel)
return ("wss://" if self.https else "ws://") + self.domain + "/socket.io/1/websocket/"+wsChannel


# --- #
# api # (to be used by pynvim.plugin)
# --- #
Expand Down
15 changes: 15 additions & 0 deletions rplugin/python3/airlatex/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import traceback
from logging import NOTSET
import random


__version__ = "0.2"
Expand Down Expand Up @@ -62,6 +63,20 @@ def debug_gui(self, message, *args, **kws):
return log


def generateId():
pid = format(random.randint(0, 32767), 'x')
machine = format(random.randint(0, 16777216), 'x')
timestamp = format(int(time.time()), 'x')

return (
'00000000'[: 8 - len(timestamp)] +
timestamp +
'000000'[: 6 - len(machine)] +
machine +
'0000'[: 4 - len(pid)] +
pid
)


def pynvimCatchException(fn, alt=None):
def wrapped(self, *args, **kwargs):
Expand Down

0 comments on commit 9a1416c

Please sign in to comment.