1.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java Tue Jan 15 11:24:05 2013 +0100
1.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java Tue Jan 15 11:45:41 2013 +0100
1.3 @@ -43,6 +43,7 @@
1.4
1.5 import org.netbeans.core.spi.multiview.MultiViewElement;
1.6 import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
1.7 +import org.netbeans.modules.csl.api.DeclarationFinder;
1.8 import org.netbeans.modules.csl.api.OccurrencesFinder;
1.9 import org.netbeans.modules.csl.api.SemanticAnalyzer;
1.10 import org.netbeans.modules.csl.api.StructureScanner;
1.11 @@ -122,5 +123,10 @@
1.12 public OccurrencesFinder getOccurrencesFinder() {
1.13 return new AntlrOccurrencesFinder();
1.14 }
1.15 +
1.16 + @Override
1.17 + public DeclarationFinder getDeclarationFinder() {
1.18 + return new AntlrDeclarationFinder();
1.19 + }
1.20
1.21 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrDeclarationFinder.java Tue Jan 15 11:45:41 2013 +0100
2.3 @@ -0,0 +1,168 @@
2.4 +/*
2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2.6 + *
2.7 + * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
2.8 + *
2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
2.10 + * Other names may be trademarks of their respective owners.
2.11 + *
2.12 + * The contents of this file are subject to the terms of either the GNU
2.13 + * General Public License Version 2 only ("GPL") or the Common
2.14 + * Development and Distribution License("CDDL") (collectively, the
2.15 + * "License"). You may not use this file except in compliance with the
2.16 + * License. You can obtain a copy of the License at
2.17 + * http://www.netbeans.org/cddl-gplv2.html
2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
2.19 + * specific language governing permissions and limitations under the
2.20 + * License. When distributing the software, include this License Header
2.21 + * Notice in each file and include the License file at
2.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
2.23 + * particular file as subject to the "Classpath" exception as provided
2.24 + * by Oracle in the GPL Version 2 section of the License file that
2.25 + * accompanied this code. If applicable, add the following below the
2.26 + * License Header, with the fields enclosed by brackets [] replaced by
2.27 + * your own identifying information:
2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
2.29 + *
2.30 + * If you wish your version of this file to be governed by only the CDDL
2.31 + * or only the GPL Version 2, indicate your decision by adding
2.32 + * "[Contributor] elects to include this software in this distribution
2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
2.34 + * single choice of license, a recipient has the option to distribute
2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
2.36 + * to extend the choice of license to its licensees as provided above.
2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
2.38 + * Version 2 license, then the option applies only if the new code is
2.39 + * made subject to such option by the copyright holder.
2.40 + *
2.41 + * Contributor(s):
2.42 + *
2.43 + * Portions Copyrighted 2013 Sun Microsystems, Inc.
2.44 + */
2.45 +package org.netbeans.modules.antlr.editor;
2.46 +
2.47 +import java.util.concurrent.atomic.AtomicReference;
2.48 +import javax.swing.text.Document;
2.49 +import org.antlr.runtime.CommonToken;
2.50 +import org.antlr.runtime.tree.CommonTree;
2.51 +import org.antlr.runtime.tree.TreeVisitor;
2.52 +import org.antlr.runtime.tree.TreeVisitorAction;
2.53 +import org.netbeans.api.lexer.Token;
2.54 +import org.netbeans.api.lexer.TokenHierarchy;
2.55 +import org.netbeans.api.lexer.TokenSequence;
2.56 +import static org.netbeans.modules.antlr.editor.AntlrTokenId.RULE_REF;
2.57 +import static org.netbeans.modules.antlr.editor.AntlrTokenId.TOKEN_REF;
2.58 +import org.netbeans.modules.antlr.editor.gen.ANTLRv3Parser;
2.59 +import org.netbeans.modules.csl.api.DeclarationFinder;
2.60 +import org.netbeans.modules.csl.api.OffsetRange;
2.61 +import org.netbeans.modules.csl.spi.ParserResult;
2.62 +import org.openide.filesystems.FileObject;
2.63 +
2.64 +/**
2.65 + *
2.66 + * @author marekfukala
2.67 + */
2.68 +public class AntlrDeclarationFinder implements DeclarationFinder {
2.69 +
2.70 + @Override
2.71 + public DeclarationLocation findDeclaration(ParserResult info, int caretOffset) {
2.72 + NbAntlrParserResult result = (NbAntlrParserResult)info;
2.73 + final FileObject file = info.getSnapshot().getSource().getFileObject();
2.74 + TokenHierarchy<?> th = info.getSnapshot().getTokenHierarchy();
2.75 + TokenSequence<AntlrTokenId> ts = th.tokenSequence(AntlrTokenId.language());
2.76 +
2.77 + int diff = ts.move(caretOffset);
2.78 +
2.79 + Token<AntlrTokenId> curr = null;
2.80 + if (diff == 0) {
2.81 + if (ts.movePrevious()) {
2.82 + curr = ts.token();
2.83 + }
2.84 + } else {
2.85 + if (ts.moveNext()) {
2.86 + curr = ts.token();
2.87 + }
2.88 + }
2.89 + if (curr == null) {
2.90 + return DeclarationLocation.NONE;
2.91 + }
2.92 +
2.93 + final AntlrTokenId id = curr.id();
2.94 + switch (id) {
2.95 + case TOKEN_REF:
2.96 + case RULE_REF:
2.97 + break;
2.98 + default:
2.99 + return DeclarationLocation.NONE;
2.100 + }
2.101 + final CharSequence searched = curr.text();
2.102 + final AtomicReference<DeclarationLocation> found = new AtomicReference<DeclarationLocation>();
2.103 + TreeVisitor visitor = new TreeVisitor();
2.104 + visitor.visit(result.getParseTree(), new TreeVisitorAction() {
2.105 + @Override
2.106 + public Object pre(Object o) {
2.107 + CommonTree t = (CommonTree) o;
2.108 + switch(t.getType()) {
2.109 + case ANTLRv3Parser.RULE:
2.110 + //get next ID rule
2.111 + CommonTree id = (CommonTree) t.getChild(0);
2.112 + assert id.getType() == ANTLRv3Parser.ID;
2.113 +
2.114 + CommonToken ct = (CommonToken)id.getToken();
2.115 + String text = ct.getText();
2.116 + if(Utils.equals(text, searched, false, false)) {
2.117 + found.set(new DeclarationLocation(file, ct.getStartIndex()));
2.118 + }
2.119 + break;
2.120 + }
2.121 + return t;
2.122 + }
2.123 +
2.124 + @Override
2.125 + public Object post(Object o) {
2.126 + CommonTree t = (CommonTree) o;
2.127 + return t;
2.128 + }
2.129 + });
2.130 +
2.131 + return found.get();
2.132 + }
2.133 +
2.134 + @Override
2.135 + public OffsetRange getReferenceSpan(final Document doc, final int caretOffset) {
2.136 + final AtomicReference<OffsetRange> ret = new AtomicReference<OffsetRange>(OffsetRange.NONE);
2.137 + doc.render(new Runnable() {
2.138 + @Override
2.139 + public void run() {
2.140 + TokenHierarchy<?> th = TokenHierarchy.get(doc);
2.141 + TokenSequence<AntlrTokenId> ts = th.tokenSequence(AntlrTokenId.language());
2.142 +
2.143 + int diff = ts.move(caretOffset);
2.144 +
2.145 + Token<AntlrTokenId> curr = null;
2.146 + if (diff == 0) {
2.147 + if (ts.movePrevious()) {
2.148 + curr = ts.token();
2.149 + }
2.150 + } else {
2.151 + if (ts.moveNext()) {
2.152 + curr = ts.token();
2.153 + }
2.154 + }
2.155 + if (curr == null) {
2.156 + return;
2.157 + }
2.158 + switch (curr.id()) {
2.159 + case TOKEN_REF:
2.160 + case RULE_REF:
2.161 + ret.set(new OffsetRange(ts.offset(), ts.offset() + curr.length()));
2.162 + break;
2.163 + }
2.164 +
2.165 + }
2.166 + });
2.167 + return ret.get();
2.168 + }
2.169 +
2.170 +
2.171 +}
3.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrTokenId.java Tue Jan 15 11:24:05 2013 +0100
3.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrTokenId.java Tue Jan 15 11:45:41 2013 +0100
3.3 @@ -174,6 +174,10 @@
3.4 this.code = code;
3.5 }
3.6
3.7 + public int getAntlrTokenId() {
3.8 + return code;
3.9 + }
3.10 +
3.11 /**
3.12 * Gets a LanguageDescription describing a set of token ids that comprise
3.13 * the given language.