Skip to content

Commit

Permalink
Allow to reference module as @param type in jsdoc comment
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Aug 12, 2018
1 parent ab75f5e commit 3bf14ec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
14 changes: 9 additions & 5 deletions src/org/klesun/deep_js_completion/entry/PathStrGoToDecl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ import scala.collection.JavaConverters._
import PathStrGoToDecl._

object PathStrGoToDecl {
def getReferencedFile(relPath: String, caretFile: PsiFile): Option[PsiFile] = {
Option(caretFile.getContainingDirectory)
.flatMap(f => Option(f.getVirtualFile))
.map(f => f.getPath + "/" + relPath)
.flatMap(fullPath => Option(LocalFileSystem.getInstance.findFileByPath(fullPath)))
.flatMap(f => Option(PsiManager.getInstance(caretFile.getProject).findFile(f)))
}

def getReferencedFile(expr: JSExpression): Option[PsiFile] = {
cast[JSLiteralExpressionImpl](expr)
.flatMap(lit => {
val relPath = lit.getValue.toString
if (relPath.startsWith("./") || relPath.startsWith("../")) {
Option(lit.getContainingFile)
.flatMap(f => Option(f.getContainingDirectory))
.flatMap(f => Option(f.getVirtualFile))
.map(f => f.getPath + "/" + relPath)
.flatMap(fullPath => Option(LocalFileSystem.getInstance.findFileByPath(fullPath)))
.flatMap(f => Option(PsiManager.getInstance(lit.getProject).findFile(f)))
.flatMap(f => getReferencedFile(relPath, f))
} else {
None
}
Expand Down
27 changes: 19 additions & 8 deletions src/org/klesun/deep_js_completion/resolvers/VarRes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ case class VarRes(ctx: ICtx) {
}
tokens.map(t => t.getText).mkString("")
.replaceAll("""\n\s*\* """, "\n")
.replaceAll("""\*\/$""", "")
}

private def findVarDecl(caretPsi: PsiElement, varName: String): Option[JSType] = {
Expand Down Expand Up @@ -126,13 +127,23 @@ case class VarRes(ctx: ICtx) {
}

private def parseDocExpr(caretPsi: PsiElement, expr: String): Option[JSType] = {
"""^\s*=\s*(\w+)(\([^\)]*\)|)\s*$""".r.findFirstMatchIn(expr)
.flatMap(found => {
val varName = found.group(1)
val isFuncCall = !found.group(2).equals("")
findVarDecl(caretPsi, varName)
.flatMap(t => if (isFuncCall) MultiType.getReturnType(t) else Some(t))
})
Option(null)
.orElse("""^\s*=\s*(\w+)(\([^\)]*\)|)\s*$""".r.findFirstMatchIn(expr)
.flatMap(found => {
val varName = found.group(1)
val isFuncCall = !found.group(2).equals("")
findVarDecl(caretPsi, varName)
.flatMap(t => if (isFuncCall) MultiType.getReturnType(t) else Some(t))
}))
.orElse("""^\s*=\s*from\('([^']+)'\)(\([^\)]*\)|)\s*$""".r.findFirstMatchIn(expr)
.flatMap(found => {
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(t => if (isFuncCall) MultiType.getReturnType(t) else Some(t))
}))
}

private def getArgDocExprType(func: JSFunction, para: JSParameter): List[JSType] = {
Expand Down Expand Up @@ -220,7 +231,7 @@ case class VarRes(ctx: ICtx) {
case prop: JSDefinitionExpression => Option(prop.getExpression)
.flatMap(expr => ctx.findExprType(expr))
case _ =>
println("Unsupported var declaration - " + psi.getClass + " " + psi.getText)
//println("Unsupported var declaration - " + psi.getClass + " " + psi.getText)
None
})
MultiType.mergeTypes(deepRef ++ pushRef ++ briefRef)
Expand Down

0 comments on commit 3bf14ec

Please sign in to comment.