This article describes the process of writing a plugin for
kate called Neznaju. The plugin is designed to co-edit text via the network Using this plugin you can create a server, or connect to a server created by someone and jointly edit some text. The plugin is as simple as possible. At the moment, it supports connecting multiple clients, as well as editing large files (since only information about changes in the document is transmitted).
Idea
Once, an acquaintance wrote to me on Skype, and offered to
test Gobby , a program for co-editing text. We phoned and downloaded stable versions of Gobby. Having run Gobby for a long time I tried to figure out how to work with it. Or rather how to create a server. As a result, having climbed all the menu items, I never found the server to create a server. Maybe you downloaded the wrong version of Gobby, maybe something else. As a result, I launched the console version of the server, and then connected to it from the client. But the problems did not end there. My friend could not connect. It turned out that in the Gobby, which is with him, encryption is enabled, and it cannot be disabled during connection.
I will not bore my readers with details, let me just say that in the end we managed to set everything up, connect and see how it works. And immediately the question arose, do you need 90% of Gobby functionality for ordinary users?
It is possible that someone needs a whole bunch of all sorts of chips like encryption, chat, list of users, rooms, etc. We decided to make the most simple tool that only does what they want from it. Namely - it will be a plugin, not a new text editor. He will be able to work as a server (listen to the specified port), as well as as a client (connect to the specified address). A simple text protocol will be used to exchange information between the server and the client so that everyone can easily write a plug-in for their favorite text editor (or IDE), and they can work together.
')
Kate was chosen as the text editor because we are both Qt lovers, and we don’t know the more common Qt editor. A quick search on the Internet issued a GSoS project of one guy from Zaporozhye, who planned to create a similar plugin for Kate over the summer using telepathy tubes. What about writing a plugin in a few days? Challenge was accepted.
Development
I installed a virtual machine with debian, downloaded the kate-dev package with the header files needed for the build, and also installed QtCreator. After that, you could proceed directly to the development.
Note that the plugin does not directly depend on kate, it is a plugin for KTextEditor, a component of a text editor in kate and kwrite.
As an example, the TimeDate plugin was taken from the
tutorial on creating plugins to kate. It is going to traditionally with the help of cmake. In addition to the source files, the plug-in requires two more files - plugin.desktop and plugin.rc (for registering the capabilities of our plugin in the editor).
After we assembled and installed the test plug-in, it's time to think about the exchange protocol and the plug-in architecture. At first, we decided to make the simplest option of all possible: for any change in the document, the client must send the full contents to the server, and the server, in turn, to the client. The message format was XML-like: the full document is edged with the full tag.
For network interactions, we used the QtNetwork module, more specifically, the QTcpServer classes for organizing the server part and QTcpSocket for the client side.
When the primitive plugin started working, we decided not to re-send the entire document, but just the changes appearing in it. In order to determine the changes we used the library diff-match-patch. It was a good decision, but on large documents the difference between the texts after each change was calculated for a very long time. Fortunately, it turned out that ktexteditor itself can send signals about changes (textInserted and textRemoved signals). Therefore, we decided to abandon the use of diff-match-patch, but simply send information about the changes transmitted through these signals. The general scheme of the plugin is:

After catching most of the bugs, we got quite a workable tool, which may be useful to someone. The sources are on
githaba .
Conclusion
And the most important thing that we have learned from this whole process:
- opensource is simple. Choose your favorite project, write the code and have fun!
- pair programming cool. Much more fun than coding alone. If you like programming, the two of you will enjoy it even more. Call up, start your favorite ide and go ahead!
- KDE radishes. They wrote to them in the
maillist , but they are silent.
Links
Github sourcesKtexteditor plugin writing guideKTextEditor API ReferenceDiff match patch - a library that defines the difference between two texts.