本文整理汇总了Java中org.netbeans.modules.parsing.api.Snapshot.getEmbeddedOffset方法的典型用法代码示例。如果您正苦于以下问题:Java Snapshot.getEmbeddedOffset方法的具体用法?Java Snapshot.getEmbeddedOffset怎么用?Java Snapshot.getEmbeddedOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.netbeans.modules.parsing.api.Snapshot
的用法示例。
在下文中一共展示了Snapshot.getEmbeddedOffset方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeSuggestions
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
@Override
public void computeSuggestions(HintsProvider.HintsManager manager, RuleContext context, List<Hint> hints, int caretOffset) {
HtmlParserResult result = (HtmlParserResult) context.parserResult;
Node root = result.root(SyntaxAnalyzerResult.FILTERED_CODE_NAMESPACE);
Snapshot snapshot = result.getSnapshot();
int embeddedCaretOffset = snapshot.getEmbeddedOffset(caretOffset);
Element found = ElementUtils.findByPhysicalRange(root, embeddedCaretOffset, false);
if (found != null) {
switch (found.type()) {
case OPEN_TAG:
case CLOSE_TAG:
Named named = (Named) found;
String elementName = named.name().toString();
Configuration conf = Configuration.get(snapshot.getSource().getFileObject());
if (conf.getTagsNames().contains(elementName)) {
//custom element
hints.add(new CustomElementHint(elementName, context, new OffsetRange(snapshot.getOriginalOffset(found.from()), snapshot.getOriginalOffset(found.to()))));
}
//TODO add check + fix for missing required attributes
}
}
}
示例2: setActiveElement
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
private void setActiveElement(HtmlParserResult result, int caretOffset) {
Snapshot snapshot = result.getSnapshot();
FileObject file = snapshot.getSource().getFileObject();
int embeddedCaretOffset = snapshot.getEmbeddedOffset(caretOffset);
if(embeddedCaretOffset == -1) {
return ;
}
//Bug 222535 - Create rule dialog is populated with parent element
Element findBack = result.findByPhysicalRange(embeddedCaretOffset, false);
if(findBack != null
&& (findBack.type() == ElementType.OPEN_TAG || findBack.type() == ElementType.CLOSE_TAG)
&& findBack.to() == embeddedCaretOffset) {
// Situation:
// ... <div class="x">| or ... </div>|
// in this case use the element before the caret
embeddedCaretOffset--;
}
Node node = result.findBySemanticRange(embeddedCaretOffset, true);
if (node != null) {
if (node.type() == ElementType.OPEN_TAG) { //may be root node!
activeElement = new HtmlSourceElementHandle((OpenTag)node, snapshot, file);
return ;
}
}
//no active element
activeElement = null;
}
示例3: createMarkOccurrencesNodeVisitor
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
/**
* Creates a generic mark occurrences node visitor for given node types.
* Only elements of the same type (from the given types list) and the same image are marked.
*/
public static <T extends Set<OffsetRange>> NodeVisitor<T> createMarkOccurrencesNodeVisitor(EditorFeatureContext context, T result, NodeType... nodeTypesToMatch) {
final Snapshot snapshot = context.getSnapshot();
int astCaretOffset = snapshot.getEmbeddedOffset(context.getCaretOffset());
if (astCaretOffset == -1) {
return null;
}
final Node current = NodeUtil.findNonTokenNodeAtOffset(context.getParseTreeRoot(), astCaretOffset);
if (current == null) {
//this may happen if the offset falls to the area outside the selectors rule node.
//(for example when the stylesheet starts or ends with whitespaces or comment and
//and the offset falls there).
//In such case root node (with null parent) is returned from NodeUtil.findNodeAtOffset()
return null;
}
Set types = EnumSet.copyOf(Arrays.asList(nodeTypesToMatch));
if(!types.contains(current.type())) {
return null;
}
final CharSequence selectedNamespacePrefixImage = current.image();
return new NodeVisitor<T>(result) {
@Override
public boolean visit(Node node) {
if (node.type() == current.type()
&& CharSequenceUtilities.textEquals(selectedNamespacePrefixImage, node.image())) {
OffsetRange documentNodeRange = Css3Utils.getDocumentOffsetRange(node, snapshot);
getResult().add(Css3Utils.getValidOrNONEOffsetRange(documentNodeRange));
}
return false;
}
};
}
示例4: findRuleAtOffset
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
private Rule findRuleAtOffset(final Snapshot snapshot, Model model, int documentOffset) {
final int astOffset = snapshot.getEmbeddedOffset(documentOffset);
if (astOffset == -1) {
return null;
}
final AtomicReference<Rule> ruleRef = new AtomicReference<Rule>();
model.runReadTask(new Model.ModelTask() {
@Override
public void run(StyleSheet styleSheet) {
styleSheet.accept(new ModelVisitor.Adapter() {
@Override
public void visitRule(Rule rule) {
if (cancelled) {
return;
}
if (astOffset >= rule.getStartOffset()) {
if(astOffset <= rule.getEndOffset()) {
//exactly in the rule or at its boundaries
ruleRef.set(rule);
} else {
//behind the rule close curly bracket.
//if the offset is at the same line as the rule
//end and there're only WS between,
//activate the rule
CharSequence source = snapshot.getText();
//weird - looks like parsing.api bug - the astoffset may point beond the snapshot's length?!?!?
int adjustedAstOffset = Math.min(source.length(), astOffset);
for(int i = adjustedAstOffset - 1; i >= 0; i--) {
if(i == rule.getEndOffset()) {
ruleRef.set(rule);
return ;
}
char c = source.charAt(i);
if(!(c == ' ' || c == '\t')) {
break; //some non-ws text //todo fix comments
}
if(c == '\n') {
break;
}
}
}
}
}
});
}
});
return ruleRef.get();
}
示例5: getMarkOccurrencesNodeVisitor
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
@Override
public <T extends Set<OffsetRange>> NodeVisitor<T> getMarkOccurrencesNodeVisitor(EditorFeatureContext context, T result) {
final Snapshot snapshot = context.getSnapshot();
int astCaretOffset = snapshot.getEmbeddedOffset(context.getCaretOffset());
if (astCaretOffset == -1) {
return null;
}
Node current = NodeUtil.findNonTokenNodeAtOffset(context.getParseTreeRoot(), astCaretOffset);
if (current == null) {
//this may happen if the offset falls to the area outside the selectors rule node.
//(for example when the stylesheet starts or ends with whitespaces or comment and
//and the offset falls there).
//In such case root node (with null parent) is returned from NodeUtil.findNodeAtOffset()
return null;
}
//process only some interesting nodes
if (!NodeUtil.isSelectorNode(current)) {
return null;
}
final NodeType nodeType = current.type();
final CharSequence currentNodeImage = current.image();
return new NodeVisitor<T>(result) {
@Override
public boolean visit(Node node) {
if (nodeType == node.type()) {
boolean ignoreCase = nodeType == NodeType.hexColor;
if (LexerUtils.equals(currentNodeImage, node.image(), ignoreCase, false)) {
int[] trimmedNodeRange = NodeUtil.getTrimmedNodeRange(node);
int docFrom = snapshot.getOriginalOffset(trimmedNodeRange[0]);
//virtual class or id handling - the class and id elements inside
//html tag's CLASS or ID attribute has the dot or hash prefix just virtual
//so if we want to highlight such occurances we need to increment the
//start offset by one
if (docFrom == -1 && (node.type() == NodeType.cssClass || node.type() == NodeType.cssId)) {
docFrom = snapshot.getOriginalOffset(trimmedNodeRange[0] + 1); //lets try +1 offset
}
int docTo = snapshot.getOriginalOffset(trimmedNodeRange[1]);
if (docFrom != -1 && docTo != -1) {
getResult().add(new OffsetRange(docFrom, docTo));
}
}
}
return false;
}
};
}
示例6: getPrefix
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
@Override
public String getPrefix(ParserResult info, final int caretOffset, boolean upToOffset) {
CssParserResult result = (CssParserResult) info;
Snapshot snapshot = info.getSnapshot();
int embeddedCaretOffset = snapshot.getEmbeddedOffset(caretOffset);
TokenHierarchy hi = snapshot.getTokenHierarchy();
String prefix = getPrefix(hi.tokenSequence(CssTokenId.language()), embeddedCaretOffset);
if (prefix == null) {
return null;
}
Node leaf = NodeUtil.findNonTokenNodeAtOffset(result.getParseTree(), embeddedCaretOffset);
if(leaf == null) {
return null;
}
boolean inPropertyDeclaration = NodeUtil.getAncestorByType(leaf, NodeType.propertyDeclaration) != null;
//really ugly handling of class or id selector prefix:
//Since the getPrefix() method is parser result based it is supposed
//to work on top of the snapshot, while GsfCompletionProvider$Task.canFilter()
//should be fast and hence operates on document, there arises a big contradiction -
//For the virtually generated class and id selectors, the dot or hash chars
//are part of the virtual source and hence becomes a part of the prefix in
//this method call, while in the real html document they are invisible and an
//attribute quote resides on their place.
//So if a GsfCompletionProvider$CompletionEnvironment is created, an anchor
//is computed from the caret offset and prefix lenght (prefix returned from
//this method). After subsequent user's keystrokes the canFilter() method
//gets text from this anchor to the caret from the edited document! So the
//prefix contains the attribute quotation and any css items cannot be filtered.
//this is a poor and hacky solution to this issue, some bug may appear for
//non class or id elements starting with dot or hash?!?!?
//do not apply the hack in property declarations as it breaks the hash colors completion items filtering
if (!inPropertyDeclaration && (prefix.length() > 0 && (prefix.charAt(0) == '.' || prefix.charAt(0) == '#'))) {
firstPrefixChar = prefix.charAt(0);
return prefix.substring(1);
} else {
firstPrefixChar = 0;
return prefix;
}
}
示例7: findPairNodesAtSelection
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
private static Element[] findPairNodesAtSelection(RuleContext context) {
HtmlParserResult result = (HtmlParserResult) context.parserResult;
if (context.selectionStart == -1 || context.selectionEnd == -1) {
//no selection - find the containing element
Snapshot snap = result.getSnapshot();
int embeddedCaret = snap.getEmbeddedOffset(context.caretOffset);
if (embeddedCaret != -1) {
Node containing = result.findBySemanticRange(embeddedCaret, false);
if (containing != null) {
if (containing.type() == ElementType.OPEN_TAG) {
OpenTag ot = (OpenTag) containing;
CloseTag ct = ot.matchingCloseTag();
if (ct != null) {
return new Element[]{ot, ct};
}
}
}
}
} else {
//selection - find the element inside selection
//check whether the selection starts at a tag and ends at a tag
//open tag
Element open = result.findByPhysicalRange(context.selectionStart, true);
if (open == null || open.type() != ElementType.OPEN_TAG) {
return null;
}
//close tag
Element close = result.findByPhysicalRange(context.selectionEnd, false);
if (close == null || close.type() != ElementType.CLOSE_TAG) {
return null;
}
//is the end tag really a pair node of the open tag?
OpenTag openTag = (OpenTag) open;
if (openTag.matchingCloseTag() != close) { //same AST ... reference test is ok
return null;
}
return new Element[]{open, close};
}
return null;
}