|
31 | 31 |
|
32 | 32 | import java.io.IOException; |
33 | 33 | import java.io.StringReader; |
| 34 | +import java.net.URISyntaxException; |
34 | 35 | import java.util.ArrayList; |
35 | 36 | import java.util.Arrays; |
36 | 37 | import java.util.List; |
|
40 | 41 | import java.util.concurrent.ExecutionException; |
41 | 42 | import java.util.concurrent.Executor; |
42 | 43 | import java.util.concurrent.ExecutorService; |
| 44 | +import java.util.function.Function; |
43 | 45 | import java.util.stream.Collectors; |
44 | 46 | import org.apache.logging.log4j.LogManager; |
45 | 47 | import org.apache.logging.log4j.Logger; |
@@ -130,17 +132,92 @@ public RascalLanguageServices(RascalTextDocumentService docService, BaseWorkspac |
130 | 132 | this.workspaceService = workspaceService; |
131 | 133 | } |
132 | 134 |
|
133 | | - public InterruptibleFuture<@Nullable IConstructor> getSummary(ISourceLocation occ, PathConfig pcfg) { |
| 135 | + static String pathToModuleName(ISourceLocation l) { |
| 136 | + var p = l.getPath(); |
| 137 | + if (isInsideJar(l)) { |
| 138 | + p = jarFilePath(l); |
| 139 | + } |
| 140 | + return p.substring(1, p.lastIndexOf('.')).replace("/", "::"); |
| 141 | + } |
| 142 | + |
| 143 | + private static boolean isInsideJar(ISourceLocation l) { |
| 144 | + return l.getScheme().startsWith("jar+"); |
| 145 | + } |
| 146 | + |
| 147 | + static @Nullable ISourceLocation libraryTplLocation(ISourceLocation modPath) { |
134 | 148 | try { |
135 | | - IString moduleName = VF.string(pcfg.getModuleName(occ)); |
136 | | - return runEvaluator("Rascal makeSummary", semanticEvaluator, eval -> { |
137 | | - IConstructor result = (IConstructor) eval.call("makeSummary", moduleName, pcfg.asConstructor()); |
138 | | - return result != null && result.asWithKeywordParameters().hasParameters() ? result : null; |
139 | | - }, null, exec, false, client); |
140 | | - } catch (IOException e) { |
141 | | - logger.error("Error looking up module name from source location {}", occ, e); |
142 | | - return InterruptibleFuture.completedFuture(null, exec); |
| 149 | + var tplFolder = libraryTplRoot(modPath); |
| 150 | + if (tplFolder != null) { |
| 151 | + var modPrefix = libraryModulePrefix(modPath); |
| 152 | + var tplFileName = "$" + URIUtil.getLocationName(URIUtil.changeExtension(modPath, "tpl")); |
| 153 | + return URIUtil.getChildLocation(URIUtil.getChildLocation(tplFolder, modPrefix), tplFileName); |
| 154 | + } |
| 155 | + } catch (URISyntaxException e) { |
| 156 | + logger.error("Error while finding TPL for {}", modPath, e); |
143 | 157 | } |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + private static @Nullable ISourceLocation libraryTplRoot(ISourceLocation modPath) throws URISyntaxException { |
| 162 | + modPath = Locations.toPhysicalIfPossible(modPath); // resolve logical paths like `std:///` |
| 163 | + if (isInsideJar(modPath)) { |
| 164 | + return URIUtil.getChildLocation(jarBasePath(modPath), "rascal"); |
| 165 | + } else if ("mvn".equals(modPath.getScheme())) { |
| 166 | + return URIUtil.changePath(modPath, "rascal"); |
| 167 | + } |
| 168 | + return null; |
| 169 | + } |
| 170 | + |
| 171 | + private static String libraryModulePrefix(ISourceLocation modPath) { |
| 172 | + modPath = URIUtil.getParentLocation(modPath); |
| 173 | + if (isInsideJar(modPath)) { |
| 174 | + // For a file within a JAR, return the sub-path within the JAR |
| 175 | + return jarFilePath(modPath); |
| 176 | + } |
| 177 | + // Otherwise, return just the sub-path |
| 178 | + return modPath.getPath(); |
| 179 | + } |
| 180 | + |
| 181 | + private static ISourceLocation jarBasePath(ISourceLocation l) throws URISyntaxException { |
| 182 | + if (!isInsideJar(l)) { |
| 183 | + throw new IllegalArgumentException("Location should have scheme jar+...: " + l); |
| 184 | + } |
| 185 | + |
| 186 | + var path = l.getPath(); |
| 187 | + return URIUtil.changePath(l, path.substring(0, path.lastIndexOf('!') + 1)); |
| 188 | + } |
| 189 | + |
| 190 | + private static String jarFilePath(ISourceLocation l) { |
| 191 | + if (!isInsideJar(l)) { |
| 192 | + throw new IllegalArgumentException("Location should have scheme jar+...: " + l); |
| 193 | + } |
| 194 | + |
| 195 | + var path = l.getPath(); |
| 196 | + return path.substring(path.lastIndexOf('!') + 1); |
| 197 | + } |
| 198 | + |
| 199 | + public InterruptibleFuture<@Nullable IConstructor> getSummary(ISourceLocation occ, Function<ISourceLocation, PathConfig> computePathConfig) { |
| 200 | + Function<Evaluator, @Nullable IConstructor> computeSummary; |
| 201 | + var tplLoc = libraryTplLocation(occ); |
| 202 | + if (tplLoc != null) { |
| 203 | + computeSummary = eval -> (IConstructor) eval.call("makeSummary", VF.string(pathToModuleName(occ)), tplLoc); |
| 204 | + } else { |
| 205 | + computeSummary = eval -> { |
| 206 | + try { |
| 207 | + var pcfg = computePathConfig.apply(occ); |
| 208 | + var moduleName = VF.string(pcfg.getModuleName(occ)); |
| 209 | + return (IConstructor) eval.call("makeSummary", moduleName, pcfg.asConstructor()); |
| 210 | + } catch (IOException e) { |
| 211 | + logger.error("Error looking up module name for source location {}", occ, e); |
| 212 | + return null; |
| 213 | + } |
| 214 | + }; |
| 215 | + } |
| 216 | + |
| 217 | + return runEvaluator("Rascal makeSummary", semanticEvaluator, eval -> { |
| 218 | + var result = computeSummary.apply(eval); |
| 219 | + return result != null && result.asWithKeywordParameters().hasParameters() ? result : null; |
| 220 | + }, null, exec, false, client); |
144 | 221 | } |
145 | 222 |
|
146 | 223 | private static Map<ISourceLocation, ISet> translateCheckResults(IMap messages) { |
|
0 commit comments