You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "matthiasblaesing (via GitHub)" <gi...@apache.org> on 2024/12/03 19:35:52 UTC
Re: [PR] Fix ugly semantic coloring on CSL languages while editing [netbeans]
matthiasblaesing commented on code in PR #8011:
URL: https://github.com/apache/netbeans/pull/8011#discussion_r1868283066
##########
ide/csl.api/src/org/netbeans/modules/csl/editor/semantic/SemanticHighlighter.java:
##########
@@ -133,139 +109,39 @@ public final void cancel() {
}
final GsfSemanticLayer layer = GsfSemanticLayer.getLayer(SemanticHighlighter.class, doc);
- SwingUtilities.invokeLater(new Runnable () {
- public void run() {
- // XXX: parsingapi
- layer.setColorings(newColoring, -1); //version
- }
+ SwingUtilities.invokeLater(() -> {
+ // XXX: parsingapi
+ layer.setColorings(newColoring);
});
} finally {
SpiSupportAccessor.getInstance().removeCancelSupport(cancel);
}
}
+ private void processColorings(ResultIterator resultIterator, SortedSet<SequenceElement> newColoring) throws ParseException {
+ String mimeType = resultIterator.getSnapshot().getMimeType();
+ Language language = LanguageRegistry.getInstance().getLanguageByMimeType(mimeType);
+ if (language != null) {
+ ColoringManager manager = language.getColoringManager();
+ SemanticAnalyzer task = language.getSemanticAnalyzer();
+ if (manager != null && task != null) {
+ Parser.Result r = resultIterator.getParserResult();
+ if (r instanceof ParserResult) {
+ process(language, (ParserResult) r, newColoring);
+ }
+ }
+ }
Review Comment:
I think this can be simplified to (`manager` and `task` can't be null, at least that is my reading from the code and assured by api annotations).
```suggestion
if (language != null) {
Parser.Result r = resultIterator.getParserResult();
if (r instanceof ParserResult parserResult) {
process(language, parserResult, newColoring);
}
}
```
##########
ide/csl.api/src/org/netbeans/modules/csl/editor/semantic/GsfSemanticLayer.java:
##########
@@ -53,19 +50,17 @@
*
* @author Tor Norbye
*/
-public class GsfSemanticLayer extends AbstractHighlightsContainer implements DocumentListener {
+public final class GsfSemanticLayer extends AbstractHighlightsContainer implements DocumentListener {
// -J-Dorg.netbeans.modules.csl.editor.semantic.GsfSemanticLayer.level=FINE
private static final Logger LOG = Logger.getLogger(GsfSemanticLayer.class.getName());
- //private Map<Token, Coloring> colorings;
- private SortedSet<SequenceElement> colorings;
- private int version;
+ private List<SequenceElement> colorings = List.of();
private List<Edit> edits;
- private Map<Language,Map<Coloring, AttributeSet>> CACHE = new HashMap<Language,Map<Coloring, AttributeSet>>();
- private Document doc;
- private List<Lookup.Result> coloringResults = new ArrayList<Lookup.Result>(3);
- private List<LookupListener> coloringListeners = new ArrayList<LookupListener>(3);
+ private final Map<Language,Map<Coloring, AttributeSet>> cache = new HashMap<>();
+ private final Document doc;
+ private final List<Lookup.Result> coloringResults = new ArrayList<>(3);
+ private final List<LookupListener> coloringListeners = new ArrayList<>(3);
Review Comment:
My take on the reason for this:
```suggestion
// Write only - used to keep strong reference to Lookup.Result/LookupListener
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final List<Lookup.Result> coloringResults = new ArrayList<>(3);
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final List<LookupListener> coloringListeners = new ArrayList<>(3);
```
##########
ide/csl.api/src/org/netbeans/modules/csl/editor/semantic/GsfSemanticLayer.java:
##########
@@ -233,101 +187,61 @@ public int getShiftedPos(int pos) {
* of ranges (e.g. for every highlight in the whole document) whereas asking for the
* current positions is typically only done for the highlights visible on the screen.
*/
- private class Edit {
- public Edit(int offset, int len, boolean insert) {
- this.offset = offset;
- this.len = len;
- this.insert = insert;
- }
-
- int offset;
- int len;
- boolean insert; // true: insert, false: delete
- }
+ private record Edit(int offset, int len, boolean insert) {}
/**
* An implementation of a HighlightsSequence which can show OffsetRange
* sections and keep them up to date during edits.
*
* @author Tor Norbye
*/
- private static final class GsfHighlightSequence implements HighlightsSequence {
- private Iterator<SequenceElement> iterator;
+ private final class GsfHighlightSequence implements HighlightsSequence {
+ private final Iterator<SequenceElement> iterator;
private SequenceElement element;
- private final GsfSemanticLayer layer;
- private final int endOffset;
- private SequenceElement nextElement;
- private int nextElementStartOffset = Integer.MAX_VALUE;
- GsfHighlightSequence(GsfSemanticLayer layer, Document doc,
- int startOffset, int endOffset,
- SortedSet<SequenceElement> colorings) {
- this.layer = layer;
- this.endOffset = endOffset;
-
- SequenceElement.ComparisonItem fromInclusive = new SequenceElement.ComparisonItem(startOffset);
- SortedSet<SequenceElement> subMap = colorings.tailSet(fromInclusive);
- iterator = subMap.iterator();
- }
-
- private SequenceElement fetchElementFromIterator(boolean updateNextElementStartOffset) {
- int seStartOffset;
- SequenceElement se;
- if (iterator != null && iterator.hasNext()) {
- se = iterator.next();
- seStartOffset = se.range.getStart();
- if (LOG.isLoggable(Level.FINE)) {
- LOG.log(Level.FINE, "Fetched highlight <{0},{1}>\n", // NOI18N
- new Object[]{seStartOffset, se.range.getEnd()});
- }
- if (seStartOffset >= endOffset) {
- se = null;
- seStartOffset = Integer.MAX_VALUE;
- iterator = null;
- }
- } else {
- se = null;
- seStartOffset = Integer.MAX_VALUE;
- iterator = null;
- }
- if (updateNextElementStartOffset) {
- nextElementStartOffset = seStartOffset;
- }
- return se;
+ GsfHighlightSequence(Iterator<SequenceElement> it) {
+ iterator = it;
}
@Override
public boolean moveNext() {
- if (nextElement != null) {
- element = nextElement;
- nextElement = fetchElementFromIterator(true);
- } else {
- if ((element = fetchElementFromIterator(false)) != null) {
- nextElement = fetchElementFromIterator(true);
- }
- }
- return (element != null);
+ element = iterator.hasNext() ? iterator.next() : null;
+ return element != null;
}
@Override
public int getStartOffset() {
- return (element != null)
- ? layer.getShiftedPos(element.range.getStart())
- : Integer.MAX_VALUE;
+ return getShiftedPos(element.range().getStart());
}
@Override
public int getEndOffset() {
- return (element != null)
- ? Math.min(layer.getShiftedPos(element.range.getEnd()), nextElementStartOffset)
- : Integer.MAX_VALUE;
+ return getShiftedPos(element.range().getEnd());
}
@Override
public AttributeSet getAttributes() {
- return (element != null)
- ? layer.getColoring(element.coloring, element.language)
- : SimpleAttributeSet.EMPTY;
+ return getColoring(element.coloring(), element.language());
}
}
+
+ private static <T> int firstSequenceElement(List<SequenceElement> l, int offset) {
Review Comment:
A comment what this method should do would be nice. I have a feeling, but knowing would be nice.
##########
ide/csl.api/src/org/netbeans/modules/csl/editor/semantic/GsfSemanticLayer.java:
##########
@@ -53,19 +50,17 @@
*
* @author Tor Norbye
*/
-public class GsfSemanticLayer extends AbstractHighlightsContainer implements DocumentListener {
+public final class GsfSemanticLayer extends AbstractHighlightsContainer implements DocumentListener {
// -J-Dorg.netbeans.modules.csl.editor.semantic.GsfSemanticLayer.level=FINE
private static final Logger LOG = Logger.getLogger(GsfSemanticLayer.class.getName());
Review Comment:
`LOG` is unused and can be removed (reduce warnings).
##########
ide/csl.api/src/org/netbeans/modules/csl/editor/semantic/HighlightImpl.java:
##########
Review Comment:
Suggested by NB:
- make all fields final
- use `StringBuilder` in `coloringsToString` instead of `StringBuffer`
- modifiy `coloringsAttributesOrder` to `private static final Collection<ColoringAttributes> coloringsAttributesOrder = List.of(`
- in `parse` drop the reassignment of `a` and move the `a.trim()` into the `valueOf` call
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists