|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright 2015 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
| 7 | + * Other names may be trademarks of their respective owners. |
| 8 | + * |
| 9 | + * The contents of this file are subject to the terms of either the GNU |
| 10 | + * General Public License Version 2 only ("GPL") or the Common |
| 11 | + * Development and Distribution License("CDDL") (collectively, the |
| 12 | + * "License"). You may not use this file except in compliance with the |
| 13 | + * License. You can obtain a copy of the License at |
| 14 | + * http://www.netbeans.org/cddl-gplv2.html |
| 15 | + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
| 16 | + * specific language governing permissions and limitations under the |
| 17 | + * License. When distributing the software, include this License Header |
| 18 | + * Notice in each file and include the License file at |
| 19 | + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
| 20 | + * particular file as subject to the "Classpath" exception as provided |
| 21 | + * by Oracle in the GPL Version 2 section of the License file that |
| 22 | + * accompanied this code. If applicable, add the following below the |
| 23 | + * License Header, with the fields enclosed by brackets [] replaced by |
| 24 | + * your own identifying information: |
| 25 | + * "Portions Copyrighted [year] [name of copyright owner]" |
| 26 | + * |
| 27 | + * If you wish your version of this file to be governed by only the CDDL |
| 28 | + * or only the GPL Version 2, indicate your decision by adding |
| 29 | + * "[Contributor] elects to include this software in this distribution |
| 30 | + * under the [CDDL or GPL Version 2] license." If you do not indicate a |
| 31 | + * single choice of license, a recipient has the option to distribute |
| 32 | + * your version of this file under either the CDDL, the GPL Version 2 or |
| 33 | + * to extend the choice of license to its licensees as provided above. |
| 34 | + * However, if you add GPL Version 2 code and therefore, elected the GPL |
| 35 | + * Version 2 license, then the option applies only if the new code is |
| 36 | + * made subject to such option by the copyright holder. |
| 37 | + * |
| 38 | + * Contributor(s): |
| 39 | + * |
| 40 | + * Portions Copyrighted 2015 Sun Microsystems, Inc. |
| 41 | + */ |
| 42 | +package org.netbeans.modules.php.cake3.editor.completion; |
| 43 | + |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.List; |
| 46 | +import javax.swing.text.Document; |
| 47 | +import javax.swing.text.JTextComponent; |
| 48 | +import org.netbeans.api.editor.mimelookup.MimeRegistration; |
| 49 | +import org.netbeans.api.lexer.Token; |
| 50 | +import org.netbeans.api.lexer.TokenSequence; |
| 51 | +import org.netbeans.modules.editor.NbEditorUtilities; |
| 52 | +import org.netbeans.modules.editor.lib2.DocUtils; |
| 53 | +import org.netbeans.modules.php.api.phpmodule.PhpModule; |
| 54 | +import static org.netbeans.modules.php.api.util.FileUtils.PHP_MIME_TYPE; |
| 55 | +import org.netbeans.modules.php.cake3.modules.CakePHP3Module; |
| 56 | +import org.netbeans.modules.php.cake3.modules.ModuleInfo; |
| 57 | +import org.netbeans.modules.php.editor.lexer.LexUtilities; |
| 58 | +import org.netbeans.modules.php.editor.lexer.PHPTokenId; |
| 59 | +import org.netbeans.spi.editor.completion.CompletionProvider; |
| 60 | +import static org.netbeans.spi.editor.completion.CompletionProvider.COMPLETION_QUERY_TYPE; |
| 61 | +import org.netbeans.spi.editor.completion.CompletionResultSet; |
| 62 | +import org.netbeans.spi.editor.completion.CompletionTask; |
| 63 | +import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery; |
| 64 | +import org.netbeans.spi.editor.completion.support.AsyncCompletionTask; |
| 65 | +import org.openide.filesystems.FileObject; |
| 66 | + |
| 67 | +/** |
| 68 | + * |
| 69 | + * @author junichi11 |
| 70 | + */ |
| 71 | +@MimeRegistration(mimeType = PHP_MIME_TYPE, service = CompletionProvider.class) |
| 72 | +public class MethodCompletionProvider extends CakePHP3CompletionProvider { |
| 73 | + |
| 74 | + @Override |
| 75 | + public CompletionTask createTask(int queryType, JTextComponent textComponent, PhpModule phpModule, FileObject fo) { |
| 76 | + if (queryType != COMPLETION_QUERY_TYPE) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + Document document = textComponent.getDocument(); |
| 80 | + if (document == null) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + TokenSequence<PHPTokenId> tokenSequence = null; |
| 84 | + int caretPosition = textComponent.getCaretPosition(); |
| 85 | + DocUtils.atomicLock(document); |
| 86 | + try { |
| 87 | + tokenSequence = LexUtilities.getPHPTokenSequence(document, caretPosition); |
| 88 | + } finally { |
| 89 | + DocUtils.atomicUnlock(document); |
| 90 | + } |
| 91 | + if (tokenSequence == null) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + tokenSequence.move(caretPosition); |
| 95 | + tokenSequence.moveNext(); |
| 96 | + Token<? extends PHPTokenId> previousToken = LexUtilities.findPreviousToken(tokenSequence, Arrays.asList(PHPTokenId.PHP_STRING, PHPTokenId.PHP_SEMICOLON)); |
| 97 | + if (previousToken == null || previousToken.id() != PHPTokenId.PHP_STRING) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + return new AsyncCompletionTask(new AsyncCompletionQuery() { |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void query(CompletionResultSet resultSet, Document doc, int caretOffset) { |
| 105 | + try { |
| 106 | + autoComplete(resultSet, doc, caretOffset); |
| 107 | + } finally { |
| 108 | + resultSet.finish(); |
| 109 | + } |
| 110 | + } |
| 111 | + }, textComponent); |
| 112 | + } |
| 113 | + |
| 114 | + private void autoComplete(CompletionResultSet resultSet, Document document, int offset) { |
| 115 | + DocUtils.atomicLock(document); |
| 116 | + TokenSequence<PHPTokenId> tokenSequence = null; |
| 117 | + try { |
| 118 | + tokenSequence = LexUtilities.getPHPTokenSequence(document, offset); |
| 119 | + } finally { |
| 120 | + DocUtils.atomicUnlock(document); |
| 121 | + } |
| 122 | + |
| 123 | + if (tokenSequence == null) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + tokenSequence.move(offset); |
| 128 | + String filter = ""; // NOI18N |
| 129 | + if (tokenSequence.moveNext()) { |
| 130 | + filter = getFilter(tokenSequence, offset); |
| 131 | + } |
| 132 | + if (tokenSequence.movePrevious()) { |
| 133 | + int startOffset = tokenSequence.offset(); |
| 134 | + if (tokenSequence.token().id() == PHPTokenId.PHP_STRING) { |
| 135 | + filter = getFilter(tokenSequence, offset); |
| 136 | + } |
| 137 | + if (tokenSequence.token().id() != PHPTokenId.PHP_PAAMAYIM_NEKUDOTAYIM |
| 138 | + && tokenSequence.token().id() != PHPTokenId.PHP_OBJECT_OPERATOR) { |
| 139 | + tokenSequence.movePrevious(); |
| 140 | + } |
| 141 | + tokenSequence.movePrevious(); |
| 142 | + if (tokenSequence.token().id() == PHPTokenId.WHITESPACE) { |
| 143 | + tokenSequence.movePrevious(); |
| 144 | + } |
| 145 | + String varName = tokenSequence.token().text().toString(); |
| 146 | + tokenSequence.moveNext(); |
| 147 | + |
| 148 | + switch (varName) { |
| 149 | + case "Flash": // NOI18N |
| 150 | + autoCompleteFlash(document, filter, resultSet, startOffset); |
| 151 | + break; |
| 152 | + default: |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + private String getFilter(TokenSequence<PHPTokenId> tokenSequence, int offset) { |
| 161 | + Token<PHPTokenId> token = tokenSequence.token(); |
| 162 | + PHPTokenId id = token.id(); |
| 163 | + if (id == PHPTokenId.PHP_STRING) { |
| 164 | + String tokenString = token.text().toString(); |
| 165 | + int filterLength = offset - tokenSequence.offset(); |
| 166 | + if (filterLength >= 0 && tokenString.length() >= filterLength) { |
| 167 | + return tokenString.substring(0, filterLength); |
| 168 | + } |
| 169 | + } |
| 170 | + return ""; // NOI18N |
| 171 | + } |
| 172 | + |
| 173 | + private void autoCompleteFlash(Document document, String filter, CompletionResultSet resultSet, int startOffset) { |
| 174 | + FileObject fileObject = NbEditorUtilities.getFileObject(document); |
| 175 | + CakePHP3Module cakeModule = CakePHP3Module.forFileObject(fileObject); |
| 176 | + ModuleInfo info = cakeModule.createModuleInfo(fileObject); |
| 177 | + List<FileObject> directories = cakeModule.getDirectories(info.getBase(), CakePHP3Module.Category.ELEMENT, info.getPluginName()); |
| 178 | + for (FileObject directory : directories) { |
| 179 | + FileObject flashDirectory = directory.getFileObject("Flash"); // NOI18N |
| 180 | + if (flashDirectory != null && flashDirectory.isFolder()) { |
| 181 | + for (FileObject child : flashDirectory.getChildren()) { |
| 182 | + if (child.isFolder()) { |
| 183 | + continue; |
| 184 | + } |
| 185 | + String name = child.getName(); |
| 186 | + if (!name.startsWith(filter)) { |
| 187 | + continue; |
| 188 | + } |
| 189 | + resultSet.addAllItems(CakePHP3CompletionItem.FlashMethodCompletionItem.createItems(name, filter, startOffset)); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments