本文整理汇总了Java中org.netbeans.modules.parsing.api.Snapshot.getTokenHierarchy方法的典型用法代码示例。如果您正苦于以下问题:Java Snapshot.getTokenHierarchy方法的具体用法?Java Snapshot.getTokenHierarchy怎么用?Java Snapshot.getTokenHierarchy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.netbeans.modules.parsing.api.Snapshot
的用法示例。
在下文中一共展示了Snapshot.getTokenHierarchy方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getElementsParserCache
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
private synchronized ElementsParserCache getElementsParserCache() {
if (elementsParserCache == null) {
CharSequence sourceCode = source.getSourceCode();
Snapshot snapshot = source.getSnapshot();
TokenHierarchy hi;
if (snapshot != null) {
//use the snapshot's token hierarchy (cached) if possible
hi = snapshot.getTokenHierarchy();
} else {
hi = TokenHierarchy.create(sourceCode, HTMLTokenId.language());
}
TokenSequence<HTMLTokenId> tokenSequence = hi.tokenSequence(HTMLTokenId.language());
elementsParserCache = new ElementsParserCache(source.getSourceCode(), tokenSequence);
}
return elementsParserCache;
}
示例2: getEmbeddings
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
@Override
public List<Embedding> getEmbeddings(Snapshot snapshot) {
if (snapshot == null) {
return Collections.emptyList();
}
TokenHierarchy th = snapshot.getTokenHierarchy();
if(th == null) {
//no lexer language for the snapshot's mimetype???
return Collections.emptyList();
}
TokenSequence<HTMLTokenId> ts = th.tokenSequence(HTMLTokenId.language());
HashMap<String, Object> state = new HashMap<>(6);
List<Embedding> embeddings = new ArrayList<>();
extractCssFromHTML(snapshot, ts, state, embeddings);
return embeddings;
}
示例3: ElementsIterator
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
public ElementsIterator(HtmlSource source) {
CharSequence sourceCode = source.getSourceCode();
Snapshot snapshot = source.getSnapshot();
TokenHierarchy hi;
if (snapshot != null) {
//use the snapshot's token hierarchy (cached) if possible
hi = snapshot.getTokenHierarchy();
} else {
hi = TokenHierarchy.create(sourceCode, HTMLTokenId.language());
}
TokenSequence<HTMLTokenId> tokenSequence = hi.tokenSequence(HTMLTokenId.language());
this.wrapped = ElementsParser.forTokenIndex(sourceCode, tokenSequence, 0);
}
示例4: getEmbeddings
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
@Override
public List<Embedding> getEmbeddings(Snapshot snapshot) {
TokenHierarchy<?> th = snapshot.getTokenHierarchy();
TokenSequence<XhtmlElTokenId> sequence = th.tokenSequence(XhtmlElTokenId.language());
List<Embedding> embeddings = new ArrayList<>();
sequence.moveStart();
boolean htmlSectionEndsWithQuotation = false;
while (sequence.moveNext()) {
Token t = sequence.token();
//unbelievable hack
if(t.id() == XhtmlElTokenId.HTML) {
char c = t.text().charAt(t.length() - 1);
htmlSectionEndsWithQuotation = c == '"' || c == '\'';
}
if (t.id() == XhtmlElTokenId.EL) {
embeddings.add(snapshot.create(sequence.offset(), t.length(), "text/x-el")); //NOI18N
if(htmlSectionEndsWithQuotation) {
//it *looks like* the EL is inside an attribute
//there's a need to distinguish between ELs inside or outside of attribute values
embeddings.add(snapshot.create(ATTRIBUTE_EL_MARKER, "text/x-el")); //NOI18N
}
// just to separate expressions for easier handling in EL parser
embeddings.add(snapshot.create(Constants.LANGUAGE_SNIPPET_SEPARATOR, "text/x-el")); //NOI18N
}
}
if (embeddings.isEmpty()) {
return Collections.emptyList();
} else {
return Collections.singletonList(Embedding.create(embeddings));
}
}
示例5: getEmbeddings
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
@Override
public List<Embedding> getEmbeddings(Snapshot snapshot) {
TokenHierarchy<?> th = snapshot.getTokenHierarchy();
TokenSequence<XhtmlElTokenId> sequence = th.tokenSequence(XhtmlElTokenId.language());
sequence.moveStart();
List<Embedding> embeddings = new ArrayList<>();
boolean lastEmbeddingIsVirtual = false;
while (sequence.moveNext()) {
Token t = sequence.token();
if (t.id() == XhtmlElTokenId.HTML) {
//lets suppose the text is always html :-(
embeddings.add(snapshot.create(sequence.offset(), t.length(), "text/html")); //NOI18N
lastEmbeddingIsVirtual = false;
} else {
//replace templating tokens by generated code marker
if (!lastEmbeddingIsVirtual) {
embeddings.add(snapshot.create(Constants.LANGUAGE_SNIPPET_SEPARATOR, "text/html"));
lastEmbeddingIsVirtual = true;
}
}
}
if (embeddings.isEmpty()) {
return Collections.emptyList();
} else {
return Collections.singletonList(Embedding.create(embeddings));
}
}
示例6: parse
import org.netbeans.modules.parsing.api.Snapshot; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void parse(Snapshot snapshot, Task task, SourceModificationEvent event) throws ParseException {
this.snapshot = snapshot;
TokenHierarchy<?> h = (TokenHierarchy<?>)snapshot.getTokenHierarchy();
XmlLexerParser tokenParser = new XmlLexerParser(h);
FxModelBuilder builder = new FxModelBuilder();
FileObject fo = snapshot.getSource().getFileObject();
if (fo != null) {
builder.setBaseURL(fo.toURL());
}
tokenParser.setContentHandler(builder);
try {
tokenParser.parse();
} catch (SAXException ex) {
throw new ParseException("Parsing failed", ex);
}
final ClasspathInfo cpInfo = ClasspathInfo.create(snapshot.getSource().getFileObject());
problems.addAll(tokenParser.getErrors());
problems.addAll(builder.getErrors());
model = builder.getModel();
class UT extends UserTask implements ClasspathInfoProvider {
@Override
public ClasspathInfo getClasspathInfo() {
return cpInfo;
}
@Override
public void run(ResultIterator resultIterator) throws Exception {
JavacParserResult res = (JavacParserResult)resultIterator.getParserResult();
info = res.get(CompilationInfo.class);
env = createBuildEnvironment();
initModelSteps();
for (ModelBuilderStep step : steps) {
FxNodeVisitor visitor = step.createVisitor(env);
model.accept(visitor);
}
}
};
UT ut = new UT();
ParserManager.parse("text/x-java", ut);
result = new ResultImpl(snapshot, model, problems, h);
env = null;
steps = null;
snapshot = null;
model = null;
info = null;
problems = new ArrayList<ErrorMark>();
}
示例7: 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;
}
}