"use babel";

import _ from "lodash";

function isMappedToGrammer(grammar) {
  return (builder) => {
    console.log(builder);
    return _.includes(builder.grammars, grammar);
  };
}

export default {
  activate() {
    this.commands = atom.commands.add("atom-workspace", {
      "gluon:build": () => { this.build(); },
    });
  },

  deactivate() {
    this.commands.dispose();
  },

  build() {
    const builder = this.builderForActiveTextEditor();
    if (!builder) { return; }

    builder.buildFunction();
  },

  provideBuildService() {
    const self = this;
    return {
      register(registration) {
        if (!registration) { return; }

        if (!self.builders) {
          self.builders = {};
        }

        self.builders[registration.name] = registration;
      },
    };
  },

  builderForActiveTextEditor() {
    const grammar = this.getActiveGrammar();
    if (!grammar) { return null; }

    const builder = _.find(this.builders, isMappedToGrammer(grammar));
    return builder;
  },

  getActiveGrammar() {
    const editor = this.getActiveTextEditor();
    if (editor) {
      return editor.getGrammar().scopeName;
    }

    return null;
  },

  getActiveTextEditor() {
    return atom.workspace.getActiveTextEditor();
  },
};

@@ -384,141 +384,8 @@
 %7D,%0A%0A
-  build() %7B%0A    const builder = this.builderForActiveTextEditor();%0A    if (!builder) %7B return; %7D%0A%0A    builder.buildFunction();%0A  %7D,%0A%0A
   pr
@@ -521,21 +521,16 @@
         
-if (!
 self.bui
@@ -538,21 +538,10 @@
 ders
-) %7B%0A        
  
+=
  sel
@@ -555,25 +555,15 @@
 ers 
-=
+%7C%7C
  %7B%7D;%0A
-        %7D%0A%0A
     
@@ -637,16 +637,149 @@
 ;%0A  %7D,%0A%0A
+  build() %7B%0A    const builder = this.builderForActiveTextEditor();%0A    if (!builder) %7B return; %7D%0A%0A    builder.buildFunction();%0A  %7D,%0A%0A
   builde
@@ -1059,16 +1059,17 @@
     if (
+!
 editor) 
@@ -1069,19 +1069,33 @@
 ditor) %7B
-%0A  
+ return null; %7D%0A%0A
     retu
@@ -1132,32 +1132,8 @@
 me;%0A
-    %7D%0A%0A    return null;%0A
   %7D,

Tweak formatting, nesting, and ordering