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

CHE-15 - Java stacktrace support (From Platform to Che Workspace) #5396

Merged
merged 5 commits into from
Jun 30, 2017
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
3 changes: 3 additions & 0 deletions ide/che-core-ide-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@
<exclude>**/*.png</exclude>
<exclude>**/*.gif</exclude>
<exclude>**/*.jpg</exclude>
<exclude>**/OutputCustomizer.java</exclude>
<exclude>**/DefaultOutputCustomizer.java</exclude>
<exclude>**/DefaultOutputCustomizerTest.java</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.command.CommandExecutor;
import org.eclipse.che.ide.api.command.CommandImpl;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.machine.ExecAgentCommandManager;
import org.eclipse.che.ide.api.machine.events.ProcessFinishedEvent;
import org.eclipse.che.ide.api.machine.events.ProcessStartedEvent;
Expand Down Expand Up @@ -66,6 +68,8 @@ public class CommandOutputConsolePresenter implements CommandOutputConsole, Outp
private boolean followOutput = true;

private final List<ActionDelegate> actionDelegates = new ArrayList<>();

private OutputCustomizer outputCustomizer = null;

@Inject
public CommandOutputConsolePresenter(final OutputConsoleView view,
Expand All @@ -75,7 +79,9 @@ public CommandOutputConsolePresenter(final OutputConsoleView view,
EventBus eventBus,
ExecAgentCommandManager execAgentCommandManager,
@Assisted CommandImpl command,
@Assisted Machine machine) {
@Assisted Machine machine,
AppContext appContext,
EditorAgent editorAgent) {
this.view = view;
this.resources = resources;
this.execAgentCommandManager = execAgentCommandManager;
Expand All @@ -84,6 +90,7 @@ public CommandOutputConsolePresenter(final OutputConsoleView view,
this.eventBus = eventBus;
this.commandExecutor = commandExecutor;

setCustomizer(new DefaultOutputCustomizer(appContext, editorAgent));
view.setDelegate(this);

final String previewUrl = command.getAttributes().get(COMMAND_PREVIEW_URL_ATTRIBUTE_NAME);
Expand Down Expand Up @@ -275,4 +282,13 @@ public String getText() {
return view.getText();
}

@Override
public OutputCustomizer getCustomizer() {
return outputCustomizer;
}

/** Sets up the text output customizer */
public void setCustomizer(OutputCustomizer customizer) {
this.outputCustomizer = customizer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.outputconsole.OutputConsole;
import org.eclipse.che.ide.machine.MachineResources;
import org.vectomatic.dom.svg.ui.SVGResource;
Expand All @@ -39,15 +41,21 @@ public class DefaultOutputConsole implements OutputConsole, OutputConsoleView.Ac
/** Follow output when printing text */
private boolean followOutput = true;

private OutputCustomizer customizer = null;

@Inject
public DefaultOutputConsole(OutputConsoleView view,
MachineResources resources,
AppContext appContext,
EditorAgent editorAgent,
@Assisted String title) {
this.view = view;
this.title = title;
this.resources = resources;
this.view.enableAutoScroll(true);

setCustomizer(new DefaultOutputCustomizer(appContext, editorAgent));

view.setDelegate(this);

view.hideCommand();
Expand Down Expand Up @@ -182,4 +190,14 @@ public void onOutputScrolled(boolean bottomReached) {
view.toggleScrollToEndButton(bottomReached);
}

@Override
public OutputCustomizer getCustomizer() {
return customizer;
}

/** Sets up the text output customizer */
public void setCustomizer(OutputCustomizer customizer) {
this.customizer = customizer;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*******************************************************************************
* Copyright (c) 2017 RedHat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* RedHat, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.console;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.gwt.regexp.shared.RegExp.compile;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.che.api.promises.client.Function;
import org.eclipse.che.api.promises.client.FunctionException;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.editor.EditorPartPresenter;
import org.eclipse.che.ide.api.editor.OpenEditorCallbackImpl;
import org.eclipse.che.ide.api.editor.text.TextPosition;
import org.eclipse.che.ide.api.editor.text.TextRange;
import org.eclipse.che.ide.api.editor.texteditor.TextEditor;
import org.eclipse.che.ide.api.resources.Container;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.resource.Path;

import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.user.client.Timer;
import com.google.inject.Inject;

/**
* Default customizer adds an anchor link to the lines that match a stack trace
* line pattern and installs a handler function for the link. The handler parses
* the stack trace line, searches for the candidate Java files to navigate to,
* opens the first file (of the found candidates) in editor and reveals it to
* the required line according to the stack trace line information
*/
public class DefaultOutputCustomizer implements OutputCustomizer {

private static final RegExp LINE_AT = compile("(\\s+at .+)");
private static final RegExp LINE_AT_EXCEPTION = compile("(\\s+at address:.+)");

private AppContext appContext;
private EditorAgent editorAgent;

@Inject
public DefaultOutputCustomizer(AppContext appContext, EditorAgent editorAgent) {
this.appContext = appContext;
this.editorAgent = editorAgent;

exportAnchorClickHandlerFunction();
}

/*
* (non-Javadoc)
*
* @see org.eclipse.che.ide.extension.machine.client.outputspanel.console.
* OutputCustomizer#canCustomize(java.lang.String)
*/
@Override
public boolean canCustomize(String text) {
return (LINE_AT.exec(text) != null && LINE_AT_EXCEPTION.exec(text) == null);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.che.ide.extension.machine.client.outputspanel.console.
* OutputCustomizer#customize(java.lang.String)
*/
@Override
public String customize(String text) {
String customText = text;

MatchResult matcher = LINE_AT.exec(text);
if (matcher != null) {
try {
int start = text.indexOf("at", 0) + "at".length(), openBracket = text.indexOf("(", start),
column = text.indexOf(":", openBracket), closingBracket = text.indexOf(")", column);
String qualifiedName = text.substring(start, openBracket).trim();
String fileName = text.substring(openBracket + "(".length(), column).trim();
int lineNumber = Integer.valueOf(text.substring(column + ":".length(), closingBracket).trim());
customText = text.substring(0, openBracket + "(".length());
customText += "<a href='javascript:open(\"" + qualifiedName + "\", \"" + fileName + "\", " + lineNumber
+ ");'>";
customText += text.substring(openBracket + "(".length(), closingBracket);
customText += "</a>";
customText += text.substring(closingBracket);
text = customText;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think that a String.format would be easier to read than concatenating tons of stuff ?

Copy link
Contributor

@benoitf benoitf Jun 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so bad idea (mine) as GWT is not supporting formatting 👎

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.format/MessageFormat.format/Formatter.format are not available for GWT application and it doesn't look like a GWT replacement exists.

} catch (IndexOutOfBoundsException ex) {
// ignore
}
}

return text;
}

/**
* A callback that is to be called for an anchor
*
* @param qualifiedName
* @param fileName
* @param lineNumber
*/
public void handleAnchorClick(String qualifiedName, String fileName, final int lineNumber) {
if (qualifiedName == null || fileName == null) {
return;
}

String qualifiedClassName = qualifiedName.lastIndexOf('.') != -1
? qualifiedName.substring(0, qualifiedName.lastIndexOf('.'))
: qualifiedName;
final String packageName = qualifiedClassName.lastIndexOf('.') != -1
? qualifiedClassName.substring(0, qualifiedClassName.lastIndexOf('.'))
: "";

String relativeFilePath = (packageName.isEmpty() ? "" :
(packageName.replace(".", "/") + "/")) + fileName;

collectChildren(appContext.getWorkspaceRoot(), Path.valueOf(relativeFilePath)).then(files -> {
if (!files.isEmpty()) {
editorAgent.openEditor(files.get(0), new OpenEditorCallbackImpl() {
@Override
public void onEditorOpened(EditorPartPresenter editor) {
Timer t = new Timer() {
@Override
public void run() {
EditorPartPresenter editorPart = editorAgent.getActiveEditor();
selectRange(editorPart, lineNumber);
}
};
t.schedule(500);
}

@Override
public void onEditorActivated(EditorPartPresenter editor) {
selectRange(editor, lineNumber);
}
});

}
});
}

/*
* Returns the list of workspace files filtered by a relative path
*/
private Promise<List<File>> collectChildren(Container root, Path relativeFilePath) {
return root.getTree(-1).then(new Function<Resource[], List<File>>() {
@Override
public List<File> apply(Resource[] children) throws FunctionException {
return Stream.of(children).filter(
child -> child.isFile() && endsWith(child.asFile().getLocation(), relativeFilePath))
.map(Resource::asFile).collect(Collectors.toList());
}
});
}

/*
* Checks if a path's last segments are equal to the provided relative path
*/
private boolean endsWith(Path path, Path relativePath) {
checkNotNull(path);
checkNotNull(relativePath);

if (path.segmentCount() < relativePath.segmentCount())
return false;

for (int i = relativePath.segmentCount() - 1, j = path.segmentCount() - 1; i >= 0; i--, j--) {
if (!nullToEmpty(relativePath.segment(i)).equals(path.segment(j))) {
return false;
}
}

return true;
}

/*
* Selects and shows the specified line of text in editor
*/
private void selectRange(EditorPartPresenter editor, int line) {
if (editor instanceof TextEditor) {
TextPosition startPosition = new TextPosition(line - 1, 0);
int lineOffsetStart = ((TextEditor) editor).getDocument().getLineStart(line - 1);
if (lineOffsetStart == -1) {
lineOffsetStart = 0;
}

int lineOffsetEnd = ((TextEditor) editor).getDocument().getLineStart(line);
if (lineOffsetEnd == -1) {
lineOffsetEnd = 0;
}
while (((TextEditor) editor).getDocument().getLineAtOffset(lineOffsetEnd) > line - 1) {
lineOffsetEnd--;
}
if (lineOffsetStart > lineOffsetEnd) {
lineOffsetEnd = lineOffsetStart;
}

TextPosition endPosition = new TextPosition(line - 1, lineOffsetEnd - lineOffsetStart);

((TextEditor) editor).getDocument().setSelectedRange(new TextRange(startPosition, endPosition), true);
((TextEditor) editor).getDocument().setCursorPosition(startPosition);
}
}

/**
* Sets up a java callback to be called for an anchor
*/
public native void exportAnchorClickHandlerFunction() /*-{
var that = this;
$wnd.open = $entry(function(qualifiedName,fileName,lineNumber) {
that.@org.eclipse.che.ide.console.DefaultOutputCustomizer::handleAnchorClick(*)(qualifiedName,fileName,lineNumber);
});
}-*/;
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ interface ActionDelegate {
/** Handle scrolling the output. */
void onOutputScrolled(boolean bottomReached);

/** Returns the customizer for the console output */
OutputCustomizer getCustomizer();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
Expand Down Expand Up @@ -336,16 +337,25 @@ public String asString() {
return " ";
}

String encoded = SafeHtmlUtils.htmlEscape(text);
if (delegate != null) {
if (delegate.getCustomizer() != null) {
if (delegate.getCustomizer().canCustomize(encoded)) {
encoded = delegate.getCustomizer().customize(encoded);
}
}
}

for (final Pair<RegExp, String> pair : output2Color) {
final MatchResult matcher = pair.first.exec(text);
final MatchResult matcher = pair.first.exec(encoded);

if (matcher != null) {
return text.replaceAll(matcher.getGroup(1),
return encoded.replaceAll(matcher.getGroup(1),
"<span style=\"color: " + pair.second + "\">" + matcher.getGroup(1) + "</span>");
}
}

return text;
return encoded;
}
};

Expand Down
Loading