from .formatter import *


class FormatterRegistry():
    def __init__(self):
        self.__formatters = []

    def populate(self):
        self.__formatters = [
            ClangFormat(), ElmFormat(), GoFormat(), JavaScriptFormat(),
            JsonFormat(), PythonFormat(), RustFormat(), TerraformFormat()
        ]

    @property
    def all(self):
        return self.__formatters

    @property
    def enabled(self):
        return [x for x in self.all if x.format_on_save]

    def find(self, predicate, default=None):
        return next((x for x in self.all if predicate(x)), default)

    def by_view(self, view):
        source = view.scope_name(0).split(' ')[0].split('.')[1]
        return self.find(lambda x: source in x.sources)

    def by_name(self, name):
        return self.find(lambda x: x.name == name)

from .formatter import *


class FormatterRegistry():
    def __init__(self):
        self.__formatters = []
        self.__formatter_source_map = {}

    def populate(self):
        self.__formatters = [
            ClangFormat(), ElmFormat(), GoFormat(), JavaScriptFormat(),
            JsonFormat(), PythonFormat(), RustFormat(), TerraformFormat()
        ]
        self.__formatter_source_map = dict((source, formatter)
                                           for formatter in self.__formatters
                                           for source in formatter.sources)

    @property
    def all(self):
        return self.__formatters

    @property
    def enabled(self):
        return [x for x in self.all if x.format_on_save]

    def by_view(self, view):
        source = view.scope_name(0).split(' ')[0].split('.')[1]
        return self.__formatter_source_map.get(source)

    def by_name(self, name):
        return next((x for x in self.all if x.name == name))

Speed up lookup of formatter by source type
Speed up lookup of formatter by source type

  from .formatter import *
  
  
  class FormatterRegistry():
      def __init__(self):
          self.__formatters = []
+         self.__formatter_source_map = {}
  
      def populate(self):
          self.__formatters = [
              ClangFormat(), ElmFormat(), GoFormat(), JavaScriptFormat(),
              JsonFormat(), PythonFormat(), RustFormat(), TerraformFormat()
          ]
+         self.__formatter_source_map = dict((source, formatter)
+                                            for formatter in self.__formatters
+                                            for source in formatter.sources)
  
      @property
      def all(self):
          return self.__formatters
  
      @property
      def enabled(self):
          return [x for x in self.all if x.format_on_save]
  
-     def find(self, predicate, default=None):
-         return next((x for x in self.all if predicate(x)), default)
- 
      def by_view(self, view):
          source = view.scope_name(0).split(' ')[0].split('.')[1]
-         return self.find(lambda x: source in x.sources)
+         return self.__formatter_source_map.get(source)
  
      def by_name(self, name):
-         return self.find(lambda x: x.name == name)
+         return next((x for x in self.all if x.name == name))
  
Speed up lookup of formatter by source type
## Code Before:
from .formatter import *


class FormatterRegistry():
    def __init__(self):
        self.__formatters = []

    def populate(self):
        self.__formatters = [
            ClangFormat(), ElmFormat(), GoFormat(), JavaScriptFormat(),
            JsonFormat(), PythonFormat(), RustFormat(), TerraformFormat()
        ]

    @property
    def all(self):
        return self.__formatters

    @property
    def enabled(self):
        return [x for x in self.all if x.format_on_save]

    def find(self, predicate, default=None):
        return next((x for x in self.all if predicate(x)), default)

    def by_view(self, view):
        source = view.scope_name(0).split(' ')[0].split('.')[1]
        return self.find(lambda x: source in x.sources)

    def by_name(self, name):
        return self.find(lambda x: x.name == name)

## Instruction:
Speed up lookup of formatter by source type
## Code After:
from .formatter import *


class FormatterRegistry():
    def __init__(self):
        self.__formatters = []
        self.__formatter_source_map = {}

    def populate(self):
        self.__formatters = [
            ClangFormat(), ElmFormat(), GoFormat(), JavaScriptFormat(),
            JsonFormat(), PythonFormat(), RustFormat(), TerraformFormat()
        ]
        self.__formatter_source_map = dict((source, formatter)
                                           for formatter in self.__formatters
                                           for source in formatter.sources)

    @property
    def all(self):
        return self.__formatters

    @property
    def enabled(self):
        return [x for x in self.all if x.format_on_save]

    def by_view(self, view):
        source = view.scope_name(0).split(' ')[0].split('.')[1]
        return self.__formatter_source_map.get(source)

    def by_name(self, name):
        return next((x for x in self.all if x.name == name))
