Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

120
LINES

< > BotCompany Repo | #1034511 // LoadableUtilsHelper

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (12927L) is out of date.

sclass LoadableUtilsHelper {
  transient settable S exampleProgramID = #1031393;
  transient settable S loadableUtilsSnippetID = #1030952;
  transient settable S includeSnippetID = #1030953;
  transient settable S globalSnippetID = #1020737;
  transient settable bool onlyImportDefined = true;
  
  // for classes and functions
  transient settable IPred<S> dontDelegatePredicate = name -> false;
  
  transient S transpilerOutput;
  transient Set<S> requestedFunctions, requestedClasses;
  transient Set<S> have, haveClasses;
  transient new Set<S> blockedFunctions;
  transient new Set<S> blockedClasses;
  transient Set<S> newFunctions, newClasses;
  transient new Set<S> imported;
  transient Set<S> importedClasses;
  transient Set<S> toImport, classesToImport;
  transient new SS modifiers;
  
  selfType transpilerOutput(S transpilerOutput) {
    this.transpilerOutput = transpilerOutput;
    this;
  }
  
  S getTranspilerOutput() {
    new RunTranspiler rt;
    rt.onPreparingTranspiler(transpiler ->
      setOpt(transpiler, printMapLikes := true));
    ret transpilerOutput = hijackPrint(-> rt.get(loadSnippet(exampleProgramID)));
  }
  
  void prepare {
    if (transpilerOutput == null)
      getTranspilerOutput();
      
    LS transpilerLines = mapLines(transpilerOutput, s -> dropLeadingAngleBracketStuff(s));
    requestedFunctions = asSet(startingWithIC_dropAndTrim("Compiling function:", transpilerLines);
    requestedClasses = asSet(startingWithIC_dropAndTrim("Compiling class:", transpilerLines);
    
    print(n2(requestedFunctions, "requested function"));
    print(n2(requestedClasses, "requested class", "requested classes"));
    
    for (PairS p : parseColonPropertyPairs(lines(transpilerLines)))
      if (functionAdjuvantKeywords().contains(p.a))
        modifiers.put(p.b, p.a);
    print(n2(modifiers, "function modifier"));
        
    S text = loadSnippet(loadableUtilsSnippetID);
    have = asSet(lmap tok_lastIdentifier(jextractAll("please include function <id>.", text)));
    haveClasses = asSet(lmap tok_lastIdentifier(jextractAll("please include class <id>.", text)));
    
    print("Have " + n2(have, "function"));
    
    S includeSnippet = joinNemptiesWithEmptyLines(
      loadSnippet(includeSnippetID),
      loadSnippet(globalSnippetID));
    
    new Matches m;
    for (S comment : trimAll(getJavaLineComments(text))) {
      if (find3("don't include function *", comment, m))
        blockedFunctions.add($1);
      if (find3("don't include class *", comment, m))
        blockedClasses.add($1);
    }
        
    for (S comment : trimAll(getJavaLineComments(includeSnippet)))
      if (swic(comment, "don't import "))
        blockedFunctions.add(tok_lastIdentifier(comment));
        
    for (S s : jextractAll(javaTok(includeSnippet), "please include function <id>."))
      blockedFunctions.add(tok_lastIdentifier(s));
      
    print(n2(blockedFunctions, "blocked function") + ": " + blockedFunctions);
    print(n2(blockedClasses, "blocked class", "blocked classes") + ": " + blockedClasses);
    
    Set<S> shouldHave = setMinusSet(requestedFunctions, blockedFunctions);
    
    newFunctions = setMinusSet(shouldHave, have);
    newFunctions = antiFilterToSet(dontDelegatePredicate, newFunctions);
    print(n2(newFunctions, "new function"));
    
    Set<S> shouldHaveClasses = setMinusSet(requestedClasses, blockedClasses);
    newClasses = setMinusSet(shouldHaveClasses, haveClasses);
    newClasses = antiFilterToSet(dontDelegatePredicate, newClasses);
    print(n2(newClasses, "new class", "new classes"));
    
    for (S s : tlft(includeSnippet)) {
      s = simpleSpaces(s);
      if (startsWith(s, "import static "))
        imported.add(tok_lastIdentifier(s));
    }
    print(n2(imported, "imported function"));
    
    importedClasses = asSet(lmap tok_lastIdentifier(jextractAll("import loadableUtils.utils.<id>;", includeSnippet)));
    print(n2(importedClasses, "imported class", "imported classes"));
    
    toImport = toTreeSet(setMinusSet(onlyImportDefined ? have : shouldHave, imported));
    toImport = antiFilterToSet(dontDelegatePredicate, toImport);
    toImport = setMinusSet(toImport, blockedFunctions);
    print(n2(toImport) + " function(s) to import");
    
    classesToImport = toTreeSet(setMinusSet(onlyImportDefined ? haveClasses : shouldHaveClasses, importedClasses));
    print(n2(classesToImport) + " class(es) to import");
  }
  
  LPairS suggestedAppends() {
    ret ll(pair(loadableUtilsSnippetID,
      mapToLines(newFunctions, f -> "please include function " + f + ".")
      + mapToLines(newClasses, c -> "please include class " + c + ".")),
      pair(includeSnippetID,
        mapToLines(toImport, f -> "import static loadableUtils.utils."
          + appendSpaceIfNempty(modifiers.get(f))
          + f + ";")
        + mapToLines(classesToImport, c -> "import loadableUtils.utils." + c + ";")
      )
    );
  }
}

Author comment

Began life as a copy of #1030951

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034511
Snippet name: LoadableUtilsHelper
Eternal ID of this version: #1034511/27
Text MD5: 57b1164438d992e01b0b7c01b2203b1f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-27 11:41:01
Source code size: 5046 bytes / 120 lines
Pitched / IR pitched: No / No
Views / Downloads: 102 / 250
Version history: 26 change(s)
Referenced in: [show references]