diff --git a/lib/buffer.js b/lib/buffer.js index 713f61f..14eb74d 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1,130 +1,254 @@ var Types = require("types.js"); -function Buffer (vimClient) { + +/** + * Represents a buffer + * @class + * @param {InternalClient} client The vim client linked to the buffer + * @param {number} buffId Id of the buffer + */ +function Buffer (client, buffId) { "use strict"; - var buffId; - var typeNum = 0; + var typeNum = 0; //WARNING : check if this should be unique to a buffer + var serNum = 0; //WARNING : check if this should be unique to a buffer /* COMMANDS */ - var addAnno = function (annoType, off) { - + /** + * Place an annotation + * @function + * @param {number} annoType Returned annoType by defineAnnoType + * @param {number|number[]} offset Position of the annotation + * @return {number} Serial number of the anno + */ + this.addAnno = function (annoType, off) { + serNum += 1; + client.sendCommand("addAnno", buffId, serNum, annoType,Types.position(off), 0 ); + return serNum; }; - var close = function(){ - + /** + * Close the buffer. + * @function + */ + this.close = function(){ + client.sendCommand("close", buffId); }; - var defineAnnoType = function (typeName, glyphFile, fg, bg) { + /** + * Define a type of annotation for this buffer + * @function + * @param {string} typeName Name that identifies this annotation type + * @param {string} glyphFile Name of icon file + * @param {string|number} fg Foreground color + * @param {string|number} bg Background color + * @return {number} Identifier of the annotation type + */ + this.defineAnnoType = function (typeName, glyphFile, fg, bg) { typeNum += 1; + client.sendCommand("defineAnnoType", buffId, typeNum, Types.string(typeName), Types.string(""), Types.string(glyphFile), Types.color(fg), Types.color(bg)); + return typeNum; }; - var startDocumentListen = function () { - + /** + * Mark the buffer to report changes with "insert" and "remove" events + * @function + */ + this.startDocumentListen = function () { + client.sendCommand("startDocumentListen", buffId); }; - var stopDocumentListen = function () { - + + /** + * Mark the buffer to stop reporting changes + * @function + */ + this.stopDocumentListen = function () { + client.sendCommand("stopDocumentListen", buffId); }; - var guard = function (off, len) { - + /** + * Mark an area in the buffer as guarded + * @function + * @param {number|number[]} off Starting position + * @param {number} len Length of the area + */ + this.guard = function (off, len) { + client.sendCommand("guard",buffId, Types.position(off), len); }; - var initDone = function () { - + /** + * Tell Vim that the buffer is ready to be used and make it the current buffer + * Fire the BufReadPost autocommand event + * @function + */ + this.initDone = function () { + client.sendCommand("initdone", buffId); }; - var putBufferNumber = function (pathname) { - }; - - var netbeansBuffer = function (isNetBeansBuffer) { - + /** + * Tell Vim an initial insert is done + * Trigger a read message being printed + * @function + */ + this.insertDone = function () { + client.sendCommand("insertDone", buffId); }; - var removeAnno = function (serNum) { + /** + * Tell Vim that the buffer is owned by the controller + * @function + * @param isNetBeansBuffer + */ + this.netbeansBuffer = function (isNetBeansBuffer) { + client.sendCommand("netbeansBuffer", buffId, Types.bool(isNetBeansBuffer)); }; - var save = function () { - + + /** + * Removes a previously added annotation + * @function + * @param {number} serNum Identifier of the annotation to remove + */ + this.removeAnno = function (serNum) { + client.sendCommand("removeAnno",buffId,serNum); }; - var saveDone = function () { - + /** + * Tell Vim to save the buffer + * Vim fire a setModified event when done + * @function + **/ + this.save = function () { + client.sendCommand("save", buffId); }; - var setBufferNumber = function () { - + /** + * Tell Vim the buffer has been saved + * @function + */ + this.saveDone = function () { + client.sendCommand("saveDone", buffId); }; - var setDot = function (off) { + /** + * Set Vim cursor position and set the buffer as current buffer + * @function + * @param {number|number[]} off Position of the cursor + */ + this.setDot = function (off) { + client.sendCommand("setDot", buffId, Types.position(off)); }; - var setFullName = function (pathName) { - + /** + * Set the name of the buffer. + * It does not read the file content + * @function + * @param {string} pathName Name of the buffer + */ + this.setFullName = function (pathName) { + client.sendCommand("setFullName", buffId, Types.string(pathName)); }; - var setModified = function (modified) { - + /** + * Toggle the buffer modified mark + * @function + * @param {bool} modified Value of the modified flag + */ + this.setModified = function (modified) { + client.sendCommand("setModified", buffId, Types.bool(modified)); }; - var setModtime = function (time) { - + /** + * Update a buffer modification time + * @function + * @param {number} time Modification time to set + */ + this.setModtime = function (time) { + client.sendCommand("setModtime", buffId, time); }; - var setReadOnly = function () { - + /** + * Mark the buffer as readonly + * @function + */ + this.setReadOnly = function () { + client.sendCommand("setReadOnly", buffId); }; - var setTitle = function (name) { - + /** + * Set the title of the function (used by controller only) + * @function + * @param {string} name Name of the buffer + */ + this.setTitle = function (name) { + client.sendCommand("setTitle", buffId, Types.string(name)); }; - var setVisible = function (visible) { - + /** + * Goto the buffer + * @function + */ + this.setVisible = function () { + client.sendCommand("setVisible", buffId, Types.bool(true)); }; - var showBalloon = function (text) { - + /** + * Display a balloon containing the text (GVim only) + * @function + * @param {string} text Text to show + */ + this.showBalloon = function (text) { + client.sendCommand("showBalloon", buffId,Types.string(text)); }; - var unguard = function (off, len) { - + /** + * Ungard the text + * @function + * @param {number|number[]} off Start position of the text to unguard + * @param {number} len Length of the text to unguard + */ + this.unguard = function (off, len) { + client.sendCommand("unguard", buffId, Types.position(off), len); + }; + + this.editFile = function (pathName){ + client.sendCommand("editFile", buffId, Types.string(pathName)); }; /* FUNCTIONS */ - var getCursor = function (callback) { - + this.getCursor = function (callback) { + client.callFunction("getCursor", buffId, callback); }; - var getLength = function (callback) { - + this.getLength = function (callback) { + client.callFunction("getLength", buffId, callback); }; - var getAnno = function (serNum, callback) { - + this.getAnno = function (serNum, callback) { + client.callFunction("getAnno", buffId, serNum, callback); }; - var getModified = function (callback) { - + this.getModified = function (callback) { + client.callFunction("getModified", buffId, callback); }; - var getText = function (callback) { - + this.getText = function (callback) { + client.callFunction("getText", buffId, callback); }; - var insert = function (off, text, callback){ - + this.insert = function (off, text, callback){ + client.callFunction("insert", buffId, Types.position(off), Types.string(text), callback); }; - var remove = function (off, length, callback) { - + this.remove = function (off, length, callback) { + client.callFunction("remove", buffId, Types.position(off), length, callback); }; } diff --git a/lib/client.js b/lib/controler.js similarity index 78% rename from lib/client.js rename to lib/controler.js index 2ab3754..95b1b32 100644 --- a/lib/client.js +++ b/lib/controler.js @@ -1,13 +1,15 @@ var Transform = require("stream").Transform; var EventEmitter = require("events").EventEmitter; +var Types = require("types"); +var Buffer = require("buffer"); + /** * A Transform Stream implementation that output full VNB protocol messages - * * @constructor - * @this {VNBMessageParser} + * @param {object} opts Options passed to the Transform constructor */ -function VNBMessageTransform(opts) { +function MessageTransform(opts) { "use strict"; Transform.call(this,opts); @@ -15,6 +17,7 @@ function VNBMessageTransform(opts) { /** * Transform stream Write implementation + * @function * @param {string} data Upstream data already converted as string * @param {string} encoding String encoding (should be utf8, but should be checked upward) * @param {function} callback Downstream Stream receiving only full VNB messages @@ -44,6 +47,7 @@ function VNBMessageTransform(opts) { /** * Flush still data. Remaining data is lost since it can only be an incomplete message + * @function * @param {function} callback Callback to call when data has been flushed **/ this._flush = function (callback) { @@ -61,13 +65,12 @@ function VNBMessageTransform(opts) { /** - * VNB Client implementation (communication from vim to the netbeans server) + * Client implementation for vim netbeans protocol (communication from vim to the netbeans server) * @constructor - * @this {VNBMessageClient} * @param {Socket} socket Incomming stream * @param {authenticationCallback} authentication Callback function called when the AUTH message is received */ -function VNBClient(socket, authentication) { +function Controler(socket, authentication) { "use strict"; EventEmitter.call(this); @@ -80,7 +83,7 @@ function VNBClient(socket, authentication) { //TODO check if this is needed socket.setEncoding("utf8"); - var messageTransform = new VNBMessageTransform(); + var messageTransform = new MessageTransform(); messageTransform.on("data",function (message) { @@ -234,13 +237,13 @@ function VNBClient(socket, authentication) { }); /** - * @function sendCommand * Send a command message + * @function sendCommand * @param {string} name Name of the command * @param {number} buffer Name of the buffer * @param {...string} arguments Parameters of the command */ - function sendCommand(name, buffer) { + this.sendCommand = function(name, buffer) { let seqno = currentSeqno; currentSeqno += 1; @@ -253,18 +256,18 @@ function VNBClient(socket, authentication) { data += "\n"; socket.write(data); - } + }; /** - * @function callFunction * Send a function message + * @function callFunction * @param {string} name Name of the function * @param {number} buffer ID of the buffer * @param {...string} arguments Parameters of the function. Use the string, bool, color and position function * of the method to convert javascript types to string * @param {function} callback Callback function called with reply value * */ - function callFunction (name, buffer) { + this.callFunction = function(name, buffer) { let seqno = currentSeqno; currentSeqno += 1; @@ -287,51 +290,94 @@ function VNBClient(socket, authentication) { socket.write(data); - } + }; socket.pipe(messageTransform); +} +InternalClient.prototype = EventEmitter; +/** + * Connexion with a vim client + * @constructor + * @param {Socket} socket Connexion's socket + * @param {authenticationCallback} authentication Callback used to authenticate the client + */ +function Client(socket, authentication) { + "use strict"; + + var client = new Controler(socket, authentication); + var buffId = 0; + /* GLOBAL COMMANDS */ - var startAtomic = function () { - + this.startAtomic = function () { + client.sendCommand("startAtomic", 0); }; - var endAtomic = function() { - + this.endAtomic = function() { + client.sendCommand("endAtmoic", 0); }; - var raise = function () { - + this.raise = function () { + client.sendCommand("raise", 0); }; - var setExitDelay = function (secondes) { - + this.setExitDelay = function (seconds) { + client.sendCommand("setExitDelay", 0, seconds); }; - var specialKeys = function () { - + this.specialKeys = function (keys) { + client.sendCommand("specialKeys", 0,Types.string(keys)); //TODO : Wild guess, no documentation about this one }; - var create = function() { - + this.create = function() { + var buffer = new Buffer(client, buffId); + buffId += 1; + client.sendCommand("create", buffId); + return buffer; }; - var editFile = function (pathName){ + /** + * putBufferNumber + * @function + * @param {string} pathname Path of the file whose buffer is identified + * @return {undefined} + */ + this.putBufferNumber = function (pathname) { + var buffer = new Buffer(client, buffId); + buffId += 1; + client.sendCommand("putBufferNumber", buffId, Types.string(pathname)); //TODO : May be better on the buffer + return buffer; + }; + /** + * Set a buffer number to a file opened by Vim and sets its buffer as current buffer + * @function + * @param {string} pathname Path of the file + */ + this.setBufferNumber = function (pathname) { + var buffer = new Buffer(client, buffId); + buffId += 1; + client.sendCommand("setBufferNumber", buffId, Types.string(pathname)); + return buffer; }; /* GLOBAL FUNCTIONS */ - var saveAndExit = function (callback) { - + /** + * saveAndExit + * + * @param callback + * @return {undefined} + */ + this.saveAndExit = function (callback) { + client.callFunction("saveAndExit", 0, callback); }; - var getModified = function (callback) { - + this.getModified = function (callback) { + client.callFunction("getModified", 0, callback); }; - } -VNBClient.prototype = EventEmitter; +module.exports.Controler = Client; diff --git a/lib/server.js b/lib/server.js new file mode 100644 index 0000000..3f9f7a2 --- /dev/null +++ b/lib/server.js @@ -0,0 +1,19 @@ +var net = require("net"); +var Controler = require("controler"); + + +function createServer(host, port, callback) { + "use strict"; + + var server = net.createServer(function (connexion) { + var ctrl = new Controler(connexion, function(pwd) { + return callback(ctrl, pwd); + }); + + }); + + server.listen(host,port, function () { + + + }); +} diff --git a/lib/types.js b/lib/types.js index 59e9659..94f9059 100644 --- a/lib/types.js +++ b/lib/types.js @@ -1,6 +1,6 @@ /** - * @function color * Convert a color to message string + * @function color * @param {string|number} value Color to convert * @return {string} Converted color */ @@ -11,8 +11,8 @@ function color (value) { } /** - * @function bool * Convert a boolean value to message string + * @function bool * @param {bool} value Boolean to convert * @return {string} Converted boolean */ @@ -23,21 +23,26 @@ function bool (value) { } /** - * @function position * Convert a position to message string + * @function position * @param {number} line Line of position starting with 1 * @param {number} col Column of position starting with 0 * @return {string} Converted position */ -function position (line, col) { +function position (pos) { "use strict"; - return line+"/"+col; + if (Array.isArray(pos)) { + return pos[0]+"/"+pos[1]; + } + else { + return pos; + } } /** - * @function string * Convert a string to message string + * @function string * @param {string} value String to convert * @return {string} Converted string */