StackTraceColorizer implements MessageColorizer { @Override public Collection<MessageFragmentStyle> colorize(String message) throws BadLocationException { initStyles(); Collection<MessageFragmentStyle> list = new ArrayList<>(); SortedSet<SubText> foundStackTraces = stackTraceFinder.findStackTraces(message); for (SubText subText : foundStackTraces) { list.add(new MessageFragmentStyle(subText.getStart(), subText.getLength(), styleStackTrace, false)); String subTextFragment = message.substring(subText.getStart(), subText.getEnd()); Matcher matcher = exceptionLine.matcher(subTextFragment); int newOffset = subText.getStart(); while (matcher.find()) { ArrayList<MessageFragmentStyle> list1= new ArrayList<>(); list1.addAll(colorizeStackTraceRegex(styleClass, subTextFragment, exceptionLine, EXCEPTION_LINE_GROUP_CLASS)); list1.addAll(colorizeStackTraceRegex(stylePackage, subTextFragment, exceptionLine, EXCEPTION_LINE_GROUP_PACKAGE)); list1.addAll(colorizeStackTraceRegex(styleMethod, subTextFragment, exceptionLine, EXCEPTION_LINE_GROUP_METHOD)); list1.addAll(colorizeStackTraceRegex(styleFile, subTextFragment, exceptionLine, EXCEPTION_LINE_GROUP_FILE)); list1.addAll(colorizeStackTraceRegex(styleCodeComment, subTextFragment, exceptionLine, EXCEPTION_LINE_GROUP_CODE_COMMENT)); list1.stream().map(msf->msf.shift(newOffset)).collect(Collectors.toCollection(()->list)); } final Collection<MessageFragmentStyle> exceptionNameAndMessage = findExceptionNameAndMessage(styleStackTrace, subTextFragment); exceptionNameAndMessage.stream().map(m->m.shift(newOffset)).collect(Collectors.toCollection(()->list)); } return list; } StackTraceColorizer(Theme theme); @Override boolean colorizingNeeded(String message); @Override Collection<MessageFragmentStyle> colorize(String message); @Override String getName(); @Override String getDescription(); @Override String getPluginableId(); @Override int getApiVersion(); static final String STYLE_ATTRIBUTE_EXCEPTION_MSG; static final String STYLE_ATTRIBUTE_LOCATION_INFO; }
@Test public void testColorize() throws IOException, BadLocationException { String string = "something\n" + "java.io.FileNotFoundException: fred.txt "\tat java.io.FileInputStream.<init>(FileInputStream.java) "\tat java.io.FileInputStream.<init>(FileInputStream.java) "\tat A.ExTest.readMyFile(ExTest.java:19) " at ExTest.main(ExTest.java:7)\n"; StackTraceColorizer colorizer = new StackTraceColorizer(new ThemeConfig(new BaseConfiguration())); Collection<MessageFragmentStyle> colorize = colorizer.colorize(string); assertEquals(colorize.size(),47); assertTrue(colorize.stream().filter(msf -> msf.getOffset() == 137 && msf.getLength() == 7 && msf.getStyle().getName().equals("stylePackage")).findAny().isPresent()); assertTrue(colorize.stream().filter(msf -> msf.getOffset() == 145 && msf.getLength() == 15 && msf.getStyle().getName().equals("styleClass")).findAny().isPresent()); assertTrue(colorize.stream().filter(msf -> msf.getOffset() == 161 && msf.getLength() == 6 && msf.getStyle().getName().equals("styleMethod")).findAny().isPresent()); assertTrue(colorize.stream().filter(msf -> msf.getOffset() == 168 && msf.getLength() == 20 && msf.getStyle().getName().equals("styleFile")).findAny().isPresent()); assertTrue(colorize.stream().filter(msf -> msf.getOffset() == 189 && msf.getLength() == 15 && msf.getStyle().getName().equals("styleCodeComment")).findAny().isPresent()); final Optional<MessageFragmentStyle> styleWithLocationOptional = colorize.stream().filter(msf -> msf.getOffset() == 229 && msf.getLength() == 14 && msf.getStyle().getName().startsWith("styleFile-LocationInfo")).findAny(); assertTrue(styleWithLocationOptional.isPresent()); final LocationInfo locationInfo = (LocationInfo) styleWithLocationOptional.get().getStyle().getAttribute(StackTraceColorizer.STYLE_ATTRIBUTE_LOCATION_INFO); assertEquals(locationInfo.getPackageName(),Optional.of("A")); assertEquals(locationInfo.getClassName(),Optional.of("A.ExTest")); assertEquals(locationInfo.getMethod(),Optional.of("readMyFile")); assertEquals(locationInfo.getFileName(),Optional.of("ExTest.java")); assertEquals(locationInfo.getLineNumber(),Optional.of(19)); assertEquals(locationInfo.getMessage(),Optional.empty()); }