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.

1  
sclass LoadableUtilsHelper {
2  
  transient settable S exampleProgramID = #1031393;
3  
  transient settable S loadableUtilsSnippetID = #1030952;
4  
  transient settable S includeSnippetID = #1030953;
5  
  transient settable S globalSnippetID = #1020737;
6  
  transient settable bool onlyImportDefined = true;
7  
  
8  
  // for classes and functions
9  
  transient settable IPred<S> dontDelegatePredicate = name -> false;
10  
  
11  
  transient S transpilerOutput;
12  
  transient Set<S> requestedFunctions, requestedClasses;
13  
  transient Set<S> have, haveClasses;
14  
  transient new Set<S> blockedFunctions;
15  
  transient new Set<S> blockedClasses;
16  
  transient Set<S> newFunctions, newClasses;
17  
  transient new Set<S> imported;
18  
  transient Set<S> importedClasses;
19  
  transient Set<S> toImport, classesToImport;
20  
  transient new SS modifiers;
21  
  
22  
  selfType transpilerOutput(S transpilerOutput) {
23  
    this.transpilerOutput = transpilerOutput;
24  
    this;
25  
  }
26  
  
27  
  S getTranspilerOutput() {
28  
    new RunTranspiler rt;
29  
    rt.onPreparingTranspiler(transpiler ->
30  
      setOpt(transpiler, printMapLikes := true));
31  
    ret transpilerOutput = hijackPrint(-> rt.get(loadSnippet(exampleProgramID)));
32  
  }
33  
  
34  
  void prepare {
35  
    if (transpilerOutput == null)
36  
      getTranspilerOutput();
37  
      
38  
    LS transpilerLines = mapLines(transpilerOutput, s -> dropLeadingAngleBracketStuff(s));
39  
    requestedFunctions = asSet(startingWithIC_dropAndTrim("Compiling function:", transpilerLines);
40  
    requestedClasses = asSet(startingWithIC_dropAndTrim("Compiling class:", transpilerLines);
41  
    
42  
    print(n2(requestedFunctions, "requested function"));
43  
    print(n2(requestedClasses, "requested class", "requested classes"));
44  
    
45  
    for (PairS p : parseColonPropertyPairs(lines(transpilerLines)))
46  
      if (functionAdjuvantKeywords().contains(p.a))
47  
        modifiers.put(p.b, p.a);
48  
    print(n2(modifiers, "function modifier"));
49  
        
50  
    S text = loadSnippet(loadableUtilsSnippetID);
51  
    have = asSet(lmap tok_lastIdentifier(jextractAll("please include function <id>.", text)));
52  
    haveClasses = asSet(lmap tok_lastIdentifier(jextractAll("please include class <id>.", text)));
53  
    
54  
    print("Have " + n2(have, "function"));
55  
    
56  
    S includeSnippet = joinNemptiesWithEmptyLines(
57  
      loadSnippet(includeSnippetID),
58  
      loadSnippet(globalSnippetID));
59  
    
60  
    new Matches m;
61  
    for (S comment : trimAll(getJavaLineComments(text))) {
62  
      if (find3("don't include function *", comment, m))
63  
        blockedFunctions.add($1);
64  
      if (find3("don't include class *", comment, m))
65  
        blockedClasses.add($1);
66  
    }
67  
        
68  
    for (S comment : trimAll(getJavaLineComments(includeSnippet)))
69  
      if (swic(comment, "don't import "))
70  
        blockedFunctions.add(tok_lastIdentifier(comment));
71  
        
72  
    for (S s : jextractAll(javaTok(includeSnippet), "please include function <id>."))
73  
      blockedFunctions.add(tok_lastIdentifier(s));
74  
      
75  
    print(n2(blockedFunctions, "blocked function") + ": " + blockedFunctions);
76  
    print(n2(blockedClasses, "blocked class", "blocked classes") + ": " + blockedClasses);
77  
    
78  
    Set<S> shouldHave = setMinusSet(requestedFunctions, blockedFunctions);
79  
    
80  
    newFunctions = setMinusSet(shouldHave, have);
81  
    newFunctions = antiFilterToSet(dontDelegatePredicate, newFunctions);
82  
    print(n2(newFunctions, "new function"));
83  
    
84  
    Set<S> shouldHaveClasses = setMinusSet(requestedClasses, blockedClasses);
85  
    newClasses = setMinusSet(shouldHaveClasses, haveClasses);
86  
    newClasses = antiFilterToSet(dontDelegatePredicate, newClasses);
87  
    print(n2(newClasses, "new class", "new classes"));
88  
    
89  
    for (S s : tlft(includeSnippet)) {
90  
      s = simpleSpaces(s);
91  
      if (startsWith(s, "import static "))
92  
        imported.add(tok_lastIdentifier(s));
93  
    }
94  
    print(n2(imported, "imported function"));
95  
    
96  
    importedClasses = asSet(lmap tok_lastIdentifier(jextractAll("import loadableUtils.utils.<id>;", includeSnippet)));
97  
    print(n2(importedClasses, "imported class", "imported classes"));
98  
    
99  
    toImport = toTreeSet(setMinusSet(onlyImportDefined ? have : shouldHave, imported));
100  
    toImport = antiFilterToSet(dontDelegatePredicate, toImport);
101  
    toImport = setMinusSet(toImport, blockedFunctions);
102  
    print(n2(toImport) + " function(s) to import");
103  
    
104  
    classesToImport = toTreeSet(setMinusSet(onlyImportDefined ? haveClasses : shouldHaveClasses, importedClasses));
105  
    print(n2(classesToImport) + " class(es) to import");
106  
  }
107  
  
108  
  LPairS suggestedAppends() {
109  
    ret ll(pair(loadableUtilsSnippetID,
110  
      mapToLines(newFunctions, f -> "please include function " + f + ".")
111  
      + mapToLines(newClasses, c -> "please include class " + c + ".")),
112  
      pair(includeSnippetID,
113  
        mapToLines(toImport, f -> "import static loadableUtils.utils."
114  
          + appendSpaceIfNempty(modifiers.get(f))
115  
          + f + ";")
116  
        + mapToLines(classesToImport, c -> "import loadableUtils.utils." + c + ";")
117  
      )
118  
    );
119  
  }
120  
}

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: 224 / 403
Version history: 26 change(s)
Referenced in: [show references]