Skip to content

Commit

Permalink
CssSemanticAnalyser needs to flatten OffsetRanges for highlights
Browse files Browse the repository at this point in the history
CssSemanticAnalyser reported overlapping ranges for highlights, for
example when an attribute is used in a :not() pseudo class construct.

The formatting behind the embedded range is lost and results in a
broken view.

The embedded OffsetRanges need to be flattened into a sequenze of
non-overlapping ranges.

Closes: #5012
  • Loading branch information
matthiasblaesing committed Jan 8, 2023
1 parent 7c23807 commit 5847f35
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.Document;
import org.netbeans.modules.csl.api.ColoringAttributes;
Expand Down Expand Up @@ -94,11 +95,13 @@ public static SemanticAnalyzerResult analyzeDeclaration(Node declarationNode) {
}

public static Map<OffsetRange, Set<ColoringAttributes>> getSemanticHighlights(FeatureContext context, FeatureCancel cancel) {
Map<OffsetRange, Set<ColoringAttributes>> all = new HashMap<>();
long start = System.nanoTime();

Map<OffsetRange, Set<ColoringAttributes>> allPrecursor = new HashMap<>();
final Collection<NodeVisitor<Map<OffsetRange, Set<ColoringAttributes>>>> visitors = new ArrayList<>();

for (CssEditorModule module : getModules()) {
NodeVisitor<Map<OffsetRange, Set<ColoringAttributes>>> visitor = module.getSemanticHighlightingNodeVisitor(context, all);
NodeVisitor<Map<OffsetRange, Set<ColoringAttributes>>> visitor = module.getSemanticHighlightingNodeVisitor(context, allPrecursor);
//modules may return null visitor instead of a dummy empty visitor
//to speed up the parse tree visiting when there're no result
if (visitor != null) {
Expand All @@ -122,6 +125,59 @@ public void run() {

NodeVisitor.visitChildren(context.getParseTreeRoot(), visitors);

long preparation = System.nanoTime();

Map<OffsetRange, Set<ColoringAttributes>> all = new HashMap<>();

List<OffsetRange> sortedRanges = new ArrayList<>(allPrecursor.keySet());
sortedRanges.sort(null);


List<OffsetRange> stack = new ArrayList<>();
OffsetRange lastAdded;

for(int i = 0; i < sortedRanges.size(); i++) {
OffsetRange currentItem = sortedRanges.get(i);
Set<ColoringAttributes> attributes = allPrecursor.get(currentItem);
OffsetRange nextItem = (i < (sortedRanges.size() - 1)) ? sortedRanges.get(i + 1) : null;
if(nextItem != null && currentItem.getEnd() > nextItem.getStart()) {
stack.add(currentItem);
currentItem = currentItem.boundTo(0, nextItem.getStart());
}
if(! currentItem.isEmpty()) {
all.put(currentItem, attributes);
}
lastAdded = currentItem;
while(true) {
if(stack.isEmpty()) {
break;
}
OffsetRange stackElement = stack.remove(stack.size() - 1);
boolean endStackProcessing = false;
if(nextItem != null && stackElement.getEnd() > nextItem.getStart()) {
stack.add(stackElement);
endStackProcessing = true;
}
Set<ColoringAttributes> stackAttributes = allPrecursor.get(stackElement);
stackElement = stackElement.boundTo(lastAdded.getEnd(), nextItem != null ? nextItem.getStart() : Integer.MAX_VALUE);
if(! stackElement.isEmpty()) {
all.put(stackElement, stackAttributes);
lastAdded = stackElement;
}
if(endStackProcessing) {
break;
}
}
}

long consolidation = System.nanoTime();

if(LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Preparation: {0} ms, Consolidation: {1} ms", new Object[]{preparation - start, consolidation - preparation});
LOGGER.log(Level.FINER, "Precursor: {0}", allPrecursor);
LOGGER.log(Level.FINER, "Final: {0}", all);
}

return all;
}

Expand Down
6 changes: 6 additions & 0 deletions ide/css.editor/test/unit/data/testfiles/coloring1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[type="checkbox"]:nth-child(2n),
[type="checkbox"]:not([hidden]),
[type="checkbox"]:not(.active),
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[|>CUSTOM1:type<|="checkbox"]|>CLASS::nth-child(2n)<|,
[|>CUSTOM1:type<|="checkbox"]|>CLASS::not([|>CUSTOM1:<|hidden|>CLASS:<|])<|,
[|>CUSTOM1:type<|="checkbox"]|>CLASS::not(|>METHOD:<|.active|>CLASS:<|)<|,
[|>CUSTOM1:type<|="checkbox"]|>CLASS::not(|>CLASS:<|:checked|>CLASS:<|)<|,
[|>CUSTOM1:type<|="checkbox"]|>CLASS::checked<| {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.css.editor.csl;

import org.netbeans.modules.css.editor.module.main.CssModuleTestBase;
import org.netbeans.modules.css.lib.api.CssParserResult;

public class CssSemanticAnalyzerTest extends CssModuleTestBase {

public CssSemanticAnalyzerTest(String name) {
super(name);
}

@Override
protected void setUp() throws Exception {
super.setUp();
CssParserResult.IN_UNIT_TESTS = true;
}

public void testColoring1() throws Exception {
checkSemantic("testfiles/coloring1.css");
}
}

0 comments on commit 5847f35

Please sign in to comment.