Java Code Examples for javax.swing.text.Document#putProperty()

The following examples show how to use javax.swing.text.Document#putProperty() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public void testSimple2() throws Exception {
    loggingOn();
    RandomTestContainer container = createContainer();
    JEditorPane pane = container.getInstance(JEditorPane.class);
    Document doc = pane.getDocument();
    doc.putProperty("mimeType", "text/plain");
    RandomTestContainer.Context gContext = container.context();
    DocumentTesting.insert(gContext, 0, "a\nb\n\n");
    DocumentTesting.remove(gContext, 2, 1);
    DocumentTesting.insert(gContext, 1, "c");
}
 
Example 2
Source Project: netbeans   File: XMLKit.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Document createDefaultDocument() {
    if(J2EE_LEXER_COLORING) {
        Document doc = new XMLEditorDocument(getContentType());
        doc.putProperty(Language.class, XMLTokenId.language());
        return doc;
    } else {
        return super.createDefaultDocument();
    }
}
 
Example 3
public void testRemoveNewline() throws Exception {
    loggingOn();
    RandomTestContainer container = createContainer();
    JEditorPane pane = container.getInstance(JEditorPane.class);
    Document doc = pane.getDocument();
    doc.putProperty("mimeType", "text/plain");
    RandomTestContainer.Context context = container.context();
    ViewHierarchyRandomTesting.disableHighlighting(container);
    DocumentTesting.insert(context, 0, "a\nb");
    DocumentTesting.remove(context, 1, 1);
    DocumentTesting.undo(context, 1);
}
 
Example 4
private static void navigateToOccurence(boolean next, JTextComponent txt) {
    if (txt != null && txt.getDocument() != null) {
        Document doc = txt.getDocument();
        int position = txt.getCaretPosition();
        int goTo = findOccurrencePosition(next, doc, position);
        if (goTo > 0) {
            txt.setCaretPosition(goTo);
            doc.putProperty(markedOccurence, new long[] {DocumentUtilities.getDocumentVersion(doc), goTo});
        } else {
            StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(GoToMarkOccurrencesAction.class, "no-marked-occurrence"));
            doc.putProperty(markedOccurence, null);
        }
    }
}
 
Example 5
Source Project: netbeans   File: BookmarkList.java    License: Apache License 2.0 5 votes vote down vote up
public static BookmarkList get (Document doc) {
    BookmarkList bookmarkList = (BookmarkList) doc.getProperty (BookmarkList.class);
    if (bookmarkList == null) {
        bookmarkList = new BookmarkList (doc);
        doc.putProperty (BookmarkList.class, bookmarkList);
    }
    return bookmarkList;
}
 
