Convert KotlinExceptionFilter to kotlin
This commit is contained in:
@@ -14,100 +14,75 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.filters;
|
||||
package org.jetbrains.kotlin.idea.filters
|
||||
|
||||
import com.intellij.execution.filters.ExceptionFilter;
|
||||
import com.intellij.execution.filters.Filter;
|
||||
import com.intellij.execution.filters.HyperlinkInfo;
|
||||
import com.intellij.execution.filters.OpenFileHyperlinkInfo;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import kotlin.CollectionsKt;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.idea.util.DebuggerUtils;
|
||||
import org.jetbrains.kotlin.psi.KtFile;
|
||||
import org.jetbrains.kotlin.resolve.jvm.JvmClassName;
|
||||
import com.intellij.execution.filters.ExceptionFilter
|
||||
import com.intellij.execution.filters.Filter
|
||||
import com.intellij.execution.filters.HyperlinkInfo
|
||||
import com.intellij.execution.filters.OpenFileHyperlinkInfo
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.idea.util.DebuggerUtils
|
||||
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
|
||||
import java.util.regex.Pattern
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
class KotlinExceptionFilter(private val searchScope: GlobalSearchScope) : Filter {
|
||||
private val exceptionFilter = ExceptionFilter(searchScope)
|
||||
|
||||
public class KotlinExceptionFilter implements Filter {
|
||||
private final ExceptionFilter exceptionFilter;
|
||||
private final GlobalSearchScope searchScope;
|
||||
private fun createHyperlinkInfo(line: String): HyperlinkInfo? {
|
||||
val project = searchScope.project ?: return null
|
||||
|
||||
public KotlinExceptionFilter(@NotNull GlobalSearchScope searchScope) {
|
||||
this.exceptionFilter = new ExceptionFilter(searchScope);
|
||||
this.searchScope = searchScope;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private HyperlinkInfo createHyperlinkInfo(@NotNull String line) {
|
||||
Project project = searchScope.getProject();
|
||||
if (project == null) return null;
|
||||
|
||||
StackTraceElement element = parseStackTraceLine(line);
|
||||
if (element == null) return null;
|
||||
val element = parseStackTraceLine(line) ?: return null
|
||||
|
||||
// All true classes should be handled correctly in the default ExceptionFilter. Special cases:
|
||||
// - static facades;
|
||||
// - package facades / package parts (generated by pre-M13 compiled);
|
||||
// - local classes (and closures) in top-level function and property declarations.
|
||||
String fileName = element.getFileName();
|
||||
val fileName = element.fileName
|
||||
// fullyQualifiedName is of format "package.Class$Inner"
|
||||
String fullyQualifiedName = element.getClassName();
|
||||
int lineNumber = element.getLineNumber() - 1;
|
||||
val fullyQualifiedName = element.className
|
||||
val lineNumber = element.lineNumber - 1
|
||||
|
||||
String internalName = fullyQualifiedName.replace('.', '/');
|
||||
JvmClassName jvmClassName = JvmClassName.byInternalName(internalName);
|
||||
val internalName = fullyQualifiedName.replace('.', '/')
|
||||
val jvmClassName = JvmClassName.byInternalName(internalName)
|
||||
|
||||
KtFile file = DebuggerUtils.findSourceFileForClass(project, searchScope, jvmClassName, fileName);
|
||||
val file = DebuggerUtils.findSourceFileForClass(project, searchScope, jvmClassName, fileName) ?: return null
|
||||
|
||||
if (file == null) return null;
|
||||
VirtualFile virtualFile = file.getVirtualFile();
|
||||
if (virtualFile == null) return null;
|
||||
val virtualFile = file.virtualFile ?: return null
|
||||
|
||||
return new OpenFileHyperlinkInfo(project, virtualFile, lineNumber);
|
||||
return OpenFileHyperlinkInfo(project, virtualFile, lineNumber)
|
||||
}
|
||||
|
||||
// Matches strings like "\tat test.TestPackage$foo$f$1.invoke(a.kt:3)\n"
|
||||
// or "\tBreakpoint reached at test.TestPackage$foo$f$1.invoke(a.kt:3)\n"
|
||||
private static final Pattern STACK_TRACE_ELEMENT_PATTERN = Pattern.compile("^[\\w|\\s]*at\\s+(.+)\\.(.+)\\((.+):(\\d+)\\)\\s*$");
|
||||
private fun patchResult(result: Filter.Result, line: String): Filter.Result {
|
||||
val newHyperlinkInfo = createHyperlinkInfo(line) ?: return result
|
||||
|
||||
return Filter.Result(result.resultItems.map {
|
||||
Filter.ResultItem(it.getHighlightStartOffset(), it.getHighlightEndOffset(), newHyperlinkInfo, it.getHighlightAttributes())
|
||||
})
|
||||
|
||||
@Nullable
|
||||
private static StackTraceElement parseStackTraceLine(@NotNull String line) {
|
||||
Matcher matcher = STACK_TRACE_ELEMENT_PATTERN.matcher(line);
|
||||
if (matcher.matches()) {
|
||||
String declaringClass = matcher.group(1);
|
||||
String methodName = matcher.group(2);
|
||||
String fileName = matcher.group(3);
|
||||
String lineNumber = matcher.group(4);
|
||||
//noinspection ConstantConditions
|
||||
return new StackTraceElement(declaringClass, methodName, fileName, Integer.parseInt(lineNumber));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Result patchResult(@NotNull Result result, @NotNull String line) {
|
||||
final HyperlinkInfo newHyperlinkInfo = createHyperlinkInfo(line);
|
||||
if (newHyperlinkInfo == null) return result;
|
||||
override fun applyFilter(line: String, entireLength: Int): Filter.Result? {
|
||||
val result = exceptionFilter.applyFilter(line, entireLength)
|
||||
return if (result == null) null else patchResult(result, line)
|
||||
}
|
||||
|
||||
return new Result(CollectionsKt.map(result.getResultItems(), new Function1<ResultItem, ResultItem>() {
|
||||
@Override
|
||||
public ResultItem invoke(ResultItem item) {
|
||||
return new ResultItem(item.getHighlightStartOffset(), item.getHighlightEndOffset(), newHyperlinkInfo,
|
||||
item.getHighlightAttributes());
|
||||
companion object {
|
||||
|
||||
// Matches strings like "\tat test.TestPackage$foo$f$1.invoke(a.kt:3)\n"
|
||||
// or "\tBreakpoint reached at test.TestPackage$foo$f$1.invoke(a.kt:3)\n"
|
||||
private val STACK_TRACE_ELEMENT_PATTERN = Pattern.compile("^[\\w|\\s]*at\\s+(.+)\\.(.+)\\((.+):(\\d+)\\)\\s*$")
|
||||
|
||||
private fun parseStackTraceLine(line: String): StackTraceElement? {
|
||||
val matcher = STACK_TRACE_ELEMENT_PATTERN.matcher(line)
|
||||
if (matcher.matches()) {
|
||||
val declaringClass = matcher.group(1)
|
||||
val methodName = matcher.group(2)
|
||||
val fileName = matcher.group(3)
|
||||
val lineNumber = matcher.group(4)
|
||||
//noinspection ConstantConditions
|
||||
return StackTraceElement(declaringClass, methodName, fileName, Integer.parseInt(lineNumber))
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Result applyFilter(String line, int entireLength) {
|
||||
Result result = exceptionFilter.applyFilter(line, entireLength);
|
||||
return result == null ? null : patchResult(result, line);
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user