Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jarek/groovy_6: fix doc functionality #13

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -78,7 +79,8 @@ public BaseEvaluator(String id,
EvaluatorParameters evaluatorParameters,
BeakerXClient beakerXClient,
MagicCommandAutocompletePatterns autocompletePatterns,
ClasspathScanner classpathScanner) {
ClasspathScanner classpathScanner,
Inspect inspect) {
shellId = id;
sessionId = sId;
executor = cellExecutor;
Expand All @@ -89,7 +91,7 @@ public BaseEvaluator(String id,
outDir = getOrCreateFile(tempFolder.toString() + File.separator + "outDir").getPath();
classPath = new Classpath();
classPath.add(new PathToJar(outDir));
inspect = new Inspect(Paths.get("."));
this.inspect = inspect;
executorService = Executors.newCachedThreadPool();
executorBgkService = Executors.newCachedThreadPool();
this.evaluatorParameters = evaluatorParameters;
Expand Down Expand Up @@ -266,7 +268,6 @@ public void cancelExecution(GroupName groupName) {
@Override
public void resetEnvironment() {
executor.killAllThreads();
inspect = new Inspect(Paths.get("./"));
doResetEnvironment();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import java.util.List;

public class ClassInspect {
String className;
String fullName;
String javadoc;
List<MethodInspect> methods;
List<MethodInspect> constructors;
private String className;
private String fullName;
private String javadoc;
private List<MethodInspect> methods;
private List<MethodInspect> constructors;

public ClassInspect(String className, String fullName, String javadoc) {
this.className = className;
Expand Down
104 changes: 2 additions & 102 deletions base-api/src/main/java/com/twosigma/beakerx/inspect/Inspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,113 +17,13 @@
package com.twosigma.beakerx.inspect;

import com.twosigma.beakerx.kernel.Imports;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


public class Inspect {
public interface Inspect {

private static final String COLOR_RED = "\u001B[31m";
private static final String COLOR_RESET = "\033[0m";
InspectResult doInspect(String code, int caretPosition, URLClassLoader classLoader, Imports imports);

private static String inspectDataPath = "beakerx_inspect.json";
private Path pathToBeakerxInspectFile;

public Inspect(Path pathToBeakerxInspectFile) {
this.pathToBeakerxInspectFile = pathToBeakerxInspectFile;
}

public InspectResult doInspect(String code, int caretPosition, URLClassLoader classLoader, Imports imports) {
InspectResult inspectResult = new InspectResult();
if (code.length() >= caretPosition) {
String methodName = CodeParsingTool.getSelectedMethodName(code, caretPosition);
String className = CodeParsingTool.getClassName(code, caretPosition, methodName);
try (InputStream inputStream = new FileInputStream(getInspectFile())) {
String inspectData = IOUtils.toString(inputStream, "UTF-8");
inspectResult = getInspectResult(caretPosition, methodName, className, inspectData);
} catch (IOException e) {
e.printStackTrace();
}
}
return inspectResult;
}

private File getInspectFile() {
// Path workingDirectory = null;
// try {
// workingDirectory= Paths.get(
// BeakerxWidget.class.getProtectionDomain().getCodeSource().getLocation().toURI()
// ).getParent().getParent();
// workingDirectory = pathToBeakerxInspectFile;
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }
return new File(pathToBeakerxInspectFile.toFile(), inspectDataPath);
}

private InspectResult getInspectResult(int caretPosition, String methodName, String className, String everything) {
HashMap<String, ClassInspect> stringClassInspectHashMap = new SerializeInspect().fromJson(everything);
InspectResult inspectResult = new InspectResult();
ClassInspect classInspect = null;
if (stringClassInspectHashMap.containsKey(className)) {
classInspect = stringClassInspectHashMap.get(className);
} else {
for (ClassInspect cls : stringClassInspectHashMap.values()) {
if (cls.getClassName().equals(className)) {
classInspect = cls;
break;
}
}
}
if (methodName == null && classInspect != null) {
List<MethodInspect> constructors = classInspect.getConstructors();
String classInfo = parseClassInfo(classInspect) + "\n\n" + parseMethodsInfo(constructors, "");
inspectResult = new InspectResult(classInfo, caretPosition);
} else {
List<MethodInspect> methodInspectsList = classInspect == null ? null : classInspect.getMethods();
if (methodInspectsList == null) {
return new InspectResult();
}
List<MethodInspect> methods = methodInspectsList.stream()
.filter(m -> m.getMethodName().equals(methodName))
.collect(Collectors.toList());
if (!methods.isEmpty()) {
return new InspectResult(parseMethodsInfo(methods, className), caretPosition);
}
}
return inspectResult;
}

private String parseClassInfo(ClassInspect classInspect) {
return COLOR_RED + "Class: " + COLOR_RESET + classInspect.fullName + "\n"
+ COLOR_RED + "JavaDoc: " + (classInspect.getJavadoc().equals("")
? "<no JavaDoc>" : COLOR_RESET + classInspect.getJavadoc());
}

public static void setInspectFileName(String inspectDataPath) {
Inspect.inspectDataPath = inspectDataPath;
}

public String parseMethodsInfo(List<MethodInspect> methods, String className) {
if (methods == null) {
return "";
}
String parsedMethods = methods.stream()
.map(m ->
COLOR_RED + "Signature: " + COLOR_RESET + className + (className.equals("") ? "" : ".")
+ m.getMethodName() + "(" + m.getSignature() + ")" + "\n" + COLOR_RED + "JavaDoc: " +
(m.getJavadoc().equals("") ? "<no JavaDoc>" : COLOR_RESET + m.getJavadoc()))
.collect(Collectors.joining("\n\n"));
return parsedMethods;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2020 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx.evaluator;

import com.twosigma.beakerx.inspect.Inspect;
import com.twosigma.beakerx.inspect.InspectResult;
import com.twosigma.beakerx.kernel.Imports;

import java.net.URLClassLoader;

public class BxInspectMock implements Inspect {



@Override
public InspectResult doInspect(String code, int caretPosition, URLClassLoader classLoader, Imports imports) {
return new InspectResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public EvaluatorTest(String id, String sId, CellExecutor cellExecutor, Evaluator
kernelParameters,
beakerXClient,
new MagicCommandAutocompletePatternsMock(),
new ClasspathScannerMock());
new ClasspathScannerMock(),
new BxInspectMock());
}

@Override
Expand Down
148 changes: 148 additions & 0 deletions base/src/main/java/com/twosigma/beakerx/evaluator/BxInspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2020 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx.evaluator;

import com.twosigma.beakerx.inspect.ClassInspect;
import com.twosigma.beakerx.inspect.CodeParsingTool;
import com.twosigma.beakerx.inspect.Inspect;
import com.twosigma.beakerx.inspect.InspectResult;
import com.twosigma.beakerx.inspect.MethodInspect;
import com.twosigma.beakerx.inspect.SerializeInspect;
import com.twosigma.beakerx.kernel.Imports;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class BxInspect implements Inspect {

private static final String COLOR_RED = "\u001B[31m";
private static final String COLOR_RESET = "\033[0m";
public static final String BEAKERX_INSPECT_JSON = "beakerx_inspect.json";

private String inspectData;
private InputStream inspectDataStream;

public BxInspect(InputStream inspectDataStream) {
this.inspectDataStream = inspectDataStream;
}

@Override
public InspectResult doInspect(String code, int caretPosition, URLClassLoader classLoader, Imports imports) {
InspectResult inspectResult = new InspectResult();
if (code.length() >= caretPosition) {
String methodName = CodeParsingTool.getSelectedMethodName(code, caretPosition);
String className = CodeParsingTool.getClassName(code, caretPosition, methodName);
inspectResult = getInspectResult(caretPosition, methodName, className, getInspectData());
}
return inspectResult;
}

private String getInspectData() {
if (inspectData == null) {
try (InputStream inputStream = inspectDataStream) {
inspectData = IOUtils.toString(inputStream, "UTF-8");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return inspectData;
}

public static InputStream getInspectFile() {
String inputFile = "jar:file:" + pathToInspectionFile(BxInspect.class).toString() + "!/" + BEAKERX_INSPECT_JSON;
try {
URL inputURL = new URL(inputFile);
JarURLConnection conn = (JarURLConnection) inputURL.openConnection();
InputStream in = conn.getInputStream();
return in;
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}

public static Path pathToInspectionFile(Class clazz) {
Path workingDirectory = null;
try {
workingDirectory = Paths.get(clazz.getProtectionDomain().getCodeSource().getLocation().toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
return workingDirectory;
}

private InspectResult getInspectResult(int caretPosition, String methodName, String className, String everything) {
HashMap<String, ClassInspect> stringClassInspectHashMap = new SerializeInspect().fromJson(everything);
InspectResult inspectResult = new InspectResult();
ClassInspect classInspect = null;
if (stringClassInspectHashMap.containsKey(className)) {
classInspect = stringClassInspectHashMap.get(className);
} else {
for (ClassInspect cls : stringClassInspectHashMap.values()) {
if (cls.getClassName().equals(className)) {
classInspect = cls;
break;
}
}
}
if (methodName == null && classInspect != null) {
List<MethodInspect> constructors = classInspect.getConstructors();
String classInfo = parseClassInfo(classInspect) + "\n\n" + parseMethodsInfo(constructors, "");
inspectResult = new InspectResult(classInfo, caretPosition);
} else {
List<MethodInspect> methodInspectsList = classInspect == null ? null : classInspect.getMethods();
if (methodInspectsList == null) {
return new InspectResult();
}
List<MethodInspect> methods = methodInspectsList.stream()
.filter(m -> m.getMethodName().equals(methodName))
.collect(Collectors.toList());
if (!methods.isEmpty()) {
return new InspectResult(parseMethodsInfo(methods, className), caretPosition);
}
}
return inspectResult;
}

private String parseClassInfo(ClassInspect classInspect) {
return COLOR_RED + "Class: " + COLOR_RESET + classInspect.getFullName() + "\n"
+ COLOR_RED + "JavaDoc: " + (classInspect.getJavadoc().equals("")
? "<no JavaDoc>" : COLOR_RESET + classInspect.getJavadoc());
}

public String parseMethodsInfo(List<MethodInspect> methods, String className) {
if (methods == null) {
return "";
}
String parsedMethods = methods.stream()
.map(m ->
COLOR_RED + "Signature: " + COLOR_RESET + className + (className.equals("") ? "" : ".")
+ m.getMethodName() + "(" + m.getSignature() + ")" + "\n" + COLOR_RED + "JavaDoc: " +
(m.getJavadoc().equals("") ? "<no JavaDoc>" : COLOR_RESET + m.getJavadoc()))
.collect(Collectors.joining("\n\n"));
return parsedMethods;
}

}