Skip to content

Commit 143b2e1

Browse files
committed
Implement QuickDoc for usage messages of user functions
1 parent 21eee47 commit 143b2e1

3 files changed

Lines changed: 47 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
## Todo
44

5-
- Show usage for own functions. "Quick Documentation" on a package symbol will show its usage message if available
65
- Better rendering for "Go to Declaration" targets
76
- Inspection for using the same variable in the e.g. Module definition list several times
87
- Quick fix for adding usages and other messages
@@ -14,6 +13,7 @@
1413

1514
## Version 3.0
1615

16+
- Quick Documentation lookup for own functions. This will show the usage message of a function if available.
1717
- Performance improvement through caching
1818
- Support for Libraries. Libraries are basically a package folder with Mathematica source code. This code is indexed and
1919
can now be used for completion and navigation. The highlighter shows which functions come from a different file

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public String generateDoc(PsiElement element, @Nullable PsiElement originalEleme
123123
private String renderCustomUsageMessage(@NotNull Symbol symbol) {
124124
final Pair<Symbol, List<String>> usages = UsageMessagesKt.extractUsageMessageString(symbol);
125125
if (usages.component2().isEmpty()) {
126-
return "";
126+
return null;
127127
}
128128
final String fileName = usages.component1().getContainingFile().getName();
129129
final String symbolName = symbol.getSymbolName();

src/de/halirutan/mathematica/lang/psi/util/UsageMessages.kt

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,74 @@
1+
/*
2+
* Copyright (c) 2017. Patrick Scheibe
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the “Software”), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
126
package de.halirutan.mathematica.lang.psi.util
227

328
import com.intellij.openapi.progress.ProgressManager
4-
import com.intellij.openapi.project.DumbService
529
import com.intellij.psi.search.searches.ReferencesSearch
630
import com.intellij.psi.util.PsiTreeUtil
731
import de.halirutan.mathematica.lang.psi.api.MessageName
832
import de.halirutan.mathematica.lang.psi.api.Symbol
933
import de.halirutan.mathematica.lang.psi.api.assignment.Set
1034
import de.halirutan.mathematica.lang.psi.api.string.MString
1135

12-
/**
36+
/** Provides a the feature of searching for a usage element and extract its usage message string
1337
*
1438
* @author patrick (02.12.17).
1539
*/
1640

1741
fun extractUsageMessageString(symbol: Symbol): Pair<Symbol, List<String>> {
1842
val resolve = symbol.resolve() ?: return Pair(symbol, emptyList())
1943

44+
// if resolve is a real symbol, then already point to the usage message in another file
45+
// this might change in future
46+
// TODO: Keep above in mind
2047
if (resolve is Symbol) {
2148
return Pair(resolve, doUsageMessageExtract(resolve))
2249
}
2350

24-
var result: Pair<Symbol, List<String>> = Pair(symbol, emptyList())
25-
26-
DumbService.getInstance(symbol.project).runReadActionInSmartMode {
27-
val elements = ReferencesSearch.search(resolve)
28-
for (elm in elements) {
29-
ProgressManager.checkCanceled()
30-
if (elm != null && elm.element is Symbol) {
31-
val sym = elm.element as Symbol
32-
val r = doUsageMessageExtract(sym)
33-
if (r.isNotEmpty()) {
34-
result = Pair(sym, r)
35-
break
36-
}
51+
// Find all references and look if one of them is a usage message
52+
ReferencesSearch.search(resolve).find { psiReference ->
53+
ProgressManager.checkCanceled()
54+
val elm = psiReference.element ?: return@find false
55+
if (elm is Symbol) {
56+
val messages = doUsageMessageExtract(elm)
57+
if (messages.isNotEmpty()) {
58+
return Pair(elm, messages)
3759
}
3860
}
61+
return@find false
3962
}
40-
return result
63+
return Pair(symbol, emptyList())
4164
}
4265

66+
/**
67+
* Takes a symbol that is located at the code `symbol::usage = ...` and extracts the left string. The it splits the
68+
* string at "\n" because this often indicates a new usage for a different call pattern. In addition, it removes the
69+
* surrounding quotes and the ellipsis character with "..."
70+
* @Note The lhs can be a combination of `<>` and this function will process the joined string of all parts
71+
*/
4372
private fun doUsageMessageExtract(symbol: Symbol): List<String> {
4473
if (symbol.parent is MessageName) {
4574
val messageName = symbol.parent as MessageName

0 commit comments

Comments
 (0)