Skip to content

Commit

Permalink
language-server: better implementation of getContainingCSSNodeIncludi…
Browse files Browse the repository at this point in the history
…ngStart
  • Loading branch information
joshtynjala committed Feb 28, 2024
1 parent 38968e5 commit 2a0bed4
Showing 1 changed file with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,33 @@
*/
package com.as3mxml.vscode.utils;

import org.apache.royale.compiler.css.ICSSDocument;
import org.apache.royale.compiler.css.ICSSFontFace;
import org.apache.royale.compiler.css.ICSSNamespaceDefinition;
import org.apache.royale.compiler.css.ICSSNode;
import org.apache.royale.compiler.css.ICSSRule;

public class CSSDocumentUtils {
public static boolean containsWithStart(ICSSNode node, int offset) {
return offset >= node.getAbsoluteStart() && offset <= node.getAbsoluteEnd();
}

public static ICSSNode getContainingCSSNodeIncludingStart(ICSSNode node, int offset) {
if (!containsWithStart(node, offset)) {
return null;
public static ICSSNode getContainingCSSNodeIncludingStart(ICSSDocument cssDocument, int offset) {
for (ICSSNamespaceDefinition cssNamespace : cssDocument.getAtNamespaces()) {
if (containsWithStart(cssNamespace, offset)) {
return cssNamespace;
}
}
for (int i = 0, count = node.getArity(); i < count; i++) {
ICSSNode child = node.getNthChild(i);
if (child.getAbsoluteStart() == -1) {
// the Royale compiler has a quirk where a node can have an
// unknown offset, but its children have known offsets. this is
// where we work around that...
for (int j = 0, innerCount = child.getArity(); j < innerCount; j++) {
ICSSNode innerChild = child.getNthChild(j);
ICSSNode result = getContainingCSSNodeIncludingStart(innerChild, offset);
if (result != null) {
return result;
}
}
continue;
for (ICSSFontFace cssFontFace : cssDocument.getFontFaces()) {
if (containsWithStart(cssFontFace, offset)) {
return cssFontFace;
}
ICSSNode result = getContainingCSSNodeIncludingStart(child, offset);
if (result != null) {
return result;
}
for (ICSSRule cssRule : cssDocument.getRules()) {
if (containsWithStart(cssRule, offset)) {
return cssRule;
}
}
return node;
return cssDocument;
}
}

0 comments on commit 2a0bed4

Please sign in to comment.