public void run() { AbstractDocument doc = (AbstractDocument)getDocument(); if (doc!=null){ doc.readLock(); try { LockView lockView = LockView.get(GapDocumentView.this); if (lockView != null) { lockView.lock(); try { layoutLock(); try { updateView(lockView); } finally { updateLayout(); layoutUnlock(); } } finally { lockView.unlock(); } } // missing lock view => likely disconnected from hierarchy } finally { doc.readUnlock(); } } }
private Document commentDoc() throws BadLocationException { Document doc = new ModificationTextDocument(); // Assign a language to the document String text = "/**abc*/"; doc.insertString(0, text, null); doc.putProperty(Language.class,TestTokenId.language()); TokenHierarchy<?> hi = TokenHierarchy.get(doc); ((AbstractDocument)doc).readLock(); try { TokenSequence<?> ts = hi.tokenSequence(); assertTrue(ts.moveNext()); assertNotNull(ts.embedded()); return doc; } finally { ((AbstractDocument)doc).readUnlock(); } }
static Rectangle2D modelToView(JTextComponent tc, int pos, Position.Bias bias) throws BadLocationException { Document doc = tc.getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { Rectangle alloc = getVisibleEditorRect(tc); if (alloc != null) { View rootView = tc.getUI().getRootView(tc); rootView.setSize(alloc.width, alloc.height); Shape s = rootView.modelToView(pos, alloc, bias); if (s != null) { return s.getBounds2D(); } } } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } return null; }
static int viewToModel(JTextComponent tc, double x, double y, Position.Bias[] biasReturn) { int offs = -1; Document doc = tc.getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { Rectangle alloc = getVisibleEditorRect(tc); if (alloc != null) { View rootView = tc.getUI().getRootView(tc); View documentView = rootView.getView(0); if (documentView instanceof EditorView) { documentView.setSize(alloc.width, alloc.height); offs = ((EditorView) documentView).viewToModelChecked(x, y, alloc, biasReturn); } else { rootView.setSize(alloc.width, alloc.height); offs = rootView.viewToModel((float) x, (float) y, alloc, biasReturn); } } } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } return offs; }
/** * Finds the namespace insertion offset in the xml document. */ public static int getNamespaceInsertionOffset(Document document) { int offset = 0; ((AbstractDocument)document).readLock(); try { TokenHierarchy th = TokenHierarchy.get(document); TokenSequence ts = th.tokenSequence(); while(ts.moveNext()) { Token nextToken = ts.token(); if(nextToken.id() == XMLTokenId.TAG && nextToken.text().toString().equals(">")) { offset = nextToken.offset(th); break; } } } finally { ((AbstractDocument)document).readUnlock(); } return offset; }
public View create(Element elem) { String kind = elem.getName(); if (kind != null) { if (kind.equals(AbstractDocument.ContentElementName)) { return new LabelView(elem); } else if (kind.equals(AbstractDocument.ParagraphElementName)) { return null; } else if (kind.equals(AbstractDocument.SectionElementName)) { return new DocumentView(elem); } else if (kind.equals(StyleConstants.ComponentElementName)) { return new ComponentView(elem); } else if (kind.equals(StyleConstants.IconElementName)) { return new IconView(elem); } } return null; }
/** * Schedule recomputation of visual bounds of all carets. */ private void requestUpdateAllCaretsBounds() { JTextComponent c = component; AbstractDocument doc; if (c != null && (doc = activeDoc) != null) { doc.readLock(); try { List<CaretInfo> sortedCarets = getSortedCarets(); for (CaretInfo caret : sortedCarets) { caret.getCaretItem().markUpdateCaretBounds(); } } finally { doc.readUnlock(); } } }
/** * Replaces text from the indicated start to end position with the * new text specified. Does nothing if the model is null. Simply * does a delete if the new string is null or empty. * <p> * This method is thread safe, although most Swing methods * are not.<p> * This method is overridden so that our Undo manager remembers it as a * single operation (it has trouble with this, especially for * <code>RSyntaxTextArea</code> and the "auto-indent" feature). * * @param str the text to use as the replacement * @param start the start position >= 0 * @param end the end position >= start * @exception IllegalArgumentException if part of the range is an * invalid position in the model * @see #insert(String, int) * @see #replaceRange(String, int, int) */ @Override public void replaceRange(String str, int start, int end) { if (end < start) { throw new IllegalArgumentException("end before start"); } Document doc = getDocument(); if (doc != null) { try { // Without this, in some cases we'll have to do two undos // for one logical operation (for example, try editing a // Java source file in an RSyntaxTextArea, and moving a line // with text already on it down via Enter. Without this // line, doing a single "undo" moves all later text up, // but the first line moved down isn't there! Doing a // second undo puts it back. undoManager.beginInternalAtomicEdit(); ((AbstractDocument)doc).replace(start, end - start, str, null); } catch (BadLocationException e) { throw new IllegalArgumentException(e.getMessage()); } finally { undoManager.endInternalAtomicEdit(); } } }
static void refreshHack () { Iterator<Document> it = managers.keySet ().iterator (); while (it.hasNext ()) { AbstractDocument document = (AbstractDocument) it.next (); document.readLock (); try { MutableTextInput mti = (MutableTextInput) document.getProperty (MutableTextInput.class); mti.tokenHierarchyControl ().rebuild (); } finally { document.readUnlock (); } // final StyledDocument document = (StyledDocument) it.next (); // NbDocument.runAtomic (document, new Runnable () { // public void run() { // MutableTextInput mti = (MutableTextInput) document.getProperty (MutableTextInput.class); // mti.tokenHierarchyControl ().rebuild (); // } // }); } }
private void refresh () { SwingUtilities.invokeLater(new Runnable() { public void run() { AbstractDocument doc = getCurrentDocument (); TokenSequence ts = null; if (doc != null) try { doc.readLock (); TokenHierarchy tokenHierarchy = TokenHierarchy.get (doc); if (tokenHierarchy == null) return; ts = tokenHierarchy.tokenSequence (); } finally { doc.readUnlock (); } if (ts == null) tree.setModel (new DefaultTreeModel (new DefaultMutableTreeNode ())); else tree.setModel (new DefaultTreeModel (new TSNode (null, ts, null, 0, 0))); JEditorPane editor = getCurrentEditor (); if (editor != null) { int position = getCurrentEditor ().getCaret ().getDot (); selectPath (position); } } }); }
public int[] findOrigin() throws BadLocationException { ((AbstractDocument) context.getDocument()).readLock(); try { int offset; if (context.isSearchingBackward()) { offset = context.getSearchOffset() - 1; } else { offset = context.getSearchOffset(); } block = ess.findMatchingBlock(offset, false); if (block == null) { return null; } else if (block.length == 0) { return block; } else { return new int [] { offset, offset }; } } finally { ((AbstractDocument) context.getDocument()).readUnlock(); } }
public int [] findOrigin() throws BadLocationException { ((AbstractDocument) context.getDocument()).readLock(); try { int result [] = BracesMatcherSupport.findChar( context.getDocument(), context.getSearchOffset(), context.isSearchingBackward() ? Math.max(context.getLimitOffset(), lowerBound) : Math.min(context.getLimitOffset(), upperBound), matchingPairs ); if (result != null) { originOffset = result[0]; originalChar = matchingPairs[result[1]]; matchingChar = matchingPairs[result[1] + result[2]]; backward = result[2] < 0; return new int [] { originOffset, originOffset + 1 }; } else { return null; } } finally { ((AbstractDocument) context.getDocument()).readUnlock(); } }
public int [] findMatches() throws BadLocationException { ((AbstractDocument) context.getDocument()).readLock(); try { int offset = BracesMatcherSupport.matchChar( context.getDocument(), backward ? originOffset : originOffset + 1, backward ? Math.max(0, lowerBound) : Math.min(context.getDocument().getLength(), upperBound), originalChar, matchingChar ); return offset != -1 ? new int [] { offset, offset + 1 } : null; } finally { ((AbstractDocument) context.getDocument()).readUnlock(); } }
/** * Parses a XML document using XMLLexer and loops through all tokens. * @param document * @throws java.lang.Exception */ protected void parse(javax.swing.text.Document document) throws Exception { ((AbstractDocument)document).readLock(); try { TokenHierarchy th = TokenHierarchy.get(document); TokenSequence ts = th.tokenSequence(); assert(true); while(ts.moveNext()) { Token token = ts.token(); assert(token.id() != null); if(DEBUG) { // System.out.println("Id :["+ token.id().name() + // "] [Text :["+ token.text()+"]"); } } } finally { ((AbstractDocument)document).readUnlock(); } }
/** * This test validates all tokens obtained by parsing test.xml against * an array of expected tokens. */ public void assertTokenSequence(javax.swing.text.Document document, TokenId[] expectedIds) throws Exception { ((AbstractDocument)document).readLock(); try { TokenHierarchy th = TokenHierarchy.get(document); TokenSequence ts = th.tokenSequence(); //assert(ts.tokenCount() == expectedIds.length); int index = 0; while(ts.moveNext()) { Token token = ts.token(); if(DEBUG) { System.out.println("Id :["+ token.id().name() + "] [Text :["+ token.text()+"]"); } assert(token.id() == expectedIds[index]); index++; } } finally { ((AbstractDocument)document).readUnlock(); } }
public static String foldHierarchyToString(JTextComponent target){ String ret = ""; AbstractDocument adoc = (AbstractDocument)target.getDocument(); // Dump fold hierarchy FoldHierarchy hierarchy = FoldHierarchy.get(target); adoc.readLock(); try { hierarchy.lock(); try { Fold root = hierarchy.getRootFold(); ret = (root == null) ? "root is null" : foldToStringChildren(root, 0); //NOI18N } finally { hierarchy.unlock(); } } finally { adoc.readUnlock(); } return ret; }
/** * Executes user code on token sequence from the document. * Read-locks the document, obtains {@link TokenSequence} from the Lexer and executes {@code userCode} * passing the initialized sequence. The sequence is moved to the desired offset and the token that contains * or starts at that position. The client can move the sequence elsewhere. * <p/> * If the {@code userCode} calls this {@code SyntaxSupport} methods like {@link #getNextToken(int)}, they will use * the <b>same TokenSequence</b> as passed to {@code userCode}. This allows to combine navigation calls from {@link XMLSyntaxSupport} * with client's own sequence movements. The TokenSequence instance passed to {@code userCode} can be queried for * current token offset after navigation. * * @param <T> * @param offset offset to position the sequence at * @param userCode code to execute * @return user-defined value * @throws BadLocationException if the user code throws BadLocationException * @throws IllegalStateException if the user code throws a checked exception */ @NullUnknown public <T> T runWithSequence(int offset, SequenceCallable<T> userCode) throws BadLocationException { T result; TokenSequence old = null; try { ((AbstractDocument)document).readLock(); old = cachedSequence.get(); cachedSequence.remove(); TokenSequence ts = getSequence(); if (ts == null) { throw new BadLocationException("No sequence for position", offset); // NOI18N } cachedSequence.set(ts); synchronized (ts) { ts.move(offset); ts.moveNext(); result = userCode.call(ts); } } finally { cachedSequence.set(old); ((AbstractDocument)document).readUnlock(); } return result; }
/** * Exeutes user code with {@link TokenSequence} positioned at a particular token. * This convenience method works much like {@link #runWithSequence(int, org.netbeans.modules.xml.text.api.dom.XMLSyntaxSupport.SequenceCallable)}, * except that Token (its starting offset) is used to position the sequence instead of raw offset value. * * @param <T> * @param startFrom token to start from * @param userCode user code to execute * @return user-defined value * @throws BadLocationException if the user code throws BadLocationException */ public <T> T runWithSequence(Token<XMLTokenId> startFrom, SequenceCallable<T> userCode) throws BadLocationException { T result; TokenSequence old = null; try { ((AbstractDocument)document).readLock(); old = cachedSequence.get(); cachedSequence.remove(); TokenHierarchy th = TokenHierarchy.get(((AbstractDocument)document)); TokenSequence ts = th.tokenSequence(); if (ts == null) { throw new BadLocationException("No sequence for position", startFrom.offset(null)); // NOI18N } cachedSequence.set(ts); synchronized (ts) { ts.move(startFrom.offset(th)); ts.moveNext(); result = userCode.call(ts); } } finally { cachedSequence.set(old); ((AbstractDocument)document).readUnlock(); } return result; }
/** * Get token at given offet or previous one if at token boundary. * It does not lock the document. Returns {@code null} for invalid offsets. * * @param offset valid position in document * @return Token instance */ public Token<XMLTokenId> getNextToken( int offset) throws BadLocationException { if (offset == 0) { offset = 1; } if (offset < 0) throw new BadLocationException("Offset " + offset + " cannot be less than 0.", offset); //NOI18N ((AbstractDocument)document).readLock(); try { TokenSequence ts = getSequence(); synchronized (ts) { return getToken(ts, offset, true, null); } } finally { ((AbstractDocument)document).readUnlock(); } }
/** Get the line object from the given position. * @param doc document for which the line is being retrieved * @param offset position in the document * @param original whether to retrieve the original line (true) before * the modifications were done or the current line (false) * @return the line object */ public static Line getLine(Document doc, int offset, boolean original) { DataObject dob = getDataObject(doc); if (dob != null) { LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class); if (lc != null) { Line.Set lineSet = lc.getLineSet(); if (lineSet != null) { Element lineRoot = (doc instanceof AbstractDocument) ? ((AbstractDocument)doc).getParagraphElement(0).getParentElement() : doc.getDefaultRootElement(); int lineIndex = lineRoot.getElementIndex(offset); return original ? lineSet.getOriginal(lineIndex) : lineSet.getCurrent(lineIndex); } } } return null; }
private static MimePath getFullMimePath(Document document, int offset) { String langPath = null; if (document instanceof AbstractDocument) { AbstractDocument adoc = (AbstractDocument)document; adoc.readLock(); try { List<TokenSequence<?>> list = TokenHierarchy.get(document).embeddedTokenSequences(offset, true); if (list.size() > 1) { langPath = list.get(list.size() - 1).languagePath().mimePath(); } } finally { adoc.readUnlock(); } } if (langPath == null) { langPath = NbEditorUtilities.getMimeType(document); } if (langPath != null) { return MimePath.parse(langPath); } else { return null; } }
public void testDocLanguagePaths() throws Exception { Document doc = new ModificationTextDocument(); // Assign a language to the document String text = "/**abc*/"; doc.insertString(0, text, null); TokenHierarchy<?> hi = TokenHierarchy.get(doc); ((AbstractDocument)doc).readLock(); try { Set<LanguagePath> lps = hi.languagePaths(); assertEquals(0, lps.size()); // Now put a valid language into document doc.putProperty(Language.class,TestLineTokenId.language()); // Re-check language paths again lps = hi.languagePaths(); assertNotNull(lps); assertEquals(1, lps.size()); assertTrue(lps.contains(LanguagePath.get(TestLineTokenId.language()))); } finally { ((AbstractDocument)doc).readUnlock(); } }
public void testSubSequenceInUnfinishedTH() throws Exception { Document doc = new ModificationTextDocument(); // 012345678 String text = "ab cd efg"; doc.insertString(0, text, null); doc.putProperty(Language.class,TestTokenId.language()); TokenHierarchy<?> hi = TokenHierarchy.get(doc); ((AbstractDocument)doc).readLock(); try { TokenSequence<?> ts = hi.tokenSequence(); assertTrue(ts.moveNext()); ts = ts.subSequence(2, 6); assertTrue(ts.moveNext()); LexerTestUtilities.assertTokenEquals(ts,TestTokenId.WHITESPACE, " ", 2); assertTrue(ts.moveNext()); LexerTestUtilities.assertTokenEquals(ts,TestTokenId.IDENTIFIER, "cd", 3); assertTrue(ts.moveNext()); LexerTestUtilities.assertTokenEquals(ts,TestTokenId.WHITESPACE, " ", 5); assertFalse(ts.moveNext()); } finally { ((AbstractDocument)doc).readUnlock(); } }
public void testSubSequenceInvalidOffset137564() throws Exception { Document doc = new ModificationTextDocument(); // 012345678 String text = "ab cd efg"; doc.insertString(0, text, null); doc.putProperty(Language.class,TestTokenId.language()); TokenHierarchy<?> hi = TokenHierarchy.get(doc); ((AbstractDocument)doc).readLock(); try { TokenSequence<?> ts = hi.tokenSequence(); assertTrue(ts.moveNext()); ts = ts.subSequence(-2, -1); assertFalse(ts.moveNext()); } finally { ((AbstractDocument)doc).readUnlock(); } }
public void testKnownMimeType() { PlainDocument doc = new PlainDocument(); doc.putProperty("mimeType", MIME_TYPE_KNOWN); TokenHierarchy th = TokenHierarchy.get(doc); ((AbstractDocument)doc).readLock(); try { Language lang = th.tokenSequence().language(); assertNotNull("There should be language for " + MIME_TYPE_KNOWN, lang); assertNotNull("Invalid mime type", lang.mimeType()); assertEquals("Wrong language's mime type", MIME_TYPE_KNOWN, lang.mimeType()); } finally { ((AbstractDocument)doc).readUnlock(); } }
private void setLengthFilter(JComboBox<?> cb) { Component editorComponent = cb.getEditor().getEditorComponent(); if (editorComponent instanceof JTextComponent) { JTextComponent tc = (JTextComponent) editorComponent; Document document = tc.getDocument(); if (document instanceof AbstractDocument) { AbstractDocument ad = (AbstractDocument) document; ad.setDocumentFilter(lengthFilter); } } }
public void propertyChange(java.beans.PropertyChangeEvent evt) { JTextComponent component = (JTextComponent)getContainer(); if (component==null || evt==null || (!EditorUI.LINE_HEIGHT_CHANGED_PROP.equals(evt.getPropertyName()) && !EditorUI.TAB_SIZE_CHANGED_PROP.equals(evt.getPropertyName()) ) ) { return; } AbstractDocument doc = (AbstractDocument)getDocument(); if (doc!=null) { doc.readLock(); try{ LockView lockView = LockView.get(this); lockView.lock(); try { rebuild(0, getViewCount()); } finally { lockView.unlock(); } } finally { doc.readUnlock(); } component.revalidate(); } }
@Override public void undoableEditHappened(UndoableEditEvent e) { UndoableEdit edit = e.getEdit(); if (edit instanceof AbstractDocument.DefaultDocumentEvent) { try { AbstractDocument.DefaultDocumentEvent event = (AbstractDocument.DefaultDocumentEvent) edit; int start = event.getOffset(); int len = event.getLength(); String text = ""; if ("addition".equals(edit.getPresentationName())) { text = event.getDocument().getText(start, len); } boolean isNeedStart = false; if (current == null || lastEditName == null || !lastEditName.equals(edit.getPresentationName()) || text.contains("\n") && !"deletion".equals(edit.getPresentationName()) || Math.abs(lastOffset - start) > 1) { isNeedStart = true; } while (pointer < edits.size() - 1) { edits.remove(edits.size() - 1); isNeedStart = true; } if (isNeedStart) { createCompoundEdit(); } current.addEdit(edit); lastEditName = edit.getPresentationName(); lastOffset = start; } catch (BadLocationException e1) { if (logger.isDebugEnabled()) { logger.debug(e1, e1); } } } }
@Override public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { try { Caret caret = target.getCaret(); boolean emptySelection = false; boolean disableNoSelectionCopy = Boolean.getBoolean("org.netbeans.editor.disable.no.selection.copy"); int caretPosition = caret.getDot(); if(!disableNoSelectionCopy && (!(caret instanceof EditorCaret)) || !(((EditorCaret)caret).getCarets().size() > 1)) { emptySelection = !Utilities.isSelectionShowing(target); // If there is no selection then pre-select a current line including newline if (emptySelection && !disableNoSelectionCopy) { Element elem = ((AbstractDocument) target.getDocument()).getParagraphElement( caretPosition); if (!Utilities.isRowWhite((BaseDocument) target.getDocument(), elem.getStartOffset())) { target.select(elem.getStartOffset(), elem.getEndOffset()); } } } target.copy(); if (emptySelection && !disableNoSelectionCopy) { target.setCaretPosition(caretPosition); } } catch (BadLocationException ble) { LOG.log(Level.FINE, null, ble); } } }
/** Construct document event instance. * @param offset position in the document where the insert/remove/change * occured * @param length number of the characters affected by the event * @param type type of the event - INSERT/REMOVE/CHANGE */ public BaseDocumentEvent(BaseDocument doc, int offset, int length, DocumentEvent.EventType type) { ((AbstractDocument)doc).super(offset, length, type); hasBeenDone2 = true; alive2 = true; inProgress2 = true; }
public static boolean noCompletion(JTextComponent target) { if (target == null || target.getCaret() == null) { return false; } int offset = target.getCaret().getDot(); if (offset < 0) { return false; } //no completion inside CDATA or comment section BaseDocument document = (BaseDocument) target.getDocument(); ((AbstractDocument) document).readLock(); try { TokenHierarchy th = TokenHierarchy.get(document); TokenSequence ts = th.tokenSequence(); if (ts == null) { return false; } ts.move(offset); Token token = ts.token(); if (token == null) { ts.moveNext(); token = ts.token(); if (token == null) { return false; } } if (token.id() == XMLTokenId.CDATA_SECTION || token.id() == XMLTokenId.BLOCK_COMMENT || token.id() == XMLTokenId.PI_START || token.id() == XMLTokenId.PI_END || token.id() == XMLTokenId.PI_CONTENT || token.id() == XMLTokenId.PI_TARGET) { return true; } } finally { ((AbstractDocument) document).readUnlock(); } return false; }
public void testEmbeddingPresence() throws Exception { Document d = new PlainDocument(); d.putProperty(Language.class,TestEmbeddingTokenId.language()); d.insertString(0, " acnacn", null); TokenHierarchy<?> h = TokenHierarchy.get(d); ((AbstractDocument)d).readLock(); try { TokenSequence<TestEmbeddingTokenId> ts = h.tokenSequence(TestEmbeddingTokenId.language()); TokenSequence<?> inner; LexerTestUtilities.assertNextTokenEquals(ts,TestEmbeddingTokenId.TEXT, " "); inner = ts.embedded(); LexerTestUtilities.assertNextTokenEquals(ts,TestEmbeddingTokenId.A, "a"); inner = ts.embedded(); LexerTestUtilities.assertNextTokenEquals(ts,TestEmbeddingTokenId.C, "c"); inner = ts.embedded(); LexerTestUtilities.assertNextTokenEquals(ts,TestEmbeddingTokenId.N, "n"); inner = ts.embedded(); LexerTestUtilities.assertNextTokenEquals(ts,TestEmbeddingTokenId.A, "a"); inner = ts.embedded(); LexerTestUtilities.assertNextTokenEquals(ts,TestEmbeddingTokenId.C, "c"); inner = ts.embedded(); LexerTestUtilities.assertNextTokenEquals(ts,TestEmbeddingTokenId.N, "n"); inner = ts.embedded(); assertEquals(1, TestEmbeddingTokenId.cEmbeddingQueryCount); assertEquals(2, TestEmbeddingTokenId.aEmbeddingQueryCount); } finally { ((AbstractDocument)d).readUnlock(); } }
public static CompletionResultItem getEndTagCompletionItem(JTextComponent component, BaseDocument document) { int caretPos = component.getCaret().getDot(); try { ((AbstractDocument) document).readLock(); TokenHierarchy tokenHierarchy = TokenHierarchy.get(document); TokenSequence tokenSequence = tokenHierarchy.tokenSequence(); String incompleteTagName = findIncompleteTagName(caretPos, tokenSequence); if (isCaretInsideTag(caretPos, tokenSequence)) return null; boolean beforeUnclosedStartTagFound = isUnclosedStartTagFoundBefore( caretPos, tokenSequence); if (! beforeUnclosedStartTagFound) return null; Token token = tokenSequence.token(); String startTagName = getTokenTagName(token); if (startTagName == null) return null; boolean closingTagFound = isClosingEndTagFoundAfter(caretPos, tokenSequence, startTagName); if (closingTagFound) return null; CompletionResultItem resultItem; if ((incompleteTagName != null) && (! startTagName.startsWith(incompleteTagName))) { resultItem = new TagLastCharResultItem(incompleteTagName, tokenSequence); } else { resultItem = new EndTagResultItem(startTagName, tokenSequence); } return resultItem; } catch(Exception e) { _logger.log(Level.WARNING, e.getMessage() == null ? e.getClass().getName() : e.getMessage(), e); return null; } finally { ((AbstractDocument) document).readUnlock(); } }
private TokenSequence getTokenSequence() { TokenSequence tokenSequence = null; try { ((AbstractDocument) document).readLock(); TokenHierarchy tokenHierarchy = TokenHierarchy.get(document); tokenSequence = tokenHierarchy.tokenSequence(); } catch(Exception e) { _logger.log(Level.WARNING, e.getMessage() == null ? e.getClass().getName() : e.getMessage(), e); } finally { ((AbstractDocument) document).readUnlock(); } return tokenSequence; }
public void checkStructureEquivalence(AbstractDocument.AbstractElement elem) { String name = elem.getName(); if (!goldenName.equals(name)) { throw new RuntimeException("Bad structure: expected element name is '" + goldenName + "' but the actual name was '" + name + "'."); } int goldenChildCount = goldenChildren.size(); int childCount = elem.getChildCount(); if (childCount != goldenChildCount) { System.out.print("D: children: "); for (int i = 0; i < childCount; i++) { System.out.print(" " + elem.getElement(i).getName()); } System.out.println(""); System.out.print("D: goldenChildren: "); for (GoldenElement ge : goldenChildren) { System.out.print(" " + ge.goldenName); } System.out.println(""); throw new RuntimeException("Bad structure: expected child count of element '" + goldenName + "' is '" + goldenChildCount + "' but the actual count was '" + childCount + "'."); } for (int i = 0; i < childCount; i++) { AbstractDocument.AbstractElement nextElem = (AbstractDocument.AbstractElement) elem.getElement(i); GoldenElement goldenElement = goldenChildren.get(i); goldenElement.checkStructureEquivalence(nextElem); } }
public static void collapseOrExpand(FoldHierarchy hierarchy, Collection foldTypes, boolean collapse) { Document d = hierarchy.getComponent().getDocument(); if (!(d instanceof AbstractDocument)) { // no op, the folding hierarchy does not work for != AbstractDocument return; } AbstractDocument adoc = (AbstractDocument)d; adoc.readLock(); try { hierarchy.lock(); try { List foldList = findRecursive(null, hierarchy.getRootFold(), foldTypes); if (collapse) { hierarchy.collapse(foldList); } else { hierarchy.expand(foldList); } } finally { hierarchy.unlock(); } } finally { adoc.readUnlock(); } }
private final void updateLineInfo(boolean fire) { ((AbstractDocument) component.getDocument()).readLock(); try { PositionsBag newPositions = getCurrentBlockPositions(component.getDocument()); synchronized (this) { if (newPositions != null) { selectionsBag.setHighlights(newPositions); } else { // newBlock is null => selection removed selectionsBag.clear(); } } } finally { ((AbstractDocument) component.getDocument()).readUnlock(); } }
public void testTokenCountWasCalledInUpdater() throws Exception { Document doc = new ModificationTextDocument(); String text = "+/* */"; doc.insertString(0, text, null); doc.putProperty(Language.class, TestTokenId.language()); TokenHierarchy<?> hi = TokenHierarchy.get(doc); TokenSequence<?> ts; ((AbstractDocument)doc).readLock(); try { ts = hi.tokenSequence(); assertTrue(ts.moveNext()); LexerTestUtilities.assertTokenEquals(ts,TestTokenId.PLUS, "+", -1); } finally { ((AbstractDocument)doc).readUnlock(); } doc.remove(1, 3); // Remove "/* " ((AbstractDocument)doc).readLock(); try { ts = hi.tokenSequence(); ts.moveEnd(); // Extra ending '\n' of the document returned by DocumentUtilities.getText(doc) and lexed assertTrue(ts.movePrevious()); LexerTestUtilities.assertTokenEquals(ts,TestTokenId.WHITESPACE, "\n", -1); assertTrue(ts.movePrevious()); LexerTestUtilities.assertTokenEquals(ts,TestTokenId.DIV, "/", -1); } finally { ((AbstractDocument)doc).readUnlock(); } }
public Object evaluate (Context context) { if (context instanceof SyntaxContext) { Object l = ((SyntaxContext) context).getASTPath ().getLeaf (); if (l instanceof ASTNode) return evaluate ((ASTNode) l); if (l instanceof ASTToken) return evaluate ((ASTToken) l); } else { AbstractDocument document = (AbstractDocument) context.getDocument (); document.readLock (); ASTToken stoken = null; try { TokenSequence tokenSequence = Utils.getTokenSequence (document, context.getOffset ()); Token token = tokenSequence.token (); if (token != null) stoken = ASTToken.create ( null, token.id ().ordinal (), token.text ().toString (), tokenSequence.offset () ); } finally { document.readUnlock (); } return evaluate (stoken); } throw new IllegalArgumentException (); }
public static boolean isJavaContext(final JTextComponent component, final int offset) { Document doc = component.getDocument(); org.netbeans.api.lexer.Language language = (org.netbeans.api.lexer.Language)doc.getProperty(org.netbeans.api.lexer.Language.class); if (language == null) { return true; } if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { TokenSequence ts = TokenHierarchy.get(component.getDocument()).tokenSequence(); if (ts == null) { return false; } if (!ts.moveNext() || ts.move(offset) == 0) { return true; } if (!ts.moveNext()) { // Move to the next token after move(offset) return false; } // I used to block completion in comments... but why should I do that? // TokenId tokenId = ts.token().id(); // // Set s = language.tokenCategories().contains(COMMENT_CATEGORY_NAME) // ? language.tokenCategoryMembers(COMMENT_CATEGORY_NAME) // : null; // // return s == null || !s.contains(tokenId); //NOI18N return true; } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument) doc).readUnlock(); } } }