Skip to content

Commit

Permalink
Allow to specify node module in the doc like `@param db = from(./../.…
Browse files Browse the repository at this point in the history
…./../Utils/Db.es6)()`

Same way it is done for requirejs
  • Loading branch information
klesun-itn committed Dec 3, 2018
1 parent 98ab9b7 commit 479c166
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
5 changes: 2 additions & 3 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>lv.midiana.misc.idea-plugins.deep-js-completion</id>
<name>deep-js-completion</name>
<version>2018.10.25.001</version>
<version>2018.12.03.001</version>
<vendor email="[email protected]" url="http://midiana.lv/entry/deep-js-completion">Klesun</vendor>

<description><![CDATA[
Expand All @@ -26,8 +26,7 @@ This plugin will be highly inspired by my other plugin, <a href="https://plugins
]]></description>

<change-notes><![CDATA[
<li>GoTo string paths like './dir/file', even if it does not end with '.js'</li>
<li>Fix: null pointer on JSLiteralExpressionImpl.getValue</li>
<li>Allow to specify node module in the doc like `@param db = from('./../../../Utils/Db.es6')()` (similar to how it works for requirejs)</li>
]]>
</change-notes>

Expand Down
41 changes: 38 additions & 3 deletions src/org/klesun/deep_js_completion/resolvers/VarRes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import java.util.Objects

import com.intellij.lang.javascript.dialects.JSDialectSpecificHandlersFactory
import com.intellij.lang.javascript.documentation.JSDocumentationUtils
import com.intellij.lang.javascript.psi.JSRecordType.TypeMember
import com.intellij.lang.javascript.psi.impl.{JSDefinitionExpressionImpl, JSFunctionImpl, JSReferenceExpressionImpl}
import com.intellij.lang.javascript.psi._
import com.intellij.lang.javascript.psi.jsdoc.JSDocTag
import com.intellij.lang.javascript.psi.jsdoc.impl.JSDocCommentImpl
import com.intellij.lang.javascript.psi.resolve.JSResolveUtil
import com.intellij.lang.javascript.psi.types.JSRecordMemberSourceFactory.EmptyMemberSource
import com.intellij.lang.javascript.psi.types.JSRecordTypeImpl.PropertySignatureImpl
import com.intellij.lang.javascript.psi.types._
import com.intellij.psi.impl.source.resolve.ResolveCache.PolyVariantResolver
import com.intellij.psi.{PsiElement, PsiFile, PsiWhiteSpace}
Expand Down Expand Up @@ -64,6 +67,35 @@ case class VarRes(ctx: ICtx) {
.flatMap(moduleSupplier => ctx.findExprType(moduleSupplier))
}

private def resolveCommonJsFormatDef(file: PsiFile): Option[JSType] = {
val types = file.getChildren
.flatMap(cast[JSExpressionStatement](_))
.flatMap(_.getChildren)
.flatMap(cast[JSAssignmentExpression](_))
.flatMap(ass => Option(ass.getROperand)
.flatMap(value => Option(ass.getLOperand)
.flatMap(vari => {
val txt = Option(vari.getText).getOrElse("")
if (txt startsWith "module.exports") {
ctx.findExprType(value)
} else if (txt startsWith "exports.") {
cast[JSDefinitionExpression](vari)
.filter(ref => Option(vari.getText).exists(txt => txt startsWith "exports."))
.flatMap(defi => Option(defi.getFirstChild))
.flatMap(cast[JSReferenceExpressionImpl](_))
.flatMap(ref => Option(ref.getReferencedName))
.flatMap(name => ctx.findExprType(value)
.map(valT => new PropertySignatureImpl(name, valT, false, new EmptyMemberSource)))
.map((prop: TypeMember) => new JSRecordTypeImpl(JSTypeSource.EMPTY, List(prop).asJava))
} else {
None
}
})
)
)
MultiType.mergeTypes(types)
}

private def resolveRequireJsFormatDef(file: PsiFile): Option[JSType] = {
val types = List[JSType]() ++
resolveKlesunWhenLoadedSupplierDef(file) ++
Expand Down Expand Up @@ -126,7 +158,7 @@ case class VarRes(ctx: ICtx) {
.lift(0)
}

private def parseDocExpr(caretPsi: PsiElement, expr: String): Option[JSType] = {
private def parseDocExpr(caretPsi: PsiElement, expr: String): Iterable[JSType] = {
Option(null)
.orElse("""^\s*=\s*(\w+)(\([^\)]*\)|)\s*$""".r.findFirstMatchIn(expr)
.flatMap(found => {
Expand All @@ -140,8 +172,11 @@ case class VarRes(ctx: ICtx) {
val path = found.group(1)
val isFuncCall = !found.group(2).equals("")
Option(caretPsi.getContainingFile)
.flatMap(f => PathStrGoToDecl.getReferencedFile(path, f))
.flatMap(file => resolveRequireJsFormatDef(file))
.flatMap(f => PathStrGoToDecl.getReferencedFile(path, f)).toList
.flatMap(file => List()
++ resolveCommonJsFormatDef(file)
++ resolveRequireJsFormatDef(file)
).lift(0)
.flatMap(t => if (isFuncCall) MultiType.getReturnType(t) else Some(t))
}))
}
Expand Down

0 comments on commit 479c166

Please sign in to comment.