From b7ac2b5da2222d2855b45bf838b114bd52d5fed7 Mon Sep 17 00:00:00 2001 From: Lars Vogel Date: Thu, 2 Oct 2025 15:30:56 +0200 Subject: [PATCH] Refactor conditional statements in projection-related classes for improved readability Using the "Convert control statement bodies to block" JDT clean-up --- .../text/source/projection/AnnotationBag.java | 9 +- .../projection/ProjectionAnnotationHover.java | 14 +- .../projection/ProjectionAnnotationModel.java | 11 +- .../projection/ProjectionRulerColumn.java | 26 +- .../source/projection/ProjectionSummary.java | 59 +++-- .../source/projection/ProjectionSupport.java | 15 +- .../source/projection/ProjectionViewer.java | 226 ++++++++++++------ .../SourceViewerInformationControl.java | 34 ++- 8 files changed, 258 insertions(+), 136 deletions(-) diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/AnnotationBag.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/AnnotationBag.java index 1afd944467e..7dc1d288469 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/AnnotationBag.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/AnnotationBag.java @@ -47,8 +47,9 @@ public AnnotationBag(String type) { * @param annotation the annotation to add */ public void add(Annotation annotation) { - if (fAnnotations == null) + if (fAnnotations == null) { fAnnotations= new HashSet<>(2); + } fAnnotations.add(annotation); } @@ -60,8 +61,9 @@ public void add(Annotation annotation) { public void remove(Annotation annotation) { if (fAnnotations != null) { fAnnotations.remove(annotation); - if (fAnnotations.isEmpty()) + if (fAnnotations.isEmpty()) { fAnnotations= null; + } } } @@ -82,8 +84,9 @@ public boolean isEmpty() { * @since 3.1 */ public Iterator iterator() { - if (!isEmpty()) + if (!isEmpty()) { return fAnnotations.iterator(); + } return null; } } diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationHover.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationHover.java index 3358d8e0568..5330f233f29 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationHover.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationHover.java @@ -76,10 +76,11 @@ private boolean isCaptionLine(Position position, IDocument document, int line) { if (position.getOffset() > -1 && position.getLength() > -1) { try { int captionOffset; - if (position instanceof IProjectionPosition) + if (position instanceof IProjectionPosition) { captionOffset= ((IProjectionPosition) position).computeCaptionOffset(document); - else + } else { captionOffset= 0; + } int startLine= document.getLineOfOffset(position.getOffset() + captionOffset); return line == startLine; } catch (BadLocationException x) { @@ -104,15 +105,18 @@ private String getProjectionTextAtLine(ISourceViewer viewer, int line, int numbe Iterator e= model.getAnnotationIterator(); while (e.hasNext()) { ProjectionAnnotation annotation= (ProjectionAnnotation) e.next(); - if (!annotation.isCollapsed()) + if (!annotation.isCollapsed()) { continue; + } Position position= model.getPosition(annotation); - if (position == null) + if (position == null) { continue; + } - if (isCaptionLine(position, document, line)) + if (isCaptionLine(position, document, line)) { return getText(document, position.getOffset(), position.getLength(), numberOfLines); + } } } catch (BadLocationException x) { diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationModel.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationModel.java index 41e2f9318b6..0354964ef44 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationModel.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationModel.java @@ -79,10 +79,11 @@ public void expand(Annotation annotation) { */ public void toggleExpansionState(Annotation annotation) { if (annotation instanceof ProjectionAnnotation projection) { - if (projection.isCollapsed()) + if (projection.isCollapsed()) { projection.markExpanded(); - else + } else { projection.markCollapsed(); + } modifyAnnotation(projection, true); } @@ -125,8 +126,9 @@ public boolean collapseAll(int offset, int length) { } } - if (collapsing) + if (collapsing) { fireModelChanged(); + } return collapsing; } @@ -162,8 +164,9 @@ protected boolean expandAll(int offset, int length, boolean fireModelChanged) { } } - if (expanding && fireModelChanged) + if (expanding && fireModelChanged) { fireModelChanged(); + } return expanding; } diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionRulerColumn.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionRulerColumn.java index 70f32d10085..79072647a1f 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionRulerColumn.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionRulerColumn.java @@ -72,8 +72,9 @@ public ProjectionRulerColumn(int width, IAnnotationAccess annotationAccess) { @Override protected void mouseClicked(int line) { clearCurrentAnnotation(); - if (fMouseDownLine != line) + if (fMouseDownLine != line) { return; + } ProjectionAnnotation annotation= findAnnotation(line, true); if (annotation != null) { ProjectionAnnotationModel model= (ProjectionAnnotationModel) getModel(); @@ -88,8 +89,9 @@ protected void mouseDown(int rulerLine) { @Override protected void mouseDoubleClicked(int rulerLine) { - if (findAnnotation(rulerLine, true) != null) + if (findAnnotation(rulerLine, true) != null) { return; + } ProjectionAnnotation annotation= findAnnotation(rulerLine, false); if (annotation != null) { @@ -121,12 +123,14 @@ private ProjectionAnnotation findAnnotation(int line, boolean exact) { Object next= e.next(); if (next instanceof ProjectionAnnotation annotation) { Position p= model.getPosition(annotation); - if (p == null) + if (p == null) { continue; + } int distance= getDistance(annotation, p, document, line); - if (distance == -1) + if (distance == -1) { continue; + } if (!exact) { if (distance < previousDistance) { @@ -167,14 +171,16 @@ private int getDistance(ProjectionAnnotation annotation, Position position, IDoc if (line < endLine) { if (annotation.isCollapsed()) { int captionOffset; - if (position instanceof IProjectionPosition) + if (position instanceof IProjectionPosition) { captionOffset= ((IProjectionPosition) position).computeCaptionOffset(document); - else + } else { captionOffset= 0; + } int captionLine= document.getLineOfOffset(position.getOffset() + captionOffset); - if (startLine <= captionLine && captionLine < endLine) + if (startLine <= captionLine && captionLine < endLine) { return Math.abs(line - captionLine); + } } return line - startLine; } @@ -206,8 +212,9 @@ public Control createControl(CompositeRuler parentRuler, Composite parentControl control.addMouseTrackListener(new MouseTrackAdapter() { @Override public void mouseExit(MouseEvent e) { - if (clearCurrentAnnotation()) + if (clearCurrentAnnotation()) { redraw(); + } } }); @@ -226,8 +233,9 @@ public void mouseExit(MouseEvent e) { redraw= true; } } - if (redraw) + if (redraw) { redraw(); + } }); return control; } diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSummary.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSummary.java index abc630a793d..0633eccf591 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSummary.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSummary.java @@ -66,8 +66,9 @@ public void reset() { public void run() { while (true) { synchronized (fLock) { - if (!fReset) + if (!fReset) { break; + } fReset= false; fProgressMonitor.setCanceled(false); } @@ -112,8 +113,9 @@ public void addAnnotationType(String annotationType) { if (fConfiguredAnnotationTypes == null) { fConfiguredAnnotationTypes= new ArrayList<>(); fConfiguredAnnotationTypes.add(annotationType); - } else if (!fConfiguredAnnotationTypes.contains(annotationType)) + } else if (!fConfiguredAnnotationTypes.contains(annotationType)) { fConfiguredAnnotationTypes.add(annotationType); + } } } @@ -127,8 +129,9 @@ public void removeAnnotationType(String annotationType) { synchronized (fLock) { if (fConfiguredAnnotationTypes != null) { fConfiguredAnnotationTypes.remove(annotationType); - if (fConfiguredAnnotationTypes.isEmpty()) + if (fConfiguredAnnotationTypes.isEmpty()) { fConfiguredAnnotationTypes= null; + } } } } @@ -139,8 +142,9 @@ public void removeAnnotationType(String annotationType) { public void updateSummaries() { synchronized (fLock) { if (fConfiguredAnnotationTypes != null) { - if (fSummarizer == null) + if (fSummarizer == null) { fSummarizer= new Summarizer(); + } fSummarizer.reset(); } } @@ -148,13 +152,15 @@ public void updateSummaries() { private void internalUpdateSummaries(IProgressMonitor monitor) { IAnnotationModel visualAnnotationModel= fProjectionViewer.getVisualAnnotationModel(); - if (visualAnnotationModel == null) + if (visualAnnotationModel == null) { return; + } removeSummaries(monitor, visualAnnotationModel); - if (isCanceled(monitor)) + if (isCanceled(monitor)) { return; + } createSummaries(monitor, visualAnnotationModel); } @@ -176,28 +182,32 @@ private void removeSummaries(IProgressMonitor monitor, IAnnotationModel visualAn while (e.hasNext()) { Annotation annotation= e.next(); if (annotation instanceof AnnotationBag) { - if (bags == null) + if (bags == null) { visualAnnotationModel.removeAnnotation(annotation); - else + } else { bags.add(annotation); + } } - if (isCanceled(monitor)) + if (isCanceled(monitor)) { return; + } } if (bags != null && !bags.isEmpty() && extension != null) { Annotation[] deletions= new Annotation[bags.size()]; bags.toArray(deletions); - if (!isCanceled(monitor)) + if (!isCanceled(monitor)) { extension.replaceAnnotations(deletions, null); + } } } private void createSummaries(IProgressMonitor monitor, IAnnotationModel visualAnnotationModel) { ProjectionAnnotationModel model= fProjectionViewer.getProjectionAnnotationModel(); - if (model == null) + if (model == null) { return; + } Map additions= new HashMap<>(); @@ -210,26 +220,30 @@ private void createSummaries(IProgressMonitor monitor, IAnnotationModel visualAn IRegion[] summaryRegions= fProjectionViewer.computeCollapsedRegions(position); if (summaryRegions != null) { Position summaryAnchor= fProjectionViewer.computeCollapsedRegionAnchor(position); - if (summaryAnchor != null) + if (summaryAnchor != null) { createSummary(additions, summaryRegions, summaryAnchor); + } } } } - if (isCanceled(monitor)) + if (isCanceled(monitor)) { return; + } } if (!additions.isEmpty()) { if (visualAnnotationModel instanceof IAnnotationModelExtension extension) { - if (!isCanceled(monitor)) + if (!isCanceled(monitor)) { extension.replaceAnnotations(null, additions); + } } else { for (Entry entry : additions.entrySet()) { AnnotationBag bag= (AnnotationBag) entry.getKey(); Position position= entry.getValue(); - if (isCanceled(monitor)) + if (isCanceled(monitor)) { return; + } visualAnnotationModel.addAnnotation(bag, position); } } @@ -252,27 +266,31 @@ private void createSummary(Map additions, IRegion[] summar } } - if (map == null) + if (map == null) { return; + } IAnnotationModel model= fProjectionViewer.getAnnotationModel(); - if (model == null) + if (model == null) { return; + } Iterator e= model.getAnnotationIterator(); while (e.hasNext()) { Annotation annotation= e.next(); AnnotationBag bag= findBagForType(map, annotation.getType()); if (bag != null) { Position position= model.getPosition(annotation); - if (includes(summaryRegions, position)) + if (includes(summaryRegions, position)) { bag.add(annotation); + } } } for (int i= 0; i < size; i++) { AnnotationBag bag= map.get(fConfiguredAnnotationTypes.get(i)); - if (!bag.isEmpty()) + if (!bag.isEmpty()) { additions.put(bag, new Position(summaryAnchor.getOffset(), summaryAnchor.getLength())); + } } } @@ -290,8 +308,9 @@ private AnnotationBag findBagForType(Map bagMap, String a private boolean includes(IRegion[] regions, Position position) { for (IRegion region : regions) { if (position != null && !position.isDeleted() - && region.getOffset() <= position.getOffset() && position.getOffset() + position.getLength() <= region.getOffset() + region.getLength()) + && region.getOffset() <= position.getOffset() && position.getOffset() + position.getLength() <= region.getOffset() + region.getLength()) { return true; + } } return false; } diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSupport.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSupport.java index 01a13c48003..7b35568475b 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSupport.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSupport.java @@ -73,8 +73,9 @@ protected IAnnotationModel findAnnotationModel(ISourceViewer sourceViewer) { @Override protected boolean skip(Annotation annotation) { - if (annotation instanceof ProjectionAnnotation) + if (annotation instanceof ProjectionAnnotation) { return !((ProjectionAnnotation) annotation).isCollapsed(); + } return super.skip(annotation); } @@ -186,8 +187,9 @@ public void addSummarizableAnnotationType(String annotationType) { if (fSummarizableTypes == null) { fSummarizableTypes= new ArrayList<>(); fSummarizableTypes.add(annotationType); - } else if (!fSummarizableTypes.contains(annotationType)) + } else if (!fSummarizableTypes.contains(annotationType)) { fSummarizableTypes.add(annotationType); + } } /** @@ -206,8 +208,9 @@ public void addSummarizableAnnotationType(String annotationType) { public void removeSummarizableAnnotationType(String annotationType) { if (fSummarizableTypes != null) { fSummarizableTypes.remove(annotationType); - if (fSummarizableTypes.isEmpty()) + if (fSummarizableTypes.isEmpty()) { fSummarizableTypes= null; + } } } @@ -254,8 +257,9 @@ public void setAnnotationPainterDrawingStrategy(AnnotationPainter.IDrawingStrate * @since 3.1 */ private AnnotationPainter.IDrawingStrategy getDrawingStrategy() { - if (fDrawingStrategy == null) + if (fDrawingStrategy == null) { fDrawingStrategy= new ProjectionDrawingStrategy(); + } return fDrawingStrategy; } @@ -325,8 +329,9 @@ private ProjectionSummary createProjectionSummary() { ProjectionSummary summary= new ProjectionSummary(fViewer, fAnnotationAccess); if (fSummarizableTypes != null) { int size= fSummarizableTypes.size(); - for (int i= 0; i < size; i++) + for (int i= 0; i < size; i++) { summary.addAnnotationType(fSummarizableTypes.get(i)); + } } return summary; } diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionViewer.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionViewer.java index 4dfee49d5af..fa4eac0e6a1 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionViewer.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionViewer.java @@ -117,12 +117,14 @@ public void modelChanged(AnnotationModelEvent event) { private void processModelChanged(IAnnotationModel model, AnnotationModelEvent event) { if (model == fProjectionAnnotationModel) { - if (fProjectionSummary != null) + if (fProjectionSummary != null) { fProjectionSummary.updateSummaries(); + } processCatchupRequest(event); - } else if (model == getAnnotationModel() && fProjectionSummary != null) + } else if (model == getAnnotationModel() && fProjectionSummary != null) { fProjectionSummary.updateSummaries(); + } } } @@ -245,14 +247,16 @@ void clear() { } boolean passedRedrawCostsThreshold() { - if (fExpectedExecutionCosts == -1) + if (fExpectedExecutionCosts == -1) { computeExpectedExecutionCosts(); + } return fExpectedExecutionCosts > REDRAW_COSTS; } boolean passedInvalidationCostsThreshold() { - if (fExpectedExecutionCosts == -1) + if (fExpectedExecutionCosts == -1) { computeExpectedExecutionCosts(); + } return fExpectedExecutionCosts > INVALIDATION_COSTS; } @@ -265,8 +269,9 @@ private void computeExpectedExecutionCosts() { while (e.hasNext()) { command= e.next(); fExpectedExecutionCosts += command.computeExpectedCosts(); - if (fExpectedExecutionCosts > max_costs) + if (fExpectedExecutionCosts > max_costs) { break; + } } } } @@ -365,8 +370,9 @@ public void setDocument(IDocument document, IAnnotationModel annotationModel, in super.setDocument(document, annotationModel, modelRangeOffset, modelRangeLength); - if (wasProjectionEnabled && document != null) + if (wasProjectionEnabled && document != null) { enableProjection(); + } } @@ -433,11 +439,13 @@ public void addProjectionListener(IProjectionListener listener) { Assert.isNotNull(listener); - if (fProjectionListeners == null) + if (fProjectionListeners == null) { fProjectionListeners= new ArrayList<>(); + } - if (!fProjectionListeners.contains(listener)) + if (!fProjectionListeners.contains(listener)) { fProjectionListeners.add(listener); + } } /** @@ -453,8 +461,9 @@ public void removeProjectionListener(IProjectionListener listener) { if (fProjectionListeners != null) { fProjectionListeners.remove(listener); - if (fProjectionListeners.isEmpty()) + if (fProjectionListeners.isEmpty()) { fProjectionListeners= null; + } } } @@ -538,11 +547,12 @@ private void expand() { if (annotation.isCollapsed()) { Position position= fProjectionAnnotationModel.getPosition(annotation); // take the first most fine grained match - if (position != null && touches(selection, position)) + if (position != null && touches(selection, position)) { if (found == null || position.includes(found.offset) && position.includes(found.offset + found.length)) { found= position; bestMatch= annotation; } + } } } @@ -567,11 +577,12 @@ private void collapse() { if (!annotation.isCollapsed()) { Position position= fProjectionAnnotationModel.getPosition(annotation); // take the first most fine grained match - if (position != null && touches(selection, position)) + if (position != null && touches(selection, position)) { if (found == null || found.includes(position.offset) && found.includes(position.offset + position.length)) { found= position; bestMatch= annotation; } + } } } @@ -667,11 +678,13 @@ private void removeMasterDocumentRange(ProjectionDocument projection, int offset * @since 3.2 */ private int toLineStart(IDocument document, int offset, boolean testLastLine) throws BadLocationException { - if (document == null) + if (document == null) { return offset; + } - if (testLastLine && offset >= document.getLineInformationOfOffset(document.getLength() - 1).getOffset()) + if (testLastLine && offset >= document.getLineInformationOfOffset(document.getLength() - 1).getOffset()) { return offset; + } return document.getLineInformationOfOffset(offset).getOffset(); } @@ -699,16 +712,18 @@ protected void setVisibleDocument(IDocument document) { @Override public void resetVisibleRegion() { super.resetVisibleRegion(); - if (fWasProjectionEnabled) + if (fWasProjectionEnabled) { enableProjection(); + } } @Override public IRegion getVisibleRegion() { disableProjection(); IRegion visibleRegion= getModelCoverage(); - if (visibleRegion == null) + if (visibleRegion == null) { visibleRegion= new Region(0, 0); + } return visibleRegion; } @@ -717,8 +732,9 @@ public IRegion getVisibleRegion() { public boolean overlapsWithVisibleRegion(int offset, int length) { disableProjection(); IRegion coverage= getModelCoverage(); - if (coverage == null) + if (coverage == null) { return false; + } boolean appending= (offset == coverage.getOffset() + coverage.getLength()) && length == 0; return appending || TextUtilities.overlaps(coverage, new Region(offset, length)); @@ -734,28 +750,32 @@ private void replaceVisibleDocument(IDocument slave) { if (fReplaceVisibleDocumentExecutionTrigger != null) { ReplaceVisibleDocumentExecutor executor= new ReplaceVisibleDocumentExecutor(slave); executor.install(fReplaceVisibleDocumentExecutionTrigger); - } else + } else { executeReplaceVisibleDocument(slave); + } } private void executeReplaceVisibleDocument(IDocument visibleDocument) { StyledText textWidget= getTextWidget(); try { - if (textWidget != null && !textWidget.isDisposed()) + if (textWidget != null && !textWidget.isDisposed()) { textWidget.setRedraw(false); + } int topIndex= getTopIndex(); Point selection= getSelectedRange(); setVisibleDocument(visibleDocument); Point currentSelection= getSelectedRange(); - if (currentSelection.x != selection.x || currentSelection.y != selection.y) + if (currentSelection.x != selection.x || currentSelection.y != selection.y) { setSelectedRange(selection.x, selection.y); + } setTopIndex(topIndex); } finally { - if (textWidget != null && !textWidget.isDisposed()) + if (textWidget != null && !textWidget.isDisposed()) { textWidget.setRedraw(true); + } } } @@ -771,9 +791,9 @@ private void collapse(int offset, int length, boolean fireRedraw) throws BadLoca ProjectionDocument projection= null; IDocument visibleDocument= getVisibleDocument(); - if (visibleDocument instanceof ProjectionDocument) + if (visibleDocument instanceof ProjectionDocument) { projection= (ProjectionDocument) visibleDocument; - else { + } else { IDocument master= getDocument(); IDocument slave= createSlaveDocument(getDocument()); if (slave instanceof ProjectionDocument) { @@ -783,8 +803,9 @@ private void collapse(int offset, int length, boolean fireRedraw) throws BadLoca } } - if (projection != null) + if (projection != null) { removeMasterDocumentRange(projection, offset, length); + } if (projection != null && fireRedraw) { // repaint line above to get the folding box @@ -827,8 +848,9 @@ private void expand(int offset, int length, boolean fireRedraw) throws BadLocati } // redraw if requested - if (fireRedraw) + if (fireRedraw) { internalInvalidateTextPresentation(offset, length); + } } } @@ -852,8 +874,9 @@ protected final void processCatchupRequest(AnnotationModelEvent event) { throw new IllegalArgumentException(x); } - } else + } else { postCatchupRequest(event); + } } else { postCatchupRequest(event); } @@ -877,8 +900,9 @@ protected final void postCatchupRequest(final AnnotationModelEvent event) { while (true) { AnnotationModelEvent ame= null; synchronized (fLock) { - if (fPendingRequests.isEmpty()) + if (fPendingRequests.isEmpty()) { return; + } ame= fPendingRequests.remove(0); } catchupWithProjectionAnnotationModel(ame); @@ -934,8 +958,9 @@ private void catchupWithProjectionAnnotationModel(AnnotationModelEvent event) th if (event.isValid()) { fPendingAnnotationWorldChange= false; reinitializeProjection(); - } else + } else { fPendingAnnotationWorldChange= true; + } } else if (fPendingAnnotationWorldChange) { if (event.isValid()) { @@ -974,8 +999,9 @@ private void catchupWithProjectionAnnotationModel(AnnotationModelEvent event) th try { boolean fireRedraw= !commandQueue.passedInvalidationCostsThreshold(); executeProjectionCommands(commandQueue, fireRedraw); - if (!fireRedraw) + if (!fireRedraw) { invalidateTextPresentation(); + } } catch (IllegalArgumentException x) { reinitializeProjection(); } @@ -997,8 +1023,9 @@ private void executeProjectionCommands(ProjectionCommandQueue commandQueue, bool removeMasterDocumentRange(command.fProjection, command.fOffset, command.fLength); break; case ProjectionCommand.INVALIDATE_PRESENTATION: - if (fireRedraw) + if (fireRedraw) { invalidateTextPresentation(command.fOffset, command.fLength); + } break; } } @@ -1063,15 +1090,17 @@ private void processDeletions(AnnotationModelEvent event, Annotation[] removedAn public IRegion computeCollapsedRegion(Position position) { try { IDocument document= getDocument(); - if (document == null) + if (document == null) { return null; + } int line= document.getLineOfOffset(position.getOffset()); int offset= document.getLineOffset(line + 1); int length= position.getLength() - (offset - position.getOffset()); - if (length > 0) + if (length > 0) { return new Region(offset, length); + } } catch (BadLocationException x) { } @@ -1090,8 +1119,9 @@ public IRegion computeCollapsedRegion(Position position) { IRegion[] computeCollapsedRegions(Position position) { try { IDocument document= getDocument(); - if (document == null) + if (document == null) { return null; + } if (position instanceof IProjectionPosition projPosition) { return projPosition.computeProjectionRegions(document); @@ -1101,8 +1131,9 @@ IRegion[] computeCollapsedRegions(Position position) { int offset= document.getLineOffset(line + 1); int length= position.getLength() - (offset - position.getOffset()); - if (length > 0) + if (length > 0) { return new IRegion[] {new Region(offset, length)}; + } return null; } catch (BadLocationException x) { @@ -1122,12 +1153,14 @@ IRegion[] computeCollapsedRegions(Position position) { public Position computeCollapsedRegionAnchor(Position position) { try { IDocument document= getDocument(); - if (document == null) + if (document == null) { return null; + } int captionOffset= position.getOffset(); - if (position instanceof IProjectionPosition) + if (position instanceof IProjectionPosition) { captionOffset+= ((IProjectionPosition) position).computeCaptionOffset(document); + } IRegion lineInfo= document.getLineInformationOfOffset(captionOffset); return new Position(lineInfo.getOffset() + lineInfo.getLength(), 0); @@ -1141,8 +1174,9 @@ private void processChanges(Annotation[] annotations, boolean fireRedraw, List

coverage, Position position) { Iterator e= coverage.iterator(); while (e.hasNext()) { Position p= e.next(); - if (p.getOffset() <= position.getOffset() && position.getOffset() + position.getLength() <= p.getOffset() + p.getLength()) + if (p.getOffset() <= position.getOffset() && position.getOffset() + position.getLength() <= p.getOffset() + p.getLength()) { return true; + } } return false; } @@ -1200,10 +1235,11 @@ public final void reinitializeProjection() throws BadLocationException { Position position= fProjectionAnnotationModel.getPosition(annotation); if (position != null) { IRegion[] regions= computeCollapsedRegions(position); - if (regions != null) + if (regions != null) { for (IRegion region : regions) { removeMasterDocumentRange(projection, region.getOffset(), region.getLength()); } + } } } } @@ -1217,8 +1253,9 @@ public final void reinitializeProjection() throws BadLocationException { protected void handleVerifyEvent(VerifyEvent e) { if (getTextWidget().getBlockSelection()) { ITextSelection selection= (ITextSelection) getSelection(); - if (exposeModelRange(new Region(selection.getOffset(), selection.getLength()))) + if (exposeModelRange(new Region(selection.getOffset(), selection.getLength()))) { setSelection(selection); + } super.handleVerifyEvent(e); return; } @@ -1230,22 +1267,25 @@ protected void handleVerifyEvent(VerifyEvent e) { try { if (selection.y == 0 && e.text.length() <= 1 && modelRange.getLength() == 1) { selection.y= 1; - if (selection.x - 1 == modelRange.getOffset()) + if (selection.x - 1 == modelRange.getOffset()) { selection.x--; + } } getDocument().replace(selection.x, selection.y, e.text); setSelectedRange(selection.x + e.text.length(), 0); } catch (BadLocationException e1) { // ignore as nothing bad happens (no log at this level) } - } else + } else { super.handleVerifyEvent(e); + } } @Override public boolean exposeModelRange(IRegion modelRange) { - if (isProjectionMode()) + if (isProjectionMode()) { return fProjectionAnnotationModel.expandAll(modelRange.getOffset(), modelRange.getLength()); + } if (!overlapsWithVisibleRegion(modelRange.getOffset(), modelRange.getLength())) { resetVisibleRegion(); @@ -1264,28 +1304,33 @@ public void setRangeIndication(int offset, int length, boolean moveCursor) { Iterator iterator= fProjectionAnnotationModel.getAnnotationIterator(); while (iterator.hasNext()) { ProjectionAnnotation annotation= (ProjectionAnnotation)iterator.next(); - if (annotation.isCollapsed() && willAutoExpand(fProjectionAnnotationModel.getPosition(annotation), offset, length)) + if (annotation.isCollapsed() && willAutoExpand(fProjectionAnnotationModel.getPosition(annotation), offset, length)) { expand.add(annotation); + } } if (!expand.isEmpty()) { Iterator e= expand.iterator(); - while (e.hasNext()) + while (e.hasNext()) { fProjectionAnnotationModel.expand(e.next()); + } } } super.setRangeIndication(offset, length, moveCursor); } private boolean willAutoExpand(Position position, int offset, int length) { - if (position == null || position.isDeleted()) + if (position == null || position.isDeleted()) { return false; + } // right or left boundary - if (position.getOffset() == offset || position.getOffset() + position.getLength() == offset + length) + if (position.getOffset() == offset || position.getOffset() + position.getLength() == offset + length) { return true; + } // completely embedded in given position - if (position.getOffset() < offset && offset + length < position.getOffset() + position.getLength()) + if (position.getOffset() < offset && offset + length < position.getOffset() + position.getLength()) { return true; + } return false; } @@ -1302,20 +1347,23 @@ protected void handleDispose() { protected void handleVisibleDocumentChanged(DocumentEvent event) { if (fHandleProjectionChanges && event instanceof ProjectionDocumentEvent e && isProjectionMode()) { DocumentEvent master= e.getMasterEvent(); - if (master != null) + if (master != null) { fReplaceVisibleDocumentExecutionTrigger= master.getDocument(); + } try { int replaceLength= e.getText() == null ? 0 : e.getText().length(); if (ProjectionDocumentEvent.PROJECTION_CHANGE == e.getChangeType()) { - if (e.getLength() == 0 && replaceLength != 0) + if (e.getLength() == 0 && replaceLength != 0) { fProjectionAnnotationModel.expandAll(e.getMasterOffset(), e.getMasterLength()); + } } else if (master != null && (replaceLength > 0 || fDeletedLines > 1)) { try { int numberOfLines= e.getDocument().getNumberOfLines(e.getOffset(), replaceLength); - if (numberOfLines > 1 || fDeletedLines > 1) + if (numberOfLines > 1 || fDeletedLines > 1) { fProjectionAnnotationModel.expandAll(master.getOffset(), replaceLength); + } } catch (BadLocationException x) { } } @@ -1342,8 +1390,9 @@ protected void handleVisibleDocumentAboutToBeChanged(DocumentEvent event) { @Override public IRegion[] getCoveredModelRanges(IRegion modelRange) { - if (fInformationMapping == null) + if (fInformationMapping == null) { return new IRegion[] { new Region(modelRange.getOffset(), modelRange.getLength()) }; + } if (fInformationMapping instanceof IDocumentInformationMappingExtension extension) { try { @@ -1376,8 +1425,9 @@ public void doOperation(int operation) { } StyledText textWidget= getTextWidget(); - if (textWidget == null) + if (textWidget == null) { return; + } ITextSelection selection= null; switch (operation) { @@ -1386,13 +1436,15 @@ public void doOperation(int operation) { if (redraws()) { selection= (ITextSelection) getSelection(); - if (exposeModelRange(new Region(selection.getOffset(), selection.getLength()))) + if (exposeModelRange(new Region(selection.getOffset(), selection.getLength()))) { setSelection(selection); + } - if (selection.getLength() == 0) + if (selection.getLength() == 0) { copyMarkedRegion(true); - else + } else { copyToClipboard(selection, true, textWidget); + } Point range= textWidget.getSelectionRange(); fireSelectionChanged(range.x, range.y); @@ -1403,10 +1455,11 @@ public void doOperation(int operation) { if (redraws()) { selection= (ITextSelection) getSelection(); - if (selection.getLength() == 0) + if (selection.getLength() == 0) { copyMarkedRegion(false); - else + } else { copyToClipboard(selection, false, textWidget); + } } break; @@ -1416,10 +1469,11 @@ public void doOperation(int operation) { try { selection= (ITextSelection) getSelection(); int length= selection.getLength(); - if (!textWidget.getBlockSelection() && (length == 0 || length == textWidget.getSelectionRange().y)) + if (!textWidget.getBlockSelection() && (length == 0 || length == textWidget.getSelectionRange().y)) { getTextWidget().invokeAction(ST.DELETE_NEXT); - else + } else { deleteSelection(selection, textWidget); + } Point range= textWidget.getSelectionRange(); fireSelectionChanged(range.x, range.y); @@ -1432,8 +1486,9 @@ public void doOperation(int operation) { case EXPAND_ALL: - if (redraws()) + if (redraws()) { expandAll(); + } break; case EXPAND: @@ -1443,8 +1498,9 @@ public void doOperation(int operation) { break; case COLLAPSE_ALL: - if (redraws()) + if (redraws()) { collapseAll(); + } break; case COLLAPSE: @@ -1483,11 +1539,13 @@ private boolean isSegmented() { } private IRegion getMarkedRegion() { - if (getTextWidget() == null) + if (getTextWidget() == null) { return null; + } - if (fMarkPosition == null || fMarkPosition.isDeleted()) + if (fMarkPosition == null || fMarkPosition.isDeleted()) { return null; + } int start= fMarkPosition.getOffset(); int end= getSelectedRange().x; @@ -1498,15 +1556,17 @@ private IRegion getMarkedRegion() { @Override protected void copyMarkedRegion(boolean delete) { IRegion markedRegion= getMarkedRegion(); - if (markedRegion != null) + if (markedRegion != null) { copyToClipboard(new TextSelection(getDocument(), markedRegion.getOffset(), markedRegion.getLength()), delete, getTextWidget()); + } } private void copyToClipboard(ITextSelection selection, boolean delete, StyledText textWidget) { String copyText= selection.getText(); - if (copyText == null) // selection.getText failed - backup using widget + if (copyText == null) { // selection.getText failed - backup using widget textWidget.copy(); + } if (copyText != null && copyText.equals(textWidget.getSelectionText())) { /* @@ -1524,8 +1584,9 @@ private void copyToClipboard(ITextSelection selection, boolean delete, StyledTex try { clipboard.setContents(data, dataTypes); } catch (SWTError e) { - if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) + if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) { throw e; + } /* * TODO see https://bugs.eclipse.org/bugs/show_bug.cgi?id=59459 * we should either log and/or inform the user @@ -1569,8 +1630,9 @@ private void deleteSelection(ITextSelection selection, StyledText textWidget) th @Override protected Point widgetSelection2ModelSelection(Point widgetSelection) { - if (!isProjectionMode()) + if (!isProjectionMode()) { return super.widgetSelection2ModelSelection(widgetSelection); + } /* * There is one requirement that governs preservation of logical @@ -1599,33 +1661,36 @@ protected Point widgetSelection2ModelSelection(Point widgetSelection) { * overlaps with the widget selection */ IRegion modelSelection= widgetRange2ModelRange(new Region(widgetSelection.x, widgetSelection.y)); - if (modelSelection == null) + if (modelSelection == null) { return null; + } int modelOffset= modelSelection.getOffset(); int modelEndOffset= modelOffset + modelSelection.getLength(); /* Case A: never expand a zero-length selection. S is *behind* P. */ - if (widgetSelection.y == 0) + if (widgetSelection.y == 0) { return new Point(modelEndOffset, 0); + } int widgetSelectionExclusiveEnd= widgetSelection.x + widgetSelection.y; Position[] annotationPositions= computeOverlappingAnnotationPositions(modelSelection); for (Position annotationPosition : annotationPositions) { IRegion[] regions = computeCollapsedRegions(annotationPosition); - if (regions == null) + if (regions == null) { continue; + } for (IRegion modelRange : regions) { IRegion widgetRange= modelRange2ClosestWidgetRange(modelRange); // only take collapsed ranges, i.e. widget length is 0 if (widgetRange != null && widgetRange.getLength() == 0) { int widgetOffset= widgetRange.getOffset(); // D) region is collapsed at S.widget_offset - if (widgetOffset == widgetSelection.x) + if (widgetOffset == widgetSelection.x) { modelOffset= Math.min(modelOffset, modelRange.getOffset()); - // C) region is collapsed at S.widget_end - else if (widgetOffset == widgetSelectionExclusiveEnd) + } else if (widgetOffset == widgetSelectionExclusiveEnd) { modelEndOffset= Math.max(modelEndOffset, modelRange.getOffset() + modelRange.getLength()); + } } } } @@ -1645,8 +1710,9 @@ private Position[] computeOverlappingAnnotationPositions(IRegion modelSelection) for (Iterator e= fProjectionAnnotationModel.getAnnotationIterator(); e.hasNext();) { ProjectionAnnotation annotation= (ProjectionAnnotation) e.next(); Position position= fProjectionAnnotationModel.getPosition(annotation); - if (position != null && position.overlapsWith(modelSelection.getOffset(), modelSelection.getLength()) && modelRange2WidgetRange(position) != null) + if (position != null && position.overlapsWith(modelSelection.getOffset(), modelSelection.getLength()) && modelRange2WidgetRange(position) != null) { positions.add(position); + } } return positions.toArray(new Position[positions.size()]); } @@ -1663,12 +1729,14 @@ protected FindReplaceDocumentAdapter getFindReplaceDocumentAdapter() { @Override protected int findAndSelect(int startPosition, String findString, boolean forwardSearch, boolean caseSensitive, boolean wholeWord, boolean regExSearch) { - if (!isProjectionMode()) + if (!isProjectionMode()) { return super.findAndSelect(startPosition, findString, forwardSearch, caseSensitive, wholeWord, regExSearch); + } StyledText textWidget= getTextWidget(); - if (textWidget == null) + if (textWidget == null) { return -1; + } try { @@ -1689,12 +1757,14 @@ protected int findAndSelect(int startPosition, String findString, boolean forwar @Override protected int findAndSelectInRange(int startPosition, String findString, boolean forwardSearch, boolean caseSensitive, boolean wholeWord, int rangeOffset, int rangeLength, boolean regExSearch) { - if (!isProjectionMode()) + if (!isProjectionMode()) { return super.findAndSelectInRange(startPosition, findString, forwardSearch, caseSensitive, wholeWord, rangeOffset, rangeLength, regExSearch); + } StyledText textWidget= getTextWidget(); - if (textWidget == null) + if (textWidget == null) { return -1; + } try { diff --git a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/SourceViewerInformationControl.java b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/SourceViewerInformationControl.java index 414163c4c52..f3e7c9d5a4e 100644 --- a/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/SourceViewerInformationControl.java +++ b/bundles/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/SourceViewerInformationControl.java @@ -139,8 +139,9 @@ public SourceViewerInformationControl(Shell parent, boolean isResizable, String @Override public void keyPressed(KeyEvent e) { - if (e.character == 0x1B) // ESC + if (e.character == 0x1B) { // ESC fShell.dispose(); + } } @Override @@ -208,10 +209,11 @@ private static RGB blend(RGB bg, RGB fg, float factor) { * @param input the input object */ public void setInput(Object input) { - if (input instanceof String) + if (input instanceof String) { setInformation((String)input); - else + } else { setInformation(null); + } } @Override @@ -232,8 +234,9 @@ public void setVisible(boolean visible) { @Override public void widgetDisposed(DisposeEvent event) { - if (fStatusTextFont != null && !fStatusTextFont.isDisposed()) + if (fStatusTextFont != null && !fStatusTextFont.isDisposed()) { fStatusTextFont.dispose(); + } fStatusTextFont= null; fTextFont= null; @@ -243,10 +246,11 @@ public void widgetDisposed(DisposeEvent event) { @Override public final void dispose() { - if (fShell != null && !fShell.isDisposed()) + if (fShell != null && !fShell.isDisposed()) { fShell.dispose(); - else + } else { widgetDisposed(null); + } } @Override @@ -260,8 +264,9 @@ public void setSize(int width, int height) { } fShell.setSize(width, height); - if (fStatusField != null) + if (fStatusField != null) { fShell.pack(true); + } } @Override @@ -281,14 +286,17 @@ public Point computeSizeHint() { int x= SWT.DEFAULT; int y= SWT.DEFAULT; Point size= fShell.computeSize(x, y); - if (size.x > fMaxWidth) + if (size.x > fMaxWidth) { x= fMaxWidth; - if (size.y > fMaxHeight) + } + if (size.y > fMaxHeight) { y= fMaxHeight; + } // recompute using the constraints if the preferred size is larger than the constraints - if (x != SWT.DEFAULT || y != SWT.DEFAULT) + if (x != SWT.DEFAULT || y != SWT.DEFAULT) { size= fShell.computeSize(x, y, false); + } return size; } @@ -382,10 +390,12 @@ public IInformationControlCreator getInformationPresenterControlCreator() { @Override public boolean containsControl(Control control) { do { - if (control == fShell) + if (control == fShell) { return true; - if (control instanceof Shell) + } + if (control instanceof Shell) { return false; + } control= control.getParent(); } while (control != null); return false;