Example 6
public void testLengthyEdit() throws Exception {
        loggingOn();
        RandomTestContainer container = createContainer();
        JEditorPane pane = container.getInstance(JEditorPane.class);
        Document doc = pane.getDocument();
        doc.putProperty("mimeType", "text/plain");
        ViewHierarchyRandomTesting.initRandomText(container);
        final RandomTestContainer.Context context = container.context();
        for (int i = 0; i < 100; i++) {
            DocumentTesting.insert(context, 0, "abcdefghijklmnopqrst\n");
        }
        DocumentTesting.setSameThreadInvoke(context, true); // Otherwise runAtomic() would lock forever waiting for EDT
        final BaseDocument bdoc = (BaseDocument) doc;
        SwingUtilities.invokeAndWait(new Runnable() {
            private boolean inRunAtomic;
            @Override
            public void run() {
                if (!inRunAtomic) {
                    inRunAtomic = true;
                    bdoc.runAtomic(this);
                    return;
                }

                try {
                    for (int i = 0; i < 100; i++) {
//                        DocumentTesting.insert(context, i * 22 + 3, "a\n");
                        DocumentTesting.remove(context, i * 20 + 2, 1);
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        });
        DocumentTesting.setSameThreadInvoke(context, false);
        EditorPaneTesting.moveOrSelect(context, SwingConstants.NORTH, false);
//        DocumentTesting.insert(context, 50, "x\nab\n");
//        EditorPaneTesting.setCaretOffset(context, 20);
    }
 
Example 7
public static synchronized OffsetsBag getBag(JTextComponent component) {
    Document doc = component.getDocument();
    OffsetsBag bag = null;
    if (doc != null) {
        bag = (OffsetsBag) doc.getProperty(SpellcheckerHighlightLayerFactory.class);
        if (bag == null) {
            doc.putProperty(SpellcheckerHighlightLayerFactory.class, bag = new OffsetsBag(doc));
        }
    }
    Spellchecker.register (component);
    
    return bag;
}
 
Example 8
public StringWithNewLinesCellEditor() {
  super(new JBTextField() {
    @Override
    public void setDocument(Document doc) {
      super.setDocument(doc);
      doc.putProperty("filterNewlines", Boolean.FALSE);
    }
  });
}
 
Example 9
Source Project: netbeans   File: HtmlEditorSupport.java    License: Apache License 2.0 5 votes vote down vote up
private void setEncodingProperty(String encoding) {
    //FEQ cannot be run in saveFromKitToStream since document is locked for writing,
    //so setting the FEQ result to document property
    Document doc = getDocument();
    //the document is already loaded so getDocument() should normally return 
    //no null value, but if a CES redirector returns null from the redirected
    //CES.getDocument() then we are not able to set the found encoding
    if (doc != null) {
        doc.putProperty(DOCUMENT_SAVE_ENCODING, encoding);
    }
}
 
Example 10
Source Project: netbeans   File: Editor.java    License: Apache License 2.0 5 votes vote down vote up
private void markChanged( DocumentEvent evt ) {
    Document doc = evt.getDocument();
    doc.putProperty( MODIFIED, Boolean.TRUE );
    
    File file = (File)doc.getProperty( FILE );
    int index = tabPane.indexOfComponent( comp );
    
    tabPane.setTitleAt( index, file.getName() + '*' );
    
    doc.removeDocumentListener( this );
}
 
Example 11
Source Project: netbeans   File: TokenHierarchyTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test120906() throws Exception {
    Document doc = new ModificationTextDocument();
    String text = "/**abc*/";
    doc.insertString(0, text, null);
    doc.putProperty(Language.class,TestTokenId.language());
    
    TokenHierarchy<?> hi = TokenHierarchy.get(doc);
    ((AbstractDocument)doc).readLock();
    try {
        List<TokenSequence<?>> ets1 = hi.embeddedTokenSequences(4, false);
        assertEquals("Wrong number of embedded TokenSequences", 2, ets1.size());
        assertEquals("Wrong offset from the most embedded TokenSequence", 3, ets1.get(1).offset());

        List<TokenSequence<?>> ets2 = hi.embeddedTokenSequences(6, false);
        assertEquals("Wrong number of embedded TokenSequences", 1, ets2.size());
        assertEquals("Wrong offset from the most embedded TokenSequence", 0, ets2.get(0).offset());

        List<TokenSequence<?>> ets3 = hi.embeddedTokenSequences(3, true);
        assertEquals("Wrong number of embedded TokenSequences", 1, ets3.size());
        assertEquals("Wrong offset from the most embedded TokenSequence", 0, ets3.get(0).offset());

        List<TokenSequence<?>> ets4 = hi.embeddedTokenSequences(0, true);
        assertEquals("Wrong number of embedded TokenSequences", 0, ets4.size());

        // Lexer works over char sequence DocumentUtilities.getText(doc) which is doc.getLength()+1 long
        List<TokenSequence<?>> ets5 = hi.embeddedTokenSequences(doc.getLength()+1, false);
        assertEquals("Wrong number of embedded TokenSequences", 0, ets5.size());
    } finally {
        ((AbstractDocument)doc).readUnlock();
    }
}
 
Example 12
Source Project: netbeans   File: TextmateLexerTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testRestart() throws Exception {
    clearWorkDir();

    FileObject grammar = FileUtil.createData(FileUtil.getConfigRoot(), "Editors/text/test/grammar.json");
    try (OutputStream out = grammar.getOutputStream();
         Writer w = new OutputStreamWriter(out)) {
        w.write("{ \"scopeName\": \"test\", " +
                " \"patterns\": [\n" +
                "  { \"name\": \"string.test\",\n" +
                "    \"begin\": \"x([^x]+)x\",\n" +
                "    \"end\": \"x\\\\1x\"\n" +
                "   },\n" +
                "  { \"name\": \"whitespace.test\",\n" +
                "    \"match\": \" +\"\n" +
                "   }\n" +
                "]}\n");
    }
    grammar.setAttribute(LanguageHierarchyImpl.GRAMMAR_MARK, "test");
    Document doc = new PlainDocument();
    doc.insertString(0, " xaax xbbx\nxccx xaax ", null);
    doc.putProperty(Language.class, new LanguageHierarchyImpl("text/test", "test").language());
    TokenHierarchy<Document> hi = TokenHierarchy.get(doc);
    TokenSequence<?> ts = hi.tokenSequence();
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " xbbx\n");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xccx ");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.UNTOKENIZED, "\n");
    assertFalse(ts.moveNext());
    doc.insertString(8, "b", null);
    ts = hi.tokenSequence();
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " xbbbx\n");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xccx ");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.UNTOKENIZED, "\n");
    assertFalse(ts.moveNext());
    doc.insertString(14, "c", null);
    ts = hi.tokenSequence();
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " xbbbx\n");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xcccx ");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.UNTOKENIZED, "\n");
    assertFalse(ts.moveNext());
    doc.insertString(3, "a", null);
    ts = hi.tokenSequence();
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " xbbbx\n");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xcccx xaax \n");
    assertFalse(ts.moveNext());
    doc.remove(3, 1);
    ts = hi.tokenSequence();
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " xbbbx\n");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xcccx ");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, "xaax");
    assertTokenProperties(ts, "test", "string.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.TEXTMATE, " ");
    assertTokenProperties(ts, "test", "whitespace.test");
    LexerTestUtilities.assertNextTokenEquals(ts, TextmateTokenId.UNTOKENIZED, "\n");
    assertFalse(ts.moveNext());
}
 
