From 6489f77bb0d21085d4850951760ebe15b428a3b2 Mon Sep 17 00:00:00 2001 From: Vinicius Arcanjo Date: Mon, 7 Jan 2019 23:07:20 -0200 Subject: [PATCH] - Handled NvimListenAddressException to allow genManifest - Cleaned up a few imports --- source/nvimhost/client.d | 21 ++++++++++----- source/nvimhost/plugin.d | 55 ++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/source/nvimhost/client.d b/source/nvimhost/client.d index 5e5d24e..eb07ac1 100644 --- a/source/nvimhost/client.d +++ b/source/nvimhost/client.d @@ -61,7 +61,6 @@ struct MethodInfo { } struct NvimClient { - import std.stdio; import std.socket; import std.process : environment; import std.typecons; @@ -71,7 +70,7 @@ struct NvimClient { import std.experimental.logger; import vibe.core.net; import eventcore.driver : IOMode; - import nvimhost.util; + import nvimhost.util : genSrcAddr; public: string nvimAddr; @@ -382,16 +381,18 @@ public: } /** - Open an async TCP connection handler to Nvim using UnixAddress + Open an async TCP connection handler to Nvim using UnixAddress. + + If NVIM_LISTEN_ADDRESS environment variable is not set throws + NvimListenAddressException. + */ void connect() { import std.path; - import std.conv; - import std.uuid; - import std.file; + this.nvimAddr = environment.get("NVIM_LISTEN_ADDRESS", ""); if (nvimAddr == "") { - throw new Exception("Couldn't get NVIM_LISTEN_ADDRESS, is nvim running?"); + throw new NvimListenAddressException("Couldn't get NVIM_LISTEN_ADDRESS, is nvim running?"); } auto unixAddr = new UnixAddress(nvimAddr); @@ -403,3 +404,9 @@ public: tracef(logEnabled, "Main thread connected to nvim"); } } + +class NvimListenAddressException : Exception { + this(string msg, string file = __FILE__, size_t line = __LINE__) { + super(msg, file, line); + } +} diff --git a/source/nvimhost/plugin.d b/source/nvimhost/plugin.d index 4deeb16..b054c7c 100644 --- a/source/nvimhost/plugin.d +++ b/source/nvimhost/plugin.d @@ -29,7 +29,6 @@ exclusively used for exchanging call messages between the Plugin functions, comm struct NvimPlugin(Struct) { import nvimhost.client; import nvimhost.api; - import std.stdio; import std.traits; import std.meta : ApplyLeft, Filter; import std.variant; @@ -42,8 +41,9 @@ struct NvimPlugin(Struct) { import std.uni : toLower; import vibe.core.net; import eventcore.driver : IOMode; - import nvimhost.util; + import nvimhost.util : genSrcAddr; import std.path; + import std.file; public: // Encapsulate Nvim API to facilitate for final consumers of this Class. @@ -92,7 +92,6 @@ private: */ void cbThread() { import msgpack; - import std.file; import std.uuid; srcAddr = genSrcAddr(); @@ -287,8 +286,6 @@ private: void ensureDirCreated(string filePath) { import std.range; - import std.file; - import std.path; auto folderPath = filePath.expandTilde.split(dirSeparator)[0 .. $ - 1]; mkdirRecurse(folderPath.join(dirSeparator)); @@ -299,28 +296,39 @@ public: this(string pluginBinPath, string outManifestFilePath) { c = nvim.getClient(); c.enableLog(); - c.connect(); - stc = Struct(nvim); ensureDirCreated(outManifestFilePath); genManifest!Struct(pluginBinPath, expandTilde(outManifestFilePath)); - mTid = thisTid; - auto tcb = new Thread(&cbThread); - tcb.isDaemon(true); - tcb.start(); - - trace(c.logEnabled, "Waiting for cbThread message"); - receiveOnly!string(); - trace(c.logEnabled, "Received cbThread message"); - - immutable string loadedName = Struct.stringof.toLower; - logf(c.logEnabled, "Setting g:" ~ loadedName ~ "_channel" ~ "=" ~ to!string(chId)); - c.call!(void)("nvim_command", "let g:" ~ loadedName ~ "_channel" ~ "=" ~ to!string(chId)); - trace(c.logEnabled, "Plugin " ~ loadedName ~ " is ready!"); + try { + c.connect(); + stc = Struct(nvim); + + mTid = thisTid; + auto tcb = new Thread(&cbThread); + tcb.isDaemon(true); + tcb.start(); + + trace(c.logEnabled, "Waiting for cbThread message"); + receiveOnly!string(); + trace(c.logEnabled, "Received cbThread message"); + + immutable string loadedName = Struct.stringof.toLower; + logf(c.logEnabled, "Setting g:" ~ loadedName ~ "_channel" ~ "=" ~ to!string(chId)); + c.call!(void)("nvim_command", "let g:" ~ loadedName ~ "_channel" ~ "=" ~ to!string(chId)); + trace(c.logEnabled, "Plugin " ~ loadedName ~ " is ready!"); + } catch(NvimListenAddressException e) { + import std.stdio : writeln; + import core.stdc.stdlib; + + writeln(e.msg); + error(c.logEnabled, e.msg); + writeln("The manifest file has been written to " ~ expandTilde(outManifestFilePath)); + exit(1); + } } - ~this(){ + ~this() { close(); } @@ -342,7 +350,7 @@ public: You should only call keepRunning if you don't have an inifinite loop in your void main(), for example: void main(){ - auto p = new NvimPlugin!MyPluginClassHere; + auto p = new NvimPlugin!(PluginStruct)(); scope(exit) p.keepRunning(); } */ @@ -363,9 +371,6 @@ public: void genManifest(Struct)(string binExecutable, string outputFile, string autoCmdPattern = "*") { import std.stdio : writeln, File; import std.string : format; - import std.uni : toLower; - import std.file; - import std.path; immutable string loadedName = Struct.stringof.toLower; // 1 -> loadedName