Skip to content

Commit a306c5f

Browse files
committed
Fix doc-provider, better annotation for missing semicolon
1 parent ed73e4b commit a306c5f

3 files changed

Lines changed: 106 additions & 128 deletions

File tree

src/de/halirutan/mathematica/codeinsight/completion/BuiltinFunctionCompletionProvider.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
/*
22
* Copyright (c) 2017 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:
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

2224
package de.halirutan.mathematica.codeinsight.completion;
@@ -52,7 +54,8 @@ void addTo(CompletionContributor contributor) {
5254
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
5355
HashMap<String, SymbolInformation> symbols = SymbolInformationProvider.getSymbolNames();
5456

55-
if (Character.isLowerCase(parameters.getPosition().getText().charAt(0))) {
57+
final char start = parameters.getPosition().getText().charAt(0);
58+
if (Character.isLowerCase(start) || Character.isDigit(start)) {
5659
return;
5760
}
5861

src/de/halirutan/mathematica/codeinsight/inspections/codestyle/ConsistentCompoundExpressionInFile.java

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
/*
2-
* Copyright (c) 2014 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

2224
package de.halirutan.mathematica.codeinsight.inspections.codestyle;
@@ -36,12 +38,10 @@
3638
import static de.halirutan.mathematica.lang.psi.util.MathematicaPsiUtilities.getNextSiblingSkippingWhitespace;
3739

3840
/**
39-
*
40-
* On file-scope it is not necessary to end every expression with a ;
41-
* It is common practise for package developers to leave those semicolons out but regarding style issue, I see this
42-
* as inconsistent (for instance because Get will only return the result of the last expression which is exactly
43-
* what a CompoundExpression is for).
44-
*
41+
* On file-scope it is not necessary to end every expression with a ; It is common practise for package developers to
42+
* leave those semicolons out but regarding style issue, I see this as inconsistent (for instance because Get will only
43+
* return the result of the last expression which is exactly what a CompoundExpression is for).
44+
* <p>
4545
* This inspection will mark missing semicolons on file scope
4646
*
4747
* @author patrick (7/8/14)
@@ -72,28 +72,22 @@ public String getGroupDisplayName() {
7272
@SuppressWarnings("OverlyComplexAnonymousInnerClass")
7373
@NotNull
7474
@Override
75-
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly,@NotNull final LocalInspectionToolSession session) {
76-
if(session.getFile().getFileType() instanceof MathematicaFileType) {
75+
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly, @NotNull final LocalInspectionToolSession session) {
76+
if (session.getFile().getFileType() instanceof MathematicaFileType) {
7777
return new MathematicaVisitor() {
7878
@Override
7979
public void visitFile(final PsiFile file) {
8080
PsiElement child = file.getFirstChild();
8181
while (child != null) {
82-
if (child instanceof PsiWhiteSpace ||
83-
child instanceof PsiComment ||
84-
child instanceof CompoundExpression && getNextSiblingSkippingWhitespace(child) == null
85-
) {
82+
if (child instanceof PsiWhiteSpace || child instanceof PsiComment || child instanceof CompoundExpression && getNextSiblingSkippingWhitespace(child) == null) {
8683
child = child.getNextSibling();
8784
continue;
8885
}
89-
holder.registerProblem(
90-
file,
91-
TextRange.from(Math.max(child.getTextOffset() + child.getTextLength() - 1, 0), 1),
92-
InspectionBundle.message("consistent.compound.expression.in.file.message"),
93-
new ConsistentCompoundExpressionQuickFix());
94-
// }
86+
if (child.getNextSibling() instanceof PsiWhiteSpace) {
87+
int errorOffset = child.getNextSibling().getTextOffset();
88+
holder.registerProblem(file, TextRange.from(Math.max(errorOffset, 0), 1), InspectionBundle.message("consistent.compound.expression.in.file.message"), new ConsistentCompoundExpressionQuickFix());
89+
}
9590
child = child.getNextSibling();
96-
9791
}
9892
}
9993
};

src/de/halirutan/mathematica/documentation/MathematicaDocumentationProvider.java

Lines changed: 58 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
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

2224
package de.halirutan.mathematica.documentation;
2325

24-
import com.intellij.codeInsight.lookup.LookupElement;
2526
import com.intellij.codeInsight.lookup.LookupEx;
2627
import com.intellij.codeInsight.lookup.LookupManager;
2728
import com.intellij.lang.documentation.AbstractDocumentationProvider;
@@ -30,7 +31,7 @@
3031
import com.intellij.psi.PsiElement;
3132
import com.intellij.psi.PsiFile;
3233
import com.intellij.psi.PsiManager;
33-
import com.intellij.psi.PsiWhiteSpace;
34+
import com.intellij.psi.util.PsiTreeUtil;
3435
import de.halirutan.mathematica.lang.psi.api.OperatorNameProvider;
3536
import de.halirutan.mathematica.lang.psi.api.Symbol;
3637
import 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

Comments
 (0)