Example 13
Source Project: netbeans   File: ModRootElement.java    License: Apache License 2.0 4 votes vote down vote up
public ModRootElement(Document doc) {
    super(doc);
    docText = DocumentUtilities.getText(doc);
    doc.putProperty(NAME, this);
}
 
Example 14
Source Project: netbeans   File: JPAProblemFinder.java    License: Apache License 2.0 4 votes vote down vote up
private void createPersistenceScopesListener(FileObject file, Document doc){
    if (doc == null){
        return;
    }
    
    LOG.fine("Creating PersistenceScopesListener on " + file.getName());
    Project project = FileOwnerQuery.getOwner(file);
    
    if (project != null){
        ClassPath cp = ClassPath.getClassPath(file, ClassPath.SOURCE);
        PersistenceScopes scopes = PersistenceScopes.getPersistenceScopes(project, cp != null ? cp.findOwnerRoot(file) : null);
        
        if (scopes != null){
            PersistenceScopesListener listener = (PersistenceScopesListener) doc.getProperty(PERSISTENCE_SCOPES_LISTENER);
            
            if (listener == null){
                listener = new PersistenceScopesListener(file);
                PropertyChangeListener weakListener = WeakListeners.create(PropertyChangeListener.class, listener, null);
                scopes.addPropertyChangeListener(weakListener);
                
                // scopes listener should live as long as the document
                doc.putProperty(PERSISTENCE_SCOPES_LISTENER, listener);
            }
            
            ArrayList<PersistenceXMLListener> pxmlListeners = new ArrayList<PersistenceXMLListener>();
            
            for (PersistenceScope scope : scopes.getPersistenceScopes()){
                FileObject persistenceXML = scope.getPersistenceXml();
                if(persistenceXML!=null){//persistence xml may be deleted/renamed
                    PersistenceXMLListener pxmlListener = new PersistenceXMLListener(file);
                    FileChangeListener weakPXMLListener = WeakListeners.create(FileChangeListener.class, pxmlListener, null);
                    persistenceXML.addFileChangeListener(weakPXMLListener);
                    pxmlListeners.add(pxmlListener);
                    LOG.fine("Added PersistenceXMLListener to " + persistenceXML.getName());
                }
            }
            
            // persistence.xml listeners should live as long as the scopes listener
            listener.setPXMLListeners(pxmlListeners);
        }
    }
}
 
