List of usage examples for javax.swing.text Document render
public void render(Runnable r);
From source file:netbeanstypescript.TSDeclarationFinder.java
@Override public OffsetRange getReferenceSpan(final Document doc, final int caretOffset) { final OffsetRange[] tokenRange = new OffsetRange[1]; doc.render(new Runnable() { @Override//from ww w.ja v a 2 s . com public void run() { TokenSequence<?> ts = TokenHierarchy.get(doc).tokenSequence(JsTokenId.javascriptLanguage()); int offsetWithinToken = ts.move(caretOffset); if (ts.moveNext()) { Token<?> tok = ts.token(); if (possibleRefs.contains(tok.id().primaryCategory())) { int start = caretOffset - offsetWithinToken; tokenRange[0] = new OffsetRange(start, start + tok.length()); return; } } // If we're right between two tokens, check the previous if (offsetWithinToken == 0 && ts.movePrevious()) { Token<?> tok = ts.token(); if (possibleRefs.contains(tok.id().primaryCategory())) { tokenRange[0] = new OffsetRange(caretOffset - tok.length(), caretOffset); } } } }); if (tokenRange[0] == null) { return OffsetRange.NONE; } // Now query the language service to see if this is actually a reference final AtomicBoolean isReference = new AtomicBoolean(); class ReferenceSpanTask extends UserTask implements Runnable { @Override public void run() { try { ParserManager.parse(Collections.singleton(Source.create(doc)), this); } catch (ParseException e) { TSService.log.log(Level.WARNING, null, e); } } @Override public void run(ResultIterator ri) throws ParseException { // Calling ResultIterator#getParserResult() ensures latest snapshot pushed to server Object defs = TSService.call("getDefsAtPosition", ri.getParserResult().getSnapshot().getSource().getFileObject(), caretOffset); isReference.set(defs != null); } } // Don't block the UI thread for too long in case server is busy RequestProcessor.Task task = RP.post(new ReferenceSpanTask()); try { task.waitFinished(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { task.cancel(); } return isReference.get() ? tokenRange[0] : OffsetRange.NONE; }
From source file:org.debux.webmotion.netbeans.javacc.lexer.impl.LexerUtils.java
public static TokenSequence<? extends TokenId> getMostEmbeddedTokenSequence(final Document doc, final int offset, boolean runUnderLock) { final AtomicReference<TokenSequence<? extends TokenId>> ref = new AtomicReference<TokenSequence<? extends TokenId>>(); Runnable r = new Runnable() { @Override/*from ww w . j a va2s .com*/ public void run() { TokenHierarchy th = TokenHierarchy.get(doc); List<TokenSequence<? extends TokenId>> sequences = th.embeddedTokenSequences(offset, false); if (sequences.isEmpty()) { //no embedding, return top level sequence; ref.set(th.tokenSequence()); } else { ref.set(sequences.get(sequences.size() - 1)); //return the most embedded one } } }; if (runUnderLock) { doc.render(r); } else { r.run(); } return ref.get(); }