junichi11 commented on code in PR #7618:
URL: https://github.com/apache/netbeans/pull/7618#discussion_r1710810276


##########
php/php.blade/src/org/netbeans/modules/php/blade/editor/BladeDeclarationFinder.java:
##########
@@ -0,0 +1,462 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.php.blade.editor;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import org.antlr.v4.runtime.CharStreams;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.csl.api.DeclarationFinder;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.ParserResult;
+import org.netbeans.editor.BaseDocument;
+import org.netbeans.modules.csl.api.ElementHandle;
+import org.netbeans.modules.csl.api.HtmlFormatter;
+import org.netbeans.modules.php.blade.csl.elements.ElementType;
+import org.netbeans.modules.php.blade.csl.elements.NamedElement;
+import org.netbeans.modules.php.blade.csl.elements.PathElement;
+import org.netbeans.modules.php.blade.csl.elements.PhpFunctionElement;
+import 
org.netbeans.modules.php.blade.editor.declaration.ComponentDeclarationService;
+import 
org.netbeans.modules.php.blade.editor.declaration.VitePathDeclarationService;
+import org.netbeans.modules.php.blade.editor.directives.CustomDirectives;
+import 
org.netbeans.modules.php.blade.editor.directives.CustomDirectives.CustomDirective;
+import org.netbeans.modules.php.blade.editor.indexing.BladeIndex;
+import org.netbeans.modules.php.blade.editor.indexing.PhpIndexFunctionResult;
+import org.netbeans.modules.php.blade.editor.indexing.PhpIndexResult;
+import org.netbeans.modules.php.blade.editor.indexing.PhpIndexUtils;
+import org.netbeans.modules.php.blade.editor.indexing.QueryUtils;
+import org.netbeans.modules.php.blade.editor.lexer.BladeLexerUtils;
+import org.netbeans.modules.php.blade.editor.parser.BladeParserResult;
+import 
org.netbeans.modules.php.blade.editor.parser.BladeParserResult.FieldAccessReference;
+import 
org.netbeans.modules.php.blade.editor.parser.BladeParserResult.Reference;
+import org.netbeans.modules.php.blade.editor.path.BladePathUtils;
+import org.netbeans.modules.php.blade.syntax.StringUtils;
+import org.netbeans.modules.php.blade.syntax.antlr4.v10.BladeAntlrLexer;
+import org.netbeans.spi.lexer.antlr4.AntlrTokenSequence;
+import org.openide.filesystems.FileObject;
+import static 
org.netbeans.modules.php.blade.syntax.antlr4.v10.BladeAntlrLexer.*;
+import org.netbeans.modules.php.blade.syntax.antlr4.v10.BladeAntlrUtils;
+import org.netbeans.spi.project.ui.support.ProjectConvertors;
+import org.openide.filesystems.FileUtil;
+
+/**
+ * focuses mainly on string inputs
+ * 
+ * ?? TODO implement a Extension module
+ *
+ * @author bhaidu
+ */
+public class BladeDeclarationFinder implements DeclarationFinder {
+
+    private int currentTokenId;
+    private String tokenText;
+
+    @Override
+    public OffsetRange getReferenceSpan(Document document, int caretOffset) {
+        BaseDocument baseDoc = (BaseDocument) document;
+
+        //baseDoc.readLock();
+        AntlrTokenSequence tokens = null;
+        OffsetRange offsetRange = OffsetRange.NONE;
+        tokenText = null;
+        int lineOffset = caretOffset;
+        try {
+            try {
+                String text = baseDoc.getText(0, baseDoc.getLength());
+                tokens = new AntlrTokenSequence(new 
BladeAntlrLexer(CharStreams.fromString(text)));
+            } catch (BadLocationException ex) {
+                //Exceptions.printStackTrace(ex);
+            }
+        } finally {
+            //baseDoc.readUnlock();
+        }
+
+        //inside php expression context ??
+        if (tokens == null || tokens.isEmpty()) {
+            return offsetRange;
+        }
+
+        tokens.seekTo(lineOffset);
+
+        if (tokens.hasNext()) {
+            org.antlr.v4.runtime.Token nt = tokens.next().get();
+
+            switch (nt.getType()) {
+                case D_CUSTOM:
+                case PHP_IDENTIFIER:
+                case PHP_NAMESPACE_PATH:
+                    return new OffsetRange(nt.getStartIndex(), 
nt.getStopIndex() + 1);
+                case HTML_COMPONENT_PREFIX:
+                    //direct detection
+                    currentTokenId = HTML_COMPONENT_PREFIX;
+                    //remove '<x-'
+                    tokenText = nt.getText().length() > 3 ? nt.getText() : 
null;
+                    return new OffsetRange(nt.getStartIndex() + 1, 
nt.getStopIndex() + 1);
+            }
+
+            if (!tokens.hasPrevious()) {
+                return offsetRange;
+            }
+
+            if (nt.getType() == BL_PARAM_STRING || nt.getType() == 
EXPR_STRING) {
+                List<Integer> tokensToMatch = 
BladeLexerUtils.TOKENS_WITH_IDENTIFIABLE_PARAM;
+                List<Integer> tokensStop = Arrays.asList(new Integer[]{HTML});
+                org.antlr.v4.runtime.Token matchedToken = 
BladeAntlrUtils.findBackward(tokens, tokensToMatch, tokensStop);
+                int offsetCorrection = caretOffset - lineOffset;
+                if (matchedToken != null) {
+                    offsetRange = new OffsetRange(nt.getStartIndex() + 
offsetCorrection, nt.getStopIndex() + offsetCorrection + 1);
+                }
+                return offsetRange;
+            }
+        }
+        return offsetRange;
+    }
+
+    @Override
+    public DeclarationLocation findDeclaration(ParserResult info, int 
caretOffset) {
+        BladeParserResult parserResult = (BladeParserResult) info;
+
+        FileObject currentFile = parserResult.getFileObject();
+        DeclarationLocation location = DeclarationLocation.NONE;
+
+        if (tokenText != null && currentTokenId == HTML_COMPONENT_PREFIX) {
+            String componentId = tokenText.substring(3);
+            String className = StringUtils.kebabToCamel(componentId);
+            ComponentDeclarationService componentComplervice = new 
ComponentDeclarationService();
+            Collection<PhpIndexResult> indexedReferences = 
componentComplervice.queryComponents(className, currentFile);
+
+            for (PhpIndexResult indexReference : indexedReferences) {
+                NamedElement resultHandle = new NamedElement(className, 
indexReference.declarationFile, ElementType.LARAVEL_COMPONENT);
+                DeclarationLocation constantLocation = new 
DeclarationFinder.DeclarationLocation(indexReference.declarationFile, 
indexReference.getStartOffset(), resultHandle);
+                if (location.equals(DeclarationLocation.NONE)) {
+                    location = constantLocation;
+                }
+                location.addAlternative(new 
AlternativeLocationImpl(constantLocation));
+
+                if (!location.equals(DeclarationLocation.NONE)) {
+                    FileObject resource = 
componentComplervice.getComponentResourceFile(componentId, indexReference.name, 
currentFile);
+                    if (resource != null) {
+                        PathElement resourceHandle = new 
PathElement(componentId, resource);
+                        DeclarationLocation resourceLocation = new 
DeclarationFinder.DeclarationLocation(resource, 
indexReference.getStartOffset(), resourceHandle);
+                        location.addAlternative(new 
AlternativeLocationImpl(resourceLocation));
+                    }
+                }
+            }
+            return location;
+        }
+
+        FieldAccessReference fieldAccessReference = 
parserResult.findFieldAccessRefrence(caretOffset);
+
+        if (fieldAccessReference != null) {
+            switch (fieldAccessReference.type) {
+                case STATIC_FIELD_ACCESS:
+                    switch (fieldAccessReference.fieldType) {
+                        case CONSTANT:
+                            Collection<PhpIndexResult> indexConstantsResults = 
PhpIndexUtils.queryExactClassConstants(currentFile, 
fieldAccessReference.fieldName, fieldAccessReference.ownerClass.identifier);
+                            for (PhpIndexResult indexResult : 
indexConstantsResults) {
+                                NamedElement resultHandle = new 
NamedElement(fieldAccessReference.fieldName, indexResult.declarationFile, 
ElementType.PHP_CONSTANT);
+                                DeclarationLocation constantLocation = new 
DeclarationFinder.DeclarationLocation(indexResult.declarationFile, 
indexResult.getStartOffset(), resultHandle);
+                                if (location.equals(DeclarationLocation.NONE)) 
{
+                                    location = constantLocation;
+                                }
+                                location.addAlternative(new 
AlternativeLocationImpl(constantLocation));
+                            }
+                            return location;
+                    }
+            }
+        }
+
+        Reference reference = parserResult.findOccuredRefrence(caretOffset);
+
+        if (reference == null) {
+            return location;
+        }
+
+        Project projectOwner = 
ProjectConvertors.getNonConvertorOwner(currentFile);
+
+        if (projectOwner == null) {
+            return location;
+        }
+
+        FileObject sourceFolder = projectOwner.getProjectDirectory();
+        String referenceIdentifier = reference.identifier;
+        
+        switch (reference.type) {

Review Comment:
   Change to a switch expression?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to