Example 15
Source Project: netbeans   File: HtmlSourceTask.java    License: Apache License 2.0 4 votes vote down vote up
@Override
    public void run(HtmlParserResult result, SchedulerEvent event) {
        Source source = result.getSnapshot().getSource();

        //embedding stuff: process only xhtml file contents, while the task needs to be bound to text/html
        if (!source.getMimeType().equals(JsfUtils.XHTML_MIMETYPE)) { //NOI18N
            return;
        }

        JsfSupportImpl sup = JsfSupportImpl.findFor(source); //activate the jsf support
        if (sup == null) {
            return;
        }

        //enable EL support it this xhtml file
        //TODO possibly add if(jsf_used()) { //enable el }
        Document doc = result.getSnapshot().getSource().getDocument(true);
        if (doc == null) {
            return;
        }
        InputAttributes inputAttributes = (InputAttributes) doc.getProperty(InputAttributes.class);
        if (inputAttributes == null) {
            inputAttributes = new InputAttributes();
//            inputAttributes.setValue(HTMLTokenId.language(), "enable el", new Object(), false); //NOI18N
            doc.putProperty(InputAttributes.class, inputAttributes);
        }

        //enable css class embedding in default facelets libraries tags
        //TODO this should be done in some more generic way but so far I haven't
        //found a way how to get an info if a tag's attribute represents css class or not.
        //It seems that almost only html library contains such tags, we should
        //probably create some metadata also for third party libraries

        //check if the default html library is defined
        String prefix = NamespaceUtils.getForNs(result.getNamespaces(), DefaultLibraryInfo.HTML.getNamespace());
        if (prefix != null) {
            //html lib declared, lets build a map of tags containing attributes whose values are
            //supposed to represent a css class. The map is then put into the document's
            //input attributes and then html lexer takes this information into account
            //when lexing the html code
            Map<String, Collection<String>> cssClassTagAttrMap = new HashMap<>();
            Library lib = sup.getLibrary(DefaultLibraryInfo.HTML.getNamespace());
            if (lib != null) {
                Collection<? extends LibraryComponent> components = lib.getComponents();
                for (LibraryComponent comp : components) {
                    Tag tag = comp.getTag();
                    //hacking datatable's attributes embedding - waiting for Tomasz' tag metadata API
                    if ("dataTable".equals(tag.getName())) { //NOI18N
                        cssClassTagAttrMap.put(prefix + ":" + tag.getName(),
                                Arrays.asList(new String[]{STYLE_CLASS_ATTR_NAME,
                                    "headerClass", "footerClass", "rowClasses", "columnClasses", "captionClass"})); //NOI18N
                    } else {
                        if (tag.getAttribute(STYLE_CLASS_ATTR_NAME) != null) {
                            cssClassTagAttrMap.put(prefix + ":" + tag.getName(), Collections.singletonList(STYLE_CLASS_ATTR_NAME));
                        }
                    }
                }
            }

            inputAttributes.setValue(HTMLTokenId.language(), CSS_CLASS_MAP_PROPERTY_KEY, cssClassTagAttrMap, true);

        } else {
            //remove the map, the html library is not declared (anymore)
            inputAttributes.setValue(HTMLTokenId.language(), CSS_CLASS_MAP_PROPERTY_KEY, null, true);
        }


    }
 
Example 16
public void testSnapshot() throws Exception {
        if (true)
            return; // Snapshots will no longer function after #87014
        Document doc = new ModificationTextDocument();
        // Assign a language to the document
        doc.putProperty(Language.class,TestTokenId.language());
        TokenHierarchy<?> hi = TokenHierarchy.get(doc);
        assertNotNull("Null token hierarchy for document", hi);
        TokenSequence<?> ts = hi.tokenSequence();
        assertFalse(ts.moveNext());
        
        // Insert text into document
        String text = "abc+def-xyz";
        doc.insertString(0, text, null);

        ts = hi.tokenSequence();
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.IDENTIFIER, "abc", 0);
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.PLUS, "+", 3);
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.IDENTIFIER, "def", 4);
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.MINUS, "-", 7);
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.IDENTIFIER, "xyz", 8);
        assertFalse(ts.moveNext());
        LexerTestUtilities.incCheck(doc, false);

        // Create snapshot1 and check hierarchy
        String hi1text = doc.getText(0, doc.getLength());
        TokenHierarchy<?> hi1 = TokenHierarchy.create(hi1text,TestTokenId.language());
