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

refact: minor refactoring of MarkerUtils class #747

Merged
merged 1 commit into from
May 11, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import java.util.stream.Collectors;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.tm4e.core.model.ITMModel;
import org.eclipse.tm4e.core.model.ModelTokensChangedEvent;
import org.eclipse.tm4e.core.model.TMToken;
Expand Down Expand Up @@ -62,7 +65,7 @@ public static void updateTextMarkers(final ModelTokensChangedEvent event) {
if (model instanceof final ITMDocumentModel docModel) {
try {
updateTextMarkers(docModel, event.ranges.get(0).fromLineNumber);
} catch (final CoreException ex) {
} catch (final Exception ex) {
TMUIPlugin.logError(ex);
}
}
Expand All @@ -74,20 +77,19 @@ public static void updateTextMarkers(final ModelTokensChangedEvent event) {
*
* @param startLineNumber 1-based
*/
private static void updateTextMarkers(final ITMDocumentModel docModel, final int startLineNumber)
throws CoreException {
private static void updateTextMarkers(final ITMDocumentModel docModel, final int startLineNumber) throws CoreException {

final var doc = docModel.getDocument();
final IDocument doc = docModel.getDocument();

final var res = ResourceUtils.findResource(doc);
final IResource res = ResourceUtils.findResource(doc);
if (res == null)
return;

final int numberOfLines = doc.getNumberOfLines();

// collect affected markers
final var markersByLineNumber = new HashMap<Integer, List<IMarker>>();
for (final var marker : res.findMarkers(TEXTMARKER_TYPE, true, 0)) {
for (final IMarker marker : res.findMarkers(TEXTMARKER_TYPE, true, 0)) {
final var lineNumberObj = getLineNumber(marker);
if (lineNumberObj == null) {
marker.delete(); // this marker is missing line information, should never happen
Expand Down Expand Up @@ -121,13 +123,14 @@ private static void updateTextMarkers(final ITMDocumentModel docModel, final int
final var outdatedMarkers = markersByLineNumber.getOrDefault(lineNumberObj, Collections.emptyList());

// iterate over all tokens of the current line
for (int tokenIndex = 0; tokenIndex < tokensCount; tokenIndex++) {
final var token = tokens.get(tokenIndex);
int tokenIndex = -1;
for (final TMToken token : tokens) {
tokenIndex++;

if (!token.type.contains("comment") || token.type.contains("definition"))
continue;

final TMToken nextToken = tokenIndex + 1 < tokensCount ? tokens.get(tokenIndex + 1) : null;
final @Nullable TMToken nextToken = tokenIndex + 1 < tokensCount ? tokens.get(tokenIndex + 1) : null;
try {
final int lineOffset = doc.getLineOffset(lineIndex);
final var commentText = doc.get(
Expand Down Expand Up @@ -164,7 +167,7 @@ private static void updateTextMarkers(final ITMDocumentModel docModel, final int
attrs.put(IMarker.CHAR_END, markerTextStartOffset + markerText.length());
res.createMarker(markerTypeId, attrs);
}
} catch (final Exception ex) {
} catch (final BadLocationException ex) {
TMUIPlugin.logError(ex);
}
}
Expand All @@ -183,8 +186,9 @@ private static void updateTextMarkers(final ITMDocumentModel docModel, final int
final Object lineNumberAttr = marker.getAttribute(IMarker.LINE_NUMBER);
if (lineNumberAttr instanceof final Integer lineNumber)
return lineNumber;
} catch (final CoreException ex) {
TMUIPlugin.logError(ex);
} catch (CoreException ex) {
if (marker.exists())
TMUIPlugin.logError(ex);
}
return null;
}
Expand All @@ -194,27 +198,31 @@ private static void updateTextMarkers(final ITMDocumentModel docModel, final int
*
* @return true if a matching marker was found and removed.
*/
private static boolean removeMatchingMarker(final List<IMarker> markers, final String type, final Map<String, ?> attributes)
throws CoreException {
private static boolean removeMatchingMarker(final List<IMarker> markers, final String type, final Map<String, ?> attributes) {
if (markers.isEmpty())
return false;

for (final var it = markers.iterator(); it.hasNext();) {
final var marker = it.next();
if (!marker.getType().equals(type))
continue;
final IMarker marker = it.next();
try {
if (!marker.getType().equals(type))
continue;

boolean hasMatchingAttrs = true;
for (final var attr : attributes.entrySet()) {
if (!Objects.equals(marker.getAttribute(attr.getKey()), attr.getValue())) {
hasMatchingAttrs = false;
break;
boolean hasMatchingAttrs = true;
for (final var attr : attributes.entrySet()) {
if (!Objects.equals(marker.getAttribute(attr.getKey()), attr.getValue())) {
hasMatchingAttrs = false;
break;
}
}
}

if (hasMatchingAttrs) {
it.remove();
return true;
if (hasMatchingAttrs) {
it.remove();
return true;
}
} catch (CoreException ex) {
if (marker.exists())
TMUIPlugin.logError(ex);
}
}
return false;
Expand Down
Loading