Fix ugly semantic coloring on CSL languages while editing#8011
Fix ugly semantic coloring on CSL languages while editing#8011lkishalmi wants to merge 1 commit intoapache:masterfrom
Conversation
matthiasblaesing
left a comment
There was a problem hiding this comment.
Looks generally sane to me (only eyeballed). My comments are mostly stylistic (you opened the can of worms ;-)).
The one place I'd really like to see improved is documentation, what firstSequenceElement should be doing.
| } | ||
| } | ||
|
|
||
| private static <T> int firstSequenceElement(List<SequenceElement> l, int offset) { |
There was a problem hiding this comment.
A comment what this method should do would be nice. I have a feeling, but knowing would be nice.
| 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()); |
There was a problem hiding this comment.
LOG is unused and can be removed (reduce warnings).
| private final List<Lookup.Result> coloringResults = new ArrayList<>(3); | ||
| private final List<LookupListener> coloringListeners = new ArrayList<>(3); |
There was a problem hiding this comment.
My take on the reason for this:
| private final List<Lookup.Result> coloringResults = new ArrayList<>(3); | |
| private final List<LookupListener> coloringListeners = new ArrayList<>(3); | |
| // 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); |
There was a problem hiding this comment.
Suggested by NB:
- make all fields final
- use
StringBuilderincoloringsToStringinstead ofStringBuffer - modifiy
coloringsAttributesOrdertoprivate static final Collection<ColoringAttributes> coloringsAttributesOrder = List.of( - in
parsedrop the reassignment ofaand move thea.trim()into thevalueOfcall
| 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); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this 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).
| 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); | |
| } | |
| } | |
| } | |
| if (language != null) { | |
| Parser.Result r = resultIterator.getParserResult(); | |
| if (r instanceof ParserResult parserResult) { | |
| process(language, parserResult, newColoring); | |
| } | |
| } |
mbien
left a comment
There was a problem hiding this comment.
Code looks good. Matthias made some good suggestions though.
I vaguely remember the old "schlieman" API impl having a similar problem. The bug could be potentially quite old. Good job hunting it down!
| public int getEndOffset() { | ||
| return (element != null) | ||
| ? Math.min(layer.getShiftedPos(element.range.getEnd()), nextElementStartOffset) | ||
| : Integer.MAX_VALUE; | ||
| return getShiftedPos(element.range().getEnd()); | ||
| } |
There was a problem hiding this comment.
wondering if you could move the fix into its own commit? Even if its just the one liner which removes Math.min.
It would get lost among the other improvements. We do also have a rule in the reviewer guide which states to try to avoid obfuscating fixes with code refactoring. (I know I did break it myself too a few times, but I believe this case here is easy to split still)
There was a problem hiding this comment.
Well, I probably close this PR and create two new ones. One with the one-line fix, the other is with the code cleanups.
While fixing the issues on HighlightImp, it turned out that it's not used at all.
I guess it is just an old implementation copied over from ...java.semantic package (that one is used), and later it turned out CSL would not use token based Highlighting.
There was a problem hiding this comment.
your choice. Commit messages is all what is left in the repo history once merged. If they describe what they do -> all good. But if you want to split this into two PRs -> also good of course.
|
Closing in favor of #8018 |
This one is bugging me for years.
Whenever I insert or write text to a CSL based editor the semantic coloring does not keep track with the document change
and some text elements get discolored as you type. The next parsing run would then set the colors right.
See the discoloration of
permissionsbefore:Screencast From 2024-12-01 13-04-47.webm
After this fix:
Screencast From 2024-12-01 13-07-58.webm
Fortunately or Unfortunately , I had to change the code here and there in order to be able to understand what's going on.
So this could be a one -liner, removing the
Math.minform:netbeans/ide/csl.api/src/org/netbeans/modules/csl/editor/semantic/GsfSemanticLayer.java
Line 322 in c2ec6e4
I submitted my whole work on this though, hoping that it would simplify that part of CSL and makes it less cryptic.
So my changes in brief (read it with the code changes):
SequenceElementas a record. TheComparisonItemgot me a lot of headache, at the end it was only used to get thetailSetmethod work. Interestingly that subset just used as an optimization, the editor works even if the highlight sequence return the whole sequence all the time. (Tested with a 3500+ lines yaml, the performance overhead was barely noticeable.) Yet, I've copied theSortedSetinto a list, where a simple binary search could be used to get a partial iterator.GsfHighlightSequencemade it a lot simpler.