//        TokenHierarchy<?> snapshot1 = hi.createSnapshot();
//        assertEquals(snapshot1.snapshotOf(), hi);
//        assertFalse(snapshot1.isSnapshotReleased());
//
//        // Check that all the non-fly tokens are mutable
//        ts = snapshot1.tokenSequence();
//        assertEquals(0, ts.moveIndex(0));
//        assertTrue(ts.moveNext());
//        
//        LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1,
//                snapshot1.tokenSequence(), snapshot1, false);
//
//        doc.insertString(4, "+", null);
//
//        // Check that the snapshot token sequence can be further operated.
//        assertEquals(0, ts.moveIndex(0));
//        assertTrue(ts.moveNext());
//        assertNotNull(ts.token());
//
//        // Check that the tokens except '+' are live
//        ts = snapshot1.tokenSequence();
//
//        LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1,
//                snapshot1.tokenSequence(), snapshot1, true);
//
//        // Create snapshot2 and check hierarchy
//        String hi2text = doc.getText(0, doc.getLength());
//        TokenHierarchy<?> hi2 = TokenHierarchy.create(hi2text,TestTokenId.language());
//        TokenHierarchy<?> snapshot2 = hi.createSnapshot();
//        assertEquals(snapshot2.snapshotOf(), hi);
//
//        // Check that all the non-fly tokens are mutable
//        ts = snapshot2.tokenSequence();
//        assertEquals(0, ts.moveIndex(0));
//        assertTrue(ts.moveNext());
//
//        LexerTestUtilities.assertTokenSequencesEqual(hi2.tokenSequence(), hi2,
//                snapshot2.tokenSequence(), snapshot2, false);
//
//        doc.remove(8, 1);
//
//        LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1,
//                snapshot1.tokenSequence(), snapshot1, false);
//        LexerTestUtilities.assertTokenSequencesEqual(hi2.tokenSequence(), hi2,
//                snapshot2.tokenSequence(), snapshot2, false);
    }
 
Example 17
Source Project: netbeans   File: TokenListUpdaterTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testRemoveUnfinishedLexingZeroLookaheadToken() throws Exception {
    Document doc = new ModificationTextDocument();
    // Assign a language to the document
    String text = "a+b";
    doc.insertString(0, text, null);

    doc.putProperty(Language.class,TestTokenId.language());
    TokenHierarchy<?> hi = TokenHierarchy.get(doc);
    ((AbstractDocument)doc).readLock();
    TokenSequence<?> ts;
    try {
        ts = hi.tokenSequence();
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.IDENTIFIER, "a", 0);
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.PLUS, "+", 1);
    } finally {
        ((AbstractDocument)doc).readUnlock();
    }
    
    // Remove "+"
    doc.remove(1, 1);
    ((AbstractDocument)doc).readLock();
    try {
        ts.moveNext();
        fail("Should not get there");
    } catch (ConcurrentModificationException e) {
        // Expected
    } finally {
        ((AbstractDocument)doc).readUnlock();
    }

    ((AbstractDocument)doc).readLock();
    try {
        ts = hi.tokenSequence();
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.IDENTIFIER, "ab", 0);
        // Extra ending '\n' of the document returned by DocumentUtilities.getText(doc) and lexed
        assertTrue(ts.moveNext());
        LexerTestUtilities.assertTokenEquals(ts,TestTokenId.WHITESPACE, "\n", 2);
        assertFalse(ts.moveNext());
    } finally {
        ((AbstractDocument)doc).readUnlock();
    }
}
 
Example 18
Source Project: netbeans   File: HintTestBase.java    License: Apache License 2.0 4 votes vote down vote up
private CompilationInfo parse(FileObject file) throws DataObjectNotFoundException, IllegalArgumentException, IOException {
    DataObject od = DataObject.find(file);
    EditorCookie ec = od.getLookup().lookup(EditorCookie.class);

    assertNotNull(ec);

    Document doc = ec.openDocument();

    doc.putProperty(Language.class, JavaTokenId.language());
    doc.putProperty("mimeType", "text/x-java");

    JavaSource js = JavaSource.create(ClasspathInfo.create(file), file);

    assertNotNull("found JavaSource for " + file, js);

    final DeadlockTask bt = new DeadlockTask(Phase.RESOLVED);

    js.runUserActionTask(bt, true);
    
    return bt.info;
}
 
