#!/usr/bin/env node

console.log("vim-js-alternate: initialized");

import ProjectionLoader from "./ProjectionLoader";
import { ProjectionResolver } from "./ProjectionResolver";

var Vim = require("vim-node-driver");
var vim = new Vim();

var projectionLoader = new ProjectionLoader();
var projectionResolver = new ProjectionResolver(projectionLoader);

var currentBuffer;
var currentAlternateFile = null;

var alternateCache = {};

vim.on("BufEnter", (args) => {
    currentBuffer = args.currentBuffer;
    if(currentBuffer) {
        currentAlternateFile = projectionResolver.getAlternate(currentBuffer);
        console.log("Resolving projection for: " + currentBuffer + " | " + projectionResolver.getAlternate(currentBuffer));
    }
});

vim.addCommand("Alternate", () => {

    var alternateFile = currentAlternateFile;

    // If not resolved, try the cache
    if(!alternateFile)
        alternateFile = alternateCache[currentBuffer];

    if(alternateFile) {
        // Store a reverse mapping so we can jump back easily
        alternateCache[alternateFile] = currentBuffer;
        vim.exec("edit " + alternateFile) 
    }

});

console.log("command registered")

#!/usr/bin/env node

console.log("vim-js-alternate: initialized");

import ProjectionLoader from "./ProjectionLoader";
import { ProjectionResolver } from "./ProjectionResolver";

var Vim = require("vim-node-driver");
var vim = new Vim();

var projectionLoader = new ProjectionLoader();
var projectionResolver = new ProjectionResolver(projectionLoader);

var currentAlternateFile = null;

vim.on("BufEnter", (args) => {
    var currentBuffer = args.currentBuffer;
    if(currentBuffer) {
        currentAlternateFile = projectionResolver.getAlternate(currentBuffer);
        console.log("Resolving projection for: " + currentBuffer + " | " + projectionResolver.getAlternate(currentBuffer));
    }
});

vim.addCommand("Alternate", () => {
    if(currentAlternateFile)
        vim.exec("edit " + currentAlternateFile)

});

console.log("command registered")

--- 
+++ 
@@ -11,10 +11,13 @@
 var projectionLoader = new ProjectionLoader();
 var projectionResolver = new ProjectionResolver(projectionLoader);
 
+var currentBuffer;
 var currentAlternateFile = null;
 
+var alternateCache = {};
+
 vim.on("BufEnter", (args) => {
-    var currentBuffer = args.currentBuffer;
+    currentBuffer = args.currentBuffer;
     if(currentBuffer) {
         currentAlternateFile = projectionResolver.getAlternate(currentBuffer);
         console.log("Resolving projection for: " + currentBuffer + " | " + projectionResolver.getAlternate(currentBuffer));
@@ -22,8 +25,18 @@
 });
 
 vim.addCommand("Alternate", () => {
-    if(currentAlternateFile)
-        vim.exec("edit " + currentAlternateFile)
+
+    var alternateFile = currentAlternateFile;
+
+    // If not resolved, try the cache
+    if(!alternateFile)
+        alternateFile = alternateCache[currentBuffer];
+
+    if(alternateFile) {
+        // Store a reverse mapping so we can jump back easily
+        alternateCache[alternateFile] = currentBuffer;
+        vim.exec("edit " + alternateFile) 
+    }
 
 });
 
Set up an alternate file cache if the reverse mapping isn't explicitly
defined
