|
| 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 | + |
1 | 26 | package de.halirutan.mathematica.lang.psi.util |
2 | 27 |
|
3 | 28 | import com.intellij.openapi.progress.ProgressManager |
4 | | -import com.intellij.openapi.project.DumbService |
5 | 29 | import com.intellij.psi.search.searches.ReferencesSearch |
6 | 30 | import com.intellij.psi.util.PsiTreeUtil |
7 | 31 | import de.halirutan.mathematica.lang.psi.api.MessageName |
8 | 32 | import de.halirutan.mathematica.lang.psi.api.Symbol |
9 | 33 | import de.halirutan.mathematica.lang.psi.api.assignment.Set |
10 | 34 | import de.halirutan.mathematica.lang.psi.api.string.MString |
11 | 35 |
|
12 | | -/** |
| 36 | +/** Provides a the feature of searching for a usage element and extract its usage message string |
13 | 37 | * |
14 | 38 | * @author patrick (02.12.17). |
15 | 39 | */ |
16 | 40 |
|
17 | 41 | fun extractUsageMessageString(symbol: Symbol): Pair<Symbol, List<String>> { |
18 | 42 | val resolve = symbol.resolve() ?: return Pair(symbol, emptyList()) |
19 | 43 |
|
| 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 |
20 | 47 | if (resolve is Symbol) { |
21 | 48 | return Pair(resolve, doUsageMessageExtract(resolve)) |
22 | 49 | } |
23 | 50 |
|
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) |
37 | 59 | } |
38 | 60 | } |
| 61 | + return@find false |
39 | 62 | } |
40 | | - return result |
| 63 | + return Pair(symbol, emptyList()) |
41 | 64 | } |
42 | 65 |
|
| 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 | + */ |
43 | 72 | private fun doUsageMessageExtract(symbol: Symbol): List<String> { |
44 | 73 | if (symbol.parent is MessageName) { |
45 | 74 | val messageName = symbol.parent as MessageName |
|
0 commit comments