Example 19
Source Project: netbeans   File: GroovyLexerIncTest.java    License: Apache License 2.0 4 votes vote down vote up
public void test3() throws BadLocationException {
    Document doc = new ModificationTextDocument();
    // Assign a language to the document
    doc.putProperty(Language.class,GroovyTokenId.language());
    TokenHierarchy<?> hi = TokenHierarchy.get(doc);
    assertNotNull("Null token hierarchy for document", hi);
    TokenSequence<?> ts = hi.tokenSequence();
    // contains \n
    assertTrue(ts.moveNext());
    assertEquals(GroovyTokenId.NLS, ts.token().id());

    assertFalse(ts.moveNext());

    // Insert text into document
    String text = "class Foo {  }";
    doc.insertString(0, text, null);

    // Last token sequence should throw exception - new must be obtained
    try {
        ts.moveNext();
        fail("TokenSequence.moveNext() did not throw exception as expected.");
    } catch (ConcurrentModificationException e) {
        // Expected exception
    }

    ts = hi.tokenSequence();
    next(ts, GroovyTokenId.LITERAL_class, "class", 0);
    next(ts, GroovyTokenId.WHITESPACE, " ", 5);
    next(ts, GroovyTokenId.IDENTIFIER, "Foo", 6);
    next(ts, GroovyTokenId.WHITESPACE, " ", 9);
    next(ts, GroovyTokenId.LBRACE, "{", 10);
    next(ts, GroovyTokenId.WHITESPACE, "  ", 11);
    next(ts, GroovyTokenId.RBRACE, "}", 13);

    // contains \n
    assertTrue(ts.moveNext());
    assertEquals(GroovyTokenId.NLS, ts.token().id());

    assertFalse(ts.moveNext());

    LexerTestUtilities.incCheck(doc, false);

    int offset = text.length() - 2;
    doc.insertString(offset, "d", null);
    doc.insertString(offset + 1, "e", null);
    doc.insertString(offset + 2, "f", null);

    ts = hi.tokenSequence();
    next(ts, GroovyTokenId.LITERAL_class, "class", 0);
    next(ts, GroovyTokenId.WHITESPACE, " ", 5);
    next(ts, GroovyTokenId.IDENTIFIER, "Foo", 6);
    next(ts, GroovyTokenId.WHITESPACE, " ", 9);
    next(ts, GroovyTokenId.LBRACE, "{", 10);
    next(ts, GroovyTokenId.WHITESPACE, " ", 11);
    next(ts, GroovyTokenId.LITERAL_def, "def", 12);
    next(ts, GroovyTokenId.WHITESPACE, " ", 15);
    next(ts, GroovyTokenId.RBRACE, "}", 16);

    // contains \n
    assertTrue(ts.moveNext());
    assertEquals(GroovyTokenId.NLS, ts.token().id());

    assertFalse(ts.moveNext());

    LexerTestUtilities.incCheck(doc, false);
}
 
Example 20
Source Project: netbeans   File: EmbeddedTokenListTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testSnapshots() throws Exception {
        Document d = new PlainDocument();
        
        d.putProperty(Language.class,TestTokenId.language());
        
        d.insertString(0, "ident ident /** @see X */", null);
        
        TokenHierarchy<?> h = TokenHierarchy.get(d);
        ((AbstractDocument)d).readLock();
        try {
            TokenSequence<?> ts = h.tokenSequence();

            LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.IDENTIFIER, "ident");
            assertEquals(0, ts.offset());

            LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.WHITESPACE, " ");
            assertEquals(5, ts.offset());

            LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.IDENTIFIER, "ident");
            assertEquals(6, ts.offset());

            LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.WHITESPACE, " ");
            assertEquals(11, ts.offset());

            LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.JAVADOC_COMMENT, "/** @see X */");
            assertEquals(12, ts.offset());

            TokenSequence<?> inner = ts.embedded();

            assertNotNull(inner);

            LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
            assertEquals(15, inner.offset());

            LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.TAG, "@see");
            assertEquals(16, inner.offset());

            LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
            assertEquals(20, inner.offset());

            LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.IDENT, "X");
            assertEquals(21, inner.offset());

            LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
            assertEquals(22, inner.offset());
        } finally {
            ((AbstractDocument)d).readUnlock();
        }
            
        
        
//        h = TokenHierarchy.get(d).createSnapshot();
//        ts = h.tokenSequence();
//        
//        LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.IDENTIFIER, "ident");
//        assertEquals(0, ts.offset());
//        
//        LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.WHITESPACE, " ");
//        assertEquals(5, ts.offset());
//        
//        LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.IDENTIFIER, "ident");
//        assertEquals(6, ts.offset());
//        
//        LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.WHITESPACE, " ");
//        assertEquals(11, ts.offset());
//        
//        LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.JAVADOC_COMMENT, "/** @see X */");
//        assertEquals(12, ts.offset());
//        
//        inner = ts.embedded();
//        
//        assertNotNull(inner);
//        
//        LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
//        assertEquals(15, inner.offset());
//        
//        LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.TAG, "@see");
//        assertEquals(16, inner.offset());
//        
//        LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
//        assertEquals(20, inner.offset());
//        
//        LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.IDENT, "X");
//        assertEquals(21, inner.offset());
//        
//        LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
//        assertEquals(22, inner.offset());
    }