Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
donat committed Jun 11, 2024
1 parent 1425085 commit ef06560
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public WorkspaceConfiguration readWorkspaceConfig() {
boolean showConsoleView = preferences.getBoolean(SHOW_CONSOLE_VIEW, true);
boolean showExecutionsView = preferences.getBoolean(SHOW_EXECUTIONS_VIEW, true);
boolean moduleSupport = preferences.getBoolean(EXPERIMENTAL_ENABLE_MODULE_SUPPORT, false);
boolean problemsApiSupport = preferences.getBoolean(PROBLEMS_API_SUPPORT, true);
boolean problemsApiSupport = preferences.getBoolean(PROBLEMS_API_SUPPORT, false);
return new WorkspaceConfiguration(distribution, gradleUserHome, javaHome, offlineMode, buildScansEnabled, autoSyncEnabled, arguments, jvmArguments, showConsoleView, showExecutionsView, moduleSupport, problemsApiSupport);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package org.eclipse.buildship.core.internal.util.progress;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import org.gradle.tooling.Failure;
import org.gradle.tooling.events.ProgressEvent;
Expand All @@ -23,6 +23,8 @@
import org.gradle.tooling.events.problems.ProblemEvent;
import org.gradle.tooling.events.problems.SingleProblemEvent;

import com.google.common.base.Strings;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
Expand Down Expand Up @@ -61,87 +63,60 @@ public void statusChanged(ProgressEvent event) {

private void reportProblem(SingleProblemEvent event) {
List<Location> locations = event.getLocations();

// 1/4 offset in file location
Optional<OffsetInFileLocation> offsetInFileLocation = locations.stream().filter(OffsetInFileLocation.class::isInstance).map(OffsetInFileLocation.class::cast).findFirst();
if (offsetInFileLocation.isPresent()) {
IResource resource = toResource(offsetInFileLocation.get());

GradleErrorMarker.createProblemMarker(
toMarkerSeverity(event.getDefinition().getSeverity()),
resource,
this.gradleBuild,
markerMessage(event),
stacktraceStringFor(event.getFailure().getFailure()),
marker -> {
OffsetInFileLocation location = offsetInFileLocation.get();
int startOffset = location.getOffset();
int endOffset = location.getLength();
try {
marker.setAttribute(IMarker.CHAR_START, startOffset);
marker.setAttribute(IMarker.CHAR_END, startOffset + endOffset);
} catch (CoreException e) {
throw new RuntimeException(e);
}
},
new ProblemEventAdapter(event)
);
return;
}

// 2/4 line in file location
Optional<LineInFileLocation> lineInFileLocation = locations.stream().filter(LineInFileLocation.class::isInstance).map(LineInFileLocation.class::cast).findFirst();
if (lineInFileLocation.isPresent()) {
IResource resource = toResource(lineInFileLocation.get());
GradleErrorMarker.createProblemMarker(
toMarkerSeverity(event.getDefinition().getSeverity()),
resource,
this.gradleBuild,
markerMessage(event),
stacktraceStringFor(event.getFailure().getFailure()),
marker -> {
Integer lineNumber = lineNumberOf(lineInFileLocation.get());
if (lineNumber >= 0) {
try {
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
} catch (CoreException e) {
throw new RuntimeException(e);
}
}
},
new ProblemEventAdapter(event)
);
return;
}

// 3/4 file location
Optional<FileLocation> fileLocation = locations.stream().filter(FileLocation.class::isInstance).map(FileLocation.class::cast).findFirst();
if (fileLocation.isPresent()) {
IResource resource = toResource(fileLocation.get());
GradleErrorMarker.createProblemMarker(
toMarkerSeverity(event.getDefinition().getSeverity()),
resource,
this.gradleBuild,
markerMessage(event),
stacktraceStringFor(event.getFailure().getFailure()),
m -> {},
new ProblemEventAdapter(event)
);
return;
}

// 4/4 no location
GradleErrorMarker.createProblemMarker(
toMarkerSeverity(event.getDefinition().getSeverity()),
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(this.gradleBuild.getBuildConfig().getRootProjectDirectory().getAbsolutePath())),
findMarkerResource(locations),
this.gradleBuild,
markerMessage(event),
stacktraceStringFor(event.getFailure().getFailure()),
m -> {},
markerPositionConfiguration(locations),
new ProblemEventAdapter(event)
);
}

private IResource findMarkerResource(List<Location> locations) {
return locations.stream()
.filter(FileLocation.class::isInstance)
.map(FileLocation.class::cast).findFirst()
.map(fl -> toResource(fl))
.orElseGet(() -> ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(rootProjectPath())));
}

private String rootProjectPath() {
return ProblemsReportingProgressListener.this.gradleBuild.getBuildConfig().getRootProjectDirectory().getAbsolutePath();
}

private Consumer<IMarker> markerPositionConfiguration(List<Location> locations) {
for (Location location : locations) {
if (location instanceof OffsetInFileLocation) {
return marker -> {
OffsetInFileLocation offsetLocation = ((OffsetInFileLocation) location);
int startOffset = offsetLocation.getOffset();
int endOffset = offsetLocation.getLength();
try {
marker.setAttribute(IMarker.CHAR_START, startOffset);
marker.setAttribute(IMarker.CHAR_END, startOffset + endOffset);
} catch (CoreException e) {
throw new RuntimeException(e);
}
};
} else if (location instanceof LineInFileLocation) {
return marker -> {

Integer lineNumber = lineNumberOf((FileLocation) location);
if (lineNumber >= 0) {
try {
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
} catch (CoreException e) {
throw new RuntimeException(e);
}
}
};
}
}
return notUsed -> {};
}

private static String markerMessage(SingleProblemEvent problem ) {
String result = problem.getDetails().getDetails();
if (result == null) {
Expand All @@ -151,7 +126,7 @@ private static String markerMessage(SingleProblemEvent problem ) {
result = problem.getDefinition().getId().getDisplayName();
}

return result == null ? "" : result;
return Strings.nullToEmpty("");
}

private static String stacktraceStringFor(Failure failure) {
Expand Down

0 comments on commit ef06560

Please sign in to comment.