package com.reason.bs.hints;

import com.reason.lang.core.HMSignature;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

public class InferredTypesImplementation implements BsQueryTypesService.InferredTypes {
    private final Map<String, HMSignature> m_let = new HashMap<>();
    private final Map<String, InferredTypesImplementation> m_modules = new HashMap<>();

    public void add(String type) {
        try {
            if (type.startsWith("val")) {
                int colonPos = type.indexOf(':');
                if (0 < colonPos && colonPos < type.length()) {
                    m_let.put(type.substring(4, colonPos - 1), new HMSignature(true, type.substring(colonPos + 1)));
                }
            } else if (type.startsWith("module")) {
                int colonPos = type.indexOf(':');
                if (0 <= colonPos) {
                    int sigPos = type.indexOf("sig");
                    InferredTypesImplementation moduleTypes = new InferredTypesImplementation();
                    m_modules.put(type.substring(7, colonPos - 1), moduleTypes);
                    String sigTypes = type.substring(sigPos + 3, type.length() - 3);
                    String[] moduleSigTypes = sigTypes.trim().split("(?=module|val|type)");
                    for (String moduleSigType : moduleSigTypes) {
                        moduleTypes.add(moduleSigType);
                    }
                }
            }
        } catch (Error e) {
            Logger.getLogger("ReasonML.types").error(e);
        }
    }

    public HMSignature getLetType(String name) {
        return m_let.get(name);
    }

    public BsQueryTypesService.InferredTypes getModuleType(String name) {
        return m_modules.get(name);
    }
}

@@ -38,68 +38,83 @@
 com.
-reason.lang.core.HMSignature;%0Aimport org.apache.log4j.Logger
+intellij.openapi.diagnostic.Logger;%0Aimport com.reason.lang.core.HMSignature
 ;%0A%0Ai
@@ -1117,16 +1117,172 @@
 Types);%0A
+                    int beginIndex = sigPos + 3;%0A                    int endIndex = type.length() - 3;%0A                    if (beginIndex %3C endIndex) %7B%0A    
         
@@ -1330,40 +1330,35 @@
 ing(
-sigPos + 3, type.length() - 3);%0A
+beginIndex, endIndex);%0A    
     
@@ -1465,16 +1465,20 @@
         
+   
+ 
 for (Str
@@ -1539,16 +1539,20 @@
         
+    
 moduleTy
@@ -1571,24 +1571,28 @@
 leSigType);%0A
+    
             
@@ -1593,32 +1593,54 @@
               %7D%0A
+                    %7D%0A
                 
@@ -1676,13 +1676,24 @@
 ch (
-Error
+RuntimeException
  e) 
@@ -1716,22 +1716,24 @@
 gger.get
-Logger
+Instance
 (%22Reason
@@ -1749,16 +1749,60 @@
 ).error(
+%22Error while decoding type %5B%22 + type + %22%5D%22, 
 e);%0A    

fix a stack trace