Skip to content

Commit 81e06ad

Browse files
authored
fix null ptr issues (#182)
1 parent 6f65617 commit 81e06ad

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/main/kotlin/com/emberjs/hbs/HbsLocalReference.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import com.emberjs.psi.EmberNamedAttribute
99
import com.emberjs.psi.EmberNamedElement
1010
import com.emberjs.refactoring.SimpleNodeFactory
1111
import com.emberjs.utils.EmberUtils
12+
import com.emberjs.utils.ifFalse
13+
import com.emberjs.utils.ifTrue
1214
import com.emberjs.utils.originalVirtualFile
1315
import com.emberjs.xml.EmberAttrDec
1416
import com.intellij.injected.editor.VirtualFileWindow
@@ -26,6 +28,7 @@ import com.intellij.lang.javascript.psi.impl.JSVariableImpl
2628
import com.intellij.lang.javascript.psi.jsdoc.impl.JSDocCommentImpl
2729
import com.intellij.lang.javascript.psi.resolve.JSContextResolver
2830
import com.intellij.lang.javascript.psi.types.JSRecordTypeImpl
31+
import com.intellij.lang.javascript.psi.types.recordImpl.PropertySignatureImpl
2932
import com.intellij.openapi.util.TextRange
3033
import com.intellij.psi.*
3134
import com.intellij.psi.css.CssRulesetList
@@ -329,7 +332,12 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
329332
}
330333
}
331334
}
332-
val followed = EmberUtils.followReferences(any as PsiElement?)
335+
336+
if (any is PropertySignatureImpl) {
337+
return resolveToJs(any.memberSource.singleElement, path.subList(1, max(path.lastIndex, 1)), resolveIncomplete, recursionCounter + 1)
338+
}
339+
340+
val followed = (any as? PsiElement?)?.let { EmberUtils.followReferences(it) } ?: any
333341
if (followed !== null && followed != any) {
334342
return resolveToJs(followed, path, resolveIncomplete, recursionCounter + 1)
335343
}
@@ -382,9 +390,14 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
382390
}
383391

384392
fun findReference(element: PsiElement): PsiReference? {
385-
val psiFile = PsiManager.getInstance(element.project).findFile(element.originalVirtualFile!!)
386-
val document = PsiDocumentManager.getInstance(element.project).getDocument(psiFile!!)!!
387-
val service = GlintLanguageServiceProvider(element.project).getService(element.originalVirtualFile!!)
393+
var isValid = element.originalVirtualFile?.isValid.ifTrue { true }
394+
val psiFile = isValid?.let { PsiManager.getInstance(element.project).findFile(element.originalVirtualFile!!) }
395+
isValid = isValid?.and(psiFile != null).ifTrue { true }
396+
val document = isValid?.let { PsiDocumentManager.getInstance(element.project).getDocument(psiFile!!) }
397+
isValid = isValid?.and(document != null).ifTrue { true }
398+
val service = isValid?.let {
399+
GlintLanguageServiceProvider(element.project).getService(element.originalVirtualFile!!)
400+
}
388401

389402
val name = element.text.replace("IntellijIdeaRulezzz", "")
390403

@@ -415,15 +428,15 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
415428
val yieldRef = (ref as? HbsLocalReference)?.resolveYield()
416429
if (yieldRef != null) {
417430
val res = resolveToJs(yieldRef, listOf(element.text))
418-
return HbsLocalReference(element, res ?: service?.getNavigationFor(document, element, true)?.firstOrNull()?.parent)
431+
return HbsLocalReference(element, res ?: service?.getNavigationFor(document!!, element, true)?.firstOrNull()?.parent)
419432
}
420433
val sig = (ref as? HbsLocalReference)?.resolved as? JSRecordType.PropertySignature
421434
if (ref != null && resolveToJs(sig ?: ref.resolve(), listOf(element.text)) != null) {
422435
return HbsLocalReference(element, resolveToJs(sig ?: ref.resolve(), listOf(element.text)))
423436
}
424437
val ref2 = sibling.references.find { it is HbReference } as HbReference?
425438
val res = resolveToJs(ref2?.resolve(), listOf(element.text))
426-
return HbsLocalReference(element, res ?: service?.getNavigationFor(document, element, true)?.firstOrNull()?.parent)
439+
return HbsLocalReference(element, res ?: service?.getNavigationFor(document!!, element, true)?.firstOrNull()?.parent)
427440
}
428441

429442
if (element.parent is HbData) {
@@ -438,7 +451,7 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
438451
val modelProp = (cls as? JSClass)?.let { it.jsType.asRecordType().properties.find { it.memberName == "model" } }
439452
return modelProp?.let { HbsLocalReference(element, it.memberSource.singleElement) }
440453
}
441-
val resolved = service?.getNavigationFor(document, element, true)?.firstOrNull()?.parent
454+
val resolved = service?.getNavigationFor(document!!, element, true)?.firstOrNull()?.parent
442455
return resolved?.let { HbsLocalReference(element, it) }
443456
}
444457
}
@@ -463,7 +476,7 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
463476
return referenceBlocks(element, name)
464477
?: resolveToLocalJs(element)
465478
?: let {
466-
val resolved = service?.getNavigationFor(document, element, true)?.firstOrNull()?.parent
479+
val resolved = service?.getNavigationFor(document!!, element, true)?.firstOrNull()?.parent
467480
resolved?.let { HbsLocalReference(element, it) }
468481
}
469482
}

src/main/kotlin/com/emberjs/utils/BooleanExt.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ inline fun <R> Boolean?.ifFalse(block: Boolean?.() -> R): R? {
2020
}
2121

2222
return null
23-
}
23+
}

0 commit comments

Comments
 (0)