Skip to content

Commit

Permalink
Change internal APIs to have a marker handler when doing code analysis.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Apr 28, 2024
1 parent 279d1d8 commit 5b9fb5c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.python.pydev.core.logging.DebugSettings;
import org.python.pydev.parser.preferences.PyDevBuilderPreferences;
import org.python.pydev.shared_core.callbacks.ICallback;
import org.python.pydev.shared_core.markers.PyMarkerUtils;
import org.python.pydev.shared_core.markers.PyMarkerUtils.MarkerInfo;
import org.python.pydev.shared_core.resources.DocumentChanged;

Expand Down Expand Up @@ -69,6 +68,8 @@ public class AnalysisBuilderRunnable extends AbstractAnalysisBuilderRunnable {
*/
public static final List<ICallback<Object, IResource>> analysisBuilderListeners = new ArrayList<ICallback<Object, IResource>>();

private IMarkerHandler markerHandler = new DefaultMarkerHandler();

// -------------------------------------------------------------------------------------------- ATTRIBUTES

private IDocument document;
Expand Down Expand Up @@ -106,7 +107,7 @@ private static boolean isHierarchicallyDerived(IResource curr) {
boolean isFullBuild, String moduleName, boolean forceAnalysis, int analysisCause,
IAnalysisBuilderRunnable oldAnalysisBuilderThread, IPythonNature nature, long documentTime,
KeyForAnalysisRunnable key, long resourceModificationStamp,
List<IExternalCodeAnalysisVisitor> externalVisitors) {
List<IExternalCodeAnalysisVisitor> externalVisitors, IMarkerHandler markerHandler) {
super(isFullBuild, moduleName, forceAnalysis, analysisCause, oldAnalysisBuilderThread, nature, documentTime,
key, resourceModificationStamp);

Expand All @@ -117,6 +118,7 @@ private static boolean isHierarchicallyDerived(IResource curr) {
this.document = document;
this.resource = resource;
this.module = module;
this.markerHandler = markerHandler;

if (externalVisitors.size() > 0) {
this.allVisitors = externalVisitors.toArray(new IExternalCodeAnalysisVisitor[0]);
Expand All @@ -143,6 +145,10 @@ private static boolean isHierarchicallyDerived(IResource curr) {
this.flake8Visitor, this.ruffVisitor };
}

for (IExternalCodeAnalysisVisitor visitor : allVisitors) {
visitor.setMarkerHandler(markerHandler);
}

// Important: we can only update the index if it was a builder... if it was the parser,
// we can't update it otherwise we could end up with data that's not saved in the index.
boolean updateIndex = analysisCause == ANALYSIS_CAUSE_BUILDER;
Expand Down Expand Up @@ -246,12 +252,11 @@ protected void doAnalysis() {
"Skipping: !makeAnalysis -- " + moduleName);
}
if (!anotherVisitorRequiresAnalysis) {
AnalysisRunner.deleteMarkers(r);
markerHandler.deleteAnalysisMarkers(r);
return;
} else {
// Only delete pydev markers (others will be deleted by the respective visitors later on).
boolean onlyPydevAnalysisMarkers = true;
AnalysisRunner.deleteMarkers(r, onlyPydevAnalysisMarkers);
markerHandler.deleteOnlyPydevAnalysisMarkers(r);
}
}

Expand Down Expand Up @@ -291,7 +296,7 @@ protected void doAnalysis() {
//We don't want to check derived resources (but we want to remove any analysis messages that
//might be already there)
if (r != null) {
AnalysisRunner.deleteMarkers(r);
markerHandler.deleteAnalysisMarkers(r);
}
return;
}
Expand Down Expand Up @@ -340,7 +345,8 @@ protected void doAnalysis() {
|| (analyzeOnlyActiveEditor
&& (!PyDevBuilderPreferences.getRemoveErrorsWhenEditorIsClosed() || OpenEditors
.isEditorOpenForResource(r)))) {
markersFromCodeAnalysis = runner.setMarkers(r, document, messages, this.internalCancelMonitor);
markersFromCodeAnalysis = runner.setMarkers(r, document, messages, this.internalCancelMonitor,
markerHandler);
} else {
if (DebugSettings.DEBUG_ANALYSIS_REQUESTS) {
org.python.pydev.shared_core.log.ToLogFile.toLogFile(this,
Expand Down Expand Up @@ -419,8 +425,7 @@ protected void doAnalysis() {
}
}
}
PyMarkerUtils.replaceMarkers(markersFromVisitor, resource, problemMarker,
true, this.internalCancelMonitor);
replaceMarkers(problemMarker, markersFromVisitor);
} else {
visitor.deleteMarkers();
}
Expand Down Expand Up @@ -448,6 +453,11 @@ protected void doAnalysis() {
}
}

