11/*
2- * Copyright (c) 2013 Patrick Scheibe
3- * Permission is hereby granted, free of charge, to any person obtaining a copy
4- * of this software and associated documentation files (the "Software"), to deal
5- * in the Software without restriction, including without limitation the rights
6- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7- * copies of the Software, and to permit persons to whom the Software is
8- * furnished to do so, subject to the following conditions:
2+ * Copyright (c) 2017 Patrick Scheibe
93 *
10- * The above copyright notice and this permission notice shall be included in
11- * all copies or substantial portions of the Software.
4+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5+ * of this software and associated documentation files (the "Software"), to deal
6+ * in the Software without restriction, including without limitation the rights
7+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ * copies of the Software, and to permit persons to whom the Software is
9+ * furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in
12+ * all copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+ * THE SOFTWARE.
1221 *
13- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19- * THE SOFTWARE.
2022 */
2123
2224package de .halirutan .mathematica .documentation ;
2325
24- import com .intellij .codeInsight .lookup .LookupElement ;
2526import com .intellij .codeInsight .lookup .LookupEx ;
2627import com .intellij .codeInsight .lookup .LookupManager ;
2728import com .intellij .lang .documentation .AbstractDocumentationProvider ;
3031import com .intellij .psi .PsiElement ;
3132import com .intellij .psi .PsiFile ;
3233import com .intellij .psi .PsiManager ;
33- import com .intellij .psi .PsiWhiteSpace ;
34+ import com .intellij .psi .util . PsiTreeUtil ;
3435import de .halirutan .mathematica .lang .psi .api .OperatorNameProvider ;
3536import de .halirutan .mathematica .lang .psi .api .Symbol ;
3637import de .halirutan .mathematica .lang .psi .util .MathematicaPsiElementFactory ;
@@ -56,11 +57,10 @@ public class MathematicaDocumentationProvider extends AbstractDocumentationProvi
5657 * operator. Then it tries to guess the usage message of the operator by converting the class name to a hopefully
5758 * valid operator name.
5859 *
59- * @param element
60- * Element which was possibly altered by {@link #getCustomDocumentationElement(Editor, PsiFile, PsiElement)} or by
61- * {@link #getDocumentationElementForLookupItem(PsiManager, Object, PsiElement)} if the lookup was active
62- * @param originalElement
63- * The original element for which the doc was called (possibly whitespace)
60+ * @param element Element which was possibly altered by {@link #getCustomDocumentationElement(Editor, PsiFile, PsiElement)} or by
61+ * {@link #getDocumentationElementForLookupItem(PsiManager, Object, PsiElement)} if the lookup was active
62+ * @param originalElement The original element for which the doc was called (possibly whitespace)
63+ *
6464 * @return The html string of the usage message or null if it could not be loaded
6565 */
6666 @ Nullable
@@ -95,12 +95,10 @@ public String generateDoc(PsiElement element, @Nullable PsiElement originalEleme
9595 /**
9696 * Calculates the correct element for which the user wants documentation.
9797 *
98- * @param editor
99- * The editor of the file
100- * @param file
101- * The file which is edited and where the doc call was made
102- * @param contextElement
103- * The element where the caret was when the doc was called
98+ * @param editor The editor of the file
99+ * @param file The file which is edited and where the doc call was made
100+ * @param contextElement The element where the caret was when the doc was called
101+ *
104102 * @return The element for which the user wants documentation. If an item of the completion list is currently
105103 * highlighted, then this element. If the cursor is over/beside an identifier, then the symbol element. As last thing
106104 * it is determined whether the PsiElement is the operator-sign of an operation, then we get the corresponding
@@ -109,44 +107,31 @@ public String generateDoc(PsiElement element, @Nullable PsiElement originalEleme
109107 @ Nullable
110108 @ Override
111109 public PsiElement getCustomDocumentationElement (@ NotNull Editor editor , @ NotNull PsiFile file , @ Nullable PsiElement contextElement ) {
110+ PsiElement docElement ;
112111
113- // Check whether there is a completion item which is currently active and give a Symbol element
114- // containing the lookup name back.
115- final LookupEx activeLookup = LookupManager .getActiveLookup (editor );
116- if ((activeLookup != null ) && activeLookup .isFocused ()) {
117- final PsiElement elementAt = file .findElementAt (editor .getCaretModel ().getOffset () - 1 );
118- // TODO: Check
119- if (elementAt instanceof Symbol ) {
120- // Symbol lookup = new SymbolImpl(elementAt.getNode());
121- final LookupElement currentItem = activeLookup .getCurrentItem ();
122- final String lookupString = currentItem != null ? currentItem .getLookupString () : "" ;
123- // lookup.setName(lookupString);
124- return elementAt ;
125- }
112+ if (contextElement != null ) {
113+ docElement = contextElement .getParent ();
114+ } else {
115+ int offset = editor .getCaretModel ().getCurrentCaret ().getOffset ();
116+ offset = offset > 0 ? offset - 1 : offset ;
117+ docElement = PsiTreeUtil .findElementOfClassAtOffset (file , offset , Symbol .class , false );
118+ docElement = docElement != null ? docElement : file .findElementAt (offset );
126119 }
127120
128- if (contextElement != null ) {
129- PsiElement parent = contextElement .getParent ();
121+ if (docElement != null && !(docElement instanceof Symbol ) && !(docElement instanceof OperatorNameProvider )) {
122+ docElement = docElement .getParent ();
123+ }
130124
131- if ((contextElement instanceof PsiWhiteSpace ) || !((parent instanceof Symbol ) || (parent instanceof OperatorNameProvider ))) {
132- PsiElement elm = file .findElementAt (editor .getCaretModel ().getOffset () - 1 );
133- if (elm != null ) {
134- contextElement = elm ;
135- parent = elm .getParent ();
136- }
137- }
125+ if (docElement instanceof Symbol ) {
126+ return docElement ;
127+ }
138128
139- // if (parent instanceof Symbol) {
140- // return new SymbolImpl(parent.getNode());
129+ // Determine if the contextElement is the operator sign of an operation.
130+ // See the doc to OperatorNameProviderImpl.
131+ if (docElement instanceof OperatorNameProvider ) {
132+ // if (((OperatorNameProvider) docElement).isOperatorSign(contextElement)) {
133+ return docElement ;
141134// }
142-
143- // Determine if the contextElement is the operator sign of an operation.
144- // See the doc to OperatorNameProviderImpl.
145- if (parent instanceof OperatorNameProvider ) {
146- if (((OperatorNameProvider ) parent ).isOperatorSign (contextElement )) {
147- return parent ;
148- }
149- }
150135 }
151136 return null ;
152137 }
@@ -155,15 +140,13 @@ public PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull
155140 * This makes it possible to have the documentation for each function while scrolling through the completion
156141 * suggestion list.
157142 *
158- * @param psiManager
159- * access to Psi related things
160- * @param object
161- * the current lookup object
162- * @param element
163- * the element, the documentation was initially called for. Note that this is typically not a valid built-in
164- * function, because you start typing Plo then the completion box pops up and when you call documentation on one
165- * of the selected lookup entries, the elements name is still Plo, while you want to check the documentation for
166- * the lookup element.
143+ * @param psiManager access to Psi related things
144+ * @param object the current lookup object
145+ * @param element the element, the documentation was initially called for. Note that this is typically not a valid built-in
146+ * function, because you start typing Plo then the completion box pops up and when you call documentation on one
147+ * of the selected lookup entries, the elements name is still Plo, while you want to check the documentation for
148+ * the lookup element.
149+ *
167150 * @return The Symbol which was created from the string of the lookup element or null if it wasn't possible.
168151 */
169152 @ Nullable
@@ -172,13 +155,11 @@ public PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Ob
172155 if (element != null ) {
173156 final LookupEx activeLookup = LookupManager .getActiveLookup (FileEditorManager .getInstance (element .getProject ()).getSelectedTextEditor ());
174157 if (activeLookup != null ) {
175- if (activeLookup .isFocused ()) {
176- MathematicaPsiElementFactory elementFactory = new MathematicaPsiElementFactory (psiManager .getProject ());
177- try {
178- return elementFactory .createSymbol (object .toString ());
179- } catch (Exception e ) {
180- return null ;
181- }
158+ MathematicaPsiElementFactory elementFactory = new MathematicaPsiElementFactory (psiManager .getProject ());
159+ try {
160+ return elementFactory .createSymbol (object .toString ());
161+ } catch (Exception e ) {
162+ return null ;
182163 }
183164 }
184165 }
0 commit comments