Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
- Handled NvimListenAddressException to allow genManifest
Browse files Browse the repository at this point in the history
- Cleaned up a few imports
  • Loading branch information
viniarck committed Jan 8, 2019
1 parent a77d7b9 commit 6489f77
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
21 changes: 14 additions & 7 deletions source/nvimhost/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ struct MethodInfo {
}

struct NvimClient {
import std.stdio;
import std.socket;
import std.process : environment;
import std.typecons;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
55 changes: 30 additions & 25 deletions source/nvimhost/plugin.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -92,7 +92,6 @@ private:
*/
void cbThread() {
import msgpack;
import std.file;
import std.uuid;

srcAddr = genSrcAddr();
Expand Down Expand Up @@ -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));
Expand All @@ -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();
}

Expand All @@ -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();
}
*/
Expand All @@ -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
Expand Down

0 comments on commit 6489f77

Please sign in to comment.