private void replaceMarkers(String problemMarker, List<MarkerInfo> markersFromVisitor) {
markerHandler.replaceMarkers(markersFromVisitor, resource, problemMarker,
true, this.internalCancelMonitor);
}

/**
* @return false if there's no modification among the current version of the file and the last version analyzed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ private static String createExistinTimeHigherMessage(long oldTime, long document
/**
* Creates a thread for analyzing some module (and stopping analysis of some other thread if there is one
* already running).
* @param externalVisitors
* @param externalVisitors
*
* @return The new runnable or null if there's one there already that has a higher document version.
*/
/*Default*/static IAnalysisBuilderRunnable createRunnable(IDocument document, IResource resource,
ICallback<IModule, Integer> module, boolean isFullBuild, String moduleName, boolean forceAnalysis,
int analysisCause, IPythonNature nature, long documentTime, long resourceModificationStamp,
List<IExternalCodeAnalysisVisitor> externalVisitors) {
List<IExternalCodeAnalysisVisitor> externalVisitors, IMarkerHandler markerHandler) {

synchronized (lock) {
Map<KeyForAnalysisRunnable, IAnalysisBuilderRunnable> available = getAvailableThreads();
Expand Down Expand Up @@ -226,7 +226,7 @@ private static String createExistinTimeHigherMessage(long oldTime, long document
}
IAnalysisBuilderRunnable analysisBuilderThread = new AnalysisBuilderRunnable(document, resource, module,
isFullBuild, moduleName, forceAnalysis, analysisCause, oldAnalysisBuilderThread, nature,
documentTime, analysisKey, resourceModificationStamp, externalVisitors);
documentTime, analysisKey, resourceModificationStamp, externalVisitors, markerHandler);

logCreate(moduleName, analysisBuilderThread, "Factory: changed");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@

public class AnalysisBuilderVisitor extends PyDevBuilderVisitor {

private IMarkerHandler markerHandler = new DefaultMarkerHandler();

public void setMarkerHandler(IMarkerHandler markerHandler) {
this.markerHandler = markerHandler;
}

@Override
protected int getPriority() {
return PyCodeCompletionVisitor.PRIORITY_CODE_COMPLETION + 1; //just after the code-completion priority
Expand Down Expand Up @@ -247,7 +253,7 @@ public IModule call(Integer arg) {

final IAnalysisBuilderRunnable runnable = AnalysisBuilderRunnableFactory.createRunnable(document, resource,
moduleCallback, isFullBuild(), moduleName, forceAnalysis, analysisCause, nature, documentTime,
resource.getModificationStamp(), externalVisitors);
resource.getModificationStamp(), externalVisitors, markerHandler);

if (runnable == null) {
//It may be null if the document version of the new one is lower than one already active.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ public static void deleteMarkers(IResource resource, boolean onlyPydevAnalysisMa
* @param document the document
* @param messages the messages to add
* @param monitor monitor to check if we should stop the process.
* @param markerHandler
* @param existing these are the existing markers. After this method, the list will contain only the ones that
* should be removed.
* @return
*/
public List<MarkerInfo> setMarkers(IResource resource, IDocument document, IMessage[] messages,
IProgressMonitor monitor) {
IProgressMonitor monitor, IMarkerHandler markerHandler) {
if (resource == null) {
return null;
}
Expand All @@ -164,7 +165,7 @@ public List<MarkerInfo> setMarkers(IResource resource, IDocument document, IMess
return null;
}

PyMarkerUtils.replaceMarkers(lst, resource, AnalysisRunner.PYDEV_ANALYSIS_PROBLEM_MARKER, true, monitor);
markerHandler.replaceMarkers(lst, resource, AnalysisRunner.PYDEV_ANALYSIS_PROBLEM_MARKER, true, monitor);
//timer.printDiff("Time to put markers: "+lst.size());
return lst;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.python.pydev.analysis.additionalinfo.builders;

import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.python.pydev.core.log.Log;
import org.python.pydev.shared_core.markers.PyMarkerUtils;
import org.python.pydev.shared_core.markers.PyMarkerUtils.MarkerInfo;

public class DefaultMarkerHandler implements IMarkerHandler {

@Override
public void replaceMarkers(final List<MarkerInfo> lst, final IResource resource, final String markerType,
final boolean removeUserEditable, IProgressMonitor monitor) {
PyMarkerUtils.replaceMarkers(lst, resource, markerType, removeUserEditable, monitor);
}

@Override
public void deleteAnalysisMarkers(IResource r) {
AnalysisRunner.deleteMarkers(r);

}

@Override
public void deleteOnlyPydevAnalysisMarkers(IResource r) {
boolean onlyPydevAnalysisMarkers = true;
AnalysisRunner.deleteMarkers(r, onlyPydevAnalysisMarkers);
}

@Override
public void deleteMarkers(IResource resource, String problemMarkerId) {
try {
resource.deleteMarkers(problemMarkerId, false, IResource.DEPTH_ZERO);
} catch (CoreException e) {
Log.log(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.python.pydev.analysis.additionalinfo.builders;

import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.python.pydev.shared_core.markers.PyMarkerUtils.MarkerInfo;

public interface IMarkerHandler {

void replaceMarkers(List<MarkerInfo> lst, IResource resource, String markerType,
boolean removeUserEditable, IProgressMonitor monitor);

void deleteAnalysisMarkers(IResource r);

void deleteOnlyPydevAnalysisMarkers(IResource r);

void deleteMarkers(IResource resource, String problemMarkerId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
import org.python.pydev.core.log.Log;
import org.python.pydev.shared_core.markers.PyMarkerUtils;

import com.python.pydev.analysis.additionalinfo.builders.IMarkerHandler;

public abstract class AbstractExternalCodeAnalysisOnlyRemoveMarkersVisitor implements IExternalCodeAnalysisVisitor {

protected final IResource resource;
private IMarkerHandler markerHandler;

public AbstractExternalCodeAnalysisOnlyRemoveMarkersVisitor(IResource resource) {
this.resource = resource;
}

@Override
public void setMarkerHandler(IMarkerHandler markerHandler) {
this.markerHandler = markerHandler;
}

@Override
public void startVisit() {
deleteMarkers();
Expand All @@ -35,7 +43,11 @@ public void deleteMarkers() {
//Whenever PyLint is passed, the markers will be deleted.
try {
if (resource != null) {
resource.deleteMarkers(getProblemMarkerId(), false, IResource.DEPTH_ZERO);
if (markerHandler != null) {
markerHandler.deleteMarkers(resource, getProblemMarkerId());
} else {
resource.deleteMarkers(getProblemMarkerId(), false, IResource.DEPTH_ZERO);
}
}
} catch (CoreException e3) {
Log.log(e3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.eclipse.core.resources.IResource;
import org.python.pydev.shared_core.markers.PyMarkerUtils;

import com.python.pydev.analysis.additionalinfo.builders.IMarkerHandler;

/**
* See: org.python.pydev.ast.builder.pylint.PyLintVisitorFactory to create PyLint visitors.
*/
Expand Down Expand Up @@ -45,4 +47,6 @@ public interface IExternalCodeAnalysisVisitor {

String getMessageId();

void setMarkerHandler(IMarkerHandler markerHandler);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class DocumentChanged {

public static final Map<IFile, Tuple3<Long, Long, WeakReference<IDocument>>> fileToSavedTime = new LRUMap<>(20);
public static final Map<IFile, Tuple3<Long, Long, WeakReference<IDocument>>> fileToSavedTime = new LRUMap<>(30);

public static void markSavedTimes(IFile file, IDocument document) {
fileToSavedTime.put(file, new Tuple3<Long, Long, WeakReference<IDocument>>(
Expand Down

0 comments on commit 5b9fb5c

Please sign in to comment.